Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 1 | /* |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 4 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU GPL. |
| 6 | See the file COPYING. |
Miklos Szeredi | 4003dfa | 2006-10-01 13:46:02 +0000 | [diff] [blame] | 7 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 8 | gcc -Wall `pkg-config fuse --cflags --libs` hello.c -o hello |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Miklos Szeredi | 4003dfa | 2006-10-01 13:46:02 +0000 | [diff] [blame] | 11 | #define FUSE_USE_VERSION 26 |
| 12 | |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 13 | #include <fuse.h> |
| 14 | #include <stdio.h> |
Miklos Szeredi | 074b4b9 | 2002-01-11 08:25:52 +0000 | [diff] [blame] | 15 | #include <string.h> |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 16 | #include <errno.h> |
| 17 | #include <fcntl.h> |
| 18 | |
| 19 | static const char *hello_str = "Hello World!\n"; |
Miklos Szeredi | ba8ec88 | 2001-12-21 09:28:33 +0000 | [diff] [blame] | 20 | static const char *hello_path = "/hello"; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 21 | |
| 22 | static int hello_getattr(const char *path, struct stat *stbuf) |
| 23 | { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 24 | int res = 0; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 26 | memset(stbuf, 0, sizeof(struct stat)); |
| 27 | if (strcmp(path, "/") == 0) { |
| 28 | stbuf->st_mode = S_IFDIR | 0755; |
| 29 | stbuf->st_nlink = 2; |
| 30 | } else if (strcmp(path, hello_path) == 0) { |
| 31 | stbuf->st_mode = S_IFREG | 0444; |
| 32 | stbuf->st_nlink = 1; |
| 33 | stbuf->st_size = strlen(hello_str); |
| 34 | } else |
| 35 | res = -ENOENT; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 36 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 37 | return res; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 40 | static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 41 | off_t offset, struct fuse_file_info *fi) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 42 | { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 43 | (void) offset; |
| 44 | (void) fi; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 45 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 46 | if (strcmp(path, "/") != 0) |
| 47 | return -ENOENT; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 48 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 49 | filler(buf, ".", NULL, 0); |
| 50 | filler(buf, "..", NULL, 0); |
| 51 | filler(buf, hello_path + 1, NULL, 0); |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 52 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 53 | return 0; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 56 | static int hello_open(const char *path, struct fuse_file_info *fi) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 57 | { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 58 | if (strcmp(path, hello_path) != 0) |
| 59 | return -ENOENT; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 60 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 61 | if ((fi->flags & 3) != O_RDONLY) |
| 62 | return -EACCES; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 63 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 64 | return 0; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 65 | } |
| 66 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 67 | static int hello_read(const char *path, char *buf, size_t size, off_t offset, |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 68 | struct fuse_file_info *fi) |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 69 | { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 70 | size_t len; |
| 71 | (void) fi; |
| 72 | if(strcmp(path, hello_path) != 0) |
| 73 | return -ENOENT; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 74 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 75 | len = strlen(hello_str); |
| 76 | if (offset < len) { |
| 77 | if (offset + size > len) |
| 78 | size = len - offset; |
| 79 | memcpy(buf, hello_str + offset, size); |
| 80 | } else |
| 81 | size = 0; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 82 | |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 83 | return size; |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Miklos Szeredi | ba8ec88 | 2001-12-21 09:28:33 +0000 | [diff] [blame] | 86 | static struct fuse_operations hello_oper = { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 87 | .getattr = hello_getattr, |
| 88 | .readdir = hello_readdir, |
| 89 | .open = hello_open, |
| 90 | .read = hello_read, |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | int main(int argc, char *argv[]) |
| 94 | { |
Miklos Szeredi | cdb8b79 | 2007-12-12 14:25:40 +0000 | [diff] [blame] | 95 | return fuse_main(argc, argv, &hello_oper, NULL); |
Miklos Szeredi | cc8c975 | 2001-11-21 10:03:39 +0000 | [diff] [blame] | 96 | } |