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