misc/libphysfs/archiver_dir.c
changeset 12213 bb5522e88ab2
parent 8524 a65e9bcf0a03
equal deleted inserted replaced
12212:ea891871f481 12213:bb5522e88ab2
    41     PHYSFS_Stat st;
    41     PHYSFS_Stat st;
    42     const char dirsep = __PHYSFS_platformDirSeparator;
    42     const char dirsep = __PHYSFS_platformDirSeparator;
    43     char *retval = NULL;
    43     char *retval = NULL;
    44     const size_t namelen = strlen(name);
    44     const size_t namelen = strlen(name);
    45     const size_t seplen = 1;
    45     const size_t seplen = 1;
    46     int exists = 0;
       
    47 
    46 
    48     assert(io == NULL);  /* shouldn't create an Io for these. */
    47     assert(io == NULL);  /* shouldn't create an Io for these. */
    49     BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &st), ERRPASS, NULL);
    48     BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &st), ERRPASS, NULL);
    50     if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
    49     if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
    51         BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
    50         BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
    52 
    51 
    53     retval = allocator.Malloc(namelen + seplen + 1);
    52     retval = allocator.Malloc(namelen + seplen + 1);
    54     BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
    53     BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
    64 
    63 
    65     return retval;
    64     return retval;
    66 } /* DIR_openArchive */
    65 } /* DIR_openArchive */
    67 
    66 
    68 
    67 
    69 static void DIR_enumerateFiles(PHYSFS_Dir *opaque, const char *dname,
    68 static void DIR_enumerateFiles(void *opaque, const char *dname,
    70                                int omitSymLinks, PHYSFS_EnumFilesCallback cb,
    69                                PHYSFS_EnumFilesCallback cb,
    71                                const char *origdir, void *callbackdata)
    70                                const char *origdir, void *callbackdata)
    72 {
    71 {
    73     char *d;
    72     char *d;
    74 
    73 
    75     CVT_TO_DEPENDENT(d, opaque, dname);
    74     CVT_TO_DEPENDENT(d, opaque, dname);
    76     if (d != NULL)
    75     if (d != NULL)
    77     {
    76     {
    78         __PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
    77         __PHYSFS_platformEnumerateFiles(d, cb, origdir, callbackdata);
    79                                         origdir, callbackdata);
       
    80         __PHYSFS_smallFree(d);
    78         __PHYSFS_smallFree(d);
    81     } /* if */
    79     } /* if */
    82 } /* DIR_enumerateFiles */
    80 } /* DIR_enumerateFiles */
    83 
    81 
    84 
    82 
    85 static PHYSFS_Io *doOpen(PHYSFS_Dir *opaque, const char *name,
    83 static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
    86                          const int mode, int *fileExists)
       
    87 {
    84 {
    88     char *f;
       
    89     PHYSFS_Io *io = NULL;
    85     PHYSFS_Io *io = NULL;
    90     int existtmp = 0;
    86     char *f = NULL;
    91 
    87 
    92     CVT_TO_DEPENDENT(f, opaque, name);
    88     CVT_TO_DEPENDENT(f, opaque, name);
    93     BAIL_IF_MACRO(!f, ERRPASS, NULL);
    89     BAIL_IF_MACRO(!f, ERRPASS, NULL);
    94 
       
    95     if (fileExists == NULL)
       
    96         fileExists = &existtmp;
       
    97 
    90 
    98     io = __PHYSFS_createNativeIo(f, mode);
    91     io = __PHYSFS_createNativeIo(f, mode);
    99     if (io == NULL)
    92     if (io == NULL)
   100     {
    93     {
   101         const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
    94         const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
   102         PHYSFS_Stat statbuf;
    95         PHYSFS_Stat statbuf;
   103         __PHYSFS_platformStat(f, fileExists, &statbuf);
    96         __PHYSFS_platformStat(f, &statbuf);
   104         __PHYSFS_setError(err);
    97         PHYSFS_setErrorCode(err);
   105     } /* if */
    98     } /* if */
   106     else
       
   107     {
       
   108         *fileExists = 1;
       
   109     } /* else */
       
   110 
    99 
   111     __PHYSFS_smallFree(f);
   100     __PHYSFS_smallFree(f);
   112 
   101 
   113     return io;
   102     return io;
   114 } /* doOpen */
   103 } /* doOpen */
   115 
   104 
   116 
   105 
   117 static PHYSFS_Io *DIR_openRead(PHYSFS_Dir *opaque, const char *fnm, int *exist)
   106 static PHYSFS_Io *DIR_openRead(void *opaque, const char *filename)
   118 {
   107 {
   119     return doOpen(opaque, fnm, 'r', exist);
   108     return doOpen(opaque, filename, 'r');
   120 } /* DIR_openRead */
   109 } /* DIR_openRead */
   121 
   110 
   122 
   111 
   123 static PHYSFS_Io *DIR_openWrite(PHYSFS_Dir *opaque, const char *filename)
   112 static PHYSFS_Io *DIR_openWrite(void *opaque, const char *filename)
   124 {
   113 {
   125     return doOpen(opaque, filename, 'w', NULL);
   114     return doOpen(opaque, filename, 'w');
   126 } /* DIR_openWrite */
   115 } /* DIR_openWrite */
   127 
   116 
   128 
   117 
   129 static PHYSFS_Io *DIR_openAppend(PHYSFS_Dir *opaque, const char *filename)
   118 static PHYSFS_Io *DIR_openAppend(void *opaque, const char *filename)
   130 {
   119 {
   131     return doOpen(opaque, filename, 'a', NULL);
   120     return doOpen(opaque, filename, 'a');
   132 } /* DIR_openAppend */
   121 } /* DIR_openAppend */
   133 
   122 
   134 
   123 
   135 static int DIR_remove(PHYSFS_Dir *opaque, const char *name)
   124 static int DIR_remove(void *opaque, const char *name)
   136 {
   125 {
   137     int retval;
   126     int retval;
   138     char *f;
   127     char *f;
   139 
   128 
   140     CVT_TO_DEPENDENT(f, opaque, name);
   129     CVT_TO_DEPENDENT(f, opaque, name);
   143     __PHYSFS_smallFree(f);
   132     __PHYSFS_smallFree(f);
   144     return retval;
   133     return retval;
   145 } /* DIR_remove */
   134 } /* DIR_remove */
   146 
   135 
   147 
   136 
   148 static int DIR_mkdir(PHYSFS_Dir *opaque, const char *name)
   137 static int DIR_mkdir(void *opaque, const char *name)
   149 {
   138 {
   150     int retval;
   139     int retval;
   151     char *f;
   140     char *f;
   152 
   141 
   153     CVT_TO_DEPENDENT(f, opaque, name);
   142     CVT_TO_DEPENDENT(f, opaque, name);
   156     __PHYSFS_smallFree(f);
   145     __PHYSFS_smallFree(f);
   157     return retval;
   146     return retval;
   158 } /* DIR_mkdir */
   147 } /* DIR_mkdir */
   159 
   148 
   160 
   149 
   161 static void DIR_closeArchive(PHYSFS_Dir *opaque)
   150 static void DIR_closeArchive(void *opaque)
   162 {
   151 {
   163     allocator.Free(opaque);
   152     allocator.Free(opaque);
   164 } /* DIR_closeArchive */
   153 } /* DIR_closeArchive */
   165 
   154 
   166 
   155 
   167 static int DIR_stat(PHYSFS_Dir *opaque, const char *name,
   156 static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
   168                     int *exists, PHYSFS_Stat *stat)
       
   169 {
   157 {
   170     int retval = 0;
   158     int retval = 0;
   171     char *d;
   159     char *d;
   172 
   160 
   173     CVT_TO_DEPENDENT(d, opaque, name);
   161     CVT_TO_DEPENDENT(d, opaque, name);
   174     BAIL_IF_MACRO(!d, ERRPASS, 0);
   162     BAIL_IF_MACRO(!d, ERRPASS, 0);
   175     retval = __PHYSFS_platformStat(d, exists, stat);
   163     retval = __PHYSFS_platformStat(d, stat);
   176     __PHYSFS_smallFree(d);
   164     __PHYSFS_smallFree(d);
   177     return retval;
   165     return retval;
   178 } /* DIR_stat */
   166 } /* DIR_stat */
   179 
   167 
   180 
   168 
   181 const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
   169 const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
   182 {
   170 {
       
   171     CURRENT_PHYSFS_ARCHIVER_API_VERSION,
   183     {
   172     {
   184         "",
   173         "",
   185         "Non-archive, direct filesystem I/O",
   174         "Non-archive, direct filesystem I/O",
   186         "Ryan C. Gordon <icculus@icculus.org>",
   175         "Ryan C. Gordon <icculus@icculus.org>",
   187         "http://icculus.org/physfs/",
   176         "https://icculus.org/physfs/",
       
   177         1,  /* supportsSymlinks */
   188     },
   178     },
   189     DIR_openArchive,        /* openArchive() method    */
   179     DIR_openArchive,
   190     DIR_enumerateFiles,     /* enumerateFiles() method */
   180     DIR_enumerateFiles,
   191     DIR_openRead,           /* openRead() method       */
   181     DIR_openRead,
   192     DIR_openWrite,          /* openWrite() method      */
   182     DIR_openWrite,
   193     DIR_openAppend,         /* openAppend() method     */
   183     DIR_openAppend,
   194     DIR_remove,             /* remove() method         */
   184     DIR_remove,
   195     DIR_mkdir,              /* mkdir() method          */
   185     DIR_mkdir,
   196     DIR_closeArchive,       /* closeArchive() method   */
   186     DIR_stat,
   197     DIR_stat                /* stat() method           */
   187     DIR_closeArchive
   198 };
   188 };
   199 
   189 
   200 /* end of dir.c ... */
   190 /* end of archiver_dir.c ... */
   201 
   191