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
|
|
|
|
*
|
2016-10-29 11:44:39 +08:00
|
|
|
* minimal example filesystem using high-level API
|
2013-06-21 01:18:18 +08:00
|
|
|
*
|
2016-10-29 11:44:39 +08:00
|
|
|
* Compile with:
|
2013-06-21 01:18:18 +08:00
|
|
|
*
|
2016-10-29 11:44:39 +08:00
|
|
|
* gcc -Wall hello.c `pkg-config fuse3 --cflags --libs` -o hello
|
2013-06-21 01:18:18 +08:00
|
|
|
*
|
2016-10-29 11:44:39 +08:00
|
|
|
* ## Source code ##
|
2013-06-21 01:18:18 +08:00
|
|
|
* \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>
|
2016-10-11 00:38:20 +08:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <assert.h>
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2016-10-29 11:44:39 +08:00
|
|
|
/*
|
2016-10-11 00:38:20 +08:00
|
|
|
* Command line options
|
|
|
|
*
|
|
|
|
* We can't set default values for the char* fields here because
|
|
|
|
* fuse_opt_parse would attempt to free() them when the user specifies
|
|
|
|
* different values on the command line.
|
|
|
|
*/
|
|
|
|
static struct options {
|
|
|
|
const char *filename;
|
|
|
|
const char *contents;
|
|
|
|
int show_help;
|
|
|
|
} options;
|
|
|
|
|
|
|
|
#define OPTION(t, p) \
|
|
|
|
{ t, offsetof(struct options, p), 1 }
|
|
|
|
static const struct fuse_opt option_spec[] = {
|
|
|
|
OPTION("--name=%s", filename),
|
|
|
|
OPTION("--contents=%s", contents),
|
|
|
|
OPTION("-h", show_help),
|
|
|
|
OPTION("--help", show_help),
|
|
|
|
FUSE_OPT_END
|
|
|
|
};
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2016-10-19 12:23:22 +08:00
|
|
|
static void *hello_init(struct fuse_conn_info *conn,
|
|
|
|
struct fuse_config *cfg)
|
|
|
|
{
|
|
|
|
(void) conn;
|
|
|
|
cfg->kernel_cache = 1;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2016-10-16 09:46:27 +08:00
|
|
|
static int hello_getattr(const char *path, struct stat *stbuf,
|
|
|
|
struct fuse_file_info *fi)
|
2001-11-21 18:03:39 +08:00
|
|
|
{
|
2016-10-16 09:46:27 +08:00
|
|
|
(void) fi;
|
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;
|
2016-10-11 00:38:20 +08:00
|
|
|
} else if (strcmp(path+1, options.filename) == 0) {
|
2007-12-12 22:25:40 +08:00
|
|
|
stbuf->st_mode = S_IFREG | 0444;
|
|
|
|
stbuf->st_nlink = 1;
|
2016-10-11 00:38:20 +08:00
|
|
|
stbuf->st_size = strlen(options.contents);
|
2007-12-12 22:25:40 +08:00
|
|
|
} 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,
|
2014-03-05 21:45:44 +08:00
|
|
|
off_t offset, struct fuse_file_info *fi,
|
|
|
|
enum fuse_readdir_flags flags)
|
2001-11-21 18:03:39 +08:00
|
|
|
{
|
2007-12-12 22:25:40 +08:00
|
|
|
(void) offset;
|
|
|
|
(void) fi;
|
2014-03-05 21:45:44 +08:00
|
|
|
(void) flags;
|
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
|
|
|
|
2014-03-05 21:45:44 +08:00
|
|
|
filler(buf, ".", NULL, 0, 0);
|
|
|
|
filler(buf, "..", NULL, 0, 0);
|
2016-10-11 00:38:20 +08:00
|
|
|
filler(buf, options.filename, NULL, 0, 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
|
|
|
{
|
2016-10-11 00:38:20 +08:00
|
|
|
if (strcmp(path+1, options.filename) != 0)
|
2007-12-12 22:25:40 +08:00
|
|
|
return -ENOENT;
|
2001-11-21 18:03:39 +08:00
|
|
|
|
2017-03-28 23:57:23 +08:00
|
|
|
if ((fi->flags & O_ACCMODE) != O_RDONLY)
|
2007-12-12 22:25:40 +08:00
|
|
|
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;
|
2016-10-11 00:38:20 +08:00
|
|
|
if(strcmp(path+1, options.filename) != 0)
|
2007-12-12 22:25:40 +08:00
|
|
|
return -ENOENT;
|
|
|
|
|
2016-10-11 00:38:20 +08:00
|
|
|
len = strlen(options.contents);
|
2007-12-12 22:25:40 +08:00
|
|
|
if (offset < len) {
|
|
|
|
if (offset + size > len)
|
|
|
|
size = len - offset;
|
2016-10-11 00:38:20 +08:00
|
|
|
memcpy(buf, options.contents + offset, size);
|
2007-12-12 22:25:40 +08:00
|
|
|
} 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 = {
|
2016-10-19 12:23:22 +08:00
|
|
|
.init = hello_init,
|
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
|
|
|
};
|
|
|
|
|
2016-10-11 00:38:20 +08:00
|
|
|
static void show_help(const char *progname)
|
|
|
|
{
|
|
|
|
printf("usage: %s [options] <mountpoint>\n\n", progname);
|
|
|
|
printf("File-system specific options:\n"
|
|
|
|
" --name=<s> Name of the \"hello\" file\n"
|
|
|
|
" (default: \"hello\")\n"
|
|
|
|
" --contents=<s> Contents \"hello\" file\n"
|
|
|
|
" (default \"Hello, World!\\n\")\n"
|
|
|
|
"\n");
|
|
|
|
}
|
|
|
|
|
2001-11-21 18:03:39 +08:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2016-10-11 00:38:20 +08:00
|
|
|
struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
|
|
|
|
|
|
|
|
/* Set defaults -- we have to use strdup so that
|
|
|
|
fuse_opt_parse can free the defaults if other
|
|
|
|
values are specified */
|
|
|
|
options.filename = strdup("hello");
|
|
|
|
options.contents = strdup("Hello World!\n");
|
|
|
|
|
|
|
|
/* Parse options */
|
|
|
|
if (fuse_opt_parse(&args, &options, option_spec, NULL) == -1)
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* When --help is specified, first print our own file-system
|
|
|
|
specific help text, then signal fuse_main to show
|
|
|
|
additional help (by adding `--help` to the options again)
|
|
|
|
without usage: line (by setting argv[0] to the empty
|
|
|
|
string) */
|
|
|
|
if (options.show_help) {
|
|
|
|
show_help(argv[0]);
|
|
|
|
assert(fuse_opt_add_arg(&args, "--help") == 0);
|
|
|
|
args.argv[0] = (char*) "";
|
|
|
|
}
|
|
|
|
|
|
|
|
return fuse_main(args.argc, args.argv, &hello_oper, NULL);
|
2001-11-21 18:03:39 +08:00
|
|
|
}
|