Default files/directories to at least 0444/0555 mode.
[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         G_LOCK(libcaptive);
65         errvfsresult=captive_directory_new_open(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir_name);
66         G_UNLOCK(libcaptive);
67         if (errvfsresult!=GNOME_VFS_OK)
68                 goto fail;
69
70         file_info.valid_fields=GNOME_VFS_FILE_INFO_FIELDS_TYPE;
71         file_info.type=GNOME_VFS_FILE_TYPE_DIRECTORY;
72         if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(captivefs_vfs,&fattr,&file_info))
73                 goto fail_unref;
74         for (csp=dots;*csp;csp++)
75                 if (0>lu_cache_add2dir(ddir,(/* de-const */ char *)*csp,NULL,&fattr)) {
76                         g_warning("Failed lu_cache_add2dir() for: %s",*csp);
77                         goto fail_unref;
78                         }
79
80         for (;;) {
81                 G_LOCK(libcaptive);
82                 errvfsresult=captive_directory_read(captive_directory_object,&file_info);
83                 G_UNLOCK(libcaptive);
84                 if (errvfsresult==GNOME_VFS_ERROR_EOF) {
85                         /* 'captive_directory_object' is now stuck at EOF - GnomeVFS behaves that way. */
86                         break;
87                         }
88                 if (errvfsresult!=GNOME_VFS_OK)
89                         goto fail_unref;
90
91                 if (!captivefs_GnomeVFSFileInfo_to_lufs_fattr(captivefs_vfs,&fattr,&file_info))
92                         goto fail_unref;
93
94                 if (0>lu_cache_add2dir(ddir,file_info.name,NULL,&fattr)) {
95                         g_warning("Failed lu_cache_add2dir() for: %s",file_info.name);
96                         goto fail_unref;
97                         }
98
99                 if (captivefs_vfs->options.debug_messages)
100                         g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_readdir: got: %s",file_info.name);
101                 }
102
103         G_LOCK(libcaptive);
104         g_object_unref(captive_directory_object);
105         G_UNLOCK(libcaptive);
106
107         return 0;
108
109 fail_unref:
110         G_LOCK(libcaptive);
111         g_object_unref(captive_directory_object);
112         G_UNLOCK(libcaptive);
113 fail:
114         return -1;
115 }
116
117
118 /* Create a directory
119  */
120 int captivefs_mkdir(struct captivefs_vfs *captivefs_vfs,const char *dir,int mode)
121 {
122 CaptiveDirectoryObject *captive_directory_object;
123 GnomeVFSResult errvfsresult;
124
125         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
126         g_return_val_if_fail(dir!=NULL,-1);
127
128         if (captivefs_vfs->options.debug_messages)
129                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_mkdir: dir=%s,mode=0x%X",dir,mode);
130
131         G_LOCK(libcaptive);
132         errvfsresult=captive_directory_new_make(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir,mode);
133         G_UNLOCK(libcaptive);
134         if (errvfsresult!=GNOME_VFS_OK)
135                 return -1;
136
137         G_LOCK(libcaptive);
138         g_object_unref(captive_directory_object);
139         G_UNLOCK(libcaptive);
140
141         return 0;
142 }
143
144
145 /* Delete a directory
146  */
147 int captivefs_rmdir(struct captivefs_vfs *captivefs_vfs,const char *dir)
148 {
149 CaptiveDirectoryObject *captive_directory_object;
150 GnomeVFSResult errvfsresult;
151
152         g_return_val_if_fail(captivefs_vfs_validate(captivefs_vfs),-1);
153         g_return_val_if_fail(dir!=NULL,-1);
154
155         if (captivefs_vfs->options.debug_messages)
156                 g_log(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG,"captivefs_rmdir: dir=%s",dir);
157
158         G_LOCK(libcaptive);
159         errvfsresult=captive_directory_new_open(&captive_directory_object,captivefs_vfs->captive_vfs_object,dir);
160         G_UNLOCK(libcaptive);
161         if (errvfsresult!=GNOME_VFS_OK)
162                 return -1;
163
164         G_LOCK(libcaptive);
165         errvfsresult=captive_directory_remove(captive_directory_object);
166         G_UNLOCK(libcaptive);
167
168         G_LOCK(libcaptive);
169         g_object_unref(captive_directory_object);
170         G_UNLOCK(libcaptive);
171
172         if (errvfsresult!=GNOME_VFS_OK)
173                 return -1;
174         return 0;
175 }