/
archiver_dir.c
191 lines (144 loc) · 4.5 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
static char *cvtToDependent(const char *prepend, const char *path, char *buf)
{
18
BAIL_IF_MACRO(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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 st;
42
const char dirsep = __PHYSFS_platformDirSeparator;
43
char *retval = NULL;
44
const size_t namelen = strlen(name);
45
const size_t seplen = 1;
46
47
assert(io == NULL); /* shouldn't create an Io for these. */
48
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &st), ERRPASS, NULL);
49
50
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
51
52
retval = allocator.Malloc(namelen + seplen + 1);
53
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
54
55
strcpy(retval, name);
56
57
58
59
60
61
62
/* 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 */
63
64
return retval;
65
66
67
} /* DIR_openArchive */
68
static void DIR_enumerateFiles(void *opaque, const char *dname,
69
PHYSFS_EnumFilesCallback cb,
70
const char *origdir, void *callbackdata)
71
{
72
73
74
char *d;
CVT_TO_DEPENDENT(d, opaque, dname);
75
76
if (d != NULL)
{
77
__PHYSFS_platformEnumerateFiles(d, cb, origdir, callbackdata);
78
__PHYSFS_smallFree(d);
79
} /* if */
80
81
82
} /* DIR_enumerateFiles */
83
static PHYSFS_Io *doOpen(void *opaque, const char *name, const int mode)
84
{
85
PHYSFS_Io *io = NULL;
86
char *f = NULL;
87
88
CVT_TO_DEPENDENT(f, opaque, name);
89
BAIL_IF_MACRO(!f, ERRPASS, NULL);
90
91
92
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
93
{
94
95
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
PHYSFS_Stat statbuf;
96
__PHYSFS_platformStat(f, &statbuf);
97
PHYSFS_setErrorCode(err);
98
} /* if */
99
100
__PHYSFS_smallFree(f);
101
102
return io;
103
104
105
} /* doOpen */
106
static PHYSFS_Io *DIR_openRead(void *opaque, const char *filename)
107
{
108
return doOpen(opaque, filename, 'r');
109
110
111
} /* DIR_openRead */
112
static PHYSFS_Io *DIR_openWrite(void *opaque, const char *filename)
113
{
114
return doOpen(opaque, filename, 'w');
115
116
117
} /* DIR_openWrite */
118
static PHYSFS_Io *DIR_openAppend(void *opaque, const char *filename)
119
{
120
return doOpen(opaque, filename, 'a');
121
122
123
} /* DIR_openAppend */
124
static int DIR_remove(void *opaque, const char *name)
125
126
{
int retval;
127
char *f;
128
129
CVT_TO_DEPENDENT(f, opaque, name);
130
BAIL_IF_MACRO(!f, ERRPASS, 0);
131
retval = __PHYSFS_platformDelete(f);
132
__PHYSFS_smallFree(f);
133
return retval;
134
135
136
} /* DIR_remove */
137
static int DIR_mkdir(void *opaque, const char *name)
138
139
{
int retval;
140
char *f;
141
142
CVT_TO_DEPENDENT(f, opaque, name);
143
BAIL_IF_MACRO(!f, ERRPASS, 0);
144
retval = __PHYSFS_platformMkDir(f);
145
__PHYSFS_smallFree(f);
146
return retval;
147
148
149
} /* DIR_mkdir */
150
static void DIR_closeArchive(void *opaque)
151
{
152
allocator.Free(opaque);
153
} /* DIR_closeArchive */
154
155
156
static int DIR_stat(void *opaque, const char *name, PHYSFS_Stat *stat)
157
158
{
int retval = 0;
159
char *d;
160
161
CVT_TO_DEPENDENT(d, opaque, name);
162
BAIL_IF_MACRO(!d, ERRPASS, 0);
163
retval = __PHYSFS_platformStat(d, stat);
164
__PHYSFS_smallFree(d);
165
166
167
return retval;
} /* DIR_stat */
168
169
170
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
171
CURRENT_PHYSFS_ARCHIVER_API_VERSION,
172
173
174
175
176
{
"",
"Non-archive, direct filesystem I/O",
"Ryan C. Gordon <icculus@icculus.org>",
"http://icculus.org/physfs/",
177
1, /* supportsSymlinks */
178
},
179
180
181
182
183
184
185
DIR_openArchive,
DIR_enumerateFiles,
DIR_openRead,
DIR_openWrite,
DIR_openAppend,
DIR_remove,
DIR_mkdir,
186
187
DIR_stat,
DIR_closeArchive
188
189
};
190
/* end of archiver_dir.c ... */