GUAC-236: Open output stream using file descriptor. Only write output file if it does not yet exist.

This commit is contained in:
Michael Jumper 2016-03-15 16:41:47 -07:00
parent 73bf5ce6f4
commit c50561ef10
2 changed files with 21 additions and 5 deletions

View File

@ -34,10 +34,14 @@
#include <guacamole/client.h> #include <guacamole/client.h>
#include <guacamole/timestamp.h> #include <guacamole/timestamp.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h> #include <assert.h>
#include <fcntl.h>
#include <inttypes.h> #include <inttypes.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name, guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name,
int width, int height, int bitrate) { int width, int height, int bitrate) {
@ -91,10 +95,18 @@ guacenc_video* guacenc_video_alloc(const char* path, const char* codec_name,
} }
/* Open output file */ /* Open output file */
FILE* output = fopen(path, "wb"); int fd = open(path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
if (output == NULL) { if (fd == -1) {
guacenc_log(GUAC_LOG_ERROR, "Failed to open output file \"%s\": %s", guacenc_log(GUAC_LOG_ERROR, "Failed to open output file \"%s\": %s",
path, strerror(errno)); path, strerror(errno));
goto fail_output_fd;
}
/* Create stream for output file */
FILE* output = fdopen(fd, "wb");
if (output == NULL) {
guacenc_log(GUAC_LOG_ERROR, "Failed to allocate stream for output "
"file \"%s\": %s", path, strerror(errno));
goto fail_output_file; goto fail_output_file;
} }
@ -123,6 +135,9 @@ fail_video:
fclose(output); fclose(output);
fail_output_file: fail_output_file:
close(fd);
fail_output_fd:
av_freep(&frame->data[0]); av_freep(&frame->data[0]);
fail_frame_data: fail_frame_data:

View File

@ -93,9 +93,10 @@ typedef struct guacenc_video {
/** /**
* Allocates a new guacenc_video which encodes video according to the given * Allocates a new guacenc_video which encodes video according to the given
* specifications, saving the output in the given file. The output file will be * specifications, saving the output in the given file. If the output file
* created if necessary and truncated if it already exists. Frames will be * already exists, encoding will be aborted, and the original file contents
* scaled up or down as necessary to fit the given width and height. * will be preserved. Frames will be scaled up or down as necessary to fit the
* given width and height.
* *
* @param path * @param path
* The full path to the file in which encoded video should be written. * The full path to the file in which encoded video should be written.