Skip to content

Latest commit

 

History

History
171 lines (131 loc) · 4.7 KB

archiver_dir.c

File metadata and controls

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