misc/libphysfs/archiver_dir.c
changeset 13881 99b265e0d1d0
parent 13880 5f819b90d479
child 13882 b172a5d40eee
equal deleted inserted replaced
13880:5f819b90d479 13881:99b265e0d1d0
     1 /*
       
     2  * Standard directory I/O 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 #define __PHYSICSFS_INTERNAL__
       
    10 #include "physfs_internal.h"
       
    11 
       
    12 /* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
       
    13 
       
    14 
       
    15 
       
    16 static char *cvtToDependent(const char *prepend, const char *path, char *buf)
       
    17 {
       
    18     BAIL_IF_MACRO(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
       
    19     sprintf(buf, "%s%s", prepend ? prepend : "", path);
       
    20 
       
    21     if (__PHYSFS_platformDirSeparator != '/')
       
    22     {
       
    23         char *p;
       
    24         for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/'))
       
    25             *p = __PHYSFS_platformDirSeparator;
       
    26     } /* if */
       
    27 
       
    28     return buf;
       
    29 } /* cvtToDependent */
       
    30 
       
    31 
       
    32 #define CVT_TO_DEPENDENT(buf, pre, dir) { \
       
    33     const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \
       
    34     buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len)); \
       
    35 }
       
    36 
       
    37 
       
    38 
       
    39 static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
       
    40 {
       
    41     PHYSFS_Stat st;
       
    42     const char dirsep = __PHYSFS_platformDirSeparator;
       
    43     char *retval = NULL;
       
    44     const size_t namelen = strlen(name);
       
    45     const size_t seplen = 1;
       
    46 
       
    47     assert(io == NULL);  /* shouldn't create an Io for these. */
       
    48     BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &st), ERRPASS, NULL);
       
    49     if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
       
    50         BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
       
    51 
       
    52     retval = allocator.Malloc(namelen + seplen + 1);
       
    53     BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
       
    54 
       
    55     strcpy(retval, name);
       
    56 
       
    57     /* make sure there's a dir separator at the end of the string */
       
    58     if (retval[namelen - 1] != dirsep)
       
    59     {
       
    60         retval[namelen] = dirsep;
       
    61         retval[namelen + 1] = '\0';
       
    62     } /* if */
       
    63 
       
    64     return retval;
       
    65 } /* DIR_openArchive */
       
    66 
       
    67 
       
    68 static void DIR_enumerateFiles(void *opaque, const char *dname,
       
    69                                PHYSFS_EnumFilesCallback cb,
       
    70                                const char *origdir, void *callbackdata)
       
    71 {
       
    72     char *d;
       
    73 
       
    74     CVT_TO_DEPENDENT(d, opaque, dname);
       
    75     if (d != NULL)
       
    76     {
       
    77         __PHYSFS_platformEnumerateFiles(d, cb, origdir, callbackdata);
       
    78         __PHYSFS_smallFree(d);
       
    79     } /* if */
       
    80 } /* DIR_enumerateFiles */
       
    81 
       
    82 
       
    83 static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
       
    84 {
       
    85     PHYSFS_Io *io = NULL;
       
    86     char *f = NULL;
       
    87 
       
    88     CVT_TO_DEPENDENT(f, opaque, name);
       
    89     BAIL_IF_MACRO(!f, ERRPASS, NULL);
       
    90 
       
    91     io = __PHYSFS_createNativeIo(f, mode);
       
    92     if (io == NULL)
       
    93     {
       
    94         const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
       
    95         PHYSFS_Stat statbuf;
       
    96         __PHYSFS_platformStat(f, &statbuf);
       
    97         PHYSFS_setErrorCode(err);
       
    98     } /* if */
       
    99 
       
   100     __PHYSFS_smallFree(f);
       
   101 
       
   102     return io;
       
   103 } /* doOpen */
       
   104 
       
   105 
       
   106 static PHYSFS_Io *DIR_openRead(void *opaque, const char *filename)
       
   107 {
       
   108     return doOpen(opaque, filename, 'r');
       
   109 } /* DIR_openRead */
       
   110 
       
   111 
       
   112 static PHYSFS_Io *DIR_openWrite(void *opaque, const char *filename)
       
   113 {
       
   114     return doOpen(opaque, filename, 'w');
       
   115 } /* DIR_openWrite */
       
   116 
       
   117 
       
   118 static PHYSFS_Io *DIR_openAppend(void *opaque, const char *filename)
       
   119 {
       
   120     return doOpen(opaque, filename, 'a');
       
   121 } /* DIR_openAppend */
       
   122 
       
   123 
       
   124 static int DIR_remove(void *opaque, const char *name)
       
   125 {
       
   126     int retval;
       
   127     char *f;
       
   128 
       
   129     CVT_TO_DEPENDENT(f, opaque, name);
       
   130     BAIL_IF_MACRO(!f, ERRPASS, 0);
       
   131     retval = __PHYSFS_platformDelete(f);
       
   132     __PHYSFS_smallFree(f);
       
   133     return retval;
       
   134 } /* DIR_remove */
       
   135 
       
   136 
       
   137 static int DIR_mkdir(void *opaque, const char *name)
       
   138 {
       
   139     int retval;
       
   140     char *f;
       
   141 
       
   142     CVT_TO_DEPENDENT(f, opaque, name);
       
   143     BAIL_IF_MACRO(!f, ERRPASS, 0);
       
   144     retval = __PHYSFS_platformMkDir(f);
       
   145     __PHYSFS_smallFree(f);
       
   146     return retval;
       
   147 } /* DIR_mkdir */
       
   148 
       
   149 
       
   150 static void DIR_closeArchive(void *opaque)
       
   151 {
       
   152     allocator.Free(opaque);
       
   153 } /* DIR_closeArchive */
       
   154 
       
   155 
       
   156 static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
       
   157 {
       
   158     int retval = 0;
       
   159     char *d;
       
   160 
       
   161     CVT_TO_DEPENDENT(d, opaque, name);
       
   162     BAIL_IF_MACRO(!d, ERRPASS, 0);
       
   163     retval = __PHYSFS_platformStat(d, stat);
       
   164     __PHYSFS_smallFree(d);
       
   165     return retval;
       
   166 } /* DIR_stat */
       
   167 
       
   168 
       
   169 const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
       
   170 {
       
   171     CURRENT_PHYSFS_ARCHIVER_API_VERSION,
       
   172     {
       
   173         "",
       
   174         "Non-archive, direct filesystem I/O",
       
   175         "Ryan C. Gordon <icculus@icculus.org>",
       
   176         "https://icculus.org/physfs/",
       
   177         1,  /* supportsSymlinks */
       
   178     },
       
   179     DIR_openArchive,
       
   180     DIR_enumerateFiles,
       
   181     DIR_openRead,
       
   182     DIR_openWrite,
       
   183     DIR_openAppend,
       
   184     DIR_remove,
       
   185     DIR_mkdir,
       
   186     DIR_stat,
       
   187     DIR_closeArchive
       
   188 };
       
   189 
       
   190 /* end of archiver_dir.c ... */
       
   191