misc/libphysfs/platform_posix.c
changeset 13881 99b265e0d1d0
parent 13880 5f819b90d479
child 13882 b172a5d40eee
equal deleted inserted replaced
13880:5f819b90d479 13881:99b265e0d1d0
     1 /*
       
     2  * Posix-esque support routines for PhysicsFS.
       
     3  *
       
     4  * Please see the file LICENSE.txt in the source's root directory.
       
     5  *
       
     6  *  This file written by Ryan C. Gordon.
       
     7  */
       
     8 
       
     9 /* !!! FIXME: check for EINTR? */
       
    10 
       
    11 #define __PHYSICSFS_INTERNAL__
       
    12 #include "physfs_platforms.h"
       
    13 
       
    14 #ifdef PHYSFS_PLATFORM_POSIX
       
    15 
       
    16 #include <unistd.h>
       
    17 #include <ctype.h>
       
    18 #include <sys/types.h>
       
    19 #include <sys/stat.h>
       
    20 #include <pwd.h>
       
    21 #include <dirent.h>
       
    22 #include <errno.h>
       
    23 #include <fcntl.h>
       
    24 
       
    25 #if ((!defined PHYSFS_NO_THREAD_SUPPORT) && (!defined PHYSFS_PLATFORM_BEOS))
       
    26 #include <pthread.h>
       
    27 #endif
       
    28 
       
    29 #include "physfs_internal.h"
       
    30 
       
    31 
       
    32 static PHYSFS_ErrorCode errcodeFromErrnoError(const int err)
       
    33 {
       
    34     switch (err)
       
    35     {
       
    36         case 0: return PHYSFS_ERR_OK;
       
    37         case EACCES: return PHYSFS_ERR_PERMISSION;
       
    38         case EPERM: return PHYSFS_ERR_PERMISSION;
       
    39         case EDQUOT: return PHYSFS_ERR_NO_SPACE;
       
    40         case EIO: return PHYSFS_ERR_IO;
       
    41         case ELOOP: return PHYSFS_ERR_SYMLINK_LOOP;
       
    42         case EMLINK: return PHYSFS_ERR_NO_SPACE;
       
    43         case ENAMETOOLONG: return PHYSFS_ERR_BAD_FILENAME;
       
    44         case ENOENT: return PHYSFS_ERR_NOT_FOUND;
       
    45         case ENOSPC: return PHYSFS_ERR_NO_SPACE;
       
    46         case ENOTDIR: return PHYSFS_ERR_NOT_FOUND;
       
    47         case EISDIR: return PHYSFS_ERR_NOT_A_FILE;
       
    48         case EROFS: return PHYSFS_ERR_READ_ONLY;
       
    49         case ETXTBSY: return PHYSFS_ERR_BUSY;
       
    50         case EBUSY: return PHYSFS_ERR_BUSY;
       
    51         case ENOMEM: return PHYSFS_ERR_OUT_OF_MEMORY;
       
    52         case ENOTEMPTY: return PHYSFS_ERR_DIR_NOT_EMPTY;
       
    53         default: return PHYSFS_ERR_OS_ERROR;
       
    54     } /* switch */
       
    55 } /* errcodeFromErrnoError */
       
    56 
       
    57 
       
    58 static inline PHYSFS_ErrorCode errcodeFromErrno(void)
       
    59 {
       
    60     return errcodeFromErrnoError(errno);
       
    61 } /* errcodeFromErrno */
       
    62 
       
    63 
       
    64 static char *getUserDirByUID(void)
       
    65 {
       
    66     uid_t uid = getuid();
       
    67     struct passwd *pw;
       
    68     char *retval = NULL;
       
    69 
       
    70     pw = getpwuid(uid);
       
    71     if ((pw != NULL) && (pw->pw_dir != NULL) && (*pw->pw_dir != '\0'))
       
    72     {
       
    73         const size_t dlen = strlen(pw->pw_dir);
       
    74         const size_t add_dirsep = (pw->pw_dir[dlen-1] != '/') ? 1 : 0;
       
    75         retval = (char *) allocator.Malloc(dlen + 1 + add_dirsep);
       
    76         if (retval != NULL)
       
    77         {
       
    78             strcpy(retval, pw->pw_dir);
       
    79             if (add_dirsep)
       
    80             {
       
    81                 retval[dlen] = '/';
       
    82                 retval[dlen+1] = '\0';
       
    83             } /* if */
       
    84         } /* if */
       
    85     } /* if */
       
    86     
       
    87     return retval;
       
    88 } /* getUserDirByUID */
       
    89 
       
    90 
       
    91 char *__PHYSFS_platformCalcUserDir(void)
       
    92 {
       
    93     char *retval = NULL;
       
    94     char *envr = getenv("HOME");
       
    95 
       
    96     /* if the environment variable was set, make sure it's really a dir. */
       
    97     if (envr != NULL)
       
    98     {
       
    99         struct stat statbuf;
       
   100         if ((stat(envr, &statbuf) != -1) && (S_ISDIR(statbuf.st_mode)))
       
   101         {
       
   102             const size_t envrlen = strlen(envr);
       
   103             const size_t add_dirsep = (envr[envrlen-1] != '/') ? 1 : 0;
       
   104             retval = allocator.Malloc(envrlen + 1 + add_dirsep);
       
   105             if (retval)
       
   106             {
       
   107                 strcpy(retval, envr);
       
   108                 if (add_dirsep)
       
   109                 {
       
   110                     retval[envrlen] = '/';
       
   111                     retval[envrlen+1] = '\0';
       
   112                 } /* if */
       
   113             } /* if */
       
   114         } /* if */
       
   115     } /* if */
       
   116 
       
   117     if (retval == NULL)
       
   118         retval = getUserDirByUID();
       
   119 
       
   120     return retval;
       
   121 } /* __PHYSFS_platformCalcUserDir */
       
   122 
       
   123 
       
   124 void __PHYSFS_platformEnumerateFiles(const char *dirname,
       
   125                                      PHYSFS_EnumFilesCallback callback,
       
   126                                      const char *origdir,
       
   127                                      void *callbackdata)
       
   128 {
       
   129     DIR *dir;
       
   130     struct dirent *ent;
       
   131     char *buf = NULL;
       
   132 
       
   133     errno = 0;
       
   134     dir = opendir(dirname);
       
   135     if (dir == NULL)
       
   136     {
       
   137         allocator.Free(buf);
       
   138         return;
       
   139     } /* if */
       
   140 
       
   141     while ((ent = readdir(dir)) != NULL)
       
   142     {
       
   143         if (strcmp(ent->d_name, ".") == 0)
       
   144             continue;
       
   145         else if (strcmp(ent->d_name, "..") == 0)
       
   146             continue;
       
   147 
       
   148         callback(callbackdata, origdir, ent->d_name);
       
   149     } /* while */
       
   150 
       
   151     allocator.Free(buf);
       
   152     closedir(dir);
       
   153 } /* __PHYSFS_platformEnumerateFiles */
       
   154 
       
   155 
       
   156 int __PHYSFS_platformMkDir(const char *path)
       
   157 {
       
   158     const int rc = mkdir(path, S_IRWXU);
       
   159     BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
       
   160     return 1;
       
   161 } /* __PHYSFS_platformMkDir */
       
   162 
       
   163 
       
   164 static void *doOpen(const char *filename, int mode)
       
   165 {
       
   166     const int appending = (mode & O_APPEND);
       
   167     int fd;
       
   168     int *retval;
       
   169     errno = 0;
       
   170 
       
   171     /* O_APPEND doesn't actually behave as we'd like. */
       
   172     mode &= ~O_APPEND;
       
   173 
       
   174     fd = open(filename, mode, S_IRUSR | S_IWUSR);
       
   175     BAIL_IF_MACRO(fd < 0, errcodeFromErrno(), NULL);
       
   176 
       
   177     if (appending)
       
   178     {
       
   179         if (lseek(fd, 0, SEEK_END) < 0)
       
   180         {
       
   181             const int err = errno;
       
   182             close(fd);
       
   183             BAIL_MACRO(errcodeFromErrnoError(err), NULL);
       
   184         } /* if */
       
   185     } /* if */
       
   186 
       
   187     retval = (int *) allocator.Malloc(sizeof (int));
       
   188     if (!retval)
       
   189     {
       
   190         close(fd);
       
   191         BAIL_MACRO(PHYSFS_ERR_OUT_OF_MEMORY, NULL);
       
   192     } /* if */
       
   193 
       
   194     *retval = fd;
       
   195     return ((void *) retval);
       
   196 } /* doOpen */
       
   197 
       
   198 
       
   199 void *__PHYSFS_platformOpenRead(const char *filename)
       
   200 {
       
   201     return doOpen(filename, O_RDONLY);
       
   202 } /* __PHYSFS_platformOpenRead */
       
   203 
       
   204 
       
   205 void *__PHYSFS_platformOpenWrite(const char *filename)
       
   206 {
       
   207     return doOpen(filename, O_WRONLY | O_CREAT | O_TRUNC);
       
   208 } /* __PHYSFS_platformOpenWrite */
       
   209 
       
   210 
       
   211 void *__PHYSFS_platformOpenAppend(const char *filename)
       
   212 {
       
   213     return doOpen(filename, O_WRONLY | O_CREAT | O_APPEND);
       
   214 } /* __PHYSFS_platformOpenAppend */
       
   215 
       
   216 
       
   217 PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer,
       
   218                                     PHYSFS_uint64 len)
       
   219 {
       
   220     const int fd = *((int *) opaque);
       
   221     ssize_t rc = 0;
       
   222 
       
   223     if (!__PHYSFS_ui64FitsAddressSpace(len))
       
   224         BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
       
   225 
       
   226     rc = read(fd, buffer, (size_t) len);
       
   227     BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), -1);
       
   228     assert(rc >= 0);
       
   229     assert(rc <= len);
       
   230     return (PHYSFS_sint64) rc;
       
   231 } /* __PHYSFS_platformRead */
       
   232 
       
   233 
       
   234 PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer,
       
   235                                      PHYSFS_uint64 len)
       
   236 {
       
   237     const int fd = *((int *) opaque);
       
   238     ssize_t rc = 0;
       
   239 
       
   240     if (!__PHYSFS_ui64FitsAddressSpace(len))
       
   241         BAIL_MACRO(PHYSFS_ERR_INVALID_ARGUMENT, -1);
       
   242 
       
   243     rc = write(fd, (void *) buffer, (size_t) len);
       
   244     BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), rc);
       
   245     assert(rc >= 0);
       
   246     assert(rc <= len);
       
   247     return (PHYSFS_sint64) rc;
       
   248 } /* __PHYSFS_platformWrite */
       
   249 
       
   250 
       
   251 int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos)
       
   252 {
       
   253     const int fd = *((int *) opaque);
       
   254     const int rc = lseek(fd, (off_t) pos, SEEK_SET);
       
   255     BAIL_IF_MACRO(rc == -1, errcodeFromErrno(), 0);
       
   256     return 1;
       
   257 } /* __PHYSFS_platformSeek */
       
   258 
       
   259 
       
   260 PHYSFS_sint64 __PHYSFS_platformTell(void *opaque)
       
   261 {
       
   262     const int fd = *((int *) opaque);
       
   263     PHYSFS_sint64 retval;
       
   264     retval = (PHYSFS_sint64) lseek(fd, 0, SEEK_CUR);
       
   265     BAIL_IF_MACRO(retval == -1, errcodeFromErrno(), -1);
       
   266     return retval;
       
   267 } /* __PHYSFS_platformTell */
       
   268 
       
   269 
       
   270 PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque)
       
   271 {
       
   272     const int fd = *((int *) opaque);
       
   273     struct stat statbuf;
       
   274     BAIL_IF_MACRO(fstat(fd, &statbuf) == -1, errcodeFromErrno(), -1);
       
   275     return ((PHYSFS_sint64) statbuf.st_size);
       
   276 } /* __PHYSFS_platformFileLength */
       
   277 
       
   278 
       
   279 int __PHYSFS_platformFlush(void *opaque)
       
   280 {
       
   281     const int fd = *((int *) opaque);
       
   282     if ((fcntl(fd, F_GETFL) & O_ACCMODE) != O_RDONLY)
       
   283         BAIL_IF_MACRO(fsync(fd) == -1, errcodeFromErrno(), 0);
       
   284     return 1;
       
   285 } /* __PHYSFS_platformFlush */
       
   286 
       
   287 
       
   288 void __PHYSFS_platformClose(void *opaque)
       
   289 {
       
   290     const int fd = *((int *) opaque);
       
   291     (void) close(fd);  /* we don't check this. You should have used flush! */
       
   292     allocator.Free(opaque);
       
   293 } /* __PHYSFS_platformClose */
       
   294 
       
   295 
       
   296 int __PHYSFS_platformDelete(const char *path)
       
   297 {
       
   298     BAIL_IF_MACRO(remove(path) == -1, errcodeFromErrno(), 0);
       
   299     return 1;
       
   300 } /* __PHYSFS_platformDelete */
       
   301 
       
   302 
       
   303 int __PHYSFS_platformStat(const char *filename, PHYSFS_Stat *st)
       
   304 {
       
   305     struct stat statbuf;
       
   306 
       
   307     BAIL_IF_MACRO(lstat(filename, &statbuf) == -1, errcodeFromErrno(), 0);
       
   308 
       
   309     if (S_ISREG(statbuf.st_mode))
       
   310     {
       
   311         st->filetype = PHYSFS_FILETYPE_REGULAR;
       
   312         st->filesize = statbuf.st_size;
       
   313     } /* if */
       
   314 
       
   315     else if(S_ISDIR(statbuf.st_mode))
       
   316     {
       
   317         st->filetype = PHYSFS_FILETYPE_DIRECTORY;
       
   318         st->filesize = 0;
       
   319     } /* else if */
       
   320 
       
   321     else if(S_ISLNK(statbuf.st_mode))
       
   322     {
       
   323         st->filetype = PHYSFS_FILETYPE_SYMLINK;
       
   324         st->filesize = 0;
       
   325     } /* else if */
       
   326 
       
   327     else
       
   328     {
       
   329         st->filetype = PHYSFS_FILETYPE_OTHER;
       
   330         st->filesize = statbuf.st_size;
       
   331     } /* else */
       
   332 
       
   333     st->modtime = statbuf.st_mtime;
       
   334     st->createtime = statbuf.st_ctime;
       
   335     st->accesstime = statbuf.st_atime;
       
   336 
       
   337     /* !!! FIXME: maybe we should just report full permissions? */
       
   338     st->readonly = access(filename, W_OK);
       
   339     return 1;
       
   340 } /* __PHYSFS_platformStat */
       
   341 
       
   342 
       
   343 #ifndef PHYSFS_PLATFORM_BEOS  /* BeOS has its own code in platform_beos.cpp */
       
   344 #if (defined PHYSFS_NO_THREAD_SUPPORT)
       
   345 
       
   346 void *__PHYSFS_platformGetThreadID(void) { return ((void *) 0x0001); }
       
   347 void *__PHYSFS_platformCreateMutex(void) { return ((void *) 0x0001); }
       
   348 void __PHYSFS_platformDestroyMutex(void *mutex) {}
       
   349 int __PHYSFS_platformGrabMutex(void *mutex) { return 1; }
       
   350 void __PHYSFS_platformReleaseMutex(void *mutex) {}
       
   351 
       
   352 #else
       
   353 
       
   354 typedef struct
       
   355 {
       
   356     pthread_mutex_t mutex;
       
   357     pthread_t owner;
       
   358     PHYSFS_uint32 count;
       
   359 } PthreadMutex;
       
   360 
       
   361 
       
   362 void *__PHYSFS_platformGetThreadID(void)
       
   363 {
       
   364     return ( (void *) ((size_t) pthread_self()) );
       
   365 } /* __PHYSFS_platformGetThreadID */
       
   366 
       
   367 
       
   368 void *__PHYSFS_platformCreateMutex(void)
       
   369 {
       
   370     int rc;
       
   371     PthreadMutex *m = (PthreadMutex *) allocator.Malloc(sizeof (PthreadMutex));
       
   372     BAIL_IF_MACRO(!m, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
       
   373     rc = pthread_mutex_init(&m->mutex, NULL);
       
   374     if (rc != 0)
       
   375     {
       
   376         allocator.Free(m);
       
   377         BAIL_MACRO(PHYSFS_ERR_OS_ERROR, NULL);
       
   378     } /* if */
       
   379 
       
   380     m->count = 0;
       
   381     m->owner = (pthread_t) 0xDEADBEEF;
       
   382     return ((void *) m);
       
   383 } /* __PHYSFS_platformCreateMutex */
       
   384 
       
   385 
       
   386 void __PHYSFS_platformDestroyMutex(void *mutex)
       
   387 {
       
   388     PthreadMutex *m = (PthreadMutex *) mutex;
       
   389 
       
   390     /* Destroying a locked mutex is a bug, but we'll try to be helpful. */
       
   391     if ((m->owner == pthread_self()) && (m->count > 0))
       
   392         pthread_mutex_unlock(&m->mutex);
       
   393 
       
   394     pthread_mutex_destroy(&m->mutex);
       
   395     allocator.Free(m);
       
   396 } /* __PHYSFS_platformDestroyMutex */
       
   397 
       
   398 
       
   399 int __PHYSFS_platformGrabMutex(void *mutex)
       
   400 {
       
   401     PthreadMutex *m = (PthreadMutex *) mutex;
       
   402     pthread_t tid = pthread_self();
       
   403     if (m->owner != tid)
       
   404     {
       
   405         if (pthread_mutex_lock(&m->mutex) != 0)
       
   406             return 0;
       
   407         m->owner = tid;
       
   408     } /* if */
       
   409 
       
   410     m->count++;
       
   411     return 1;
       
   412 } /* __PHYSFS_platformGrabMutex */
       
   413 
       
   414 
       
   415 void __PHYSFS_platformReleaseMutex(void *mutex)
       
   416 {
       
   417     PthreadMutex *m = (PthreadMutex *) mutex;
       
   418     assert(m->owner == pthread_self());  /* catch programming errors. */
       
   419     assert(m->count > 0);  /* catch programming errors. */
       
   420     if (m->owner == pthread_self())
       
   421     {
       
   422         if (--m->count == 0)
       
   423         {
       
   424             m->owner = (pthread_t) 0xDEADBEEF;
       
   425             pthread_mutex_unlock(&m->mutex);
       
   426         } /* if */
       
   427     } /* if */
       
   428 } /* __PHYSFS_platformReleaseMutex */
       
   429 
       
   430 #endif /* !PHYSFS_NO_THREAD_SUPPORT */
       
   431 #endif /* !PHYSFS_PLATFORM_BEOS */
       
   432 
       
   433 #endif  /* PHYSFS_PLATFORM_POSIX */
       
   434 
       
   435 /* end of posix.c ... */
       
   436