Skip to content

Latest commit

 

History

History
174 lines (133 loc) · 4.67 KB

archiver_dir.c

File metadata and controls

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