archivers/dir.c
changeset 467 99664d9842cb
parent 427 c38ace41039f
child 504 3420d82f9b01
equal deleted inserted replaced
466:1e3b97bcdee4 467:99664d9842cb
    21 #include "physfs_internal.h"
    21 #include "physfs_internal.h"
    22 
    22 
    23 static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
    23 static PHYSFS_sint64 DIR_read(FileHandle *handle, void *buffer,
    24                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    24                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    25 static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
    25 static PHYSFS_sint64 DIR_write(FileHandle *handle, const void *buffer,
       
    26                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
       
    27 static PHYSFS_sint64 DIR_dummyRead(FileHandle *handle, void *buffer,
       
    28                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
       
    29 static PHYSFS_sint64 DIR_dummyWrite(FileHandle *handle, const void *buffer,
    26                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    30                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    27 static int DIR_eof(FileHandle *handle);
    31 static int DIR_eof(FileHandle *handle);
    28 static PHYSFS_sint64 DIR_tell(FileHandle *handle);
    32 static PHYSFS_sint64 DIR_tell(FileHandle *handle);
    29 static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
    33 static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
    30 static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
    34 static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
    33 static DirHandle *DIR_openArchive(const char *name, int forWriting);
    37 static DirHandle *DIR_openArchive(const char *name, int forWriting);
    34 static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
    38 static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
    35                                             const char *dname,
    39                                             const char *dname,
    36                                             int omitSymLinks);
    40                                             int omitSymLinks);
    37 static int DIR_exists(DirHandle *h, const char *name);
    41 static int DIR_exists(DirHandle *h, const char *name);
    38 static int DIR_isDirectory(DirHandle *h, const char *name);
    42 static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists);
    39 static int DIR_isSymLink(DirHandle *h, const char *name);
    43 static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists);
    40 static FileHandle *DIR_openRead(DirHandle *h, const char *filename);
    44 static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist);
    41 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *name);
    45 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *f, int *e);
    42 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename);
    46 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename);
    43 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename);
    47 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename);
    44 static int DIR_remove(DirHandle *h, const char *name);
    48 static int DIR_remove(DirHandle *h, const char *name);
    45 static int DIR_mkdir(DirHandle *h, const char *name);
    49 static int DIR_mkdir(DirHandle *h, const char *name);
    46 static void DIR_dirClose(DirHandle *h);
    50 static void DIR_dirClose(DirHandle *h);
    56 
    60 
    57 
    61 
    58 static const FileFunctions __PHYSFS_FileFunctions_DIR =
    62 static const FileFunctions __PHYSFS_FileFunctions_DIR =
    59 {
    63 {
    60     DIR_read,       /* read() method       */
    64     DIR_read,       /* read() method       */
    61     NULL,           /* write() method      */
    65     DIR_dummyWrite, /* write() method      */
    62     DIR_eof,        /* eof() method        */
    66     DIR_eof,        /* eof() method        */
    63     DIR_tell,       /* tell() method       */
    67     DIR_tell,       /* tell() method       */
    64     DIR_seek,       /* seek() method       */
    68     DIR_seek,       /* seek() method       */
    65     DIR_fileLength, /* fileLength() method */
    69     DIR_fileLength, /* fileLength() method */
    66     DIR_fileClose   /* fileClose() method  */
    70     DIR_fileClose   /* fileClose() method  */
    67 };
    71 };
    68 
    72 
    69 
    73 
    70 static const FileFunctions __PHYSFS_FileFunctions_DIRW =
    74 static const FileFunctions __PHYSFS_FileFunctions_DIRW =
    71 {
    75 {
    72     NULL,           /* read() method       */
    76     DIR_dummyRead,  /* read() method       */
    73     DIR_write,      /* write() method      */
    77     DIR_write,      /* write() method      */
    74     DIR_eof,        /* eof() method        */
    78     DIR_eof,        /* eof() method        */
    75     DIR_tell,       /* tell() method       */
    79     DIR_tell,       /* tell() method       */
    76     DIR_seek,       /* seek() method       */
    80     DIR_seek,       /* seek() method       */
    77     DIR_fileLength, /* fileLength() method */
    81     DIR_fileLength, /* fileLength() method */
   114     retval = __PHYSFS_platformWrite(handle->opaque, buffer, objSize, objCount);
   118     retval = __PHYSFS_platformWrite(handle->opaque, buffer, objSize, objCount);
   115     return(retval);
   119     return(retval);
   116 } /* DIR_write */
   120 } /* DIR_write */
   117 
   121 
   118 
   122 
       
   123 static PHYSFS_sint64 DIR_dummyRead(FileHandle *handle, void *buffer,
       
   124                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
       
   125 {
       
   126     BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
       
   127 } /* DIR_dummyRead */
       
   128 
       
   129 
       
   130 static PHYSFS_sint64 DIR_dummyWrite(FileHandle *handle, const void *buffer,
       
   131                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
       
   132 {
       
   133     BAIL_MACRO(ERR_NOT_SUPPORTED, -1);
       
   134 } /* DIR_dummyWrite */
       
   135 
       
   136 
   119 static int DIR_eof(FileHandle *handle)
   137 static int DIR_eof(FileHandle *handle)
   120 {
   138 {
   121     return(__PHYSFS_platformEOF(handle->opaque));
   139     return(__PHYSFS_platformEOF(handle->opaque));
   122 } /* DIR_eof */
   140 } /* DIR_eof */
   123 
   141 
   215     free(f);
   233     free(f);
   216     return(retval);
   234     return(retval);
   217 } /* DIR_exists */
   235 } /* DIR_exists */
   218 
   236 
   219 
   237 
   220 static int DIR_isDirectory(DirHandle *h, const char *name)
   238 static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists)
   221 {
   239 {
   222     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   240     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   223     int retval;
   241     int retval = 0;
   224 
   242 
   225     BAIL_IF_MACRO(d == NULL, NULL, 0);
   243     BAIL_IF_MACRO(d == NULL, NULL, 0);
   226     retval = __PHYSFS_platformIsDirectory(d);
   244     *fileExists = __PHYSFS_platformExists(d);
       
   245     if (*fileExists)
       
   246         retval = __PHYSFS_platformIsDirectory(d);
   227     free(d);
   247     free(d);
   228     return(retval);
   248     return(retval);
   229 } /* DIR_isDirectory */
   249 } /* DIR_isDirectory */
   230 
   250 
   231 
   251 
   232 static int DIR_isSymLink(DirHandle *h, const char *name)
   252 static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists)
   233 {
   253 {
   234     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   254     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   235     int retval;
   255     int retval = 0;
   236 
   256 
   237     BAIL_IF_MACRO(f == NULL, NULL, 0);
   257     BAIL_IF_MACRO(f == NULL, NULL, 0);
   238     retval = __PHYSFS_platformIsSymLink(f);
   258     *fileExists = __PHYSFS_platformExists(f);
       
   259     if (*fileExists)
       
   260         retval = __PHYSFS_platformIsSymLink(f);
   239     free(f);
   261     free(f);
   240     return(retval);
   262     return(retval);
   241 } /* DIR_isSymLink */
   263 } /* DIR_isSymLink */
   242 
   264 
   243 
   265 
   244 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *name)
   266 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h,
       
   267                                         const char *name,
       
   268                                         int *fileExists)
   245 {
   269 {
   246     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   270     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   247     PHYSFS_sint64 retval;
   271     PHYSFS_sint64 retval = -1;
   248 
   272 
   249     BAIL_IF_MACRO(d == NULL, NULL, 0);
   273     BAIL_IF_MACRO(d == NULL, NULL, 0);
   250     retval = __PHYSFS_platformGetLastModTime(d);
   274     *fileExists = __PHYSFS_platformExists(d);
       
   275     if (*fileExists)
       
   276         retval = __PHYSFS_platformGetLastModTime(d);
   251     free(d);
   277     free(d);
   252     return(retval);
   278     return(retval);
   253 } /* DIR_getLastModTime */
   279 } /* DIR_getLastModTime */
   254 
   280 
   255 
   281 
   256 static FileHandle *doOpen(DirHandle *h, const char *name,
   282 static FileHandle *doOpen(DirHandle *h, const char *name,
   257                           void *(*openFunc)(const char *filename),
   283                           void *(*openFunc)(const char *filename),
   258                           const FileFunctions *fileFuncs)
   284                           int *fileExists, const FileFunctions *fileFuncs)
   259 {
   285 {
   260     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   286     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   261     void *rc;
   287     void *rc;
   262     FileHandle *retval;
   288     FileHandle *retval;
   263 
   289 
   264     BAIL_IF_MACRO(f == NULL, NULL, NULL);
   290     BAIL_IF_MACRO(f == NULL, NULL, NULL);
       
   291 
       
   292     if (fileExists != NULL)
       
   293     {
       
   294         *fileExists = __PHYSFS_platformExists(f);
       
   295         if (!(*fileExists))
       
   296         {
       
   297             free(f);
       
   298             return(NULL);
       
   299         } /* if */
       
   300     } /* if */
   265 
   301 
   266     retval = (FileHandle *) malloc(sizeof (FileHandle));
   302     retval = (FileHandle *) malloc(sizeof (FileHandle));
   267     if (!retval)
   303     if (!retval)
   268     {
   304     {
   269         free(f);
   305         free(f);
   285 
   321 
   286     return(retval);
   322     return(retval);
   287 } /* doOpen */
   323 } /* doOpen */
   288 
   324 
   289 
   325 
   290 static FileHandle *DIR_openRead(DirHandle *h, const char *filename)
   326 static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist)
   291 {
   327 {
   292     return(doOpen(h, filename, __PHYSFS_platformOpenRead,
   328     return(doOpen(h, fnm, __PHYSFS_platformOpenRead, exist,
   293                   &__PHYSFS_FileFunctions_DIR));
   329                   &__PHYSFS_FileFunctions_DIR));
   294 } /* DIR_openRead */
   330 } /* DIR_openRead */
   295 
   331 
   296 
   332 
   297 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
   333 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
   298 {
   334 {
   299     return(doOpen(h, filename, __PHYSFS_platformOpenWrite,
   335     return(doOpen(h, filename, __PHYSFS_platformOpenWrite, NULL,
   300                   &__PHYSFS_FileFunctions_DIRW));
   336                   &__PHYSFS_FileFunctions_DIRW));
   301 } /* DIR_openWrite */
   337 } /* DIR_openWrite */
   302 
   338 
   303 
   339 
   304 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
   340 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
   305 {
   341 {
   306     return(doOpen(h, filename, __PHYSFS_platformOpenAppend,
   342     return(doOpen(h, filename, __PHYSFS_platformOpenAppend, NULL,
   307                   &__PHYSFS_FileFunctions_DIRW));
   343                   &__PHYSFS_FileFunctions_DIRW));
   308 } /* DIR_openAppend */
   344 } /* DIR_openAppend */
   309 
   345 
   310 
   346 
   311 static int DIR_remove(DirHandle *h, const char *name)
   347 static int DIR_remove(DirHandle *h, const char *name)