Skip to content

Latest commit

 

History

History
312 lines (244 loc) · 9.18 KB

dir.c

File metadata and controls

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