Skip to content

Latest commit

 

History

History
286 lines (220 loc) · 7.71 KB

dir.c

File metadata and controls

286 lines (220 loc) · 7.71 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
Sep 26, 2004
Sep 26, 2004
94
retval = 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
107
108
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
int omitSymLinks, PHYSFS_StringCallback cb,
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
113
114
115
if (d != NULL)
{
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb, callbackdata);
free(d);
} /* if */
Jul 8, 2001
Jul 8, 2001
116
117
118
} /* DIR_enumerateFiles */
Sep 26, 2004
Sep 26, 2004
119
static int DIR_exists(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
120
{
Sep 26, 2004
Sep 26, 2004
121
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
122
123
124
125
126
127
128
129
130
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
131
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
132
{
Sep 26, 2004
Sep 26, 2004
133
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
134
int retval = 0;
Jul 8, 2001
Jul 8, 2001
135
136
BAIL_IF_MACRO(d == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
137
138
139
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
Jul 8, 2001
Jul 8, 2001
140
141
142
143
144
free(d);
return(retval);
} /* DIR_isDirectory */
Sep 26, 2004
Sep 26, 2004
145
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
Jul 8, 2001
Jul 8, 2001
146
{
Sep 26, 2004
Sep 26, 2004
147
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
148
int retval = 0;
Jul 8, 2001
Jul 8, 2001
149
May 21, 2002
May 21, 2002
150
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
151
152
153
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
Jul 8, 2001
Jul 8, 2001
154
155
156
157
158
free(f);
return(retval);
} /* DIR_isSymLink */
Sep 26, 2004
Sep 26, 2004
159
static PHYSFS_sint64 DIR_getLastModTime(dvoid *opaque,
Aug 21, 2002
Aug 21, 2002
160
161
const char *name,
int *fileExists)
May 25, 2002
May 25, 2002
162
{
Sep 26, 2004
Sep 26, 2004
163
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Aug 21, 2002
Aug 21, 2002
164
PHYSFS_sint64 retval = -1;
May 25, 2002
May 25, 2002
165
166
BAIL_IF_MACRO(d == NULL, NULL, 0);
Aug 21, 2002
Aug 21, 2002
167
168
169
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformGetLastModTime(d);
May 25, 2002
May 25, 2002
170
171
172
173
174
free(d);
return(retval);
} /* DIR_getLastModTime */
Sep 26, 2004
Sep 26, 2004
175
176
177
static fvoid *doOpen(dvoid *opaque, const char *name,
void *(*openFunc)(const char *filename),
int *fileExists)
Jul 8, 2001
Jul 8, 2001
178
{
Sep 26, 2004
Sep 26, 2004
179
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Sep 26, 2004
Sep 26, 2004
180
void *rc = NULL;
Jul 8, 2001
Jul 8, 2001
181
182
183
BAIL_IF_MACRO(f == NULL, NULL, NULL);
Aug 21, 2002
Aug 21, 2002
184
185
186
187
188
189
190
191
192
193
if (fileExists != NULL)
{
*fileExists = __PHYSFS_platformExists(f);
if (!(*fileExists))
{
free(f);
return(NULL);
} /* if */
} /* if */
Mar 25, 2002
Mar 25, 2002
194
rc = openFunc(f);
Jul 8, 2001
Jul 8, 2001
195
196
free(f);
Sep 26, 2004
Sep 26, 2004
197
return((fvoid *) rc);
Jul 8, 2001
Jul 8, 2001
198
199
200
} /* doOpen */
Sep 26, 2004
Sep 26, 2004
201
static fvoid *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
202
{
Sep 26, 2004
Sep 26, 2004
203
return(doOpen(opaque, fnm, __PHYSFS_platformOpenRead, exist));
Jul 8, 2001
Jul 8, 2001
204
205
206
} /* DIR_openRead */
Sep 26, 2004
Sep 26, 2004
207
static fvoid *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
208
{
Sep 26, 2004
Sep 26, 2004
209
return(doOpen(opaque, filename, __PHYSFS_platformOpenWrite, NULL));
Jul 8, 2001
Jul 8, 2001
210
211
212
} /* DIR_openWrite */
Sep 26, 2004
Sep 26, 2004
213
static fvoid *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
214
{
Sep 26, 2004
Sep 26, 2004
215
return(doOpen(opaque, filename, __PHYSFS_platformOpenAppend, NULL));
Jul 8, 2001
Jul 8, 2001
216
217
218
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
219
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
220
{
Sep 26, 2004
Sep 26, 2004
221
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
222
223
224
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
225
retval = __PHYSFS_platformDelete(f);
Jul 8, 2001
Jul 8, 2001
226
227
228
229
230
free(f);
return(retval);
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
231
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
232
{
Sep 26, 2004
Sep 26, 2004
233
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
Jul 8, 2001
Jul 8, 2001
234
235
236
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 23, 2001
Aug 23, 2001
237
retval = __PHYSFS_platformMkDir(f);
Jul 8, 2001
Jul 8, 2001
238
239
240
241
242
free(f);
return(retval);
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
243
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
244
{
Sep 26, 2004
Sep 26, 2004
245
free(opaque);
Jul 8, 2001
Jul 8, 2001
246
247
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
248
249
250
251
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
282
283
284
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
"Ryan C. Gordon <icculus@clutteredmind.org>",
"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
285
/* end of dir.c ... */