/
archiver_dir.c
213 lines (163 loc) · 5.78 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
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
18
19
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
20
{
21
const char *dirsep = PHYSFS_getDirSeparator();
22
char *retval = NULL;
23
24
const size_t namelen = strlen(name);
const size_t seplen = strlen(dirsep);
25
26
assert(io == NULL); /* shouldn't create an Io for these. */
27
BAIL_IF_MACRO(!__PHYSFS_platformIsDirectory(name), ERR_NOT_AN_ARCHIVE, NULL);
28
29
retval = allocator.Malloc(namelen + seplen + 1);
30
31
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
32
33
/* make sure there's a dir separator at the end of the string */
/* !!! FIXME: is there really any place where (seplen != 1)? */
34
strcpy(retval, name);
35
if (strcmp((name + namelen) - seplen, dirsep) != 0)
36
strcat(retval, dirsep);
37
38
return retval;
39
40
41
} /* DIR_openArchive */
42
43
/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
44
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
45
46
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
47
{
48
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
49
50
if (d != NULL)
{
51
52
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
53
allocator.Free(d);
54
} /* if */
55
56
57
} /* DIR_enumerateFiles */
58
static int DIR_exists(dvoid *opaque, const char *name)
59
{
60
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
61
62
63
64
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
retval = __PHYSFS_platformExists(f);
65
allocator.Free(f);
66
return retval;
67
68
69
} /* DIR_exists */
70
static int DIR_isDirectory(dvoid *opaque, const char *name, int *fileExists)
71
{
72
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
73
int retval = 0;
74
75
BAIL_IF_MACRO(d == NULL, NULL, 0);
76
77
78
*fileExists = __PHYSFS_platformExists(d);
if (*fileExists)
retval = __PHYSFS_platformIsDirectory(d);
79
allocator.Free(d);
80
return retval;
81
82
83
} /* DIR_isDirectory */
84
static int DIR_isSymLink(dvoid *opaque, const char *name, int *fileExists)
85
{
86
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
87
int retval = 0;
88
89
BAIL_IF_MACRO(f == NULL, NULL, 0);
90
91
92
*fileExists = __PHYSFS_platformExists(f);
if (*fileExists)
retval = __PHYSFS_platformIsSymLink(f);
93
allocator.Free(f);
94
return retval;
95
96
97
} /* DIR_isSymLink */
98
99
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
100
{
101
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
102
103
104
105
106
107
108
PHYSFS_Io *io = NULL;
int existtmp = 0;
if (fileExists == NULL)
fileExists = &existtmp;
*fileExists = 0;
109
110
111
BAIL_IF_MACRO(f == NULL, NULL, NULL);
112
113
114
io = __PHYSFS_createNativeIo(f, mode);
allocator.Free(f);
if (io == NULL)
115
116
{
*fileExists = __PHYSFS_platformExists(f);
117
return NULL;
118
119
} /* if */
120
121
*fileExists = 1;
return io;
122
123
124
} /* doOpen */
125
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
126
{
127
return doOpen(opaque, fnm, 'r', exist);
128
129
130
} /* DIR_openRead */
131
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
132
{
133
return doOpen(opaque, filename, 'w', NULL);
134
135
136
} /* DIR_openWrite */
137
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
138
{
139
return doOpen(opaque, filename, 'a', NULL);
140
141
142
} /* DIR_openAppend */
143
static int DIR_remove(dvoid *opaque, const char *name)
144
{
145
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
146
147
148
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
149
retval = __PHYSFS_platformDelete(f);
150
allocator.Free(f);
151
return retval;
152
153
154
} /* DIR_remove */
155
static int DIR_mkdir(dvoid *opaque, const char *name)
156
{
157
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
158
159
160
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
161
retval = __PHYSFS_platformMkDir(f);
162
allocator.Free(f);
163
return retval;
164
165
166
} /* DIR_mkdir */
167
static void DIR_dirClose(dvoid *opaque)
168
{
169
allocator.Free(opaque);
170
171
} /* DIR_dirClose */
172
173
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
174
175
176
177
178
179
180
181
182
183
184
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 */
185
186
187
188
189
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
190
"Ryan C. Gordon <icculus@icculus.org>",
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
&__PHYSFS_ArchiveInfo_DIR,
DIR_openArchive, /* openArchive() method */
DIR_enumerateFiles, /* enumerateFiles() method */
DIR_exists, /* exists() method */
DIR_isDirectory, /* isDirectory() method */
DIR_isSymLink, /* isSymLink() 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 */
209
DIR_stat /* stat() method */
210
211
};
212
/* end of dir.c ... */