/
archiver_dir.c
201 lines (153 loc) · 5.15 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
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, &st), ERRPASS, NULL);
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
52
53
retval = allocator.Malloc(namelen + seplen + 1);
54
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
55
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(PHYSFS_Dir *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
static PHYSFS_Io *doOpen(PHYSFS_Dir *opaque, const char *name,
86
const int mode, int *fileExists)
87
{
88
char *f;
89
90
91
PHYSFS_Io *io = NULL;
int existtmp = 0;
92
CVT_TO_DEPENDENT(f, opaque, name);
93
BAIL_IF_MACRO(!f, ERRPASS, NULL);
94
95
96
97
98
99
if (fileExists == NULL)
fileExists = &existtmp;
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
100
{
101
102
const PHYSFS_ErrorCode err = PHYSFS_getLastErrorCode();
PHYSFS_Stat statbuf;
103
__PHYSFS_platformStat(f, fileExists, &statbuf);
104
__PHYSFS_setError(err);
105
} /* if */
106
107
108
109
110
else
{
*fileExists = 1;
} /* else */
111
__PHYSFS_smallFree(f);
112
113
return io;
114
115
116
} /* doOpen */
117
static PHYSFS_Io *DIR_openRead(PHYSFS_Dir *opaque, const char *fnm, int *exist)
118
{
119
return doOpen(opaque, fnm, 'r', exist);
120
121
122
} /* DIR_openRead */
123
static PHYSFS_Io *DIR_openWrite(PHYSFS_Dir *opaque, const char *filename)
124
{
125
return doOpen(opaque, filename, 'w', NULL);
126
127
128
} /* DIR_openWrite */
129
static PHYSFS_Io *DIR_openAppend(PHYSFS_Dir *opaque, const char *filename)
130
{
131
return doOpen(opaque, filename, 'a', NULL);
132
133
134
} /* DIR_openAppend */
135
static int DIR_remove(PHYSFS_Dir *opaque, const char *name)
136
137
{
int retval;
138
char *f;
139
140
CVT_TO_DEPENDENT(f, opaque, name);
141
BAIL_IF_MACRO(!f, ERRPASS, 0);
142
retval = __PHYSFS_platformDelete(f);
143
__PHYSFS_smallFree(f);
144
return retval;
145
146
147
} /* DIR_remove */
148
static int DIR_mkdir(PHYSFS_Dir *opaque, const char *name)
149
150
{
int retval;
151
char *f;
152
153
CVT_TO_DEPENDENT(f, opaque, name);
154
BAIL_IF_MACRO(!f, ERRPASS, 0);
155
retval = __PHYSFS_platformMkDir(f);
156
__PHYSFS_smallFree(f);
157
return retval;
158
159
160
} /* DIR_mkdir */
161
static void DIR_closeArchive(PHYSFS_Dir *opaque)
162
{
163
allocator.Free(opaque);
164
} /* DIR_closeArchive */
165
166
167
168
static int DIR_stat(PHYSFS_Dir *opaque, const char *name,
int *exists, PHYSFS_Stat *stat)
169
170
{
int retval = 0;
171
char *d;
172
173
CVT_TO_DEPENDENT(d, opaque, name);
174
BAIL_IF_MACRO(!d, ERRPASS, 0);
175
retval = __PHYSFS_platformStat(d, exists, stat);
176
__PHYSFS_smallFree(d);
177
178
179
return retval;
} /* DIR_stat */
180
181
182
const PHYSFS_Archiver __PHYSFS_Archiver_DIR =
{
183
184
185
186
187
188
{
"",
"Non-archive, direct filesystem I/O",
"Ryan C. Gordon <icculus@icculus.org>",
"http://icculus.org/physfs/",
},
189
190
191
192
193
194
195
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 */
196
DIR_closeArchive, /* closeArchive() method */
197
DIR_stat /* stat() method */
198
199
};
200
/* end of dir.c ... */