Skip to content

Latest commit

 

History

History
289 lines (223 loc) · 7.98 KB

archiver_dir.c

File metadata and controls

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