server: Replace switch by a table to map disposition to file creation flags

Dmitry Timoshkov dmitry at codeweavers.com
Wed Sep 12 01:04:43 CDT 2007


Hello,

Changelog:
    server: Replace switch by a table to map disposition to file creation flags.

---
 server/file.c       |   58 +++++++++++++++++++++++++++-----------------------
 server/protocol.def |    2 +-
 server/trace.c      |    2 +-
 3 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/server/file.c b/server/file.c
index a8152da..df1fca4 100644
--- a/server/file.c
+++ b/server/file.c
@@ -147,30 +147,35 @@ static struct object *create_file_obj( struct fd *fd, unsigned int access, mode_
 }
 
 static struct object *create_file( const char *nameptr, data_size_t len, unsigned int access,
-                                   unsigned int sharing, int create, unsigned int options,
-                                   unsigned int attrs )
+                                   unsigned int sharing, unsigned int create,
+                                   unsigned int options, unsigned int attrs )
 {
+    static const int flags[FILE_MAXIMUM_DISPOSITION + 1] =
+    {
+        O_CREAT | O_TRUNC, /* FILE_SUPERSEDE */
+        0,                 /* FILE_OPEN */
+        O_CREAT | O_EXCL,  /* FILE_CREATE */
+        O_CREAT,           /* FILE_OPEN_IF */
+        O_TRUNC,           /* FILE_OVERWRITE */
+        O_CREAT | O_TRUNC  /* FILE_OVERWRITE_IF
+                              FIXME: the difference with FILE_SUPERSEDE is
+                              whether we trash existing attr or not */
+    };
     struct object *obj = NULL;
     struct fd *fd;
-    int flags;
     char *name;
     mode_t mode;
 
+    if (create > FILE_MAXIMUM_DISPOSITION)
+    {
+        set_error( STATUS_INVALID_PARAMETER );
+        return NULL;
+    }
+
     if (!(name = mem_alloc( len + 1 ))) return NULL;
     memcpy( name, nameptr, len );
     name[len] = 0;
 
-    switch(create)
-    {
-    case FILE_CREATE:       flags = O_CREAT | O_EXCL; break;
-    case FILE_OVERWRITE_IF: /* FIXME: the difference is whether we trash existing attr or not */
-    case FILE_SUPERSEDE:    flags = O_CREAT | O_TRUNC; break;
-    case FILE_OPEN:         flags = 0; break;
-    case FILE_OPEN_IF:      flags = O_CREAT; break;
-    case FILE_OVERWRITE:    flags = O_TRUNC; break;
-    default:                set_error( STATUS_INVALID_PARAMETER ); goto done;
-    }
-
     mode = (attrs & FILE_ATTRIBUTE_READONLY) ? 0444 : 0666;
 
     if (len >= 4 &&
@@ -180,19 +185,18 @@ static struct object *create_file( const char *nameptr, data_size_t len, unsigne
     access = generic_file_map_access( access );
 
     /* FIXME: should set error to STATUS_OBJECT_NAME_COLLISION if file existed before */
-    fd = open_fd( name, flags | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
-    if (!fd) goto done;
-
-    if (S_ISDIR(mode))
-        obj = create_dir_obj( fd );
-    else if (S_ISCHR(mode) && is_serial_fd( fd ))
-        obj = create_serial( fd );
-    else
-        obj = create_file_obj( fd, access, mode );
-
-    release_object( fd );
-
-done:
+    fd = open_fd( name, flags[create] | O_NONBLOCK | O_LARGEFILE, &mode, access, sharing, options );
+    if (fd)
+    {
+        if (S_ISDIR(mode))
+            obj = create_dir_obj( fd );
+        else if (S_ISCHR(mode) && is_serial_fd( fd ))
+            obj = create_serial( fd );
+        else
+            obj = create_file_obj( fd, access, mode );
+
+        release_object( fd );
+    }
     free( name );
     return obj;
 }
diff --git a/server/protocol.def b/server/protocol.def
index e210e43..6739b4b 100644
--- a/server/protocol.def
+++ b/server/protocol.def
@@ -839,7 +839,7 @@ enum event_op { PULSE_EVENT, SET_EVENT, RESET_EVENT };
     unsigned int access;        /* wanted access rights */
     unsigned int attributes;    /* object attributes */
     unsigned int sharing;       /* sharing flags */
-    int          create;        /* file create action */
+    unsigned int create;        /* file create action */
     unsigned int options;       /* file options */
     unsigned int attrs;         /* file attributes for creation */
     VARARG(filename,string);    /* file name */
diff --git a/server/trace.c b/server/trace.c
index 49f4479..08e990e 100644
--- a/server/trace.c
+++ b/server/trace.c
@@ -1242,7 +1242,7 @@ static void dump_create_file_request( const struct create_file_request *req )
     fprintf( stderr, " access=%08x,", req->access );
     fprintf( stderr, " attributes=%08x,", req->attributes );
     fprintf( stderr, " sharing=%08x,", req->sharing );
-    fprintf( stderr, " create=%d,", req->create );
+    fprintf( stderr, " create=%08x,", req->create );
     fprintf( stderr, " options=%08x,", req->options );
     fprintf( stderr, " attrs=%08x,", req->attrs );
     fprintf( stderr, " filename=" );
-- 
1.5.3.1






More information about the wine-patches mailing list