libfuse/example/hello.c

89 lines
1.8 KiB
C
Raw Normal View History

2001-11-21 18:03:39 +08:00
/*
FUSE: Filesystem in Userspace
2004-07-08 03:19:53 +08:00
Copyright (C) 2001-2004 Miklos Szeredi <miklos@szeredi.hu>
2001-11-21 18:03:39 +08:00
This program can be distributed under the terms of the GNU GPL.
See the file COPYING.
*/
#include <fuse.h>
#include <stdio.h>
2002-01-11 16:25:52 +08:00
#include <string.h>
2001-11-21 18:03:39 +08:00
#include <errno.h>
#include <fcntl.h>
static const char *hello_str = "Hello World!\n";
2001-12-21 17:28:33 +08:00
static const char *hello_path = "/hello";
2001-11-21 18:03:39 +08:00
static int hello_getattr(const char *path, struct stat *stbuf)
{
int res = 0;
memset(stbuf, 0, sizeof(struct stat));
if(strcmp(path, "/") == 0) {
stbuf->st_mode = S_IFDIR | 0755;
stbuf->st_nlink = 2;
}
2001-12-21 17:28:33 +08:00
else if(strcmp(path, hello_path) == 0) {
stbuf->st_mode = S_IFREG | 0444;
2001-11-21 18:03:39 +08:00
stbuf->st_nlink = 1;
stbuf->st_size = strlen(hello_str);
}
else
res = -ENOENT;
return res;
}
2004-11-09 01:32:25 +08:00
static int hello_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
2001-11-21 18:03:39 +08:00
{
if(strcmp(path, "/") != 0)
return -ENOENT;
2004-11-08 22:48:52 +08:00
filler(h, ".", 0, 0);
filler(h, "..", 0, 0);
filler(h, hello_path + 1, 0, 0);
2001-11-21 18:03:39 +08:00
return 0;
}
static int hello_open(const char *path, int flags)
{
2001-12-21 17:28:33 +08:00
if(strcmp(path, hello_path) != 0)
2001-11-21 18:03:39 +08:00
return -ENOENT;
if((flags & 3) != O_RDONLY)
return -EACCES;
return 0;
}
static int hello_read(const char *path, char *buf, size_t size, off_t offset)
{
2004-07-29 17:27:49 +08:00
size_t len;
2001-12-21 17:28:33 +08:00
if(strcmp(path, hello_path) != 0)
2001-11-21 18:03:39 +08:00
return -ENOENT;
2004-07-29 17:27:49 +08:00
len = strlen(hello_str);
if (offset < len) {
if (offset + size > len)
size = len - offset;
memcpy(buf, hello_str + offset, size);
} else
size = 0;
2001-11-21 18:03:39 +08:00
return size;
}
2001-12-21 17:28:33 +08:00
static struct fuse_operations hello_oper = {
2004-01-13 23:33:12 +08:00
.getattr = hello_getattr,
.getdir = hello_getdir,
.open = hello_open,
.read = hello_read,
2001-11-21 18:03:39 +08:00
};
int main(int argc, char *argv[])
{
2004-10-21 17:35:10 +08:00
return fuse_main(argc, argv, &hello_oper);
2001-11-21 18:03:39 +08:00
}