libfuse: add FUSE_CAP_NO_OPEN_SUPPORT flag to ->init()
[fuse.git] / example / hello.c
blob3c24c8be3310c8dee80dd85f1f8b554b7070cc91
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 /** @file
11 * hello.c - minimal FUSE example featuring fuse_main usage
13 * \section section_compile compiling this example
15 * gcc -Wall hello.c `pkg-config fuse3 --cflags --libs` -o hello
17 * \section section_usage usage
18 \verbatim
19 % mkdir mnt
20 % ./hello mnt # program will vanish into the background
21 % ls -la mnt
22 total 4
23 drwxr-xr-x 2 root root 0 Jan 1 1970 ./
24 drwxrwx--- 1 root vboxsf 4096 Jun 16 23:12 ../
25 -r--r--r-- 1 root root 13 Jan 1 1970 hello
26 % cat mnt/hello
27 Hello World!
28 % fusermount -u mnt
29 \endverbatim
31 * \section section_source the complete source
32 * \include hello.c
36 #define FUSE_USE_VERSION 30
38 #include <config.h>
40 #include <fuse.h>
41 #include <stdio.h>
42 #include <string.h>
43 #include <errno.h>
44 #include <fcntl.h>
46 static const char *hello_str = "Hello World!\n";
47 static const char *hello_path = "/hello";
49 static int hello_getattr(const char *path, struct stat *stbuf)
51 int res = 0;
53 memset(stbuf, 0, sizeof(struct stat));
54 if (strcmp(path, "/") == 0) {
55 stbuf->st_mode = S_IFDIR | 0755;
56 stbuf->st_nlink = 2;
57 } else if (strcmp(path, hello_path) == 0) {
58 stbuf->st_mode = S_IFREG | 0444;
59 stbuf->st_nlink = 1;
60 stbuf->st_size = strlen(hello_str);
61 } else
62 res = -ENOENT;
64 return res;
67 static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
68 off_t offset, struct fuse_file_info *fi,
69 enum fuse_readdir_flags flags)
71 (void) offset;
72 (void) fi;
73 (void) flags;
75 if (strcmp(path, "/") != 0)
76 return -ENOENT;
78 filler(buf, ".", NULL, 0, 0);
79 filler(buf, "..", NULL, 0, 0);
80 filler(buf, hello_path + 1, NULL, 0, 0);
82 return 0;
85 static int hello_open(const char *path, struct fuse_file_info *fi)
87 if (strcmp(path, hello_path) != 0)
88 return -ENOENT;
90 if ((fi->flags & 3) != O_RDONLY)
91 return -EACCES;
93 return 0;
96 static int hello_read(const char *path, char *buf, size_t size, off_t offset,
97 struct fuse_file_info *fi)
99 size_t len;
100 (void) fi;
101 if(strcmp(path, hello_path) != 0)
102 return -ENOENT;
104 len = strlen(hello_str);
105 if (offset < len) {
106 if (offset + size > len)
107 size = len - offset;
108 memcpy(buf, hello_str + offset, size);
109 } else
110 size = 0;
112 return size;
115 static struct fuse_operations hello_oper = {
116 .getattr = hello_getattr,
117 .readdir = hello_readdir,
118 .open = hello_open,
119 .read = hello_read,
122 int main(int argc, char *argv[])
124 return fuse_main(argc, argv, &hello_oper, NULL);