1 |
/* |
2 |
FUSE: Filesystem in Userspace |
3 |
Copyright (C) 2001-2004 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 |
|
9 |
#include <fuse.h> |
10 |
#include <stdio.h> |
11 |
#include <string.h> |
12 |
#include <errno.h> |
13 |
#include <fcntl.h> |
14 |
|
15 |
static const char *hello_str = "Hello World!\n"; |
16 |
static const char *hello_path = "/hello"; |
17 |
|
18 |
static int hello_getattr(const char *path, struct stat *stbuf) |
19 |
{ |
20 |
int res = 0; |
21 |
|
22 |
memset(stbuf, 0, sizeof(struct stat)); |
23 |
if(strcmp(path, "/") == 0) { |
24 |
stbuf->st_mode = S_IFDIR | 0755; |
25 |
stbuf->st_nlink = 2; |
26 |
} |
27 |
else if(strcmp(path, hello_path) == 0) { |
28 |
stbuf->st_mode = S_IFREG | 0444; |
29 |
stbuf->st_nlink = 1; |
30 |
stbuf->st_size = strlen(hello_str); |
31 |
} |
32 |
else |
33 |
res = -ENOENT; |
34 |
|
35 |
return res; |
36 |
} |
37 |
|
38 |
static int hello_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler) |
39 |
{ |
40 |
if(strcmp(path, "/") != 0) |
41 |
return -ENOENT; |
42 |
|
43 |
filler(h, ".", 0); |
44 |
filler(h, "..", 0); |
45 |
filler(h, hello_path + 1, 0); |
46 |
|
47 |
return 0; |
48 |
} |
49 |
|
50 |
static int hello_open(const char *path, int flags) |
51 |
{ |
52 |
if(strcmp(path, hello_path) != 0) |
53 |
return -ENOENT; |
54 |
|
55 |
if((flags & 3) != O_RDONLY) |
56 |
return -EACCES; |
57 |
|
58 |
return 0; |
59 |
} |
60 |
|
61 |
static int hello_read(const char *path, char *buf, size_t size, off_t offset) |
62 |
{ |
63 |
size_t len; |
64 |
if(strcmp(path, hello_path) != 0) |
65 |
return -ENOENT; |
66 |
|
67 |
len = strlen(hello_str); |
68 |
if (offset < len) { |
69 |
if (offset + size > len) |
70 |
size = len - offset; |
71 |
memcpy(buf, hello_str + offset, size); |
72 |
} else |
73 |
size = 0; |
74 |
|
75 |
return size; |
76 |
} |
77 |
|
78 |
static struct fuse_operations hello_oper = { |
79 |
.getattr = hello_getattr, |
80 |
.getdir = hello_getdir, |
81 |
.open = hello_open, |
82 |
.read = hello_read, |
83 |
}; |
84 |
|
85 |
int main(int argc, char *argv[]) |
86 |
{ |
87 |
fuse_main(argc, argv, &hello_oper); |
88 |
return 0; |
89 |
} |