1 /**
2 * @file libcomprex/file.c File structures
3 *
4 * $Id: file.c,v 1.37 2003/01/01 06:22:35 chipx86 Exp $
5 *
6 * @Copyright (C) 2001-2003 The GNUpdate Project.
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
17 *
18 * You should have received a copy of the GNU Library General Public
19 * License along with this library; if not, write to the
20 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
22 */
23 #include <libcomprex/internal.h>
24 #include <libcomprex/file.h>
25
26 CxFile *
cxNewFile(void)27 cxNewFile(void)
28 {
29 CxFile *file;
30
31 file = (CxFile *)cxNewFsNode();
32
33 MEM_CHECK(file->u.file = (CxFileData *)malloc(sizeof(CxFileData)));
34 memset(file->u.file, 0, sizeof(CxFileData));
35
36 cxSetFsNodeType(file, CX_FSNODETYPE_FILE);
37
38 return file;
39 }
40
41 void
cxDestroyFile(CxFile * file)42 cxDestroyFile(CxFile *file)
43 {
44 CxArchive *archive;
45
46 if (file == NULL || file->refCount == 0)
47 return;
48
49 if ((file->refCount - 1) > 0)
50 return;
51
52 if (file->u.file->physPath != NULL)
53 free(file->u.file->physPath);
54
55 archive = cxGetFileArchive(file);
56
57 /* XXX This shouldn't be NULL, but check anyway. */
58 if (archive != NULL && archive->module != NULL &&
59 archive->module->ops.archive->destroyFile != NULL)
60 {
61 archive->module->ops.archive->destroyFile(file);
62 }
63
64 if (file->moduleData != NULL)
65 {
66 free(file->moduleData);
67 file->moduleData = NULL;
68 }
69
70 if (archive != NULL)
71 {
72 if (cxGetFileParent(file) != NULL)
73 cxDirRemoveFile(cxGetFileParent(file), file);
74
75 cxDestroyArchive(archive);
76 }
77
78 free(file->u.file);
79
80 cxDestroyFsNode(file);
81 }
82
83 void
cxSetFileArchive(CxFile * file,CxArchive * archive)84 cxSetFileArchive(CxFile *file, CxArchive *archive)
85 {
86 cxSetFsNodeArchive(file, archive);
87 }
88
89 void
cxSetFileParent(CxFile * file,CxDirectory * parent)90 cxSetFileParent(CxFile *file, CxDirectory *parent)
91 {
92 cxSetFsNodeParent(file, parent);
93 }
94
95 void
cxSetFileName(CxFile * file,const char * name)96 cxSetFileName(CxFile *file, const char *name)
97 {
98 cxSetFsNodeName(file, name);
99 }
100
101 void
cxSetFilePhysicalPath(CxFile * file,const char * path)102 cxSetFilePhysicalPath(CxFile *file, const char *path)
103 {
104 if (file == NULL)
105 return;
106
107 if (file->u.file->physPath != NULL)
108 free(file->u.file->physPath);
109
110 file->u.file->physPath = (path == NULL ? NULL : strdup(path));
111 }
112
113 void
cxSetFileMode(CxFile * file,mode_t mode)114 cxSetFileMode(CxFile *file, mode_t mode)
115 {
116 cxSetFsNodeMode(file, mode);
117 }
118
119 void
cxSetFileUid(CxFile * file,uid_t uid)120 cxSetFileUid(CxFile *file, uid_t uid)
121 {
122 cxSetFsNodeUid(file, uid);
123 }
124
125 void
cxSetFileGid(CxFile * file,gid_t gid)126 cxSetFileGid(CxFile *file, gid_t gid)
127 {
128 cxSetFsNodeGid(file, gid);
129 }
130
131 void
cxSetFileCompressedSize(CxFile * file,unsigned int size)132 cxSetFileCompressedSize(CxFile *file, unsigned int size)
133 {
134 if (file == NULL)
135 return;
136
137 file->u.file->compressedSize = size;
138 }
139
140 void
cxSetFileSize(CxFile * file,unsigned int size)141 cxSetFileSize(CxFile *file, unsigned int size)
142 {
143 if (file == NULL)
144 return;
145
146 file->u.file->uncompressedSize = size;
147 }
148
149 void
cxSetFileDate(CxFile * file,time_t date)150 cxSetFileDate(CxFile *file, time_t date)
151 {
152 cxSetFsNodeDate(file, date);
153 }
154
155 void
cxSetFileLocal(CxFile * file,char isLocal)156 cxSetFileLocal(CxFile *file, char isLocal)
157 {
158 cxSetFsNodeLocal(file, isLocal);
159 }
160
161 CxArchive *
cxGetFileArchive(CxFile * file)162 cxGetFileArchive(CxFile *file)
163 {
164 return cxGetFsNodeArchive(file);
165 }
166
167 CxDirectory *
cxGetFileParent(CxFile * file)168 cxGetFileParent(CxFile *file)
169 {
170 return cxGetFsNodeParent(file);
171 }
172
173 const char *
cxGetFileName(CxFile * file)174 cxGetFileName(CxFile *file)
175 {
176 return cxGetFsNodeName(file);
177 }
178
179 const char *
cxGetFilePath(CxFile * file)180 cxGetFilePath(CxFile *file)
181 {
182 return cxGetFsNodePath(file);
183 }
184
185 const char *
cxGetFilePhysicalPath(CxFile * file)186 cxGetFilePhysicalPath(CxFile *file)
187 {
188 if (file == NULL)
189 return NULL;
190
191 return file->u.file->physPath;
192 }
193
194 mode_t
cxGetFileMode(CxFile * file)195 cxGetFileMode(CxFile *file)
196 {
197 return cxGetFsNodeMode(file);
198 }
199
200 uid_t
cxGetFileUid(CxFile * file)201 cxGetFileUid(CxFile *file)
202 {
203 return cxGetFsNodeUid(file);
204 }
205
206 gid_t
cxGetFileGid(CxFile * file)207 cxGetFileGid(CxFile *file)
208 {
209 return cxGetFsNodeGid(file);
210 }
211
212 unsigned int
cxGetFileCompressedSize(CxFile * file)213 cxGetFileCompressedSize(CxFile *file)
214 {
215 if (file == NULL)
216 return 0;
217
218 return file->u.file->compressedSize;
219 }
220
221 unsigned int
cxGetFileSize(CxFile * file)222 cxGetFileSize(CxFile *file)
223 {
224 if (file == NULL)
225 return 0;
226
227 return file->u.file->uncompressedSize;
228 }
229
230 time_t
cxGetFileDate(CxFile * file)231 cxGetFileDate(CxFile *file)
232 {
233 return cxGetFsNodeDate(file);
234 }
235
236 char
cxIsFileLocal(CxFile * file)237 cxIsFileLocal(CxFile *file)
238 {
239 return cxIsFsNodeLocal(file);
240 }
241
242 CxFile *
cxGetNextFile(CxFile * file)243 cxGetNextFile(CxFile *file)
244 {
245 if (file == NULL)
246 return NULL;
247
248 return file->next;
249 }
250
251 CxFile *
cxGetPreviousFile(CxFile * file)252 cxGetPreviousFile(CxFile *file)
253 {
254 if (file == NULL)
255 return NULL;
256
257 return file->prev;
258 }
259
260