OSDN Git Service

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