blob: bcde80a8b361232063956dd693daa95aa3913574 [file] [log] [blame]
Miklos Szeredicc8c9752001-11-21 10:03:39 +00001/*
Miklos Szeredicdb8b792007-12-12 14:25:40 +00002 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredicc8c9752001-11-21 10:03:39 +00004
Miklos Szeredicdb8b792007-12-12 14:25:40 +00005 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
Miklos Szeredi4003dfa2006-10-01 13:46:02 +00007
Miklos Szeredicdb8b792007-12-12 14:25:40 +00008 gcc -Wall `pkg-config fuse --cflags --libs` hello.c -o hello
Miklos Szeredicc8c9752001-11-21 10:03:39 +00009*/
10
Miklos Szeredi4003dfa2006-10-01 13:46:02 +000011#define FUSE_USE_VERSION 26
12
Miklos Szeredicc8c9752001-11-21 10:03:39 +000013#include <fuse.h>
14#include <stdio.h>
Miklos Szeredi074b4b92002-01-11 08:25:52 +000015#include <string.h>
Miklos Szeredicc8c9752001-11-21 10:03:39 +000016#include <errno.h>
17#include <fcntl.h>
18
19static const char *hello_str = "Hello World!\n";
Miklos Szerediba8ec882001-12-21 09:28:33 +000020static const char *hello_path = "/hello";
Miklos Szeredicc8c9752001-11-21 10:03:39 +000021
22static int hello_getattr(const char *path, struct stat *stbuf)
23{
Miklos Szeredicdb8b792007-12-12 14:25:40 +000024 int res = 0;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000025
Miklos Szeredicdb8b792007-12-12 14:25:40 +000026 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 Szeredicc8c9752001-11-21 10:03:39 +000036
Miklos Szeredicdb8b792007-12-12 14:25:40 +000037 return res;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000038}
39
Miklos Szerediab974562005-04-07 15:40:21 +000040static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
Miklos Szeredicdb8b792007-12-12 14:25:40 +000041 off_t offset, struct fuse_file_info *fi)
Miklos Szeredicc8c9752001-11-21 10:03:39 +000042{
Miklos Szeredicdb8b792007-12-12 14:25:40 +000043 (void) offset;
44 (void) fi;
Miklos Szerediab974562005-04-07 15:40:21 +000045
Miklos Szeredicdb8b792007-12-12 14:25:40 +000046 if (strcmp(path, "/") != 0)
47 return -ENOENT;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000048
Miklos Szeredicdb8b792007-12-12 14:25:40 +000049 filler(buf, ".", NULL, 0);
50 filler(buf, "..", NULL, 0);
51 filler(buf, hello_path + 1, NULL, 0);
Miklos Szeredicc8c9752001-11-21 10:03:39 +000052
Miklos Szeredicdb8b792007-12-12 14:25:40 +000053 return 0;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000054}
55
Miklos Szeredifb28c5e2004-11-26 12:15:06 +000056static int hello_open(const char *path, struct fuse_file_info *fi)
Miklos Szeredicc8c9752001-11-21 10:03:39 +000057{
Miklos Szeredicdb8b792007-12-12 14:25:40 +000058 if (strcmp(path, hello_path) != 0)
59 return -ENOENT;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000060
Miklos Szeredicdb8b792007-12-12 14:25:40 +000061 if ((fi->flags & 3) != O_RDONLY)
62 return -EACCES;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000063
Miklos Szeredicdb8b792007-12-12 14:25:40 +000064 return 0;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000065}
66
Miklos Szeredifb28c5e2004-11-26 12:15:06 +000067static int hello_read(const char *path, char *buf, size_t size, off_t offset,
Miklos Szeredicdb8b792007-12-12 14:25:40 +000068 struct fuse_file_info *fi)
Miklos Szeredicc8c9752001-11-21 10:03:39 +000069{
Miklos Szeredicdb8b792007-12-12 14:25:40 +000070 size_t len;
71 (void) fi;
72 if(strcmp(path, hello_path) != 0)
73 return -ENOENT;
Miklos Szeredie5183742005-02-02 11:14:04 +000074
Miklos Szeredicdb8b792007-12-12 14:25:40 +000075 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 Szeredi891b8742004-07-29 09:27:49 +000082
Miklos Szeredicdb8b792007-12-12 14:25:40 +000083 return size;
Miklos Szeredicc8c9752001-11-21 10:03:39 +000084}
85
Miklos Szerediba8ec882001-12-21 09:28:33 +000086static struct fuse_operations hello_oper = {
Miklos Szeredicdb8b792007-12-12 14:25:40 +000087 .getattr = hello_getattr,
88 .readdir = hello_readdir,
89 .open = hello_open,
90 .read = hello_read,
Miklos Szeredicc8c9752001-11-21 10:03:39 +000091};
92
93int main(int argc, char *argv[])
94{
Miklos Szeredicdb8b792007-12-12 14:25:40 +000095 return fuse_main(argc, argv, &hello_oper, NULL);
Miklos Szeredicc8c9752001-11-21 10:03:39 +000096}