Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

os_oflags.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 1997-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: os_oflags.c,v 12.2 2005/06/16 20:23:26 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 #include <sys/stat.h>
00015 
00016 #ifdef HAVE_SHMGET
00017 #include <sys/ipc.h>
00018 #include <sys/shm.h>
00019 #endif
00020 
00021 #include <fcntl.h>
00022 #endif
00023 
00024 #include "db_int.h"
00025 
00026 /*
00027  * __db_oflags --
00028  *      Convert open(2) flags to DB flags.
00029  *
00030  * PUBLIC: u_int32_t __db_oflags __P((int));
00031  */
00032 u_int32_t
00033 __db_oflags(oflags)
00034         int oflags;
00035 {
00036         u_int32_t dbflags;
00037 
00038         dbflags = 0;
00039 
00040         if (oflags & O_CREAT)
00041                 dbflags |= DB_CREATE;
00042 
00043         if (oflags & O_TRUNC)
00044                 dbflags |= DB_TRUNCATE;
00045 
00046         /*
00047          * !!!
00048          * Convert POSIX 1003.1 open(2) mode flags to DB flags.  This isn't
00049          * an exact science as few POSIX implementations have a flag value
00050          * for O_RDONLY, it's simply the lack of a write flag.
00051          */
00052 #ifndef O_ACCMODE
00053 #define O_ACCMODE       (O_RDONLY | O_RDWR | O_WRONLY)
00054 #endif
00055         switch (oflags & O_ACCMODE) {
00056         case O_RDWR:
00057         case O_WRONLY:
00058                 break;
00059         default:
00060                 dbflags |= DB_RDONLY;
00061                 break;
00062         }
00063         return (dbflags);
00064 }
00065 
00066 #ifdef DB_WIN32
00067 #ifndef S_IRUSR
00068 #define S_IRUSR S_IREAD         /* R for owner */
00069 #endif
00070 #ifndef S_IWUSR
00071 #define S_IWUSR S_IWRITE        /* W for owner */
00072 #endif
00073 #ifndef S_IXUSR
00074 #define S_IXUSR 0               /* X for owner */
00075 #endif
00076 #ifndef S_IRGRP
00077 #define S_IRGRP 0               /* R for group */
00078 #endif
00079 #ifndef S_IWGRP
00080 #define S_IWGRP 0               /* W for group */
00081 #endif
00082 #ifndef S_IXGRP
00083 #define S_IXGRP 0               /* X for group */
00084 #endif
00085 #ifndef S_IROTH
00086 #define S_IROTH 0               /* R for other */
00087 #endif
00088 #ifndef S_IWOTH
00089 #define S_IWOTH 0               /* W for other */
00090 #endif
00091 #ifndef S_IXOTH
00092 #define S_IXOTH 0               /* X for other */
00093 #endif
00094 #else
00095 #ifndef S_IRUSR
00096 #define S_IRUSR 0000400         /* R for owner */
00097 #endif
00098 #ifndef S_IWUSR
00099 #define S_IWUSR 0000200         /* W for owner */
00100 #endif
00101 #ifndef S_IXUSR
00102 #define S_IXUSR 0000100         /* X for owner */
00103 #endif
00104 #ifndef S_IRGRP
00105 #define S_IRGRP 0000040         /* R for group */
00106 #endif
00107 #ifndef S_IWGRP
00108 #define S_IWGRP 0000020         /* W for group */
00109 #endif
00110 #ifndef S_IXGRP
00111 #define S_IXGRP 0000010         /* X for group */
00112 #endif
00113 #ifndef S_IROTH
00114 #define S_IROTH 0000004         /* R for other */
00115 #endif
00116 #ifndef S_IWOTH
00117 #define S_IWOTH 0000002         /* W for other */
00118 #endif
00119 #ifndef S_IXOTH
00120 #define S_IXOTH 0000001         /* X for other */
00121 #endif
00122 #endif /* DB_WIN32 */
00123 
00124 /*
00125  * __db_omode --
00126  *      Convert a permission string to the correct open(2) flags.
00127  *
00128  * PUBLIC: int __db_omode __P((const char *));
00129  */
00130 int
00131 __db_omode(perm)
00132         const char *perm;
00133 {
00134         int mode;
00135         mode = 0;
00136         if (perm[0] == 'r')
00137                 mode |= S_IRUSR;
00138         if (perm[1] == 'w')
00139                 mode |= S_IWUSR;
00140         if (perm[2] == 'x')
00141                 mode |= S_IXUSR;
00142         if (perm[3] == 'r')
00143                 mode |= S_IRGRP;
00144         if (perm[4] == 'w')
00145                 mode |= S_IWGRP;
00146         if (perm[5] == 'x')
00147                 mode |= S_IXGRP;
00148         if (perm[6] == 'r')
00149                 mode |= S_IROTH;
00150         if (perm[7] == 'w')
00151                 mode |= S_IWOTH;
00152         if (perm[8] == 'x')
00153                 mode |= S_IXOTH;
00154         return (mode);
00155 }
00156 
00157 #ifdef HAVE_SHMGET
00158 
00159 #ifndef SHM_R
00160 #define SHM_R   0400
00161 #endif
00162 #ifndef SHM_W
00163 #define SHM_W   0200
00164 #endif
00165 
00166 /*
00167  * __db_shm_mode --
00168  *      Map the DbEnv::open method file mode permissions to shmget call
00169  *      permissions.
00170  *
00171  * PUBLIC: int __db_shm_mode __P((DB_ENV *));
00172  */
00173 int
00174 __db_shm_mode(dbenv)
00175         DB_ENV *dbenv;
00176 {
00177         int mode;
00178 
00179         /* Default to r/w owner, r/w group. */
00180         if (dbenv->db_mode == 0)
00181                 return (SHM_R | SHM_W | SHM_R >> 3 | SHM_W >> 3);
00182 
00183         mode = 0;
00184         if (dbenv->db_mode & S_IRUSR)
00185                 mode |= SHM_R;
00186         if (dbenv->db_mode & S_IWUSR)
00187                 mode |= SHM_W;
00188         if (dbenv->db_mode & S_IRGRP)
00189                 mode |= SHM_R >> 3;
00190         if (dbenv->db_mode & S_IWGRP)
00191                 mode |= SHM_W >> 3;
00192         if (dbenv->db_mode & S_IROTH)
00193                 mode |= SHM_R >> 6;
00194         if (dbenv->db_mode & S_IWOTH)
00195                 mode |= SHM_W >> 6;
00196         return (mode);
00197 }
00198 #endif

Generated on Sun Dec 25 12:14:42 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2