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