From 9d43e2259281dd8525e8339b6cec5d34bfaf0bcb Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 15 Mar 2016 17:08:39 -0700 Subject: [PATCH] GUAC-236: Acquire read lock on input files for guacenc. Refuse to encode in-progress recordings. --- src/guacenc/encode.c | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/src/guacenc/encode.c b/src/guacenc/encode.c index d7fe8eba..6e5fd022 100644 --- a/src/guacenc/encode.c +++ b/src/guacenc/encode.c @@ -30,10 +30,10 @@ #include #include -#include -#include #include #include +#include +#include #include #include @@ -93,6 +93,32 @@ int guacenc_encode(const char* path, const char* out_path, const char* codec, return 1; } + /* Lock entire input file for reading by the current process */ + struct flock file_lock = { + .l_type = F_RDLCK, + .l_whence = SEEK_SET, + .l_start = 0, + .l_len = 0, + .l_pid = getpid() + }; + + /* Abort if file cannot be locked for reading */ + if (fcntl(fd, F_SETLK, &file_lock) == -1) { + + /* Warn if lock cannot be acquired */ + if (errno == EACCES || errno == EAGAIN) + guacenc_log(GUAC_LOG_WARNING, "Refusing to encode in-progress " + "recording \"%s\".", path); + + /* Log an error if locking fails in an unexpected way */ + else + guacenc_log(GUAC_LOG_ERROR, "Cannot lock \"%s\" for reading: %s", + path, strerror(errno)); + + close(fd); + return 1; + } + /* Allocate display for encoding process */ guacenc_display* display = guacenc_display_alloc(out_path, codec, width, height, bitrate);