From da7168e161302f2893825786c3f99cc09338d0dd Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Mon, 21 Oct 2013 23:52:15 -0700 Subject: [PATCH] Correct (theoretically) semantics of open create disposition. --- src/protocols/rdp/guac_rdpdr/rdpdr_fs.c | 40 +++++++++++++------------ 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c index 7b2bff93..d63800e0 100644 --- a/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c +++ b/src/protocols/rdp/guac_rdpdr/rdpdr_fs.c @@ -35,6 +35,7 @@ * * ***** END LICENSE BLOCK ***** */ +#include #include #include #include @@ -151,36 +152,44 @@ int guac_rdpdr_fs_open(guac_rdpdr_device* device, if (access & ACCESS_FILE_APPEND_DATA) flags |= O_APPEND; + /* Normalize path, return no-such-file if invalid */ + if (guac_rdpdr_fs_normalize_path(path, normalized_path)) + return GUAC_RDPDR_FS_ENOENT; + + /* Translate normalized path to real path */ + __guac_rdpdr_fs_translate_path(device, normalized_path, real_path); + switch (create_disposition) { - /* Supersede (replace) if exists, otherwise create */ - case DISP_FILE_SUPERSEDE: - flags |= O_TRUNC | O_CREAT; + /* Create if not exist, fail otherwise */ + case DISP_FILE_CREATE: + flags |= O_CREAT | O_EXCL; break; /* Open file if exists and do not overwrite, fail otherwise */ case DISP_FILE_OPEN: - flags |= O_APPEND; - break; - - /* Create if not exist, fail otherwise */ - case DISP_FILE_CREATE: - flags |= O_EXCL; + /* No flag necessary - default functionality of open */ break; /* Open if exists, create otherwise */ case DISP_FILE_OPEN_IF: - flags |= O_APPEND | O_CREAT; + flags |= O_CREAT; break; /* Overwrite if exists, fail otherwise */ case DISP_FILE_OVERWRITE: - /* No flag necessary - default functionality of open */ + flags |= O_TRUNC; break; /* Overwrite if exists, create otherwise */ case DISP_FILE_OVERWRITE_IF: - flags |= O_CREAT; + flags |= O_CREAT | O_TRUNC; + break; + + /* Supersede (replace) if exists, otherwise create */ + case DISP_FILE_SUPERSEDE: + unlink(real_path); + flags |= O_CREAT | O_TRUNC; break; /* Unrecognised disposition */ @@ -189,13 +198,6 @@ int guac_rdpdr_fs_open(guac_rdpdr_device* device, } - /* Normalize path, return no-such-file if invalid */ - if (guac_rdpdr_fs_normalize_path(path, normalized_path)) - return GUAC_RDPDR_FS_ENOENT; - - /* Translate normalized path to real path */ - __guac_rdpdr_fs_translate_path(device, normalized_path, real_path); - /* Open file */ fd = open(real_path, flags, 0600); if (fd == -1)