aboutsummaryrefslogtreecommitdiffstats
path: root/example
diff options
context:
space:
mode:
authorMiklos Szeredi <miklos@szeredi.hu>2001-11-21 10:03:39 +0000
committerMiklos Szeredi <miklos@szeredi.hu>2001-11-21 10:03:39 +0000
commitcc8c975f7757ce89d15aad31df6c8b7086169653 (patch)
tree7e313f7a29871bec3496cd4e48febdcf1216d4b2 /example
parent0e8702ba48357fd1596bee4648aede7a9b2adc4e (diff)
downloadlibfuse-cc8c975f7757ce89d15aad31df6c8b7086169653.tar.gz
writing modules made more easy
Diffstat (limited to 'example')
-rw-r--r--example/.cvsignore1
-rw-r--r--example/Makefile.am3
-rw-r--r--example/fusexmp.c96
-rw-r--r--example/hello.c92
-rw-r--r--example/null.c94
5 files changed, 100 insertions, 186 deletions
diff --git a/example/.cvsignore b/example/.cvsignore
index 94a32e3..e4188bc 100644
--- a/example/.cvsignore
+++ b/example/.cvsignore
@@ -3,3 +3,4 @@ Makefile
.deps
fusexmp
null
+hello
diff --git a/example/Makefile.am b/example/Makefile.am
index 19a3c32..a3880fa 100644
--- a/example/Makefile.am
+++ b/example/Makefile.am
@@ -1,8 +1,9 @@
## Process this file with automake to produce Makefile.in
-noinst_PROGRAMS = fusexmp null
+noinst_PROGRAMS = fusexmp null hello
fusexmp_SOURCES = fusexmp.c
null_SOURCES = null.c
+hello_SOURCES = hello.c
LDADD = ../lib/libfuse.a -lpthread
diff --git a/example/fusexmp.c b/example/fusexmp.c
index dc2a569..b9df79c 100644
--- a/example/fusexmp.c
+++ b/example/fusexmp.c
@@ -11,18 +11,12 @@
#define _XOPEN_SOURCE 500
#endif
-/* For setgroups() */
-#define _BSD_SOURCE
-
#include <fuse.h>
#include <stdio.h>
-#include <stdlib.h>
#include <unistd.h>
+#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
-#include <signal.h>
-#include <utime.h>
-#include <fcntl.h>
static int xmp_getattr(const char *path, struct stat *stbuf)
{
@@ -237,7 +231,6 @@ static int xmp_write(const char *path, const char *buf, size_t size,
return res;
}
-
static struct fuse_operations xmp_oper = {
getattr: xmp_getattr,
readlink: xmp_readlink,
@@ -258,93 +251,8 @@ static struct fuse_operations xmp_oper = {
write: xmp_write,
};
-static void cleanup()
-{
- close(0);
- system(getenv("FUSE_UNMOUNT_CMD"));
-}
-
-static void exit_handler()
-{
- exit(0);
-}
-
-static void set_signal_handlers()
-{
- struct sigaction sa;
-
- sa.sa_handler = exit_handler;
- sigemptyset(&(sa.sa_mask));
- sa.sa_flags = 0;
-
- if (sigaction(SIGHUP, &sa, NULL) == -1 ||
- sigaction(SIGINT, &sa, NULL) == -1 ||
- sigaction(SIGTERM, &sa, NULL) == -1) {
-
- perror("Cannot set exit signal handlers");
- exit(1);
- }
-
- sa.sa_handler = SIG_IGN;
-
- if(sigaction(SIGPIPE, &sa, NULL) == -1) {
- perror("Cannot set ignored signals");
- exit(1);
- }
-}
-
int main(int argc, char *argv[])
{
- int argctr;
- int flags;
- int multithreaded;
- struct fuse *fuse;
-
- argctr = 1;
-
- atexit(cleanup);
- set_signal_handlers();
-
- flags = 0;
- multithreaded = 1;
- for(; argctr < argc && argv[argctr][0] == '-'; argctr ++) {
- switch(argv[argctr][1]) {
- case 'd':
- flags |= FUSE_DEBUG;
- break;
-
- case 's':
- multithreaded = 0;
- break;
-
- case 'h':
- fprintf(stderr,
- "usage: %s [options] \n"
- "Options:\n"
- " -d enable debug output\n"
- " -s disable multithreaded operation\n"
- " -h print help\n",
- argv[0]);
- exit(1);
- break;
-
- default:
- fprintf(stderr, "invalid option: %s\n", argv[argctr]);
- exit(1);
- }
- }
- if(argctr != argc) {
- fprintf(stderr, "missing or surplus argument\n");
- exit(1);
- }
-
- fuse = fuse_new(0, flags);
- fuse_set_operations(fuse, &xmp_oper);
-
- if(multithreaded)
- fuse_loop_mt(fuse);
- else
- fuse_loop(fuse);
-
+ fuse_main(argc, argv, &xmp_oper);
return 0;
}
diff --git a/example/hello.c b/example/hello.c
new file mode 100644
index 0000000..5cf75f4
--- /dev/null
+++ b/example/hello.c
@@ -0,0 +1,92 @@
+/*
+ FUSE: Filesystem in Userspace
+ Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu)
+
+ This program can be distributed under the terms of the GNU GPL.
+ See the file COPYING.
+*/
+
+#include <fuse.h>
+#include <stdio.h>
+#include <errno.h>
+#include <fcntl.h>
+
+static const char *hello_str = "Hello World!\n";
+
+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;
+ }
+ else if(strcmp(path, "/hello") == 0) {
+ stbuf->st_mode = S_IFREG | 0644;
+ stbuf->st_nlink = 1;
+ stbuf->st_size = strlen(hello_str);
+ }
+ else
+ res = -ENOENT;
+
+ return res;
+}
+
+static int hello_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler)
+{
+ if(strcmp(path, "/") != 0)
+ return -ENOENT;
+
+ filler(h, ".", 0);
+ filler(h, "..", 0);
+ filler(h, "hello", 0);
+
+ return 0;
+}
+
+static int hello_open(const char *path, int flags)
+{
+ if(strcmp(path, "/hello") != 0)
+ 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)
+{
+ if(strcmp(path, "/hello") != 0)
+ return -ENOENT;
+
+ memcpy(buf, hello_str + offset, size);
+ return size;
+}
+
+static struct fuse_operations null_oper = {
+ getattr: hello_getattr,
+ readlink: NULL,
+ getdir: hello_getdir,
+ mknod: NULL,
+ mkdir: NULL,
+ symlink: NULL,
+ unlink: NULL,
+ rmdir: NULL,
+ rename: NULL,
+ link: NULL,
+ chmod: NULL,
+ chown: NULL,
+ truncate: NULL,
+ utime: NULL,
+ open: hello_open,
+ read: hello_read,
+ write: NULL,
+};
+
+int main(int argc, char *argv[])
+{
+ fuse_main(argc, argv, &null_oper);
+ return 0;
+}
diff --git a/example/null.c b/example/null.c
index c9816f1..7be4ab4 100644
--- a/example/null.c
+++ b/example/null.c
@@ -7,13 +7,10 @@
*/
#include <fuse.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <errno.h>
+
#include <unistd.h>
-#include <signal.h>
#include <time.h>
+#include <errno.h>
#define UNUSED __attribute__((unused))
@@ -88,93 +85,8 @@ static struct fuse_operations null_oper = {
write: null_write,
};
-static void cleanup()
-{
- close(0);
- system(getenv("FUSE_UNMOUNT_CMD"));
-}
-
-static void exit_handler()
-{
- exit(0);
-}
-
-static void set_signal_handlers()
-{
- struct sigaction sa;
-
- sa.sa_handler = exit_handler;
- sigemptyset(&(sa.sa_mask));
- sa.sa_flags = 0;
-
- if (sigaction(SIGHUP, &sa, NULL) == -1 ||
- sigaction(SIGINT, &sa, NULL) == -1 ||
- sigaction(SIGTERM, &sa, NULL) == -1) {
-
- perror("Cannot set exit signal handlers");
- exit(1);
- }
-
- sa.sa_handler = SIG_IGN;
-
- if(sigaction(SIGPIPE, &sa, NULL) == -1) {
- perror("Cannot set ignored signals");
- exit(1);
- }
-}
-
int main(int argc, char *argv[])
{
- int argctr;
- int flags;
- int multithreaded;
- struct fuse *fuse;
-
- argctr = 1;
-
- atexit(cleanup);
- set_signal_handlers();
-
- flags = 0;
- multithreaded = 1;
- for(; argctr < argc && argv[argctr][0] == '-'; argctr ++) {
- switch(argv[argctr][1]) {
- case 'd':
- flags |= FUSE_DEBUG;
- break;
-
- case 's':
- multithreaded = 0;
- break;
-
- case 'h':
- fprintf(stderr,
- "usage: %s [options] \n"
- "Options:\n"
- " -d enable debug output\n"
- " -s disable multithreaded operation\n"
- " -h print help\n",
- argv[0]);
- exit(1);
- break;
-
- default:
- fprintf(stderr, "invalid option: %s\n", argv[argctr]);
- exit(1);
- }
- }
- if(argctr != argc) {
- fprintf(stderr, "missing or surplus argument\n");
- exit(1);
- }
-
- fuse = fuse_new(0, flags);
- fuse_set_operations(fuse, &null_oper);
-
- if(multithreaded)
- fuse_loop_mt(fuse);
- else
- fuse_loop(fuse);
-
+ fuse_main(argc, argv, &null_oper);
return 0;
}