/
archiver_dir.c
174 lines (133 loc) · 4.67 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
static void *DIR_openArchive(PHYSFS_Io *io, const char *name, int forWriting)
15
{
16
PHYSFS_Stat statbuf;
17
const char dirsep = __PHYSFS_platformDirSeparator;
18
char *retval = NULL;
19
const size_t namelen = strlen(name);
20
const size_t seplen = 1;
21
int exists = 0;
22
23
assert(io == NULL); /* shouldn't create an Io for these. */
24
25
26
BAIL_IF_MACRO(!__PHYSFS_platformStat(name, &exists, &statbuf), NULL, NULL);
if ((!exists) || (statbuf.filetype != PHYSFS_FILETYPE_DIRECTORY))
BAIL_MACRO(ERR_NOT_AN_ARCHIVE, NULL);
27
28
retval = allocator.Malloc(namelen + seplen + 1);
29
30
BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL);
31
strcpy(retval, name);
32
33
34
35
36
37
38
/* 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 */
39
40
return retval;
41
42
43
} /* DIR_openArchive */
44
45
/* !!! FIXME: I would like to smallAlloc() all these conversions somehow. */
46
static void DIR_enumerateFiles(dvoid *opaque, const char *dname,
47
48
int omitSymLinks, PHYSFS_EnumFilesCallback cb,
const char *origdir, void *callbackdata)
49
{
50
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, dname, NULL);
51
52
if (d != NULL)
{
53
54
__PHYSFS_platformEnumerateFiles(d, omitSymLinks, cb,
origdir, callbackdata);
55
allocator.Free(d);
56
} /* if */
57
58
59
} /* DIR_enumerateFiles */
60
61
static PHYSFS_Io *doOpen(dvoid *opaque, const char *name,
const int mode, int *fileExists)
62
{
63
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
64
65
66
67
68
69
PHYSFS_Io *io = NULL;
int existtmp = 0;
if (fileExists == NULL)
fileExists = &existtmp;
70
71
BAIL_IF_MACRO(f == NULL, NULL, NULL);
72
73
io = __PHYSFS_createNativeIo(f, mode);
if (io == NULL)
74
{
75
PHYSFS_Stat statbuf; /* !!! FIXME: this changes the error message. */
76
__PHYSFS_platformStat(f, fileExists, &statbuf);
77
} /* if */
78
79
80
81
82
83
else
{
*fileExists = 1;
} /* else */
allocator.Free(f);
84
85
return io;
86
87
88
} /* doOpen */
89
static PHYSFS_Io *DIR_openRead(dvoid *opaque, const char *fnm, int *exist)
90
{
91
return doOpen(opaque, fnm, 'r', exist);
92
93
94
} /* DIR_openRead */
95
static PHYSFS_Io *DIR_openWrite(dvoid *opaque, const char *filename)
96
{
97
return doOpen(opaque, filename, 'w', NULL);
98
99
100
} /* DIR_openWrite */
101
static PHYSFS_Io *DIR_openAppend(dvoid *opaque, const char *filename)
102
{
103
return doOpen(opaque, filename, 'a', NULL);
104
105
106
} /* DIR_openAppend */
107
static int DIR_remove(dvoid *opaque, const char *name)
108
{
109
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
110
111
112
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
113
retval = __PHYSFS_platformDelete(f);
114
allocator.Free(f);
115
return retval;
116
117
118
} /* DIR_remove */
119
static int DIR_mkdir(dvoid *opaque, const char *name)
120
{
121
char *f = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
122
123
124
int retval;
BAIL_IF_MACRO(f == NULL, NULL, 0);
125
retval = __PHYSFS_platformMkDir(f);
126
allocator.Free(f);
127
return retval;
128
129
130
} /* DIR_mkdir */
131
static void DIR_dirClose(dvoid *opaque)
132
{
133
allocator.Free(opaque);
134
135
} /* DIR_dirClose */
136
137
static int DIR_stat(dvoid *opaque, const char *name, int *exists,
138
139
140
141
142
143
144
145
146
147
148
PHYSFS_Stat *stat)
{
char *d = __PHYSFS_platformCvtToDependent((char *) opaque, name, NULL);
int retval = 0;
BAIL_IF_MACRO(d == NULL, NULL, 0);
retval = __PHYSFS_platformStat(d, exists, stat);
allocator.Free(d);
return retval;
} /* DIR_stat */
149
150
151
152
153
const PHYSFS_ArchiveInfo __PHYSFS_ArchiveInfo_DIR =
{
"",
DIR_ARCHIVE_DESCRIPTION,
154
"Ryan C. Gordon <icculus@icculus.org>",
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"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 */
170
DIR_stat /* stat() method */
171
172
};
173
/* end of dir.c ... */