2 * lufs interface module directory 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"
27 #include <captive/client-vfs.h>
28 #include <captive/client-directory.h>
31 #include <lufs/proto.h>
34 /* Read a directory's content
35 * For each directory entry, call
36 * lu_cache_add2dir(struct directory *dir, char *name, char *link, struct lufs_fattr *fattr)
37 * to add its information.
38 * The link is optional(NULL) and applicable only if the entry is a
39 * (sym)link and we have the target info at hand.
42 * dir_name is an absolute path. However, it is generally a good idea
43 * to either change to that directory here or at least keep track of what
44 * directory was being called on, for function calls that might not get
46 * If your filesystem doesn't natively support '.' or '..', don't forget
47 * to add them to the cache first-thing here.
49 int captivefs_readdir(struct captivefs_vfs *captivefs_vfs,const char *dir_name,struct directory *ddir)
51 struct lufs_fattr fattr;
52 GnomeVFSResult errvfsresult;
53 CaptiveDirectoryObject *captive_directory_object;
54 GnomeVFSFileInfo file_info;
55 const char *dots[]={".","..",NULL},**csp;
57 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
58 g_return_val_if_fail(dir_name!=NULL,-1);
59 g_return_val_if_fail(ddir!=NULL,-1);
61 if (captivefs_vfs->options.debug_messages)
62 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_readdir: dir_name=%s",dir_name);
64 dir_name=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir_name);
67 errvfsresult=captive_directory_new_open(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir_name);
69 if (errvfsresult!=GNOME_VFS_OK)
72 file_info.valid_fields=GNOME_VFS_FILE_INFO_FIELDS_TYPE;
73 file_info.type=GNOME_VFS_FILE_TYPE_DIRECTORY;
74 if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(captivefs_vfs,&fattr,&file_info))
76 for (csp=dots;*csp;csp++)
77 if (0>lu_cache_add2dir(ddir,(/* de-const */ char *)*csp,NULL,&fattr)) {
78 g_warning("Failed lu_cache_add2dir() for: %s",*csp);
83 char *file_info_name_filename;
87 errvfsresult=captive_directory_read(captive_directory_object,&file_info);
89 if (errvfsresult==GNOME_VFS_ERROR_EOF) {
90 /* 'captive_directory_object' is now stuck at EOF - GnomeVFS behaves that way. */
93 if (errvfsresult!=GNOME_VFS_OK)
96 if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(captivefs_vfs,&fattr,&file_info))
99 file_info_name_filename=captivefs_filename_from_utf8_malloc_errorchecking(file_info.name);
100 errint=lu_cache_add2dir(ddir,
101 (file_info_name_filename ? file_info_name_filename : file_info.name),
103 g_free(file_info_name_filename); /* may be NULL */
105 g_warning("Failed lu_cache_add2dir() for: %s",file_info.name);
109 if (captivefs_vfs->options.debug_messages)
110 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_readdir: got: %s",file_info.name);
114 g_object_unref(captive_directory_object);
115 G_UNLOCK(libcaptive);
121 g_object_unref(captive_directory_object);
122 G_UNLOCK(libcaptive);
128 /* Create a directory
130 int captivefs_mkdir(struct captivefs_vfs *captivefs_vfs,const char *dir,int mode)
132 CaptiveDirectoryObject *captive_directory_object;
133 GnomeVFSResult errvfsresult;
135 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
136 g_return_val_if_fail(dir!=NULL,-1);
138 if (captivefs_vfs->options.debug_messages)
139 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mkdir: dir=%s,mode=0x%X",dir,mode);
141 dir=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir);
144 errvfsresult=captive_directory_new_make(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir,mode);
145 G_UNLOCK(libcaptive);
146 if (errvfsresult!=GNOME_VFS_OK)
150 g_object_unref(captive_directory_object);
151 G_UNLOCK(libcaptive);
157 /* Delete a directory
159 int captivefs_rmdir(struct captivefs_vfs *captivefs_vfs,const char *dir)
161 CaptiveDirectoryObject *captive_directory_object;
162 GnomeVFSResult errvfsresult;
164 g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
165 g_return_val_if_fail(dir!=NULL,-1);
167 if (captivefs_vfs->options.debug_messages)
168 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_rmdir: dir=%s",dir);
170 dir=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir);
173 errvfsresult=captive_directory_new_open(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir);
174 G_UNLOCK(libcaptive);
175 if (errvfsresult!=GNOME_VFS_OK)
179 errvfsresult=captive_directory_remove(captive_directory_object);
180 G_UNLOCK(libcaptive);
183 g_object_unref(captive_directory_object);
184 G_UNLOCK(libcaptive);
186 if (errvfsresult!=GNOME_VFS_OK)