Skip to content

Latest commit

 

History

History
206 lines (156 loc) · 5.31 KB

archiver_dir.c

File metadata and controls

206 lines (156 loc) · 5.31 KB
 
Jul 7, 2001
Jul 7, 2001
1
2
3
/*
* Standard directory I/O support routines for PhysicsFS.
*
Mar 11, 2007
Mar 11, 2007
4
* Please see the file LICENSE.txt in the source's root directory.
Jul 7, 2001
Jul 7, 2001
5
6
7
8
9
10
11
*
* This file written by Ryan C. Gordon.
*/
#define __PHYSICSFS_INTERNAL__
#include "physfs_internal.h"
Aug 30, 2010
Aug 30, 2010
12
/* There's no PHYSFS_Io interface here. Use __PHYSFS_createNativeIo(). */
Jul 9, 2001
Jul 9, 2001
13
Mar 15, 2012
Mar 15, 2012
14
15
16
17
static char *cvtToDependent(const char *prepend, const char *path, char *buf)
{
Mar 20, 2012
Mar 20, 2012
18
BAIL_IF_MACRO(buf == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Mar 15, 2012
Mar 15, 2012
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)); \
}
Aug 30, 2010
Aug 30, 2010
39
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
Jul 8, 2001
Jul 8, 2001
40
{
Mar 20, 2012
Mar 20, 2012
41
PHYSFS_Stat st;
Mar 15, 2012
Mar 15, 2012
42
const char dirsep = __PHYSFS_platformDirSeparator;
Sep 26, 2004
Sep 26, 2004
43
char *retval = NULL;
Aug 30, 2010
Aug 30, 2010
44
const size_t namelen = strlen(name);
Mar 15, 2012
Mar 15, 2012
45
const size_t seplen = 1;
Sep 5, 2010
Sep 5, 2010
46
int exists = 0;
Jul 8, 2001
Jul 8, 2001
47
Aug 30, 2010
Aug 30, 2010
48
assert(io == NULL); /* shouldn't create an Io for these. */
Mar 20, 2012
Mar 20, 2012
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);
Jul 8, 2001
Jul 8, 2001
55
Mar 14, 2005
Mar 14, 2005
56
retval = allocator.Malloc(namelen + seplen + 1);
Mar 20, 2012
Mar 20, 2012
57
BAIL_IF_MACRO(retval == NULL, PHYSFS_ERR_OUT_OF_MEMORY, NULL);
Jul 8, 2001
Jul 8, 2001
58
Sep 26, 2004
Sep 26, 2004
59
strcpy(retval, name);
Mar 15, 2012
Mar 15, 2012
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 */
Oct 9, 2001
Oct 9, 2001
67
Jan 28, 2010
Jan 28, 2010
68
return retval;
Jul 8, 2001
Jul 8, 2001
69
70
71
} /* DIR_openArchive */
Sep 29, 2004
Sep 29, 2004
72
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
73
74
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 8, 2001
Jul 8, 2001
75
{
Mar 15, 2012
Mar 15, 2012
76
77
78
char *d;
CVT_TO_DEPENDENT(d, opaque, dname);
Sep 29, 2004
Sep 29, 2004
79
80
if (d != NULL)
{
Sep 18, 2005
Sep 18, 2005
81
82
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
Mar 15, 2012
Mar 15, 2012
83
__PHYSFS_smallFree(d);
Sep 29, 2004
Sep 29, 2004
84
} /* if */
Jul 8, 2001
Jul 8, 2001
85
86
87
} /* DIR_enumerateFiles */
Aug 30, 2010
Aug 30, 2010
88
89
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
Jul 8, 2001
Jul 8, 2001
90
{
Mar 15, 2012
Mar 15, 2012
91
char *f;
Aug 30, 2010
Aug 30, 2010
92
93
94
PHYSFS_Io *io = NULL;
int existtmp = 0;
Mar 15, 2012
Mar 15, 2012
95
CVT_TO_DEPENDENT(f, opaque, name);
Mar 20, 2012
Mar 20, 2012
96
BAIL_IF_MACRO(!f, ERRPASS, NULL);
Mar 15, 2012
Mar 15, 2012
97
Aug 30, 2010
Aug 30, 2010
98
99
100
101
102
if (fileExists == NULL)
fileExists = &existtmp;
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
Aug 21, 2002
Aug 21, 2002
103
{
Mar 9, 2012
Mar 9, 2012
104
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
Sep 5, 2010
Sep 5, 2010
105
__PHYSFS_platformStat(f, fileExists, &statbuf);
Aug 21, 2002
Aug 21, 2002
106
} /* if */
Mar 13, 2012
Mar 13, 2012
107
108
109
110
111
else
{
*fileExists = 1;
} /* else */
Mar 15, 2012
Mar 15, 2012
112
__PHYSFS_smallFree(f);
Aug 21, 2002
Aug 21, 2002
113
Aug 30, 2010
Aug 30, 2010
114
return io;
Jul 8, 2001
Jul 8, 2001
115
116
117
} /* doOpen */
Aug 30, 2010
Aug 30, 2010
118
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
119
{
Aug 30, 2010
Aug 30, 2010
120
return doOpen(opaque, fnm, 'r', exist);
Jul 8, 2001
Jul 8, 2001
121
122
123
} /* DIR_openRead */
Aug 30, 2010
Aug 30, 2010
124
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
125
{
Aug 30, 2010
Aug 30, 2010
126
return doOpen(opaque, filename, 'w', NULL);
Jul 8, 2001
Jul 8, 2001
127
128
129
} /* DIR_openWrite */
Aug 30, 2010
Aug 30, 2010
130
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
131
{
Aug 30, 2010
Aug 30, 2010
132
return doOpen(opaque, filename, 'a', NULL);
Jul 8, 2001
Jul 8, 2001
133
134
135
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
136
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
137
138
{
int retval;
Mar 15, 2012
Mar 15, 2012
139
char *f;
Jul 8, 2001
Jul 8, 2001
140
Mar 15, 2012
Mar 15, 2012
141
CVT_TO_DEPENDENT(f, opaque, name);
Mar 20, 2012
Mar 20, 2012
142
BAIL_IF_MACRO(!f, ERRPASS, 0);
Mar 25, 2002
Mar 25, 2002
143
retval = __PHYSFS_platformDelete(f);
Mar 15, 2012
Mar 15, 2012
144
__PHYSFS_smallFree(f);
Jan 28, 2010
Jan 28, 2010
145
return retval;
Jul 8, 2001
Jul 8, 2001
146
147
148
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
149
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
150
151
{
int retval;
Mar 15, 2012
Mar 15, 2012
152
char *f;
Jul 8, 2001
Jul 8, 2001
153
Mar 15, 2012
Mar 15, 2012
154
CVT_TO_DEPENDENT(f, opaque, name);
Mar 20, 2012
Mar 20, 2012
155
BAIL_IF_MACRO(!f, ERRPASS, 0);
Aug 23, 2001
Aug 23, 2001
156
retval = __PHYSFS_platformMkDir(f);
Mar 15, 2012
Mar 15, 2012
157
__PHYSFS_smallFree(f);
Jan 28, 2010
Jan 28, 2010
158
return retval;
Jul 8, 2001
Jul 8, 2001
159
160
161
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
162
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
163
{
Mar 14, 2005
Mar 14, 2005
164
allocator.Free(opaque);
Jul 8, 2001
Jul 8, 2001
165
166
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
167
Aug 22, 2010
Aug 22, 2010
168
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
Feb 15, 2010
Feb 15, 2010
169
170
171
PHYSFS_Stat *stat)
{
int retval = 0;
Mar 15, 2012
Mar 15, 2012
172
char *d;
Feb 15, 2010
Feb 15, 2010
173
Mar 15, 2012
Mar 15, 2012
174
CVT_TO_DEPENDENT(d, opaque, name);
Mar 20, 2012
Mar 20, 2012
175
BAIL_IF_MACRO(!d, ERRPASS, 0);
Feb 15, 2010
Feb 15, 2010
176
retval = __PHYSFS_platformStat(d, exists, stat);
Mar 15, 2012
Mar 15, 2012
177
__PHYSFS_smallFree(d);
Feb 15, 2010
Feb 15, 2010
178
179
180
return retval;
} /* DIR_stat */
Sep 29, 2004
Sep 29, 2004
181
182
183
184
185
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
186
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
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 */
Aug 30, 2010
Aug 30, 2010
202
DIR_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
203
204
};
Jul 7, 2001
Jul 7, 2001
205
/* end of dir.c ... */