Skip to content

Latest commit

 

History

History
203 lines (153 loc) · 5.13 KB

archiver_dir.c

File metadata and controls

203 lines (153 loc) · 5.13 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
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)); \
}
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
{
Sep 5, 2010
Sep 5, 2010
41
PHYSFS_Stat statbuf;
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. */
Sep 5, 2010
Sep 5, 2010
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);
Jul 8, 2001
Jul 8, 2001
52
Mar 14, 2005
Mar 14, 2005
53
retval = allocator.Malloc(namelen + seplen + 1);
Jul 8, 2001
Jul 8, 2001
54
55
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
Sep 26, 2004
Sep 26, 2004
56
strcpy(retval, name);
Mar 15, 2012
Mar 15, 2012
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 */
Oct 9, 2001
Oct 9, 2001
64
Jan 28, 2010
Jan 28, 2010
65
return retval;
Jul 8, 2001
Jul 8, 2001
66
67
68
} /* DIR_openArchive */
Sep 29, 2004
Sep 29, 2004
69
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
Sep 18, 2005
Sep 18, 2005
70
71
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
Jul 8, 2001
Jul 8, 2001
72
{
Mar 15, 2012
Mar 15, 2012
73
74
75
char *d;
CVT_TO_DEPENDENT(d, opaque, dname);
Sep 29, 2004
Sep 29, 2004
76
77
if (d != NULL)
{
Sep 18, 2005
Sep 18, 2005
78
79
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
Mar 15, 2012
Mar 15, 2012
80
__PHYSFS_smallFree(d);
Sep 29, 2004
Sep 29, 2004
81
} /* if */
Jul 8, 2001
Jul 8, 2001
82
83
84
} /* DIR_enumerateFiles */
Aug 30, 2010
Aug 30, 2010
85
86
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
Jul 8, 2001
Jul 8, 2001
87
{
Mar 15, 2012
Mar 15, 2012
88
char *f;
Aug 30, 2010
Aug 30, 2010
89
90
91
PHYSFS_Io *io = NULL;
int existtmp = 0;
Mar 15, 2012
Mar 15, 2012
92
93
94
CVT_TO_DEPENDENT(f, opaque, name);
BAIL_IF_MACRO(f == NULL, NULL, NULL);
Aug 30, 2010
Aug 30, 2010
95
96
97
98
99
if (fileExists == NULL)
fileExists = &existtmp;
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
Aug 21, 2002
Aug 21, 2002
100
{
Mar 9, 2012
Mar 9, 2012
101
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
Sep 5, 2010
Sep 5, 2010
102
__PHYSFS_platformStat(f, fileExists, &statbuf);
Aug 21, 2002
Aug 21, 2002
103
} /* if */
Mar 13, 2012
Mar 13, 2012
104
105
106
107
108
else
{
*fileExists = 1;
} /* else */
Mar 15, 2012
Mar 15, 2012
109
__PHYSFS_smallFree(f);
Aug 21, 2002
Aug 21, 2002
110
Aug 30, 2010
Aug 30, 2010
111
return io;
Jul 8, 2001
Jul 8, 2001
112
113
114
} /* doOpen */
Aug 30, 2010
Aug 30, 2010
115
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
Jul 8, 2001
Jul 8, 2001
116
{
Aug 30, 2010
Aug 30, 2010
117
return doOpen(opaque, fnm, 'r', exist);
Jul 8, 2001
Jul 8, 2001
118
119
120
} /* DIR_openRead */
Aug 30, 2010
Aug 30, 2010
121
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
122
{
Aug 30, 2010
Aug 30, 2010
123
return doOpen(opaque, filename, 'w', NULL);
Jul 8, 2001
Jul 8, 2001
124
125
126
} /* DIR_openWrite */
Aug 30, 2010
Aug 30, 2010
127
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
Jul 8, 2001
Jul 8, 2001
128
{
Aug 30, 2010
Aug 30, 2010
129
return doOpen(opaque, filename, 'a', NULL);
Jul 8, 2001
Jul 8, 2001
130
131
132
} /* DIR_openAppend */
Sep 26, 2004
Sep 26, 2004
133
static int DIR_remove(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
134
135
{
int retval;
Mar 15, 2012
Mar 15, 2012
136
char *f;
Jul 8, 2001
Jul 8, 2001
137
Mar 15, 2012
Mar 15, 2012
138
CVT_TO_DEPENDENT(f, opaque, name);
Jul 8, 2001
Jul 8, 2001
139
BAIL_IF_MACRO(f == NULL, NULL, 0);
Mar 25, 2002
Mar 25, 2002
140
retval = __PHYSFS_platformDelete(f);
Mar 15, 2012
Mar 15, 2012
141
__PHYSFS_smallFree(f);
Jan 28, 2010
Jan 28, 2010
142
return retval;
Jul 8, 2001
Jul 8, 2001
143
144
145
} /* DIR_remove */
Sep 26, 2004
Sep 26, 2004
146
static int DIR_mkdir(dvoid *opaque, const char *name)
Jul 8, 2001
Jul 8, 2001
147
148
{
int retval;
Mar 15, 2012
Mar 15, 2012
149
char *f;
Jul 8, 2001
Jul 8, 2001
150
Mar 15, 2012
Mar 15, 2012
151
CVT_TO_DEPENDENT(f, opaque, name);
Jul 8, 2001
Jul 8, 2001
152
BAIL_IF_MACRO(f == NULL, NULL, 0);
Aug 23, 2001
Aug 23, 2001
153
retval = __PHYSFS_platformMkDir(f);
Mar 15, 2012
Mar 15, 2012
154
__PHYSFS_smallFree(f);
Jan 28, 2010
Jan 28, 2010
155
return retval;
Jul 8, 2001
Jul 8, 2001
156
157
158
} /* DIR_mkdir */
Sep 26, 2004
Sep 26, 2004
159
static void DIR_dirClose(dvoid *opaque)
Jul 8, 2001
Jul 8, 2001
160
{
Mar 14, 2005
Mar 14, 2005
161
allocator.Free(opaque);
Jul 8, 2001
Jul 8, 2001
162
163
} /* DIR_dirClose */
Sep 29, 2004
Sep 29, 2004
164
Aug 22, 2010
Aug 22, 2010
165
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
Feb 15, 2010
Feb 15, 2010
166
167
168
PHYSFS_Stat *stat)
{
int retval = 0;
Mar 15, 2012
Mar 15, 2012
169
char *d;
Feb 15, 2010
Feb 15, 2010
170
Mar 15, 2012
Mar 15, 2012
171
CVT_TO_DEPENDENT(d, opaque, name);
Feb 15, 2010
Feb 15, 2010
172
173
BAIL_IF_MACRO(d == NULL, NULL, 0);
retval = __PHYSFS_platformStat(d, exists, stat);
Mar 15, 2012
Mar 15, 2012
174
__PHYSFS_smallFree(d);
Feb 15, 2010
Feb 15, 2010
175
176
177
return retval;
} /* DIR_stat */
Sep 29, 2004
Sep 29, 2004
178
179
180
181
182
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
Jan 1, 2006
Jan 1, 2006
183
"Ryan C. Gordon <icculus@icculus.org>",
Sep 29, 2004
Sep 29, 2004
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 */
Aug 30, 2010
Aug 30, 2010
199
DIR_stat /* stat() method */
Sep 29, 2004
Sep 29, 2004
200
201
};
Jul 7, 2001
Jul 7, 2001
202
/* end of dir.c ... */