archivers/grp.c
changeset 648 5c993684b8f2
parent 504 3420d82f9b01
child 650 298b8bb26775
equal deleted inserted replaced
647:c83b6827fc7d 648:5c993684b8f2
    59     GRPentry *entry;
    59     GRPentry *entry;
    60     PHYSFS_uint32 curPos;
    60     PHYSFS_uint32 curPos;
    61 } GRPfileinfo;
    61 } GRPfileinfo;
    62 
    62 
    63 
    63 
    64 static void GRP_dirClose(DirHandle *h);
    64 static void GRP_dirClose(void *opaque);
    65 static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
    65 static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
    66                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    66                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    67 static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
    67 static PHYSFS_sint64 GRP_write(FileHandle *handle, const void *buffer,
    68                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    68                                PHYSFS_uint32 objSize, PHYSFS_uint32 objCount);
    69 static int GRP_eof(FileHandle *handle);
    69 static int GRP_eof(FileHandle *handle);
    70 static PHYSFS_sint64 GRP_tell(FileHandle *handle);
    70 static PHYSFS_sint64 GRP_tell(FileHandle *handle);
    71 static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
    71 static int GRP_seek(FileHandle *handle, PHYSFS_uint64 offset);
    72 static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
    72 static PHYSFS_sint64 GRP_fileLength(FileHandle *handle);
    73 static int GRP_fileClose(FileHandle *handle);
    73 static int GRP_fileClose(FileHandle *handle);
    74 static int GRP_isArchive(const char *filename, int forWriting);
    74 static int GRP_isArchive(const char *filename, int forWriting);
    75 static DirHandle *GRP_openArchive(const char *name, int forWriting);
    75 static void *GRP_openArchive(const char *name, int forWriting);
    76 static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
    76 static LinkedStringList *GRP_enumerateFiles(void *opaque,
    77                                             const char *dirname,
    77                                             const char *dirname,
    78                                             int omitSymLinks);
    78                                             int omitSymLinks);
    79 static int GRP_exists(DirHandle *h, const char *name);
    79 static int GRP_exists(void *opaque, const char *name);
    80 static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists);
    80 static int GRP_isDirectory(void *opaque, const char *name, int *fileExists);
    81 static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists);
    81 static int GRP_isSymLink(void *opaque, const char *name, int *fileExists);
    82 static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h, const char *n, int *e);
    82 static PHYSFS_sint64 GRP_getLastModTime(void *opaque, const char *n, int *e);
    83 static FileHandle *GRP_openRead(DirHandle *h, const char *name, int *exist);
    83 static FileHandle *GRP_openRead(void *opaque, const char *name, int *exist);
    84 static FileHandle *GRP_openWrite(DirHandle *h, const char *name);
    84 static FileHandle *GRP_openWrite(void *opaque, const char *name);
    85 static FileHandle *GRP_openAppend(DirHandle *h, const char *name);
    85 static FileHandle *GRP_openAppend(void *opaque, const char *name);
    86 static int GRP_remove(DirHandle *h, const char *name);
    86 static int GRP_remove(void *opaque, const char *name);
    87 static int GRP_mkdir(DirHandle *h, const char *name);
    87 static int GRP_mkdir(void *opaque, const char *name);
    88 
    88 
    89 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
    89 const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_GRP =
    90 {
    90 {
    91     "GRP",
    91     "GRP",
    92     GRP_ARCHIVE_DESCRIPTION,
    92     GRP_ARCHIVE_DESCRIPTION,
   125     GRP_dirClose            /* dirClose() method       */
   125     GRP_dirClose            /* dirClose() method       */
   126 };
   126 };
   127 
   127 
   128 
   128 
   129 
   129 
   130 static void GRP_dirClose(DirHandle *h)
   130 static void GRP_dirClose(void *opaque)
   131 {
   131 {
   132     GRPinfo *info = ((GRPinfo *) h->opaque);
   132     GRPinfo *info = ((GRPinfo *) opaque);
   133     free(info->filename);
   133     free(info->filename);
   134     free(info->entries);
   134     free(info->entries);
   135     free(info);
   135     free(info);
   136     free(h);
       
   137 } /* GRP_dirClose */
   136 } /* GRP_dirClose */
   138 
   137 
   139 
   138 
   140 static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
   139 static PHYSFS_sint64 GRP_read(FileHandle *handle, void *buffer,
   141                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   140                               PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
   327                   grp_entry_cmp, grp_entry_swap);
   326                   grp_entry_cmp, grp_entry_swap);
   328     return(1);
   327     return(1);
   329 } /* grp_load_entries */
   328 } /* grp_load_entries */
   330 
   329 
   331 
   330 
   332 static DirHandle *GRP_openArchive(const char *name, int forWriting)
   331 static void *GRP_openArchive(const char *name, int forWriting)
   333 {
   332 {
   334     GRPinfo *info;
       
   335     DirHandle *retval = malloc(sizeof (DirHandle));
       
   336     PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
   333     PHYSFS_sint64 modtime = __PHYSFS_platformGetLastModTime(name);
   337 
   334     GRPinfo *info = malloc(sizeof (GRPinfo));
   338     BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
   335 
   339     info = retval->opaque = malloc(sizeof (GRPinfo));
   336     BAIL_IF_MACRO(info == NULL, ERR_OUT_OF_MEMORY, 0);
   340     if (info == NULL)
   337 
       
   338     memset(info, '\0', sizeof (GRPinfo));
       
   339     info->filename = (char *) malloc(strlen(name) + 1);
       
   340     if (info->filename == NULL)
   341     {
   341     {
   342         __PHYSFS_setError(ERR_OUT_OF_MEMORY);
   342         __PHYSFS_setError(ERR_OUT_OF_MEMORY);
   343         goto GRP_openArchive_failed;
   343         goto GRP_openArchive_failed;
   344     } /* if */
   344     } /* if */
   345 
   345 
   346     memset(info, '\0', sizeof (GRPinfo));
       
   347 
       
   348     info->filename = (char *) malloc(strlen(name) + 1);
       
   349     if (info->filename == NULL)
       
   350     {
       
   351         __PHYSFS_setError(ERR_OUT_OF_MEMORY);
       
   352         goto GRP_openArchive_failed;
       
   353     } /* if */
       
   354 
       
   355     if (!grp_load_entries(name, forWriting, info))
   346     if (!grp_load_entries(name, forWriting, info))
   356         goto GRP_openArchive_failed;
   347         goto GRP_openArchive_failed;
   357 
   348 
   358     strcpy(info->filename, name);
   349     strcpy(info->filename, name);
   359     info->last_mod_time = modtime;
   350     info->last_mod_time = modtime;
   360     retval->funcs = &__PHYSFS_DirFunctions_GRP;
   351 
   361     return(retval);
   352     return(info);
   362 
   353 
   363 GRP_openArchive_failed:
   354 GRP_openArchive_failed:
   364     if (retval != NULL)
   355     if (info != NULL)
   365     {
   356     {
   366         if (retval->opaque != NULL)
   357         if (info->filename != NULL)
   367         {
   358             free(info->filename);
   368             if (info->filename != NULL)
   359         if (info->entries != NULL)
   369                 free(info->filename);
   360             free(info->entries);
   370             if (info->entries != NULL)
   361         free(info);
   371                 free(info->entries);
       
   372             free(info);
       
   373         } /* if */
       
   374         free(retval);
       
   375     } /* if */
   362     } /* if */
   376 
   363 
   377     return(NULL);
   364     return(NULL);
   378 } /* GRP_openArchive */
   365 } /* GRP_openArchive */
   379 
   366 
   380 
   367 
   381 static LinkedStringList *GRP_enumerateFiles(DirHandle *h,
   368 static LinkedStringList *GRP_enumerateFiles(void *opaque,
   382                                             const char *dirname,
   369                                             const char *dirname,
   383                                             int omitSymLinks)
   370                                             int omitSymLinks)
   384 {
   371 {
   385     GRPinfo *info = ((GRPinfo *) h->opaque);
   372     GRPinfo *info = ((GRPinfo *) opaque);
   386     GRPentry *entry = info->entries;
   373     GRPentry *entry = info->entries;
   387     LinkedStringList *retval = NULL, *p = NULL;
   374     LinkedStringList *retval = NULL, *p = NULL;
   388     PHYSFS_uint32 max = info->entryCount;
   375     PHYSFS_uint32 max = info->entryCount;
   389     PHYSFS_uint32 i;
   376     PHYSFS_uint32 i;
   390 
   377 
   429 
   416 
   430     BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
   417     BAIL_MACRO(ERR_NO_SUCH_FILE, NULL);
   431 } /* grp_find_entry */
   418 } /* grp_find_entry */
   432 
   419 
   433 
   420 
   434 static int GRP_exists(DirHandle *h, const char *name)
   421 static int GRP_exists(void *opaque, const char *name)
   435 {
   422 {
   436     return(grp_find_entry(((GRPinfo *) h->opaque), name) != NULL);
   423     return(grp_find_entry(((GRPinfo *) opaque), name) != NULL);
   437 } /* GRP_exists */
   424 } /* GRP_exists */
   438 
   425 
   439 
   426 
   440 static int GRP_isDirectory(DirHandle *h, const char *name, int *fileExists)
   427 static int GRP_isDirectory(void *opaque, const char *name, int *fileExists)
   441 {
   428 {
   442     *fileExists = GRP_exists(h, name);
   429     *fileExists = GRP_exists(opaque, name);
   443     return(0);  /* never directories in a groupfile. */
   430     return(0);  /* never directories in a groupfile. */
   444 } /* GRP_isDirectory */
   431 } /* GRP_isDirectory */
   445 
   432 
   446 
   433 
   447 static int GRP_isSymLink(DirHandle *h, const char *name, int *fileExists)
   434 static int GRP_isSymLink(void *opaque, const char *name, int *fileExists)
   448 {
   435 {
   449     *fileExists = GRP_exists(h, name);
   436     *fileExists = GRP_exists(opaque, name);
   450     return(0);  /* never symlinks in a groupfile. */
   437     return(0);  /* never symlinks in a groupfile. */
   451 } /* GRP_isSymLink */
   438 } /* GRP_isSymLink */
   452 
   439 
   453 
   440 
   454 static PHYSFS_sint64 GRP_getLastModTime(DirHandle *h,
   441 static PHYSFS_sint64 GRP_getLastModTime(void *opaque,
   455                                         const char *name,
   442                                         const char *name,
   456                                         int *fileExists)
   443                                         int *fileExists)
   457 {
   444 {
   458     GRPinfo *info = ((GRPinfo *) h->opaque);
   445     GRPinfo *info = ((GRPinfo *) opaque);
   459     PHYSFS_sint64 retval = -1;
   446     PHYSFS_sint64 retval = -1;
   460 
   447 
   461     *fileExists = (grp_find_entry(info, name) != NULL);
   448     *fileExists = (grp_find_entry(info, name) != NULL);
   462     if (*fileExists)  /* use time of GRP itself in the physical filesystem. */
   449     if (*fileExists)  /* use time of GRP itself in the physical filesystem. */
   463         retval = ((GRPinfo *) h->opaque)->last_mod_time;
   450         retval = info->last_mod_time;
   464 
   451 
   465     return(retval);
   452     return(retval);
   466 } /* GRP_getLastModTime */
   453 } /* GRP_getLastModTime */
   467 
   454 
   468 
   455 
   469 static FileHandle *GRP_openRead(DirHandle *h, const char *fnm, int *fileExists)
   456 static FileHandle *GRP_openRead(void *opaque, const char *fnm, int *fileExists)
   470 {
   457 {
   471     GRPinfo *info = ((GRPinfo *) h->opaque);
   458     GRPinfo *info = ((GRPinfo *) opaque);
   472     FileHandle *retval;
   459     FileHandle *retval;
   473     GRPfileinfo *finfo;
   460     GRPfileinfo *finfo;
   474     GRPentry *entry;
   461     GRPentry *entry;
   475 
   462 
   476     entry = grp_find_entry(info, fnm);
   463     entry = grp_find_entry(info, fnm);
   497 
   484 
   498     finfo->curPos = 0;
   485     finfo->curPos = 0;
   499     finfo->entry = entry;
   486     finfo->entry = entry;
   500     retval->opaque = (void *) finfo;
   487     retval->opaque = (void *) finfo;
   501     retval->funcs = &__PHYSFS_FileFunctions_GRP;
   488     retval->funcs = &__PHYSFS_FileFunctions_GRP;
   502     retval->dirHandle = h;
       
   503     return(retval);
   489     return(retval);
   504 } /* GRP_openRead */
   490 } /* GRP_openRead */
   505 
   491 
   506 
   492 
   507 static FileHandle *GRP_openWrite(DirHandle *h, const char *name)
   493 static FileHandle *GRP_openWrite(void *opaque, const char *name)
   508 {
   494 {
   509     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   495     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   510 } /* GRP_openWrite */
   496 } /* GRP_openWrite */
   511 
   497 
   512 
   498 
   513 static FileHandle *GRP_openAppend(DirHandle *h, const char *name)
   499 static FileHandle *GRP_openAppend(void *opaque, const char *name)
   514 {
   500 {
   515     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   501     BAIL_MACRO(ERR_NOT_SUPPORTED, NULL);
   516 } /* GRP_openAppend */
   502 } /* GRP_openAppend */
   517 
   503 
   518 
   504 
   519 static int GRP_remove(DirHandle *h, const char *name)
   505 static int GRP_remove(void *opaque, const char *name)
   520 {
   506 {
   521     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   507     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   522 } /* GRP_remove */
   508 } /* GRP_remove */
   523 
   509 
   524 
   510 
   525 static int GRP_mkdir(DirHandle *h, const char *name)
   511 static int GRP_mkdir(void *opaque, const char *name)
   526 {
   512 {
   527     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   513     BAIL_MACRO(ERR_NOT_SUPPORTED, 0);
   528 } /* GRP_mkdir */
   514 } /* GRP_mkdir */
   529 
   515 
   530 #endif  /* defined PHYSFS_SUPPORTS_GRP */
   516 #endif  /* defined PHYSFS_SUPPORTS_GRP */