/
dir.c
283 lines (218 loc) · 7.84 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
*
* This file written by Ryan C. Gordon.
*/
#include <stdio.h>
#include <stdlib.h>
11
#include <string.h>
12
#include "physfs.h"
13
14
15
16
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
17
static PHYSFS_sint64 DIR_read(fvoid *opaque, void *buffer,
18
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
19
{
20
PHYSFS_sint64 retval;
21
retval = __PHYSFS_platformRead(opaque, buffer, objSize, objCount);
22
return(retval);
23
24
25
} /* DIR_read */
26
static PHYSFS_sint64 DIR_write(fvoid *opaque, const void *buffer,
27
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
28
{
29
PHYSFS_sint64 retval;
30
retval = __PHYSFS_platformWrite(opaque, buffer, objSize, objCount);
31
return(retval);
32
33
34
} /* DIR_write */
35
static int DIR_eof(fvoid *opaque)
36
{
37
return(__PHYSFS_platformEOF(opaque));
38
39
40
} /* DIR_eof */
41
static PHYSFS_sint64 DIR_tell(fvoid *opaque)
42
{
43
return(__PHYSFS_platformTell(opaque));
44
45
46
} /* DIR_tell */
47
static int DIR_seek(fvoid *opaque, PHYSFS_uint64 offset)
48
{
49
return(__PHYSFS_platformSeek(opaque, offset));
50
51
52
} /* DIR_seek */
53
static PHYSFS_sint64 DIR_fileLength(fvoid *opaque)
54
{
55
return(__PHYSFS_platformFileLength(opaque));
56
57
58
} /* DIR_fileLength */
59
static int DIR_fileClose(fvoid *opaque)
60
61
{
/*
62
* we manually flush the buffer, since that's the place a close will
63
* most likely fail, but that will leave the file handle in an undefined
64
* state if it fails. Flush failures we can recover from.
65
*/
66
67
BAIL_IF_MACRO(!__PHYSFS_platformFlush(opaque), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_platformClose(opaque), NULL, 0);
68
69
70
71
72
73
74
75
76
77
78
return(1);
} /* DIR_fileClose */
static int DIR_isArchive(const char *filename, int forWriting)
{
/* directories ARE archives in this driver... */
return(__PHYSFS_platformIsDirectory(filename));
} /* DIR_isArchive */
79
static void *DIR_openArchive(const char *name, int forWriting)
80
{
81
const char *dirsep = PHYSFS_getDirSeparator();
82
char *retval = NULL;
83
84
size_t namelen = strlen(name);
size_t seplen = strlen(dirsep);
85
86
/* !!! FIXME: when is this not called right before openArchive? */
87
BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
88
ERR_UNSUPPORTED_ARCHIVE, 0);
89
90
retval = allocator.Malloc(namelen + seplen + 1);
91
92
93
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
/* make sure there's a dir separator at the end of the string */
94
strcpy(retval, name);
95
if (strcmp((name + namelen) - seplen, dirsep) != 0)
96
strcat(retval, dirsep);
97
98
99
100
101
return(retval);
} /* DIR_openArchive */
102
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
103
104
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
105
{
106
char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
107
108
if (d != NULL)
{
109
110
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
111
allocator.Free(d);
112
} /* if */
113
114
115
} /* DIR_enumerateFiles */
116
static int DIR_exists(dvoid *opaque, const char *name)
117
{
118
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
119
120
121
122
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformExists(f);
123
allocator.Free(f);
124
125
126
127
return(retval);
} /* DIR_exists */
128
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
129
{
130
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
131
int retval = 0;
132
133
BAIL_IF_MACRO(d == NULL, NULL, 0);
134
135
136
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
137
allocator.Free(d);
138
139
140
141
return(retval);
} /* DIR_isDirectory */
142
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
143
{
144
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
145
int retval = 0;
146
147
BAIL_IF_MACRO(f == NULL, NULL, 0);
148
149
150
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
151
allocator.Free(f);
152
153
154
155
return(retval);
} /* DIR_isSymLink */
156
static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
157
158
const char *name,
int *fileExists)
159
{
160
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
161
PHYSFS_sint64 retval = -1;
162
163
BAIL_IF_MACRO(d == NULL, NULL, 0);
164
165
166
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformGetLastModTime(d);
167
allocator.Free(d);
168
169
170
171
return(retval);
} /* DIR_getLastModTime */
172
173
174
static fvoid *doOpen(dvoid *opaque, const char *name,
void *(*openFunc)(const char *filename),
int *fileExists)
175
{
176
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
177
void *rc = NULL;
178
179
180
BAIL_IF_MACRO(f == NULL, NULL, NULL);
181
182
183
184
185
if (fileExists != NULL)
{
*fileExists = __PHYSFS_platformExists(f);
if (!(*fileExists))
{
186
allocator.Free(f);
187
188
189
190
return(NULL);
} /* if */
} /* if */
191
rc = openFunc(f);
192
allocator.Free(f);
193
194
return((fvoid *) rc);
195
196
197
} /* doOpen */
198
static fvoid *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
199
{
200
return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist));
201
202
203
} /* DIR_openRead */
204
static fvoid *DIR_openWrite(dvoid *opaque, const char *filename)
205
{
206
return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL));
207
208
209
} /* DIR_openWrite */
210
static fvoid *DIR_openAppend(dvoid *opaque, const char *filename)
211
{
212
return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL));
213
214
215
} /* DIR_openAppend */
216
static int DIR_remove(dvoid *opaque, const char *name)
217
{
218
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
219
220
221
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
222
retval = __PHYSFS_platformDelete(f);
223
allocator.Free(f);
224
225
226
227
return(retval);
} /* DIR_remove */
228
static int DIR_mkdir(dvoid *opaque, const char *name)
229
{
230
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
231
232
233
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
234
retval = __PHYSFS_platformMkDir(f);
235
allocator.Free(f);
236
237
238
239
return(retval);
} /* DIR_mkdir */
240
static void DIR_dirClose(dvoid *opaque)
241
{
242
allocator.Free(opaque);
243
244
} /* DIR_dirClose */
245
246
247
248
249
250
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
251
"Ryan C. Gordon <icculus@icculus.org>",
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
&__PHYSFS_ArchiveInfo_DIR,
DIR_isArchive, /* isArchive() method */
DIR_openArchive, /* openArchive() method */
DIR_enumerateFiles, /* enumerateFiles() method */
DIR_exists, /* exists() method */
DIR_isDirectory, /* isDirectory() method */
DIR_isSymLink, /* isSymLink() method */
DIR_getLastModTime, /* getLastModTime() method */
DIR_openRead, /* openRead() method */
DIR_openWrite, /* openWrite() method */
DIR_openAppend, /* openAppend() method */
DIR_remove, /* remove() method */
DIR_mkdir, /* mkdir() method */
DIR_dirClose, /* dirClose() method */
DIR_read, /* read() method */
DIR_write, /* write() method */
DIR_eof, /* eof() method */
DIR_tell, /* tell() method */
DIR_seek, /* seek() method */
DIR_fileLength, /* fileLength() method */
DIR_fileClose /* fileClose() method */
};
282
/* end of dir.c ... */