2 * lufs interface module file objects implementation for libcaptive
3 * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; exactly version 2 of June 1991 is required
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include <glib/gmessages.h>
23 #include "captivefs-misc.h"
24 #include "captivefs-attr.h"
25 #include "captivefs-vfs.h"
28 #include <captive/client-vfs.h>
29 #include <captive/client-file.h>
34 /* Read a file/dir's attributes
35 * Fill all relevant data into the fattr structure.
36 * The uid/gid fields are just ownership hints hints:
37 * != 0 => we own the file
38 * == 0 => we don't own it
39 * The credentials structure (if applicable and saved from _init)
40 * can help determine ownership based on remote uids/gids.
43 * If your filesysem doesn't natively support '.' or '..',
44 * don't forget to special-case them here.
45 * It is best to assume that name is a relative path, not an
46 * absolute one. Thus, you need to either be keeping track of the
47 * last accessed directory in readdir, or, as this code does, changing
48 * to the current directory there.
50 int captivefs_stat(struct captivefs_vfs *captivefs_vfs,const char *name,struct lufs_fattr *fattr)
52 GnomeVFSResult errvfsresult;
53 CaptiveFileObject *captive_file_object;
54 GnomeVFSFileInfo file_info;
56 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
57 g_return_val_if_fail(name!=NULL,-1);
58 g_return_val_if_fail(fattr!=NULL,-1);
60 if (captivefs_vfs->options.debug_messages)
61 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_stat: name=%s",name);
64 errvfsresult=captive_file_new_open(&captive_file_object,captivefs_vfs->captive_vfs_object,name,GNOME_VFS_OPEN_READ);
66 if (errvfsresult!=GNOME_VFS_OK)
70 errvfsresult=captive_file_file_info_get(captive_file_object,&file_info);
72 if (errvfsresult!=GNOME_VFS_OK)
76 g_object_unref(captive_file_object);
79 if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(fattr,&file_info))
86 g_object_unref(captive_file_object);
95 int captivefs_create(struct captivefs_vfs *captivefs_vfs,const char *file,int mode)
97 GnomeVFSResult errvfsresult;
98 CaptiveFileObject *captive_file_object;
100 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
101 g_return_val_if_fail(file!=NULL,-1);
103 if (captivefs_vfs->options.debug_messages)
104 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_create: file=%s,mode=0x%X",file,mode);
107 errvfsresult=captive_file_new_create(&captive_file_object,captivefs_vfs->captive_vfs_object,file,GNOME_VFS_OPEN_WRITE,
108 FALSE, /* exclusive */
110 G_UNLOCK(libcaptive);
111 if (errvfsresult!=GNOME_VFS_OK)
115 g_object_unref(captive_file_object);
116 G_UNLOCK(libcaptive);
124 int captivefs_unlink(struct captivefs_vfs *captivefs_vfs,const char *file)
126 GnomeVFSResult errvfsresult;
127 CaptiveFileObject *captive_file_object;
129 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
130 g_return_val_if_fail(file!=NULL,-1);
132 if (captivefs_vfs->options.debug_messages)
133 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_unlink: file=%s",file);
136 errvfsresult=captive_file_new_open(&captive_file_object,captivefs_vfs->captive_vfs_object,file,GNOME_VFS_OPEN_WRITE);
137 G_UNLOCK(libcaptive);
138 if (errvfsresult!=GNOME_VFS_OK)
142 errvfsresult=captive_file_remove(captive_file_object);
143 G_UNLOCK(libcaptive);
144 if (errvfsresult!=GNOME_VFS_OK)
148 g_object_unref(captive_file_object);
149 G_UNLOCK(libcaptive);
155 g_object_unref(captive_file_object);
156 G_UNLOCK(libcaptive);
164 int captivefs_rename(struct captivefs_vfs *captivefs_vfs,const char *old_name,const char *new_name)
166 GnomeVFSResult errvfsresult;
167 CaptiveFileObject *captive_file_object;
169 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
170 g_return_val_if_fail(old_name!=NULL,-1);
171 g_return_val_if_fail(new_name!=NULL,-1);
173 if (captivefs_vfs->options.debug_messages)
174 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_rename: old_name=%s,new_name=%s",old_name,new_name);
177 errvfsresult=captive_file_new_open(&captive_file_object,captivefs_vfs->captive_vfs_object,old_name,GNOME_VFS_OPEN_WRITE);
178 G_UNLOCK(libcaptive);
179 if (errvfsresult!=GNOME_VFS_OK)
183 errvfsresult=captive_file_move(captive_file_object,new_name,
184 FALSE); /* force_replace */
185 G_UNLOCK(libcaptive);
186 if (errvfsresult!=GNOME_VFS_OK)
190 g_object_unref(captive_file_object);
191 G_UNLOCK(libcaptive);
197 g_object_unref(captive_file_object);
198 G_UNLOCK(libcaptive);
204 /* map: (const gchar *) -> (CaptiveFileObject *) */
205 static GHashTable *FileHandle_hash;
207 static void FileHandle_hash_value_destroy_func(CaptiveFileObject *captive_file_object)
209 g_return_if_fail(CAPTIVE_FILE_IS_OBJECT(captive_file_object));
211 g_object_unref(captive_file_object);
214 static void FileHandle_hash_init(void)
218 FileHandle_hash=g_hash_table_new_full(g_str_hash,g_str_equal,
219 (GDestroyNotify)g_free, /* key_destroy_func */
220 (GDestroyNotify)FileHandle_hash_value_destroy_func); /* value_destroy_func */
223 static CaptiveFileObject *FileHandle_lookup(const char *name)
225 CaptiveFileObject *captive_file_object;
227 g_return_val_if_fail(name!=NULL,NULL);
229 FileHandle_hash_init();
230 if (!(captive_file_object=g_hash_table_lookup(FileHandle_hash,name))) {
231 g_warning("FileHandle_lookup: FileHandle not found of: %s",name);
235 g_assert(CAPTIVE_FILE_IS_OBJECT(captive_file_object));
237 return captive_file_object;
244 * By default, LUFS has no concept of file handles. To implement file
245 * handles, take a look at the atbl class in sshfs - it is easy to cut&paste
246 * for use, and can be easily adapted for whatever purpose you need handles
249 * Unlike the POSIX open command which has both a "mode" variable and
250 * a "flags" variable, this only has a "mode" variable. To convert to the
251 * POSIX version, ->flags=mode^O_ACCMODE and ->mode=mode&O_ACCMODE.
253 int captivefs_open(struct captivefs_vfs *captivefs_vfs,const char *file,unsigned mode)
255 CaptiveFileObject *captive_file_object;
256 GnomeVFSOpenMode gnome_vfs_open_mode;
257 GnomeVFSResult errvfsresult;
259 /* We may be called from the parent. */
260 g_return_val_if_fail(captivefs_vfs!=NULL,FALSE);
261 g_return_val_if_fail(file!=NULL,-1);
263 if (captivefs_vfs->options.debug_messages)
264 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_open: file=%s,mode=0x%X",file,mode);
266 if (!captivefs_vfs_validate(captivefs_vfs))
269 FileHandle_hash_init();
270 if ((captive_file_object=g_hash_table_lookup(FileHandle_hash,file))) {
271 g_assert(CAPTIVE_FILE_IS_OBJECT(captive_file_object));
272 g_warning("captivefs_open: FileHandle already exists of: %s",file);
276 if (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
277 g_warning("captivefs_open: Unrecognized 'mode' 0x%X",mode);
280 switch (mode & (O_RDONLY|O_WRONLY|O_RDWR)) {
281 case O_RDONLY: gnome_vfs_open_mode=GNOME_VFS_OPEN_READ; break;
282 case O_WRONLY: gnome_vfs_open_mode=GNOME_VFS_OPEN_WRITE; break;
283 case O_RDWR: gnome_vfs_open_mode=GNOME_VFS_OPEN_READ|GNOME_VFS_OPEN_WRITE; break;
285 g_warning("captivefs_open: Unrecognized 'mode' 0x%X",mode);
290 errvfsresult=captive_file_new_open(&captive_file_object,captivefs_vfs->captive_vfs_object,file,
291 gnome_vfs_open_mode|GNOME_VFS_OPEN_RANDOM);
292 G_UNLOCK(libcaptive);
293 if (errvfsresult!=GNOME_VFS_OK)
296 g_hash_table_insert(FileHandle_hash,g_strdup(file),captive_file_object);
302 int captivefs_release(struct captivefs_vfs *captivefs_vfs,const char *file)
304 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
305 g_return_val_if_fail(file!=NULL,-1);
307 if (captivefs_vfs->options.debug_messages)
308 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_release: file=%s",file);
310 FileHandle_hash_init();
311 if (!g_hash_table_remove(FileHandle_hash,file)) {
312 g_warning("captivefs_release: FileHandle not found of: %s",file);
320 /* Read from a file. Changed to use the (2) routines not for efficiency,
321 * but to make it work with 64-bit offsets :-(.
323 int captivefs_read(struct captivefs_vfs *captivefs_vfs,const char *file,long long offset,unsigned long count,void *buf)
325 CaptiveFileObject *captive_file_object;
326 GnomeVFSFileSize bytes_read;
327 GnomeVFSResult errvfsresult;
329 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
330 g_return_val_if_fail(file!=NULL,-1);
331 g_return_val_if_fail(buf!=NULL || count==0,-1);
333 if (captivefs_vfs->options.debug_messages)
334 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_read: file=%s,offset=%" G_GINT64_FORMAT ",count=0x%lX",
335 file,(gint64)offset,count);
337 if (!(captive_file_object=FileHandle_lookup(file)))
341 errvfsresult=captive_file_seek(captive_file_object,GNOME_VFS_SEEK_START,offset);
342 G_UNLOCK(libcaptive);
343 if (errvfsresult!=GNOME_VFS_OK)
347 errvfsresult=captive_file_read(captive_file_object,buf,count,&bytes_read);
348 G_UNLOCK(libcaptive);
349 if (errvfsresult!=GNOME_VFS_OK)
352 if (bytes_read>INT_MAX) {
353 g_warning("captivefs_read: Read %" G_GUINT64_FORMAT " bytes > INT_MAX=%d; dropped data",
354 (guint64)bytes_read,INT_MAX);
364 int captivefs_write(struct captivefs_vfs *captivefs_vfs,const char *file,long long offset,unsigned long count,const void *buf)
366 CaptiveFileObject *captive_file_object;
367 GnomeVFSFileSize bytes_written;
368 GnomeVFSResult errvfsresult;
370 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
371 g_return_val_if_fail(file!=NULL,-1);
372 g_return_val_if_fail(buf!=NULL || count==0,-1);
374 if (captivefs_vfs->options.debug_messages)
375 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_write: file=%s,offset=%" G_GINT64_FORMAT ",count=0x%lX",
376 file,(gint64)offset,count);
378 if (!(captive_file_object=FileHandle_lookup(file)))
382 errvfsresult=captive_file_seek(captive_file_object,GNOME_VFS_SEEK_START,offset);
383 G_UNLOCK(libcaptive);
384 if (errvfsresult!=GNOME_VFS_OK)
388 errvfsresult=captive_file_write(captive_file_object,buf,count,&bytes_written);
389 G_UNLOCK(libcaptive);
390 if (errvfsresult!=GNOME_VFS_OK)
393 if (bytes_written>INT_MAX) {
394 g_warning("captivefs_written: Written %" G_GUINT64_FORMAT " bytes > INT_MAX=%d; dropped data",
395 (guint64)bytes_written,INT_MAX);
396 bytes_written=INT_MAX;
399 return bytes_written;
403 /* Change a file/dir's attributes
405 int captivefs_setattr(struct captivefs_vfs *captivefs_vfs,const char *file,const struct lufs_fattr *fattr)
407 GnomeVFSFileInfo file_info;
408 GnomeVFSResult errvfsresult;
409 CaptiveFileObject *captive_file_object;
411 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
412 g_return_val_if_fail(file!=NULL,-1);
413 g_return_val_if_fail(fattr!=NULL,-1);
415 if (captivefs_vfs->options.debug_messages)
416 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_setattr: file=%s",file);
418 if (!captivefs_lufs_fattr_to_GnomeVFSFileInfo(&file_info,fattr))
422 errvfsresult=captive_file_new_open(&captive_file_object,captivefs_vfs->captive_vfs_object,file,GNOME_VFS_OPEN_WRITE);
423 G_UNLOCK(libcaptive);
424 if (errvfsresult!=GNOME_VFS_OK)
428 errvfsresult=captive_file_file_info_set(captive_file_object,&file_info,file_info.valid_fields);
429 G_UNLOCK(libcaptive);
430 if (errvfsresult!=GNOME_VFS_OK)
434 g_object_unref(captive_file_object);
435 G_UNLOCK(libcaptive);
441 g_object_unref(captive_file_object);
442 G_UNLOCK(libcaptive);