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