archivers/dir.c
changeset 648 5c993684b8f2
parent 504 3420d82f9b01
child 650 298b8bb26775
equal deleted inserted replaced
647:c83b6827fc7d 648:5c993684b8f2
    30 static PHYSFS_sint64 DIR_tell(FileHandle *handle);
    30 static PHYSFS_sint64 DIR_tell(FileHandle *handle);
    31 static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
    31 static int DIR_seek(FileHandle *handle, PHYSFS_uint64 offset);
    32 static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
    32 static PHYSFS_sint64 DIR_fileLength(FileHandle *handle);
    33 static int DIR_fileClose(FileHandle *handle);
    33 static int DIR_fileClose(FileHandle *handle);
    34 static int DIR_isArchive(const char *filename, int forWriting);
    34 static int DIR_isArchive(const char *filename, int forWriting);
    35 static DirHandle *DIR_openArchive(const char *name, int forWriting);
    35 static void *DIR_openArchive(const char *name, int forWriting);
    36 static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
    36 static LinkedStringList *DIR_enumerateFiles(void *opaque,
    37                                             const char *dname,
    37                                             const char *dname,
    38                                             int omitSymLinks);
    38                                             int omitSymLinks);
    39 static int DIR_exists(DirHandle *h, const char *name);
    39 static int DIR_exists(void *opaque, const char *name);
    40 static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists);
    40 static int DIR_isDirectory(void *opaque, const char *name, int *fileExists);
    41 static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists);
    41 static int DIR_isSymLink(void *opaque, const char *name, int *fileExists);
    42 static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist);
    42 static FileHandle *DIR_openRead(void *opaque, const char *fnm, int *exist);
    43 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h, const char *f, int *e);
    43 static PHYSFS_sint64 DIR_getLastModTime(void *opaque, const char *f, int *e);
    44 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename);
    44 static FileHandle *DIR_openWrite(void *opaque, const char *filename);
    45 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename);
    45 static FileHandle *DIR_openAppend(void *opaque, const char *filename);
    46 static int DIR_remove(DirHandle *h, const char *name);
    46 static int DIR_remove(void *opaque, const char *name);
    47 static int DIR_mkdir(DirHandle *h, const char *name);
    47 static int DIR_mkdir(void *opaque, const char *name);
    48 static void DIR_dirClose(DirHandle *h);
    48 static void DIR_dirClose(void *opaque);
    49 
    49 
    50 
    50 
    51 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
    51 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
    52 {
    52 {
    53     "",
    53     "",
   175     /* directories ARE archives in this driver... */
   175     /* directories ARE archives in this driver... */
   176     return(__PHYSFS_platformIsDirectory(filename));
   176     return(__PHYSFS_platformIsDirectory(filename));
   177 } /* DIR_isArchive */
   177 } /* DIR_isArchive */
   178 
   178 
   179 
   179 
   180 static DirHandle *DIR_openArchive(const char *name, int forWriting)
   180 static void *DIR_openArchive(const char *name, int forWriting)
   181 {
   181 {
   182     const char *dirsep = PHYSFS_getDirSeparator();
   182     const char *dirsep = PHYSFS_getDirSeparator();
   183     DirHandle *retval = NULL;
   183     char *retval = NULL;
   184     size_t namelen = strlen(name);
   184     size_t namelen = strlen(name);
   185     size_t seplen = strlen(dirsep);
   185     size_t seplen = strlen(dirsep);
   186 
   186 
       
   187     /* !!! FIXME: when is this not called right before openArchive? */
   187     BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
   188     BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
   188                     ERR_UNSUPPORTED_ARCHIVE, NULL);
   189                     ERR_UNSUPPORTED_ARCHIVE, 0);
   189 
   190 
   190     retval = (DirHandle *) malloc(sizeof (DirHandle));
   191     retval = malloc(namelen + seplen + 1);
   191     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
   192     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
   192     retval->opaque = malloc(namelen + seplen + 1);
       
   193     if (retval->opaque == NULL)
       
   194     {
       
   195         free(retval);
       
   196         BAIL_MACRO(ERR_OUT_OF_MEMORY, NULL);
       
   197     } /* if */
       
   198 
   193 
   199         /* make sure there's a dir separator at the end of the string */
   194         /* make sure there's a dir separator at the end of the string */
   200     strcpy((char *) (retval->opaque), name);
   195     strcpy(retval, name);
   201     if (strcmp((name + namelen) - seplen, dirsep) != 0)
   196     if (strcmp((name + namelen) - seplen, dirsep) != 0)
   202         strcat((char *) (retval->opaque), dirsep);
   197         strcat(retval, dirsep);
   203 
       
   204     retval->funcs = &__PHYSFS_DirFunctions_DIR;
       
   205 
   198 
   206     return(retval);
   199     return(retval);
   207 } /* DIR_openArchive */
   200 } /* DIR_openArchive */
   208 
   201 
   209 
   202 
   210 static LinkedStringList *DIR_enumerateFiles(DirHandle *h,
   203 static LinkedStringList *DIR_enumerateFiles(void *opaque,
   211                                             const char *dname,
   204                                             const char *dname,
   212                                             int omitSymLinks)
   205                                             int omitSymLinks)
   213 {
   206 {
   214     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque),dname,NULL);
   207     char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
   215     LinkedStringList *retval;
   208     LinkedStringList *retval;
   216 
   209 
   217     BAIL_IF_MACRO(d == NULL, NULL, NULL);
   210     BAIL_IF_MACRO(d == NULL, NULL, NULL);
   218     retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
   211     retval = __PHYSFS_platformEnumerateFiles(d, omitSymLinks);
   219     free(d);
   212     free(d);
   220     return(retval);
   213     return(retval);
   221 } /* DIR_enumerateFiles */
   214 } /* DIR_enumerateFiles */
   222 
   215 
   223 
   216 
   224 static int DIR_exists(DirHandle *h, const char *name)
   217 static int DIR_exists(void *opaque, const char *name)
   225 {
   218 {
   226     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   219     char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   227     int retval;
   220     int retval;
   228 
   221 
   229     BAIL_IF_MACRO(f == NULL, NULL, 0);
   222     BAIL_IF_MACRO(f == NULL, NULL, 0);
   230     retval = __PHYSFS_platformExists(f);
   223     retval = __PHYSFS_platformExists(f);
   231     free(f);
   224     free(f);
   232     return(retval);
   225     return(retval);
   233 } /* DIR_exists */
   226 } /* DIR_exists */
   234 
   227 
   235 
   228 
   236 static int DIR_isDirectory(DirHandle *h, const char *name, int *fileExists)
   229 static int DIR_isDirectory(void *opaque, const char *name, int *fileExists)
   237 {
   230 {
   238     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   231     char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   239     int retval = 0;
   232     int retval = 0;
   240 
   233 
   241     BAIL_IF_MACRO(d == NULL, NULL, 0);
   234     BAIL_IF_MACRO(d == NULL, NULL, 0);
   242     *fileExists = __PHYSFS_platformExists(d);
   235     *fileExists = __PHYSFS_platformExists(d);
   243     if (*fileExists)
   236     if (*fileExists)
   245     free(d);
   238     free(d);
   246     return(retval);
   239     return(retval);
   247 } /* DIR_isDirectory */
   240 } /* DIR_isDirectory */
   248 
   241 
   249 
   242 
   250 static int DIR_isSymLink(DirHandle *h, const char *name, int *fileExists)
   243 static int DIR_isSymLink(void *opaque, const char *name, int *fileExists)
   251 {
   244 {
   252     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   245     char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   253     int retval = 0;
   246     int retval = 0;
   254 
   247 
   255     BAIL_IF_MACRO(f == NULL, NULL, 0);
   248     BAIL_IF_MACRO(f == NULL, NULL, 0);
   256     *fileExists = __PHYSFS_platformExists(f);
   249     *fileExists = __PHYSFS_platformExists(f);
   257     if (*fileExists)
   250     if (*fileExists)
   259     free(f);
   252     free(f);
   260     return(retval);
   253     return(retval);
   261 } /* DIR_isSymLink */
   254 } /* DIR_isSymLink */
   262 
   255 
   263 
   256 
   264 static PHYSFS_sint64 DIR_getLastModTime(DirHandle *h,
   257 static PHYSFS_sint64 DIR_getLastModTime(void *opaque,
   265                                         const char *name,
   258                                         const char *name,
   266                                         int *fileExists)
   259                                         int *fileExists)
   267 {
   260 {
   268     char *d = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   261     char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   269     PHYSFS_sint64 retval = -1;
   262     PHYSFS_sint64 retval = -1;
   270 
   263 
   271     BAIL_IF_MACRO(d == NULL, NULL, 0);
   264     BAIL_IF_MACRO(d == NULL, NULL, 0);
   272     *fileExists = __PHYSFS_platformExists(d);
   265     *fileExists = __PHYSFS_platformExists(d);
   273     if (*fileExists)
   266     if (*fileExists)
   275     free(d);
   268     free(d);
   276     return(retval);
   269     return(retval);
   277 } /* DIR_getLastModTime */
   270 } /* DIR_getLastModTime */
   278 
   271 
   279 
   272 
   280 static FileHandle *doOpen(DirHandle *h, const char *name,
   273 static FileHandle *doOpen(void *opaque, const char *name,
   281                           void *(*openFunc)(const char *filename),
   274                           void *(*openFunc)(const char *filename),
   282                           int *fileExists, const FileFunctions *fileFuncs)
   275                           int *fileExists, const FileFunctions *fileFuncs)
   283 {
   276 {
   284     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   277     char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   285     void *rc;
   278     void *rc;
   286     FileHandle *retval;
   279     FileHandle *retval;
   287 
   280 
   288     BAIL_IF_MACRO(f == NULL, NULL, NULL);
   281     BAIL_IF_MACRO(f == NULL, NULL, NULL);
   289 
   282 
   312         free(retval);
   305         free(retval);
   313         return(NULL);
   306         return(NULL);
   314     } /* if */
   307     } /* if */
   315 
   308 
   316     retval->opaque = (void *) rc;
   309     retval->opaque = (void *) rc;
   317     retval->dirHandle = h;
       
   318     retval->funcs = fileFuncs;
   310     retval->funcs = fileFuncs;
   319 
   311 
   320     return(retval);
   312     return(retval);
   321 } /* doOpen */
   313 } /* doOpen */
   322 
   314 
   323 
   315 
   324 static FileHandle *DIR_openRead(DirHandle *h, const char *fnm, int *exist)
   316 static FileHandle *DIR_openRead(void *opaque, const char *fnm, int *exist)
   325 {
   317 {
   326     return(doOpen(h, fnm, __PHYSFS_platformOpenRead, exist,
   318     return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist,
   327                   &__PHYSFS_FileFunctions_DIR));
   319                   &__PHYSFS_FileFunctions_DIR));
   328 } /* DIR_openRead */
   320 } /* DIR_openRead */
   329 
   321 
   330 
   322 
   331 static FileHandle *DIR_openWrite(DirHandle *h, const char *filename)
   323 static FileHandle *DIR_openWrite(void *opaque, const char *filename)
   332 {
   324 {
   333     return(doOpen(h, filename, __PHYSFS_platformOpenWrite, NULL,
   325     return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL,
   334                   &__PHYSFS_FileFunctions_DIRW));
   326                   &__PHYSFS_FileFunctions_DIRW));
   335 } /* DIR_openWrite */
   327 } /* DIR_openWrite */
   336 
   328 
   337 
   329 
   338 static FileHandle *DIR_openAppend(DirHandle *h, const char *filename)
   330 static FileHandle *DIR_openAppend(void *opaque, const char *filename)
   339 {
   331 {
   340     return(doOpen(h, filename, __PHYSFS_platformOpenAppend, NULL,
   332     return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL,
   341                   &__PHYSFS_FileFunctions_DIRW));
   333                   &__PHYSFS_FileFunctions_DIRW));
   342 } /* DIR_openAppend */
   334 } /* DIR_openAppend */
   343 
   335 
   344 
   336 
   345 static int DIR_remove(DirHandle *h, const char *name)
   337 static int DIR_remove(void *opaque, const char *name)
   346 {
   338 {
   347     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   339     char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   348     int retval;
   340     int retval;
   349 
   341 
   350     BAIL_IF_MACRO(f == NULL, NULL, 0);
   342     BAIL_IF_MACRO(f == NULL, NULL, 0);
   351     retval = __PHYSFS_platformDelete(f);
   343     retval = __PHYSFS_platformDelete(f);
   352     free(f);
   344     free(f);
   353     return(retval);
   345     return(retval);
   354 } /* DIR_remove */
   346 } /* DIR_remove */
   355 
   347 
   356 
   348 
   357 static int DIR_mkdir(DirHandle *h, const char *name)
   349 static int DIR_mkdir(void *opaque, const char *name)
   358 {
   350 {
   359     char *f = __PHYSFS_platformCvtToDependent((char *)(h->opaque), name, NULL);
   351     char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
   360     int retval;
   352     int retval;
   361 
   353 
   362     BAIL_IF_MACRO(f == NULL, NULL, 0);
   354     BAIL_IF_MACRO(f == NULL, NULL, 0);
   363     retval = __PHYSFS_platformMkDir(f);
   355     retval = __PHYSFS_platformMkDir(f);
   364     free(f);
   356     free(f);
   365     return(retval);
   357     return(retval);
   366 } /* DIR_mkdir */
   358 } /* DIR_mkdir */
   367 
   359 
   368 
   360 
   369 static void DIR_dirClose(DirHandle *h)
   361 static void DIR_dirClose(void *opaque)
   370 {
   362 {
   371     free(h->opaque);
   363     free(opaque);
   372     free(h);
       
   373 } /* DIR_dirClose */
   364 } /* DIR_dirClose */
   374 
   365 
   375 /* end of dir.c ... */
   366 /* end of dir.c ... */
   376 
   367