Correct (theoretically) semantics of open create disposition.

This commit is contained in:
Michael Jumper 2013-10-21 23:52:15 -07:00
parent f9cf524993
commit da7168e161

View File

@ -35,6 +35,7 @@
* *
* ***** END LICENSE BLOCK ***** */ * ***** END LICENSE BLOCK ***** */
#include <unistd.h>
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <dirent.h> #include <dirent.h>
@ -151,36 +152,44 @@ int guac_rdpdr_fs_open(guac_rdpdr_device* device,
if (access & ACCESS_FILE_APPEND_DATA) if (access & ACCESS_FILE_APPEND_DATA)
flags |= O_APPEND; 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) { switch (create_disposition) {
/* Supersede (replace) if exists, otherwise create */ /* Create if not exist, fail otherwise */
case DISP_FILE_SUPERSEDE: case DISP_FILE_CREATE:
flags |= O_TRUNC | O_CREAT; flags |= O_CREAT | O_EXCL;
break; break;
/* Open file if exists and do not overwrite, fail otherwise */ /* Open file if exists and do not overwrite, fail otherwise */
case DISP_FILE_OPEN: case DISP_FILE_OPEN:
flags |= O_APPEND; /* No flag necessary - default functionality of open */
break;
/* Create if not exist, fail otherwise */
case DISP_FILE_CREATE:
flags |= O_EXCL;
break; break;
/* Open if exists, create otherwise */ /* Open if exists, create otherwise */
case DISP_FILE_OPEN_IF: case DISP_FILE_OPEN_IF:
flags |= O_APPEND | O_CREAT; flags |= O_CREAT;
break; break;
/* Overwrite if exists, fail otherwise */ /* Overwrite if exists, fail otherwise */
case DISP_FILE_OVERWRITE: case DISP_FILE_OVERWRITE:
/* No flag necessary - default functionality of open */ flags |= O_TRUNC;
break; break;
/* Overwrite if exists, create otherwise */ /* Overwrite if exists, create otherwise */
case DISP_FILE_OVERWRITE_IF: 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; break;
/* Unrecognised disposition */ /* 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 */ /* Open file */
fd = open(real_path, flags, 0600); fd = open(real_path, flags, 0600);
if (fd == -1) if (fd == -1)