Skip to content

Latest commit

 

History

History
287 lines (221 loc) · 7.88 KB

dir.c

File metadata and controls

287 lines (221 loc) · 7.88 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)
Jul 8, 2001
Jul 8, 2001
23
{
Mar 25, 2002
Mar 25, 2002
24
PHYSFS_sint64 retval;
Sep 26, 2004
Sep 26, 2004
25
retval = __PHYSFS_platformRead(opaque, buffer, objSize, objCount);
Mar 25, 2002
Mar 25, 2002
26
return(retval);
Jul 8, 2001
Jul 8, 2001
27
28
29
} /* DIR_read */
Sep 26, 2004
Sep 26, 2004
30
static PHYSFS_sint64 DIR_write(fvoid *opaque, const void *buffer,
Mar 24, 2002
Mar 24, 2002
31
PHYSFS_uint32 objSize, PHYSFS_uint32 objCount)
Jul 8, 2001
Jul 8, 2001
32
{
Mar 25, 2002
Mar 25, 2002
33
PHYSFS_sint64 retval;
Sep 26, 2004
Sep 26, 2004
34
retval = __PHYSFS_platformWrite(opaque, buffer, objSize, objCount);
Mar 25, 2002
Mar 25, 2002
35
return(retval);
Jul 8, 2001
Jul 8, 2001
36
37
38
} /* DIR_write */
Sep 26, 2004
Sep 26, 2004
39
static int DIR_eof(fvoid *opaque)
Aug 21, 2002
Aug 21, 2002
40
{
Sep 26, 2004
Sep 26, 2004
41
return(__PHYSFS_platformEOF(opaque));
Jul 8, 2001
Jul 8, 2001
42
43
44
} /* DIR_eof */
Sep 26, 2004
Sep 26, 2004
45
static PHYSFS_sint64 DIR_tell(fvoid *opaque)
Jul 8, 2001
Jul 8, 2001
46
{
Sep 26, 2004
Sep 26, 2004
47
return(__PHYSFS_platformTell(opaque));
Jul 8, 2001
Jul 8, 2001
48
49
50
} /* DIR_tell */
Sep 26, 2004
Sep 26, 2004
51
static int DIR_seek(fvoid *opaque, PHYSFS_uint64 offset)
Jul 8, 2001
Jul 8, 2001
52
{
Sep 26, 2004
Sep 26, 2004
53
return(__PHYSFS_platformSeek(opaque, offset));
Jul 8, 2001
Jul 8, 2001
54
55
56
} /* DIR_seek */
Sep 26, 2004
Sep 26, 2004
57
static PHYSFS_sint64 DIR_fileLength(fvoid *opaque)
Jul 9, 2001
Jul 9, 2001
58
{
Sep 26, 2004
Sep 26, 2004
59
return(__PHYSFS_platformFileLength(opaque));
Jul 9, 2001
Jul 9, 2001
60
61
62
} /* DIR_fileLength */
Sep 26, 2004
Sep 26, 2004
63
static int DIR_fileClose(fvoid *opaque)
Jul 8, 2001
Jul 8, 2001
64
65
{
/*
Mar 25, 2002
Mar 25, 2002
66
* we manually flush the buffer, since that's the place a close will
Jul 8, 2001
Jul 8, 2001
67
* most likely fail, but that will leave the file handle in an undefined
Mar 25, 2002
Mar 25, 2002
68
* state if it fails. Flush failures we can recover from.
Jul 8, 2001
Jul 8, 2001
69
*/
Sep 26, 2004
Sep 26, 2004
70
71
BAIL_IF_MACRO(!__PHYSFS_platformFlush(opaque), NULL, 0);
BAIL_IF_MACRO(!__PHYSFS_platformClose(opaque), NULL, 0);
Jul 8, 2001
Jul 8, 2001
72
73
74
75
76
77
78
79
80
81
82
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
83
static void *DIR_openArchive(const char *name, int forWriting)
Jul 8, 2001
Jul 8, 2001
84
{
Jul 8, 2001
Jul 8, 2001
85
const char *dirsep = PHYSFS_getDirSeparator();
Sep 26, 2004
Sep 26, 2004
86
char *retval = NULL;
Sep 2, 2001
Sep 2, 2001
87
88
size_t namelen = strlen(name);
size_t seplen = strlen(dirsep);
Jul 8, 2001
Jul 8, 2001
89
Sep 26, 2004
Sep 26, 2004
90
/* !!! FIXME: when is this not called right before openArchive? */
Jul 8, 2001
Jul 8, 2001
91
BAIL_IF_MACRO(!DIR_isArchive(name, forWriting),
Sep 26, 2004
Sep 26, 2004
92
ERR_UNSUPPORTED_ARCHIVE, 0);
Jul 8, 2001
Jul 8, 2001
93
Mar 14, 2005
Mar 14, 2005
94
retval = allocator.Malloc(namelen + seplen + 1);
Jul 8, 2001
Jul 8, 2001
95
96
97
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
98
strcpy(retval, name);
Jul 8, 2001
Jul 8, 2001
99
if (strcmp((name + namelen) - seplen, dirsep) != 0)
Sep 26, 2004
Sep 26, 2004
100
strcat(retval, dirsep);
Oct 9, 2001
Oct 9, 2001
101
Jul 8, 2001
Jul 8, 2001
102
103
104
105
return(retval);
} /* DIR_openArchive */
Sep 29, 2004
Sep 29, 2004
106
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
107
108
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 8, 2001
Jul 8, 2001
109
{
Sep 26, 2004
Sep 26, 2004
110
char *d = __PHYSFS_platformCvtToDependent((char *)opaque, dname, NULL);
Sep 29, 2004
Sep 29, 2004
111
112
if (d != NULL)
{
Sep 18, 2005
Sep 18, 2005
113
114
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
Mar 14, 2005
Mar 14, 2005
115
allocator.Free(d);
Sep 29, 2004
Sep 29, 2004
116
} /* if */
Jul 8, 2001
Jul 8, 2001
117
118
119
} /* DIR_enumerateFiles */
Sep 26, 2004
Sep 26, 2004
120
static int DIR_exists(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
121
{
Sep 26, 2004
Sep 26, 2004
122
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
123
124
125
126
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformExists(f);
Mar 14, 2005
Mar 14, 2005
127
allocator.Free(f);
Jul 8, 2001
Jul 8, 2001
128
129
130
131
return(retval);
} /* DIR_exists */
Sep 26, 2004
Sep 26, 2004
132
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
133
{
Sep 26, 2004
Sep 26, 2004
134
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
135
int retval = 0;
Jul 8, 2001
Jul 8, 2001
136
137
BAIL_IF_MACRO(d == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
138
139
140
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
Mar 14, 2005
Mar 14, 2005
141
allocator.Free(d);
Jul 8, 2001
Jul 8, 2001
142
143
144
145
return(retval);
} /* DIR_isDirectory */
Sep 26, 2004
Sep 26, 2004
146
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
147
{
Sep 26, 2004
Sep 26, 2004
148
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
149
int retval = 0;
Jul 8, 2001
Jul 8, 2001
150
May 21, 2002
May 21, 2002
151
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
152
153
154
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
Mar 14, 2005
Mar 14, 2005
155
allocator.Free(f);
Jul 8, 2001
Jul 8, 2001
156
157
158
159
return(retval);
} /* DIR_isSymLink */
Sep 26, 2004
Sep 26, 2004
160
static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
Aug 21, 2002
Aug 21, 2002
161
162
const char *name,
int *fileExists)
May 25, 2002
May 25, 2002
163
{
Sep 26, 2004
Sep 26, 2004
164
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
165
PHYSFS_sint64 retval = -1;
May 25, 2002
May 25, 2002
166
167
BAIL_IF_MACRO(d == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
168
169
170
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformGetLastModTime(d);
Mar 14, 2005
Mar 14, 2005
171
allocator.Free(d);
May 25, 2002
May 25, 2002
172
173
174
175
return(retval);
} /* DIR_getLastModTime */
Sep 26, 2004
Sep 26, 2004
176
177
178
static fvoid *doOpen(dvoid *opaque, const char *name,
void *(*openFunc)(const char *filename),
int *fileExists)
Jul 8, 2001
Jul 8, 2001
179
{
Sep 26, 2004
Sep 26, 2004
180
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Sep 26, 2004
Sep 26, 2004
181
void *rc = NULL;
Jul 8, 2001
Jul 8, 2001
182
183
184
BAIL_IF_MACRO(f == NULL, NULL, NULL);
Aug 21, 2002
Aug 21, 2002
185
186
187
188
189
if (fileExists != NULL)
{
*fileExists = __PHYSFS_platformExists(f);
if (!(*fileExists))
{
Mar 14, 2005
Mar 14, 2005
190
allocator.Free(f);
Aug 21, 2002
Aug 21, 2002
191
192
193
194
return(NULL);
} /* if */
} /* if */
Mar 25, 2002
Mar 25, 2002
195
rc = openFunc(f);
Mar 14, 2005
Mar 14, 2005
196
allocator.Free(f);
Jul 8, 2001
Jul 8, 2001
197
Sep 26, 2004
Sep 26, 2004
198
return((fvoid *) rc);
Jul 8, 2001
Jul 8, 2001
199
200
201
} /* doOpen */
Sep 26, 2004
Sep 26, 2004
202
static fvoid *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
203
{
Sep 26, 2004
Sep 26, 2004
204
return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist));
Jul 8, 2001
Jul 8, 2001
205
206
207
} /* DIR_openRead */
Sep 26, 2004
Sep 26, 2004
208
static fvoid *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
209
{
Sep 26, 2004
Sep 26, 2004
210
return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL));
Jul 8, 2001
Jul 8, 2001
211
212
213
} /* DIR_openWrite */
Sep 26, 2004
Sep 26, 2004
214
static fvoid *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
215
{
Sep 26, 2004
Sep 26, 2004
216
return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL));
Jul 8, 2001
Jul 8, 2001
217
218
219
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
220
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
221
{
Sep 26, 2004
Sep 26, 2004
222
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
223
224
225
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
226
retval = __PHYSFS_platformDelete(f);
Mar 14, 2005
Mar 14, 2005
227
allocator.Free(f);
Jul 8, 2001
Jul 8, 2001
228
229
230
231
return(retval);
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
232
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
233
{
Sep 26, 2004
Sep 26, 2004
234
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
235
236
237
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 23, 2001
Aug 23, 2001
238
retval = __PHYSFS_platformMkDir(f);
Mar 14, 2005
Mar 14, 2005
239
allocator.Free(f);
Jul 8, 2001
Jul 8, 2001
240
241
242
243
return(retval);
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
244
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
245
{
Mar 14, 2005
Mar 14, 2005
246
allocator.Free(opaque);
Jul 8, 2001
Jul 8, 2001
247
248
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
249
250
251
252
253
254
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
255
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
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
282
283
284
285
"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 */
};
Jul 7, 2001
Jul 7, 2001
286
/* end of dir.c ... */