[vlc-devel] [PATCH v3] keystore/file: use POSIX file locking when flock() is unavailable

Thomas Guillem thomas at gllm.fr
Tue Apr 12 17:40:00 CEST 2016


Merged, thanks!
(I removed some stray white spaces in posix_lock_fd and replaced #else
#if by #elsif in file_open)

On Tue, Apr 12, 2016, at 17:14, Sean McGovern wrote:
> Additionally, remove the unlock call as fclose() will drop
> the lock for us.
> ---
> I helieve this properly handles both read and write locking as necessary
> now.
> ---
>  modules/keystore/file.c |   42
>  +++++++++++++++++++++++++++++++++++-------
>  1 file changed, 35 insertions(+), 7 deletions(-)
> 
> diff --git a/modules/keystore/file.c b/modules/keystore/file.c
> index 381ec87..794cb04 100644
> --- a/modules/keystore/file.c
> +++ b/modules/keystore/file.c
> @@ -27,6 +27,9 @@
>  #ifdef HAVE_FLOCK
>  #include <sys/file.h>
>  #endif
> +#ifdef HAVE_FCNTL
> +#include <fcntl.h>
> +#endif
>  
>  #include <vlc_common.h>
>  #include <vlc_plugin.h>
> @@ -262,6 +265,27 @@ end:
>      }
>      return VLC_SUCCESS;
>  }
> + 
> +#if (!defined(HAVE_FLOCK) && defined (HAVE_FCNTL) && defined (F_SETLKW))
> +static int
> +posix_lock_fd(int fd)
> +{
> +    struct flock lock;
> +    int flags;
> +    
> +    if(fd == -1)
> +        return -1;
> +
> +    flags = fcntl(fd, F_GETFL);
> +    
> +    lock.l_start = 0;
> +    lock.l_len = 0;
> +    lock.l_whence = SEEK_SET;
> +    lock.l_type = (flags & O_ACCMODE) == O_RDONLY ? F_RDLCK : F_WRLCK;
> +    
> +    return fcntl(fd, F_SETLKW, &lock);
> +}
> +#endif
>  
>  static int
>  file_open(const char *psz_file, const char *psz_mode, FILE **pp_file)
> @@ -279,17 +303,21 @@ file_open(const char *psz_file, const char
> *psz_mode, FILE **pp_file)
>      {
>          fclose(p_file);
>      }
> +#else
> +#if defined (HAVE_FCNTL) && defined (F_SETLKW)
> +    if (posix_lock_fd(i_fd) != 0)
> +    {
> +        fclose(p_file);
> +    }
> +#endif
>  #endif
>      *pp_file = p_file;
>      return i_fd;
>  }
>  
>  static void
> -file_close(FILE *p_file, int i_fd)
> +file_close(FILE *p_file)
>  {
> -#ifdef HAVE_FLOCK
> -    flock(i_fd, LOCK_UN);
> -#endif
>      fclose(p_file);
>  }
>  
> @@ -349,7 +377,7 @@ Store(vlc_keystore *p_keystore, const char *const
> ppsz_values[KEY_MAX],
>      i_ret = file_save(p_keystore, p_file, i_fd, &list);
>  
>  end:
> -    file_close(p_file, i_fd);
> +    file_close(p_file);
>      ks_list_free(&list);
>      return i_ret;
>  }
> @@ -413,7 +441,7 @@ Find(vlc_keystore *p_keystore, const char *const
> ppsz_values[KEY_MAX],
>  
>      *pp_entries = out_list.p_entries;
>  end:
> -    file_close(p_file, i_fd);
> +    file_close(p_file);
>      ks_list_free(&list);
>      return out_list.i_count;
>  }
> @@ -445,7 +473,7 @@ Remove(vlc_keystore *p_keystore, const char *const
> ppsz_values[KEY_MAX])
>          i_count = 0;
>  
>  end:
> -    file_close(p_file, i_fd);
> +    file_close(p_file);
>      ks_list_free(&list);
>      return i_count;
>  }
> -- 
> 1.7.9.2
> 
> _______________________________________________
> vlc-devel mailing list
> To unsubscribe or modify your subscription options:
> https://mailman.videolan.org/listinfo/vlc-devel


More information about the vlc-devel mailing list