From 7c7a68975b5ad66aaa8f96cb0684dd43be42e8f5 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Wed, 27 Jan 2016 12:13:25 -0800 Subject: [PATCH] GUAC-1452: Append .timing to end of basename for timing file. --- src/terminal/typescript.c | 42 ++++++++++++++++++++++++--------------- src/terminal/typescript.h | 11 ++++++++-- 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/src/terminal/typescript.c b/src/terminal/typescript.c index 9d071104..f47f05bd 100644 --- a/src/terminal/typescript.c +++ b/src/terminal/typescript.c @@ -69,7 +69,7 @@ static int guac_terminal_typescript_open_data_file(const char* path, /* Concatenate path and name (separated by a single slash) */ int basename_length = snprintf(basename, - basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH , + basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH, "%s/%s", path, name); /* Abort if maximum length reached */ @@ -84,21 +84,26 @@ static int guac_terminal_typescript_open_data_file(const char* path, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); - /* Prepare basename for additional suffix */ - basename[basename_length] = '.'; - char* suffix = basename + basename_length + 1; + /* Continuously retry with alternate names on failure */ + if (data_fd == -1) { - /* Continue retrying alternative suffixes if file already exists */ - for (i = 1; data_fd == -1 && errno == EEXIST - && i <= GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX; i++) { + /* Prepare basename for additional suffix */ + basename[basename_length] = '.'; + char* suffix = &(basename[basename_length + 1]); - /* Append new suffix */ - sprintf(suffix, "%i", i); + /* Continue retrying alternative suffixes if file already exists */ + for (i = 1; data_fd == -1 && errno == EEXIST + && i <= GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX; i++) { - /* Retry with newly-suffixed filename */ - data_fd = open(basename, - O_CREAT | O_EXCL | O_WRONLY, - S_IRUSR | S_IWUSR); + /* Append new suffix */ + sprintf(suffix, "%i", i); + + /* Retry with newly-suffixed filename */ + data_fd = open(basename, + O_CREAT | O_EXCL | O_WRONLY, + S_IRUSR | S_IWUSR); + + } } @@ -110,6 +115,7 @@ guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path, const char* name, int create_path) { char basename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH]; + char timing_name[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH]; guac_terminal_typescript* typescript; int data_fd, timing_fd; @@ -119,13 +125,17 @@ guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path, return NULL; /* Attempt to open typescript data file */ - data_fd = guac_terminal_typescript_open_data_file(path, name, - basename, sizeof(basename)); + data_fd = guac_terminal_typescript_open_data_file(path, name, basename, + sizeof(basename) - sizeof(GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX)); if (data_fd == -1) return NULL; + /* Append suffix to basename */ + sprintf(timing_name, "%s.%s", basename, + GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX); + /* Attempt to open typescript timing file */ - timing_fd = open("/tmp/typescript-timing", + timing_fd = open(timing_name, O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR); if (timing_fd == -1) { diff --git a/src/terminal/typescript.h b/src/terminal/typescript.h index 52f2f69d..8075afea 100644 --- a/src/terminal/typescript.h +++ b/src/terminal/typescript.h @@ -56,16 +56,23 @@ /** * The maximum length of the string containing a sequential numeric suffix - * between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes. + * between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes, + * including NULL terminator. */ #define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH 4 /** * The maximum overall length of the full path to the typescript file, - * including any additional suffix, in bytes. + * including any additional suffix and NULL terminator, in bytes. */ #define GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH 2048 +/** + * The suffix which will be appended to the typescript data file's name to + * produce the name of the timing file. + */ +#define GUAC_TERMINAL_TYPESCRIPT_TIMING_SUFFIX "timing" + /** * An active typescript, consisting of a data file (raw terminal output) and * timing file (related timestamps and byte counts).