OSDN Git Service

Fix format strings warnings.
[android-x86/external-exfat.git] / fuse / main.c
1 /*
2  *  main.c
3  *  FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
4  *
5  *  Created by Andrew Nayenko on 01.09.09.
6  *  This software is distributed under the GNU General Public License 
7  *  version 3 or any later.
8  */
9
10 #include <fuse.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <exfat.h>
17 #include <inttypes.h>
18
19 #define exfat_debug(format, ...)
20
21 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
22         #error FUSE 2.6 or later is required
23 #endif
24
25 struct exfat ef;
26
27 static struct exfat_node* get_node(const struct fuse_file_info* fi)
28 {
29         return (struct exfat_node*) (size_t) fi->fh;
30 }
31
32 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
33 {
34         fi->fh = (uint64_t) (size_t) node;
35 }
36
37 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
38 {
39         struct exfat_node* node;
40         int rc;
41
42         exfat_debug("[fuse_exfat_getattr] %s", path);
43
44         rc = exfat_lookup(&ef, &node, path);
45         if (rc != 0)
46                 return rc;
47
48         exfat_stat(&ef, node, stbuf);
49         exfat_put_node(&ef, node);
50         return 0;
51 }
52
53 static int fuse_exfat_truncate(const char* path, off_t size)
54 {
55         struct exfat_node* node;
56         int rc;
57
58         exfat_debug("[fuse_exfat_truncate] %s, %"PRIu64, path, (uint64_t) size);
59
60         rc = exfat_lookup(&ef, &node, path);
61         if (rc != 0)
62                 return rc;
63
64         exfat_truncate(&ef, node, size);
65         exfat_put_node(&ef, node);
66         return 0;
67 }
68
69 static int fuse_exfat_readdir(const char* path, void* buffer,
70                 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
71 {
72         struct exfat_node* parent;
73         struct exfat_node* node;
74         struct exfat_iterator it;
75         int rc;
76         char name[EXFAT_NAME_MAX + 1];
77
78         exfat_debug("[fuse_exfat_readdir] %s", path);
79
80         rc = exfat_lookup(&ef, &parent, path);
81         if (rc != 0)
82                 return rc;
83         if (!(parent->flags & EXFAT_ATTRIB_DIR))
84         {
85                 exfat_put_node(&ef, parent);
86                 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
87                 return -ENOTDIR;
88         }
89
90         filler(buffer, ".", NULL, 0);
91         filler(buffer, "..", NULL, 0);
92
93         rc = exfat_opendir(&ef, parent, &it);
94         if (rc != 0)
95         {
96                 exfat_put_node(&ef, parent);
97                 exfat_error("failed to open directory `%s'", path);
98                 return rc;
99         }
100         while ((node = exfat_readdir(&ef, &it)))
101         {
102                 exfat_get_name(node, name, EXFAT_NAME_MAX);
103                 exfat_debug("[fuse_exfat_readdir] %s: %s, %"PRIu64" bytes, cluster %u",
104                                 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
105                                 (uint64_t) node->size, node->start_cluster);
106                 filler(buffer, name, NULL, 0);
107                 exfat_put_node(&ef, node);
108         }
109         exfat_closedir(&ef, &it);
110         exfat_put_node(&ef, parent);
111         return 0;
112 }
113
114 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
115 {
116         struct exfat_node* node;
117         int rc;
118
119         exfat_debug("[fuse_exfat_open] %s", path);
120
121         rc = exfat_lookup(&ef, &node, path);
122         if (rc != 0)
123                 return rc;
124         set_node(fi, node);
125         return 0;
126 }
127
128 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
129 {
130         exfat_put_node(&ef, get_node(fi));
131         return 0;
132 }
133
134 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
135                 off_t offset, struct fuse_file_info* fi)
136 {
137         exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
138         return exfat_read(&ef, get_node(fi), buffer, size, offset);
139 }
140
141 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
142                 off_t offset, struct fuse_file_info* fi)
143 {
144         exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
145         return exfat_write(&ef, get_node(fi), buffer, size, offset);
146 }
147
148 static int fuse_exfat_unlink(const char* path)
149 {
150         struct exfat_node* node;
151         int rc;
152
153         exfat_debug("[fuse_exfat_unlink] %s", path);
154
155         rc = exfat_lookup(&ef, &node, path);
156         if (rc != 0)
157                 return rc;
158
159         rc = exfat_unlink(&ef, node);
160         exfat_put_node(&ef, node);
161         return rc;
162 }
163
164 static int fuse_exfat_rmdir(const char* path)
165 {
166         struct exfat_node* node;
167         int rc;
168
169         exfat_debug("[fuse_exfat_rmdir] %s", path);
170
171         rc = exfat_lookup(&ef, &node, path);
172         if (rc != 0)
173                 return rc;
174
175         rc = exfat_rmdir(&ef, node);
176         exfat_put_node(&ef, node);
177         return rc;
178 }
179
180 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
181 {
182         exfat_debug("[fuse_exfat_mknod] %s", path);
183         return exfat_mknod(&ef, path);
184 }
185
186 static int fuse_exfat_mkdir(const char* path, mode_t mode)
187 {
188         exfat_debug("[fuse_exfat_mkdir] %s", path);
189         return exfat_mkdir(&ef, path);
190 }
191
192 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
193 {
194         struct exfat_node* node;
195         int rc;
196
197         exfat_debug("[fuse_exfat_utimens] %s", path);
198
199         rc = exfat_lookup(&ef, &node, path);
200         if (rc != 0)
201                 return rc;
202
203         exfat_utimes(node, tv);
204         exfat_put_node(&ef, node);
205         return 0;
206 }
207
208 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
209 {
210         const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
211
212         sfs->f_bsize = BLOCK_SIZE(*ef.sb);
213         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
214         sfs->f_blocks = block_count;
215         sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
216         sfs->f_bfree = sfs->f_bavail;
217         sfs->f_namemax = EXFAT_NAME_MAX;
218
219         /*
220            Below are fake values because in exFAT there is
221            a) no simple way to count files;
222            b) no such thing as inode;
223            So here we assume that inode = cluster.
224         */
225         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
226         sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
227         sfs->f_ffree = sfs->f_bavail;
228
229         return 0;
230 }
231
232 static void fuse_exfat_destroy(void* unused)
233 {
234         exfat_unmount(&ef);
235 }
236
237 static void usage(const char* prog)
238 {
239         fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
240         exit(1);
241 }
242
243 static struct fuse_operations fuse_exfat_ops =
244 {
245         .getattr        = fuse_exfat_getattr,
246         .truncate       = fuse_exfat_truncate,
247         .readdir        = fuse_exfat_readdir,
248         .open           = fuse_exfat_open,
249         .release        = fuse_exfat_release,
250         .read           = fuse_exfat_read,
251         .write          = fuse_exfat_write,
252         .unlink         = fuse_exfat_unlink,
253         .rmdir          = fuse_exfat_rmdir,
254         .mknod          = fuse_exfat_mknod,
255         .mkdir          = fuse_exfat_mkdir,
256         .utimens        = fuse_exfat_utimens,
257         .statfs         = fuse_exfat_statfs,
258         .destroy        = fuse_exfat_destroy,
259 };
260
261 int main(int argc, char* argv[])
262 {
263         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
264         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
265         const char* spec = NULL;
266         const char* mount_point = NULL;
267         const char* mount_options = "";
268         int debug = 0;
269         struct fuse_chan* fc = NULL;
270         struct fuse* fh = NULL;
271         char** pp;
272
273         printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
274
275         for (pp = argv + 1; *pp; pp++)
276         {
277                 if (strcmp(*pp, "-o") == 0)
278                 {
279                         pp++;
280                         if (*pp == NULL)
281                                 usage(argv[0]);
282                         mount_options = *pp;
283                 }
284                 else if (strcmp(*pp, "-d") == 0)
285                         debug = 1;
286                 else if (spec == NULL)
287                         spec = *pp;
288                 else if (mount_point == NULL)
289                         mount_point = *pp;
290                 else
291                         usage(argv[0]);
292         }
293         if (spec == NULL || mount_point == NULL)
294                 usage(argv[0]);
295
296         /* create arguments for fuse_mount() */
297         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
298                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
299                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
300                 return 1;
301
302         /* create FUSE mount point */
303         fc = fuse_mount(mount_point, &mount_args);
304         fuse_opt_free_args(&mount_args);
305         if (fc == NULL)
306                 return 1;
307
308         /* create arguments for fuse_new() */
309         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
310                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
311         {
312                 fuse_unmount(mount_point, fc);
313                 return 1;
314         }
315
316         /* create new FUSE file system */
317         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
318                         sizeof(struct fuse_operations), NULL);
319         fuse_opt_free_args(&newfs_args);
320         if (fh == NULL)
321         {
322                 fuse_unmount(mount_point, fc);
323                 return 1;
324         }
325
326         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
327         if (fuse_set_signal_handlers(fuse_get_session(fh)))
328         {
329                 fuse_unmount(mount_point, fc);
330                 fuse_destroy(fh);
331                 return 1;
332         }
333
334         if (exfat_mount(&ef, spec, mount_options) != 0)
335         {
336                 fuse_unmount(mount_point, fc);
337                 fuse_destroy(fh);
338                 return 1;
339         }
340
341         /* go to background unless "-d" option is passed */
342         fuse_daemonize(debug);
343
344         /* FUSE main loop */
345         fuse_loop(fh);
346
347         /* it's quite illogical but fuse_unmount() must be called BEFORE
348            fuse_destroy() */
349         fuse_unmount(mount_point, fc);
350         fuse_destroy(fh);
351         return 0;
352 }