/
physfs_archiver_dir.c
195 lines (149 loc) · 4.68 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
#if !__PHYSFS_STANDARD_DIRSEP
assert(__PHYSFS_platformDirSeparator != '/');
24
25
26
27
28
{
char *p;
for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = __PHYSFS_platformDirSeparator;
} /* if */
29
#endif
30
31
32
33
34
35
36
return buf;
} /* cvtToDependent */
#define CVT_TO_DEPENDENT(buf, pre, dir) { \
const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \
37
buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len),len); \
38
39
40
41
}
42
43
static void *DIR_openArchive(PHYSFS_Io *io, const char *name,
int forWriting, int *claimed)
44
{
45
PHYSFS_Stat st;
46
const char dirsep = __PHYSFS_platformDirSeparator;
47
char *retval = NULL;
48
const size_t namelen = strlen(name);
49
const size_t seplen = 1;
50
51
assert(io == NULL); /* shouldn't create an Io for these. */
52
BAIL_IF_ERRPASS(!__PHYSFS_platformStat(name, &st), NULL);
53
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
54
BAIL(PHYSFS_ERR_UNSUPPORTED, NULL);
55
56
*claimed = 1;
57
retval = allocator.Malloc(namelen + seplen + 1);
58
BAIL_IF(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
59
60
strcpy(retval, name);
61
62
63
64
65
66
67
/* 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 */
68
69
return retval;
70
71
72
} /* DIR_openArchive */
73
74
static PHYSFS_EnumerateCallbackResult DIR_enumerate(void *opaque,
const char *dname, PHYSFS_EnumerateCallback cb,
75
const char *origdir, void *callbackdata)
76
{
77
char *d;
78
PHYSFS_EnumerateCallbackResult retval;
79
CVT_TO_DEPENDENT(d, opaque, dname);
80
BAIL_IF_ERRPASS(!d, PHYSFS_ENUM_ERROR);
81
82
83
84
retval = __PHYSFS_platformEnumerate(d, cb, origdir, callbackdata);
__PHYSFS_smallFree(d);
return retval;
} /* DIR_enumerate */
85
86
87
static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
88
{
89
PHYSFS_Io *io = NULL;
90
char *f = NULL;
91
92
CVT_TO_DEPENDENT(f, opaque, name);
93
BAIL_IF_ERRPASS(!f, NULL);
94
95
96
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
97
{
98
99
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
PHYSFS_Stat statbuf;
100
__PHYSFS_platformStat(f, &statbuf);
101
PHYSFS_setErrorCode(err);
102
} /* if */
103
104
__PHYSFS_smallFree(f);
105
106
return io;
107
108
109
} /* doOpen */
110
static PHYSFS_Io *DIR_openRead(void *opaque, const char *filename)
111
{
112
return doOpen(opaque, filename, 'r');
113
114
115
} /* DIR_openRead */
116
static PHYSFS_Io *DIR_openWrite(void *opaque, const char *filename)
117
{
118
return doOpen(opaque, filename, 'w');
119
120
121
} /* DIR_openWrite */
122
static PHYSFS_Io *DIR_openAppend(void *opaque, const char *filename)
123
{
124
return doOpen(opaque, filename, 'a');
125
126
127
} /* DIR_openAppend */
128
static int DIR_remove(void *opaque, const char *name)
129
130
{
int retval;
131
char *f;
132
133
CVT_TO_DEPENDENT(f, opaque, name);
134
BAIL_IF_ERRPASS(!f, 0);
135
retval = __PHYSFS_platformDelete(f);
136
__PHYSFS_smallFree(f);
137
return retval;
138
139
140
} /* DIR_remove */
141
static int DIR_mkdir(void *opaque, const char *name)
142
143
{
int retval;
144
char *f;
145
146
CVT_TO_DEPENDENT(f, opaque, name);
147
BAIL_IF_ERRPASS(!f, 0);
148
retval = __PHYSFS_platformMkDir(f);
149
__PHYSFS_smallFree(f);
150
return retval;
151
152
153
} /* DIR_mkdir */
154
static void DIR_closeArchive(void *opaque)
155
{
156
allocator.Free(opaque);
157
} /* DIR_closeArchive */
158
159
160
static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
161
162
{
int retval = 0;
163
char *d;
164
165
CVT_TO_DEPENDENT(d, opaque, name);
166
BAIL_IF_ERRPASS(!d, 0);
167
retval = __PHYSFS_platformStat(d, stat);
168
__PHYSFS_smallFree(d);
169
170
171
return retval;
} /* DIR_stat */
172
173
174
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
175
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
176
177
178
179
{
"",
"Non-archive, direct filesystem I/O",
"Ryan C. Gordon <icculus@icculus.org>",
180
"https://icculus.org/physfs/",
181
1, /* supportsSymlinks */
182
},
183
DIR_openArchive,
184
DIR_enumerate,
185
186
187
188
189
DIR_openRead,
DIR_openWrite,
DIR_openAppend,
DIR_remove,
DIR_mkdir,
190
191
DIR_stat,
DIR_closeArchive
192
193
};
194
/* end of physfs_archiver_dir.c ... */