Tizen 2.1 base
[framework/base/fuse.git] / example / hello_ll.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7
8   gcc -Wall hello_ll.c `pkg-config fuse --cflags --libs` -o hello_ll
9 */
10
11 #define FUSE_USE_VERSION 26
12
13 #include <fuse_lowlevel.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <fcntl.h>
19 #include <unistd.h>
20 #include <assert.h>
21
22 static const char *hello_str = "Hello World!\n";
23 static const char *hello_name = "hello";
24
25 static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
26 {
27         stbuf->st_ino = ino;
28         switch (ino) {
29         case 1:
30                 stbuf->st_mode = S_IFDIR | 0755;
31                 stbuf->st_nlink = 2;
32                 break;
33
34         case 2:
35                 stbuf->st_mode = S_IFREG | 0444;
36                 stbuf->st_nlink = 1;
37                 stbuf->st_size = strlen(hello_str);
38                 break;
39
40         default:
41                 return -1;
42         }
43         return 0;
44 }
45
46 static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
47                              struct fuse_file_info *fi)
48 {
49         struct stat stbuf;
50
51         (void) fi;
52
53         memset(&stbuf, 0, sizeof(stbuf));
54         if (hello_stat(ino, &stbuf) == -1)
55                 fuse_reply_err(req, ENOENT);
56         else
57                 fuse_reply_attr(req, &stbuf, 1.0);
58 }
59
60 static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
61 {
62         struct fuse_entry_param e;
63
64         if (parent != 1 || strcmp(name, hello_name) != 0)
65                 fuse_reply_err(req, ENOENT);
66         else {
67                 memset(&e, 0, sizeof(e));
68                 e.ino = 2;
69                 e.attr_timeout = 1.0;
70                 e.entry_timeout = 1.0;
71                 hello_stat(e.ino, &e.attr);
72
73                 fuse_reply_entry(req, &e);
74         }
75 }
76
77 struct dirbuf {
78         char *p;
79         size_t size;
80 };
81
82 static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
83                        fuse_ino_t ino)
84 {
85         struct stat stbuf;
86         size_t oldsize = b->size;
87         b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
88         b->p = (char *) realloc(b->p, b->size);
89         memset(&stbuf, 0, sizeof(stbuf));
90         stbuf.st_ino = ino;
91         fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
92                           b->size);
93 }
94
95 #define min(x, y) ((x) < (y) ? (x) : (y))
96
97 static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
98                              off_t off, size_t maxsize)
99 {
100         if (off < bufsize)
101                 return fuse_reply_buf(req, buf + off,
102                                       min(bufsize - off, maxsize));
103         else
104                 return fuse_reply_buf(req, NULL, 0);
105 }
106
107 static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
108                              off_t off, struct fuse_file_info *fi)
109 {
110         (void) fi;
111
112         if (ino != 1)
113                 fuse_reply_err(req, ENOTDIR);
114         else {
115                 struct dirbuf b;
116
117                 memset(&b, 0, sizeof(b));
118                 dirbuf_add(req, &b, ".", 1);
119                 dirbuf_add(req, &b, "..", 1);
120                 dirbuf_add(req, &b, hello_name, 2);
121                 reply_buf_limited(req, b.p, b.size, off, size);
122                 free(b.p);
123         }
124 }
125
126 static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
127                           struct fuse_file_info *fi)
128 {
129         if (ino != 2)
130                 fuse_reply_err(req, EISDIR);
131         else if ((fi->flags & 3) != O_RDONLY)
132                 fuse_reply_err(req, EACCES);
133         else
134                 fuse_reply_open(req, fi);
135 }
136
137 static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
138                           off_t off, struct fuse_file_info *fi)
139 {
140         (void) fi;
141
142         assert(ino == 2);
143         reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
144 }
145
146 static struct fuse_lowlevel_ops hello_ll_oper = {
147         .lookup         = hello_ll_lookup,
148         .getattr        = hello_ll_getattr,
149         .readdir        = hello_ll_readdir,
150         .open           = hello_ll_open,
151         .read           = hello_ll_read,
152 };
153
154 int main(int argc, char *argv[])
155 {
156         struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
157         struct fuse_chan *ch;
158         char *mountpoint;
159         int err = -1;
160
161         if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
162             (ch = fuse_mount(mountpoint, &args)) != NULL) {
163                 struct fuse_session *se;
164
165                 se = fuse_lowlevel_new(&args, &hello_ll_oper,
166                                        sizeof(hello_ll_oper), NULL);
167                 if (se != NULL) {
168                         if (fuse_set_signal_handlers(se) != -1) {
169                                 fuse_session_add_chan(se, ch);
170                                 err = fuse_session_loop(se);
171                                 fuse_remove_signal_handlers(se);
172                                 fuse_session_remove_chan(ch);
173                         }
174                         fuse_session_destroy(se);
175                 }
176                 fuse_unmount(mountpoint, ch);
177         }
178         fuse_opt_free_args(&args);
179
180         return err ? 1 : 0;
181 }