/
archiver_dir.c
203 lines (153 loc) · 5.13 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
11
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
12
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
static char *cvtToDependent(const char *prepend, const char *path, char *buf)
{
BAIL_IF_MACRO(buf == NULL, ERR_OUT_OF_MEMORY, NULL);
sprintf(buf, "%s%s", prepend ? prepend : "", path);
if (__PHYSFS_platformDirSeparator != '/')
{
char *p;
for (p = strchr(buf, '/'); p != NULL; p = strchr(p + 1, '/'))
*p = __PHYSFS_platformDirSeparator;
} /* if */
return buf;
} /* cvtToDependent */
#define CVT_TO_DEPENDENT(buf, pre, dir) { \
const size_t len = ((pre) ? strlen((char *) pre) : 0) + strlen(dir) + 1; \
buf = cvtToDependent((char*)pre,dir,(char*)__PHYSFS_smallAlloc(len)); \
}
39
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
40
{
41
PHYSFS_Stat statbuf;
42
const char dirsep = __PHYSFS_platformDirSeparator;
43
char *retval = NULL;
44
const size_t namelen = strlen(name);
45
const size_t seplen = 1;
46
int exists = 0;
47
48
assert(io == NULL); /* shouldn't create an Io for these. */
49
50
51
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &statbuf), NULL, NULL);
if ((!exists) || (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY))
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
52
53
retval = allocator.Malloc(namelen + seplen + 1);
54
55
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
56
strcpy(retval, name);
57
58
59
60
61
62
63
/* make sure there's a dir separator at the end of the string */
if (retval[namelen - 1] != dirsep)
{
retval[namelen] = dirsep;
retval[namelen + 1] = '\0';
} /* if */
64
65
return retval;
66
67
68
} /* DIR_openArchive */
69
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
70
71
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
72
{
73
74
75
char *d;
CVT_TO_DEPENDENT(d, opaque, dname);
76
77
if (d != NULL)
{
78
79
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
80
__PHYSFS_smallFree(d);
81
} /* if */
82
83
84
} /* DIR_enumerateFiles */
85
86
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
87
{
88
char *f;
89
90
91
PHYSFS_Io *io = NULL;
int existtmp = 0;
92
93
94
CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(f == NULL, NULL, NULL);
95
96
97
98
99
if (fileExists == NULL)
fileExists = &existtmp;
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
100
{
101
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
102
__PHYSFS_platformStat(f, fileExists, &statbuf);
103
} /* if */
104
105
106
107
108
else
{
*fileExists = 1;
} /* else */
109
__PHYSFS_smallFree(f);
110
111
return io;
112
113
114
} /* doOpen */
115
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
116
{
117
return doOpen(opaque, fnm, 'r', exist);
118
119
120
} /* DIR_openRead */
121
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
122
{
123
return doOpen(opaque, filename, 'w', NULL);
124
125
126
} /* DIR_openWrite */
127
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
128
{
129
return doOpen(opaque, filename, 'a', NULL);
130
131
132
} /* DIR_openAppend */
133
static int DIR_remove(dvoid *opaque, const char *name)
134
135
{
int retval;
136
char *f;
137
138
CVT_TO_DEPENDENT(f, opaque, name);
139
BAIL_IF_MACRO(f == NULL, NULL, 0);
140
retval = __PHYSFS_platformDelete(f);
141
__PHYSFS_smallFree(f);
142
return retval;
143
144
145
} /* DIR_remove */
146
static int DIR_mkdir(dvoid *opaque, const char *name)
147
148
{
int retval;
149
char *f;
150
151
CVT_TO_DEPENDENT(f, opaque, name);
152
BAIL_IF_MACRO(f == NULL, NULL, 0);
153
retval = __PHYSFS_platformMkDir(f);
154
__PHYSFS_smallFree(f);
155
return retval;
156
157
158
} /* DIR_mkdir */
159
static void DIR_dirClose(dvoid *opaque)
160
{
161
allocator.Free(opaque);
162
163
} /* DIR_dirClose */
164
165
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
166
167
168
PHYSFS_Stat *stat)
{
int retval = 0;
169
char *d;
170
171
CVT_TO_DEPENDENT(d, opaque, name);
172
173
BAIL_IF_MACRO(d == NULL, NULL, 0);
retval = __PHYSFS_platformStat(d, exists, stat);
174
__PHYSFS_smallFree(d);
175
176
177
return retval;
} /* DIR_stat */
178
179
180
181
182
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
183
"Ryan C. Gordon <icculus@icculus.org>",
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
"http://icculus.org/physfs/",
};
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
&__PHYSFS_ArchiveInfo_DIR,
DIR_openArchive, /* openArchive() method */
DIR_enumerateFiles, /* enumerateFiles() 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 */
199
DIR_stat /* stat() method */
200
201
};
202
/* end of dir.c ... */