Initial original import from: fuse-2.4.2-2.fc4
[captive.git] / src / client / lufs / captivefs-directory.c
1 /* $Id$
2  * lufs interface module directory objects implementation for libcaptive
3  * Copyright (C) 2003 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
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
8  * 
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.
13  * 
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
17  */
18
19
20 #include "config.h"
21
22 #include <glib/gmessages.h>
23 #include "captivefs-misc.h"
24 #include "captivefs-attr.h"
25 #include "captivefs-vfs.h"
26
27 #include <captive/client-vfs.h>
28 #include <captive/client-directory.h>
29
30 #include <lufs/fs.h>
31 #include <lufs/proto.h>
32
33
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.
40  *
41  * Notes:
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
45  * absolute paths.
46  *     If your filesystem doesn't natively support '.' or '..', don't forget
47  * to add them to the cache first-thing here.
48  */
49 int captivefs_readdir(struct captivefs_vfs *captivefs_vfs,const char *dir_name,struct directory *ddir)
50 {
51 struct lufs_fattr fattr;
52 GnomeVFSResult errvfsresult;
53 CaptiveDirectoryObject *captive_directory_object;
54 GnomeVFSFileInfo file_info;
55 const char *dots[]={".","..",NULL},**csp;
56
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);
60
61         if (captivefs_vfs->options.debug_messages)
62                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_readdir: dir_name=%s",dir_name);
63
64         dir_name=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir_name);
65
66         G_LOCK(libcaptive);
67         errvfsresult=captive_directory_new_open(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir_name);
68         G_UNLOCK(libcaptive);
69         if (errvfsresult!=GNOME_VFS_OK)
70                 goto fail;
71
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))
75                 goto fail_unref;
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);
79                         goto fail_unref;
80                         }
81
82         for (;;) {
83 char *file_info_name_filename;
84 int errint;
85
86                 G_LOCK(libcaptive);
87                 errvfsresult=captive_directory_read(captive_directory_object,&file_info);
88                 G_UNLOCK(libcaptive);
89                 if (errvfsresult==GNOME_VFS_ERROR_EOF) {
90                         /* 'captive_directory_object' is now stuck at EOF - GnomeVFS behaves that way. */
91                         break;
92                         }
93                 if (errvfsresult!=GNOME_VFS_OK)
94                         goto fail_unref;
95
96                 if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(captivefs_vfs,&fattr,&file_info))
97                         goto fail_unref;
98
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),
102                                 NULL,&fattr);
103                 g_free(file_info_name_filename);        /* may be NULL */
104                 if (0>errint) {
105                         g_warning("Failed lu_cache_add2dir() for: %s",file_info.name);
106                         goto fail_unref;
107                         }
108
109                 if (captivefs_vfs->options.debug_messages)
110                         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_readdir: got: %s",file_info.name);
111                 }
112
113         G_LOCK(libcaptive);
114         g_object_unref(captive_directory_object);
115         G_UNLOCK(libcaptive);
116
117         return 0;
118
119 fail_unref:
120         G_LOCK(libcaptive);
121         g_object_unref(captive_directory_object);
122         G_UNLOCK(libcaptive);
123 fail:
124         return -1;
125 }
126
127
128 /* Create a directory
129  */
130 int captivefs_mkdir(struct captivefs_vfs *captivefs_vfs,const char *dir,int mode)
131 {
132 CaptiveDirectoryObject *captive_directory_object;
133 GnomeVFSResult errvfsresult;
134
135         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
136         g_return_val_if_fail(dir!=NULL,-1);
137
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);
140
141         dir=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir);
142
143         G_LOCK(libcaptive);
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)
147                 return -1;
148
149         G_LOCK(libcaptive);
150         g_object_unref(captive_directory_object);
151         G_UNLOCK(libcaptive);
152
153         return 0;
154 }
155
156
157 /* Delete a directory
158  */
159 int captivefs_rmdir(struct captivefs_vfs *captivefs_vfs,const char *dir)
160 {
161 CaptiveDirectoryObject *captive_directory_object;
162 GnomeVFSResult errvfsresult;
163
164         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
165         g_return_val_if_fail(dir!=NULL,-1);
166
167         if (captivefs_vfs->options.debug_messages)
168                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_rmdir: dir=%s",dir);
169
170         dir=CAPTIVEFS_FILENAME_TO_UTF8_ALLOCA(dir);
171
172         G_LOCK(libcaptive);
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)
176                 return -1;
177
178         G_LOCK(libcaptive);
179         errvfsresult=captive_directory_remove(captive_directory_object);
180         G_UNLOCK(libcaptive);
181
182         G_LOCK(libcaptive);
183         g_object_unref(captive_directory_object);
184         G_UNLOCK(libcaptive);
185
186         if (errvfsresult!=GNOME_VFS_OK)
187                 return -1;
188         return 0;
189 }