/
archiver_dir.c
206 lines (156 loc) · 5.31 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
52
53
54
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &st), ERRPASS, NULL);
/* !!! FIXME: the failed Stat() call will BAIL before we check (exists) */
/* !!! FIXME: ...should we even be checking for existance here...? */
BAIL_IF_MACRO(!exists, PHYSFS_ERR_NO_SUCH_PATH, NULL);
if (st.filetype != PHYSFS_FILETYPE_DIRECTORY)
BAIL_MACRO(PHYSFS_ERR_UNSUPPORTED, NULL);
55
56
retval = allocator.Malloc(namelen + seplen + 1);
57
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
58
59
strcpy(retval, name);
60
61
62
63
64
65
66
/* 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 */
67
68
return retval;
69
70
71
} /* DIR_openArchive */
72
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
73
74
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
75
{
76
77
78
char *d;
CVT_TO_DEPENDENT(d, opaque, dname);
79
80
if (d != NULL)
{
81
82
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
83
__PHYSFS_smallFree(d);
84
} /* if */
85
86
87
} /* DIR_enumerateFiles */
88
89
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
90
{
91
char *f;
92
93
94
PHYSFS_Io *io = NULL;
int existtmp = 0;
95
CVT_TO_DEPENDENT(f, opaque, name);
96
BAIL_IF_MACRO(!f, ERRPASS, NULL);
97
98
99
100
101
102
if (fileExists == NULL)
fileExists = &existtmp;
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
103
{
104
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
105
__PHYSFS_platformStat(f, fileExists, &statbuf);
106
} /* if */
107
108
109
110
111
else
{
*fileExists = 1;
} /* else */
112
__PHYSFS_smallFree(f);
113
114
return io;
115
116
117
} /* doOpen */
118
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
119
{
120
return doOpen(opaque, fnm, 'r', exist);
121
122
123
} /* DIR_openRead */
124
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
125
{
126
return doOpen(opaque, filename, 'w', NULL);
127
128
129
} /* DIR_openWrite */
130
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
131
{
132
return doOpen(opaque, filename, 'a', NULL);
133
134
135
} /* DIR_openAppend */
136
static int DIR_remove(dvoid *opaque, const char *name)
137
138
{
int retval;
139
char *f;
140
141
CVT_TO_DEPENDENT(f, opaque, name);
142
BAIL_IF_MACRO(!f, ERRPASS, 0);
143
retval = __PHYSFS_platformDelete(f);
144
__PHYSFS_smallFree(f);
145
return retval;
146
147
148
} /* DIR_remove */
149
static int DIR_mkdir(dvoid *opaque, const char *name)
150
151
{
int retval;
152
char *f;
153
154
CVT_TO_DEPENDENT(f, opaque, name);
155
BAIL_IF_MACRO(!f, ERRPASS, 0);
156
retval = __PHYSFS_platformMkDir(f);
157
__PHYSFS_smallFree(f);
158
return retval;
159
160
161
} /* DIR_mkdir */
162
static void DIR_dirClose(dvoid *opaque)
163
{
164
allocator.Free(opaque);
165
166
} /* DIR_dirClose */
167
168
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
169
170
171
PHYSFS_Stat *stat)
{
int retval = 0;
172
char *d;
173
174
CVT_TO_DEPENDENT(d, opaque, name);
175
BAIL_IF_MACRO(!d, ERRPASS, 0);
176
retval = __PHYSFS_platformStat(d, exists, stat);
177
__PHYSFS_smallFree(d);
178
179
180
return retval;
} /* DIR_stat */
181
182
183
184
185
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
186
"Ryan C. Gordon <icculus@icculus.org>",
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
"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 */
202
DIR_stat /* stat() method */
203
204
};
205
/* end of dir.c ... */