OSDN Git Service

Implement existing files writing in FUSE driver.
[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
18 #define exfat_debug(format, ...)
19
20 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
21         #error FUSE 2.6 or later is required
22 #endif
23
24 struct exfat ef;
25
26 static struct exfat_node* get_node(const struct fuse_file_info *fi)
27 {
28         return (struct exfat_node*) (size_t) fi->fh;
29 }
30
31 static void set_node(struct fuse_file_info *fi, struct exfat_node* node)
32 {
33         fi->fh = (uint64_t) (size_t) node;
34 }
35
36 static int fuse_exfat_getattr(const char *path, struct stat *stbuf)
37 {
38         struct exfat_node* node;
39         int rc;
40
41         exfat_debug("[fuse_exfat_getattr] %s", path);
42
43         rc = exfat_lookup(&ef, &node, path);
44         if (rc != 0)
45                 return rc;
46
47         exfat_stat(node, stbuf);
48         exfat_put_node(node);
49         return 0;
50 }
51
52 static int fuse_exfat_truncate(const char *path, off_t size)
53 {
54         struct exfat_node* node;
55         int rc;
56
57         exfat_debug("[fuse_exfat_truncate] %s, %llu", path, size);
58
59         rc = exfat_lookup(&ef, &node, path);
60         if (rc != 0)
61                 return rc;
62
63         exfat_truncate(&ef, node, size);
64         exfat_put_node(node);
65         return 0;
66 }
67
68 static int fuse_exfat_readdir(const char *path, void *buffer,
69                 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi)
70 {
71         struct exfat_node* parent;
72         struct exfat_node* node;
73         struct exfat_iterator it;
74         int rc;
75         char name[EXFAT_NAME_MAX + 1];
76
77         exfat_debug("[fuse_exfat_readdir] %s", path);
78
79         rc = exfat_lookup(&ef, &parent, path);
80         if (rc != 0)
81                 return rc;
82         if (!(parent->flags & EXFAT_ATTRIB_DIR))
83         {
84                 exfat_put_node(parent);
85                 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
86                 return -ENOTDIR;
87         }
88
89         filler(buffer, ".", NULL, 0);
90         filler(buffer, "..", NULL, 0);
91
92         rc = exfat_opendir(&ef, parent, &it);
93         if (rc != 0)
94         {
95                 exfat_put_node(parent);
96                 exfat_error("failed to open directory `%s'", path);
97                 return rc;
98         }
99         while ((node = exfat_readdir(&ef, &it)))
100         {
101                 exfat_get_name(node, name, EXFAT_NAME_MAX);
102                 exfat_debug("[fuse_exfat_readdir] %s: %s, %llu bytes, cluster %u",
103                                 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
104                                 node->size, node->start_cluster);
105                 filler(buffer, name, NULL, 0);
106                 exfat_put_node(node);
107         }
108         exfat_closedir(&it);
109         exfat_put_node(parent);
110         return 0;
111 }
112
113 static int fuse_exfat_open(const char *path, struct fuse_file_info *fi)
114 {
115         struct exfat_node* node;
116         int rc;
117
118         exfat_debug("[fuse_exfat_open] %s", path);
119
120         rc = exfat_lookup(&ef, &node, path);
121         if (rc != 0)
122                 return rc;
123         set_node(fi, node);
124         return 0;
125 }
126
127 static int fuse_exfat_release(const char *path, struct fuse_file_info *fi)
128 {
129         exfat_put_node(get_node(fi));
130         return 0;
131 }
132
133 static int fuse_exfat_read(const char *path, char *buffer, size_t size,
134                 off_t offset, struct fuse_file_info *fi)
135 {
136         exfat_debug("[fuse_exfat_read] %s (%zu bytes)", path, size);
137         return exfat_read(&ef, get_node(fi), buffer, size, offset);
138 }
139
140 static int fuse_exfat_write(const char *path, const char *buffer, size_t size,
141                 off_t offset, struct fuse_file_info *fi)
142 {
143         exfat_debug("[fuse_exfat_write] %s (%zu bytes)", path, size);
144         return exfat_write(&ef, get_node(fi), buffer, size, offset);
145 }
146
147 static int fuse_exfat_statfs(const char *path, struct statvfs *sfs)
148 {
149         const uint64_t block_count = le64_to_cpu(ef.sb->block_count);
150
151         sfs->f_bsize = BLOCK_SIZE(*ef.sb);
152         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
153         sfs->f_blocks = block_count;
154         sfs->f_bavail = block_count - ef.sb->allocated_percent * block_count / 100;
155         sfs->f_bfree = sfs->f_bavail;
156         sfs->f_namemax = EXFAT_NAME_MAX;
157
158         /*
159            Below are fake values because in exFAT there is
160            a) no simple way to count files;
161            b) no such thing as inode;
162            So here we assume that inode = cluster.
163         */
164         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->bpc_bits;
165         sfs->f_favail = sfs->f_bfree >> ef.sb->bpc_bits;
166         sfs->f_ffree = sfs->f_bavail;
167
168         return 0;
169 }
170
171 void fuse_exfat_destroy(void* unused)
172 {
173         exfat_unmount(&ef);
174 }
175
176 static void usage(const char* prog)
177 {
178         fprintf(stderr, "Usage: %s <spec> <mountpoint> [-o options]\n", prog);
179         exit(1);
180 }
181
182 static struct fuse_operations fuse_exfat_ops =
183 {
184         .getattr        = fuse_exfat_getattr,
185         .truncate       = fuse_exfat_truncate,
186         .readdir        = fuse_exfat_readdir,
187         .open           = fuse_exfat_open,
188         .release        = fuse_exfat_release,
189         .read           = fuse_exfat_read,
190         .write          = fuse_exfat_write,
191         .statfs         = fuse_exfat_statfs,
192         .destroy        = fuse_exfat_destroy,
193 };
194
195 int main(int argc, char* argv[])
196 {
197         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
198         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
199         const char* spec = NULL;
200         const char* mount_point = NULL;
201         const char* mount_options = "";
202         struct fuse_chan* fc = NULL;
203         struct fuse* fh = NULL;
204         char** pp;
205
206         printf("FUSE exfat %u.%u\n", EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR);
207
208         for (pp = argv + 1; *pp; pp++)
209         {
210                 if (strcmp(*pp, "-o") == 0)
211                 {
212                         pp++;
213                         if (*pp == NULL)
214                                 usage(argv[0]);
215                         mount_options = *pp;
216                 }
217                 else if (spec == NULL)
218                         spec = *pp;
219                 else if (mount_point == NULL)
220                         mount_point = *pp;
221                 else
222                         usage(argv[0]);
223         }
224         if (spec == NULL || mount_point == NULL)
225                 usage(argv[0]);
226
227         /* create arguments for fuse_mount() */
228         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
229                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
230                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
231                 return 1;
232
233         /* create FUSE mount point */
234         fc = fuse_mount(mount_point, &mount_args);
235         fuse_opt_free_args(&mount_args);
236         if (fc == NULL)
237                 return 1;
238
239         /* create arguments for fuse_new() */
240         if (fuse_opt_add_arg(&newfs_args, "") != 0)
241         {
242                 fuse_unmount(mount_point, fc);
243                 return 1;
244         }
245
246         /* create new FUSE file system */
247         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
248                         sizeof(struct fuse_operations), NULL);
249         fuse_opt_free_args(&newfs_args);
250         if (fh == NULL)
251         {
252                 fuse_unmount(mount_point, fc);
253                 return 1;
254         }
255
256         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
257         if (fuse_set_signal_handlers(fuse_get_session(fh)))
258         {
259                 fuse_unmount(mount_point, fc);
260                 fuse_destroy(fh);
261                 return 1;
262         }
263
264         if (exfat_mount(&ef, spec) != 0)
265         {
266                 fuse_unmount(mount_point, fc);
267                 fuse_destroy(fh);
268                 return 1;
269         }
270
271         /* FUSE main loop */
272         fuse_loop(fh);
273
274         /* it's quite illogical but fuse_unmount() must be called BEFORE
275            fuse_destroy() */
276         fuse_unmount(mount_point, fc);
277         fuse_destroy(fh);
278         return 0;
279 }