0
|
1 /* This program is free software; you can redistribute it and/or modify
|
|
2 * it under the terms of the GNU General Public Licensse as published by
|
|
3 * the Free Software Foundation; either version 2 of the License, or
|
|
4 * (at your option) any later version.
|
|
5 *
|
|
6 * This program is distributed in the hope that it will be useful,
|
|
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
9 * GNU General Public License for more details.
|
|
10 *
|
|
11 * You should have received a copy of the GNU General Public License
|
|
12 * along with this program; if not, write to the Free Software
|
|
13 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
14 */
|
|
15
|
|
16 #include "vfs.h"
|
|
17 #include <string.h>
|
|
18 #include <libgnomevfs/gnome-vfs.h>
|
|
19
|
|
20
|
|
21 struct _VFSFile
|
|
22 {
|
|
23 GnomeVFSHandle *handle;
|
|
24 };
|
|
25
|
|
26
|
|
27 static void mode_to_gnome_vfs(const gchar * mode,
|
|
28 GnomeVFSOpenMode * g_mode,
|
|
29 gboolean * truncate,
|
|
30 gboolean * append);
|
|
31
|
|
32 gboolean
|
|
33 vfs_init(void)
|
|
34 {
|
|
35 if (!gnome_vfs_init())
|
|
36 return FALSE;
|
|
37
|
|
38 g_atexit(gnome_vfs_shutdown);
|
|
39 return TRUE;
|
|
40 }
|
|
41
|
|
42 VFSFile *
|
|
43 vfs_fopen(const gchar * path,
|
|
44 const gchar * mode)
|
|
45 {
|
|
46 VFSFile *file;
|
|
47 GnomeVFSResult g_result;
|
|
48 GnomeVFSOpenMode g_mode;
|
|
49 gboolean truncate, append;
|
|
50
|
|
51 file = g_new(VFSFile, 1);
|
|
52
|
|
53 mode_to_gnome_vfs(mode, &g_mode, &truncate, &append);
|
|
54
|
|
55 if (!truncate) {
|
|
56 g_result = gnome_vfs_open(&(file->handle), path, g_mode);
|
|
57
|
|
58 if (append && g_result == GNOME_VFS_ERROR_NOT_FOUND) {
|
|
59 g_result = gnome_vfs_create(&(file->handle),
|
|
60 path, g_mode, TRUE,
|
|
61 S_IRUSR | S_IWUSR |
|
|
62 S_IRGRP | S_IWGRP |
|
|
63 S_IROTH | S_IWOTH);
|
|
64 }
|
|
65
|
|
66 if (append && g_result == GNOME_VFS_OK) {
|
|
67 g_result = gnome_vfs_seek(file->handle, GNOME_VFS_SEEK_END, 0);
|
|
68 if (g_result != GNOME_VFS_OK)
|
|
69 gnome_vfs_close(file->handle);
|
|
70 }
|
|
71 }
|
|
72 else {
|
|
73 g_result = gnome_vfs_create(&(file->handle),
|
|
74 path, g_mode, FALSE,
|
|
75 S_IRUSR | S_IWUSR |
|
|
76 S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
|
|
77 }
|
|
78
|
|
79 if (g_result != GNOME_VFS_OK)
|
|
80 file->handle = NULL;
|
|
81
|
|
82 if (file->handle == NULL) {
|
|
83 g_free(file);
|
|
84 file = NULL;
|
|
85 }
|
|
86
|
|
87 return file;
|
|
88 }
|
|
89
|
|
90 gint
|
|
91 vfs_fclose(VFSFile * file)
|
|
92 {
|
|
93 gint ret = 0;
|
|
94
|
|
95 if (file->handle) {
|
|
96 if (gnome_vfs_close(file->handle) != GNOME_VFS_OK)
|
|
97 ret = -1;
|
|
98 }
|
|
99
|
|
100 g_free(file);
|
|
101
|
|
102 return ret;
|
|
103 }
|
|
104
|
|
105 size_t
|
|
106 vfs_fread(gpointer ptr,
|
|
107 size_t size,
|
|
108 size_t nmemb,
|
|
109 VFSFile * file)
|
|
110 {
|
|
111 GnomeVFSResult result;
|
|
112 GnomeVFSFileSize bytes_read;
|
|
113
|
|
114 result = gnome_vfs_read(file->handle, ptr, size * nmemb, &bytes_read);
|
|
115 if (result == GNOME_VFS_OK)
|
|
116 return bytes_read;
|
|
117 if (result == GNOME_VFS_ERROR_EOF)
|
|
118 return 0;
|
|
119 else
|
|
120 return -1;
|
|
121 }
|
|
122
|
|
123 size_t
|
|
124 vfs_fwrite(gconstpointer ptr,
|
|
125 size_t size,
|
|
126 size_t nmemb,
|
|
127 VFSFile * file)
|
|
128 {
|
|
129 GnomeVFSResult result;
|
|
130 GnomeVFSFileSize bytes_written;
|
|
131
|
|
132 result = gnome_vfs_write(file->handle, ptr, size * nmemb, &bytes_written);
|
|
133 if (result == GNOME_VFS_OK)
|
|
134 return bytes_written;
|
|
135 else
|
|
136 return -1;
|
|
137 }
|
|
138
|
|
139 gint
|
|
140 vfs_fseek(VFSFile * file,
|
|
141 glong offset,
|
|
142 gint whence)
|
|
143 {
|
|
144 GnomeVFSResult result;
|
|
145 GnomeVFSSeekPosition g_whence;
|
|
146
|
|
147 switch (whence) {
|
|
148 case SEEK_SET:
|
|
149 g_whence = GNOME_VFS_SEEK_START;
|
|
150 break;
|
|
151 case SEEK_CUR:
|
|
152 g_whence = GNOME_VFS_SEEK_CURRENT;
|
|
153 break;
|
|
154 case SEEK_END:
|
|
155 g_whence = GNOME_VFS_SEEK_END;
|
|
156 break;
|
|
157 default:
|
|
158 g_warning("vfs_fseek: invalid whence value");
|
|
159 return -1;
|
|
160 }
|
|
161
|
|
162 result = gnome_vfs_seek(file->handle, g_whence, offset);
|
|
163
|
|
164 if (result == GNOME_VFS_OK)
|
|
165 return 0;
|
|
166 else
|
|
167 return -1;
|
|
168 }
|
|
169
|
|
170 void
|
|
171 vfs_rewind(VFSFile * file)
|
|
172 {
|
|
173 vfs_fseek(file, 0L, SEEK_SET);
|
|
174 }
|
|
175
|
|
176 glong
|
|
177 vfs_ftell(VFSFile * file)
|
|
178 {
|
|
179 GnomeVFSResult result;
|
|
180 GnomeVFSFileSize position;
|
|
181
|
|
182 result = gnome_vfs_tell(file->handle, &position);
|
|
183
|
|
184 if (result == GNOME_VFS_OK)
|
|
185 return position;
|
|
186 else
|
|
187 return -1;
|
|
188 }
|
|
189
|
|
190 gboolean
|
|
191 vfs_file_test(const gchar * path,
|
|
192 GFileTest test)
|
|
193 {
|
|
194 GnomeVFSResult result;
|
|
195 GnomeVFSFileInfo info;
|
|
196 GFileTest file_test;
|
|
197
|
|
198 result = gnome_vfs_get_file_info(path, &info,
|
|
199 GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
|
|
200
|
|
201 if (test == G_FILE_TEST_EXISTS)
|
|
202 return (result == GNOME_VFS_OK) ? TRUE : FALSE;
|
|
203 else if (test == G_FILE_TEST_IS_EXECUTABLE)
|
|
204 return (info.permissions & GNOME_VFS_PERM_ACCESS_EXECUTABLE)
|
|
205 ? TRUE : FALSE;
|
|
206
|
|
207 switch (info.type) {
|
|
208 case GNOME_VFS_FILE_TYPE_REGULAR:
|
|
209 file_test = G_FILE_TEST_IS_REGULAR;
|
|
210 break;
|
|
211 case GNOME_VFS_FILE_TYPE_SYMBOLIC_LINK:
|
|
212 file_test = G_FILE_TEST_IS_SYMLINK;
|
|
213 break;
|
|
214 case GNOME_VFS_FILE_TYPE_DIRECTORY:
|
|
215 file_test = G_FILE_TEST_IS_DIR;
|
|
216 default:
|
|
217 return FALSE;
|
|
218 }
|
|
219
|
|
220 if (test == file_test)
|
|
221 return TRUE;
|
|
222 else
|
|
223 return FALSE;
|
|
224 }
|
|
225
|
|
226 gboolean
|
|
227 vfs_is_writeable(const gchar * path)
|
|
228 {
|
|
229 GnomeVFSFileInfo info;
|
|
230
|
|
231 if (gnome_vfs_get_file_info(path, &info, GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS)
|
|
232 != GNOME_VFS_OK) {
|
|
233 return FALSE;
|
|
234 }
|
|
235
|
|
236 return (info.permissions & GNOME_VFS_PERM_ACCESS_WRITABLE ? TRUE : FALSE);
|
|
237 }
|
|
238
|
|
239 gint
|
|
240 vfs_truncate(VFSFile * file,
|
|
241 glong length)
|
|
242 {
|
|
243 GnomeVFSResult result;
|
|
244
|
|
245 result = gnome_vfs_truncate_handle(file->handle, (GnomeVFSFileSize) length);
|
|
246
|
|
247 if (result == GNOME_VFS_OK)
|
|
248 return 0;
|
|
249 else
|
|
250 return -1;
|
|
251 }
|
|
252
|
|
253 static gchar *strdup_exclude_chars(const gchar * s,
|
|
254 const gchar * c);
|
|
255
|
|
256 static void
|
|
257 mode_to_gnome_vfs(const gchar * mode,
|
|
258 GnomeVFSOpenMode * g_mode,
|
|
259 gboolean * truncate,
|
|
260 gboolean * append)
|
|
261 {
|
|
262 gchar *s;
|
|
263
|
|
264 *g_mode = GNOME_VFS_OPEN_RANDOM;
|
|
265 *truncate = *append = FALSE;
|
|
266
|
|
267 s = strdup_exclude_chars(mode, "bt");
|
|
268 switch (s[0]) {
|
|
269 case 'r':
|
|
270 *g_mode |= GNOME_VFS_OPEN_READ;
|
|
271
|
|
272 if (s[1] == '+')
|
|
273 *g_mode |= GNOME_VFS_OPEN_WRITE;
|
|
274
|
|
275 break;
|
|
276 case 'w':
|
|
277 case 'a':
|
|
278 *g_mode |= GNOME_VFS_OPEN_WRITE;
|
|
279
|
|
280 if (s[0] == 'w')
|
|
281 *truncate = TRUE;
|
|
282 else
|
|
283 *append = TRUE;
|
|
284
|
|
285 if (s[1] == '+')
|
|
286 *g_mode |= GNOME_VFS_OPEN_READ;
|
|
287
|
|
288 break;
|
|
289 default:
|
|
290 g_warning("mode_to_gnome_vfs: unhandled mode character");
|
|
291 }
|
|
292 g_free(s);
|
|
293 }
|
|
294
|
|
295 static gchar *
|
|
296 strdup_exclude_chars(const gchar * s,
|
|
297 const gchar * c)
|
|
298 {
|
|
299 gint i, j, k;
|
|
300 gint newlen = 0;
|
|
301 gchar *newstr;
|
|
302 gboolean found;
|
|
303
|
|
304 /* Calculate number of chars in new string */
|
|
305 for (i = 0; s[i] != '\0'; i++) {
|
|
306 found = FALSE;
|
|
307
|
|
308 for (j = 0; j < strlen(c) && !found; j++)
|
|
309 if (s[i] == c[j])
|
|
310 found = TRUE;
|
|
311
|
|
312 if (!found)
|
|
313 newlen++;
|
|
314 }
|
|
315
|
|
316 newstr = g_malloc(newlen + 1);
|
|
317
|
|
318 /* Copy valid chars to new string */
|
|
319 for (i = k = 0; s[i] != '\0'; i++) {
|
|
320 found = FALSE;
|
|
321
|
|
322 for (j = 0; j < strlen(c) && !found; j++)
|
|
323 if (s[i] == c[j])
|
|
324 found = TRUE;
|
|
325
|
|
326 if (!found)
|
|
327 newstr[k++] = s[i];
|
|
328 }
|
|
329
|
|
330 newstr[k] = '\0';
|
|
331
|
|
332 return newstr;
|
|
333 }
|