diff --git a/src/terminal/typescript.c b/src/terminal/typescript.c index 2460cadb..9127d785 100644 --- a/src/terminal/typescript.c +++ b/src/terminal/typescript.c @@ -26,6 +26,7 @@ #include +#include #include #include #include @@ -34,21 +35,88 @@ #include #include +/** + * Attempts to open a new typescript data file within the given path and having + * the given name. If such a file already exists, sequential numeric suffixes + * (.1, .2, .3, etc.) are appended until a filename is found which does not + * exist (or until the maximum number of numeric suffixes has been tried). If + * the file absolutely cannot be opened due to an error, -1 is returned and + * errno is set appropriately. + * + * @param path + * The full path to the directory in which the data file should be created. + * + * @param name + * The name of the data file which should be crated within the given path. + * + * @param basename + * A buffer in which the path, a path separator, the filename, any + * necessary suffix, and a NULL terminator will be stored. If insufficient + * space is available, -1 will be returned, and errno will be set to + * ENAMETOOLONG. + * + * @param basename_size + * The number of bytes available within the provided basename buffer. + * + * @return + * The file descriptor of the open data file if open succeeded, or -1 on + * failure. + */ +static int guac_terminal_typescript_open_data_file(const char* path, + const char* name, char* basename, int basename_size) { + + int i; + + /* Concatenate path and name (separated by a single slash) */ + int basename_length = snprintf(basename, + basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH , + "%s/%s", path, name); + + /* Abort if maximum length reached */ + if (basename_length == + basename_size - GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX_LENGTH) { + errno = ENAMETOOLONG; + return -1; + } + + /* Attempt to open typescript data file */ + int data_fd = open(basename, + O_CREAT | O_EXCL | O_WRONLY, + S_IRUSR | S_IWUSR); + + /* Prepare basename for additional suffix */ + basename[basename_length] = '.'; + char* suffix = basename + basename_length + 1; + + /* Continue retrying alternative suffixes if file already exists */ + for (i = 1; data_fd == -1 && errno == EEXIST + && i <= GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX; i++) { + + /* 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); + + } + + return data_fd; + +} + guac_terminal_typescript* guac_terminal_typescript_alloc(const char* path, const char* name, int create_path) { + char basename[GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH]; + guac_terminal_typescript* typescript; int data_fd, timing_fd; - /* TODO: Determing data and timing filenames prior to open() calls. - * Be sure not to use open() itself to test for existence, as that could - * result in tons of typescript data files being unnecessarily created. - */ - /* Attempt to open typescript data file */ - data_fd = open("/tmp/typescript-data", - O_CREAT | O_EXCL | O_WRONLY, - S_IRUSR | S_IWUSR); + data_fd = guac_terminal_typescript_open_data_file(path, name, + basename, sizeof(basename)); if (data_fd == -1) return NULL; diff --git a/src/terminal/typescript.h b/src/terminal/typescript.h index b6bba80f..52f2f69d 100644 --- a/src/terminal/typescript.h +++ b/src/terminal/typescript.h @@ -47,6 +47,25 @@ */ #define GUAC_TERMINAL_TYPESCRIPT_MAX_DELAY 86400000 +/** + * The maximum numeric value allowed for the .1, .2, .3, etc. suffix appended + * to the end of the typescript filename if a typescript having the requested + * name already exists. + */ +#define GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX 255 + +/** + * The maximum length of the string containing a sequential numeric suffix + * between 1 and GUAC_TERMINAL_TYPESCRIPT_MAX_SUFFIX inclusive, in bytes. + */ +#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. + */ +#define GUAC_TERMINAL_TYPESCRIPT_MAX_NAME_LENGTH 2048 + /** * An active typescript, consisting of a data file (raw terminal output) and * timing file (related timestamps and byte counts).