2001-11-21 18:03:39 +08:00
|
|
|
/*
|
2007-12-12 22:25:40 +08:00
|
|
|
FUSE: Filesystem in Userspace
|
|
|
|
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
This program can be distributed under the terms of the GNU GPL.
|
|
|
|
See the file COPYING.
|
2001-11-21 18:03:39 +08:00
|
|
|
*/
|
|
|
|
|
2013-06-21 01:18:18 +08:00
|
|
|
/** @file
|
|
|
|
*
|
|
|
|
* hello.c - minimal FUSE example featuring fuse_main usage
|
|
|
|
*
|
2013-07-02 23:37:29 +08:00
|
|
|
* \section section_compile compiling this example
|
2013-06-21 01:18:18 +08:00
|
|
|
*
|
2013-07-25 22:54:42 +08:00
|
|
|
* gcc -Wall hello.c `pkg-config fuse3 --cflags --libs` -o hello
|
2013-06-21 01:18:18 +08:00
|
|
|
*
|
|
|
|
* \section section_usage usage
|
|
|
|
\verbatim
|
|
|
|
% mkdir mnt
|
|
|
|
% ./hello mnt # program will vanish into the background
|
|
|
|
% ls -la mnt
|
|
|
|
total 4
|
|
|
|
drwxr-xr-x 2 root root 0 Jan 1 1970 ./
|
|
|
|
drwxrwx--- 1 root vboxsf 4096 Jun 16 23:12 ../
|
|
|
|
-r--r--r-- 1 root root 13 Jan 1 1970 hello
|
|
|
|
% cat mnt/hello
|
|
|
|
Hello World!
|
|
|
|
% fusermount -u mnt
|
|
|
|
\endverbatim
|
|
|
|
*
|
|
|
|
* \section section_source the complete source
|
|
|
|
* \include hello.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2012-07-20 00:40:11 +08:00
|
|
|
#define FUSE_USE_VERSION 30
|
2006-10-01 21:46:02 +08:00
|
|
|
|
2013-07-24 23:09:26 +08:00
|
|
|
#include <config.h>
|
|
|
|
|
2001-11-21 18:03:39 +08:00
|
|
|
#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)
|
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
memset(stbuf, 0, sizeof(struct stat));
|
|
|
|
if (strcmp(path, "/") == 0) {
|
|
|
|
stbuf->st_mode = S_IFDIR | 0755;
|
|
|
|
stbuf->st_nlink = 2;
|
|
|
|
} else if (strcmp(path, hello_path) == 0) {
|
|
|
|
stbuf->st_mode = S_IFREG | 0444;
|
|
|
|
stbuf->st_nlink = 1;
|
|
|
|
stbuf->st_size = strlen(hello_str);
|
|
|
|
} else
|
|
|
|
res = -ENOENT;
|
|
|
|
|
|
|
|
return res;
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|
|
|
|
|
2005-04-07 23:40:21 +08:00
|
|
|
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
|
2007-12-12 22:25:40 +08:00
|
|
|
off_t offset, struct fuse_file_info *fi)
|
2001-11-21 18:03:39 +08:00
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
(void) offset;
|
|
|
|
(void) fi;
|
2005-04-07 23:40:21 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
if (strcmp(path, "/") != 0)
|
|
|
|
return -ENOENT;
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
filler(buf, ".", NULL, 0);
|
|
|
|
filler(buf, "..", NULL, 0);
|
|
|
|
filler(buf, hello_path + 1, NULL, 0);
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
return 0;
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|
|
|
|
|
2004-11-26 20:15:06 +08:00
|
|
|
static int hello_open(const char *path, struct fuse_file_info *fi)
|
2001-11-21 18:03:39 +08:00
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
if (strcmp(path, hello_path) != 0)
|
|
|
|
return -ENOENT;
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
if ((fi->flags & 3) != O_RDONLY)
|
|
|
|
return -EACCES;
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2007-12-12 22:25:40 +08:00
|
|
|
return 0;
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|
|
|
|
|
2004-11-26 20:15:06 +08:00
|
|
|
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
|
2007-12-12 22:25:40 +08:00
|
|
|
struct fuse_file_info *fi)
|
2001-11-21 18:03:39 +08:00
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
size_t len;
|
|
|
|
(void) fi;
|
|
|
|
if(strcmp(path, hello_path) != 0)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
len = strlen(hello_str);
|
|
|
|
if (offset < len) {
|
|
|
|
if (offset + size > len)
|
|
|
|
size = len - offset;
|
|
|
|
memcpy(buf, hello_str + offset, size);
|
|
|
|
} else
|
|
|
|
size = 0;
|
|
|
|
|
|
|
|
return size;
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|
|
|
|
|
2001-12-21 17:28:33 +08:00
|
|
|
static struct fuse_operations hello_oper = {
|
2007-12-12 22:25:40 +08:00
|
|
|
.getattr = hello_getattr,
|
|
|
|
.readdir = hello_readdir,
|
|
|
|
.open = hello_open,
|
|
|
|
.read = hello_read,
|
2001-11-21 18:03:39 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
return fuse_main(argc, argv, &hello_oper, NULL);
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|