diff options
author | Thomas Schwinge <tschwinge@gnu.org> | 2005-12-02 00:51:26 +0000 |
---|---|---|
committer | Thomas Schwinge <tschwinge@gnu.org> | 2005-12-02 00:51:26 +0000 |
commit | 642211d7e04e629db879330e07c2e053b079529f (patch) | |
tree | cae8640650d88347c824ea5284ddcf3746e86892 | |
parent | a107f27f664238e0c84e87d5f276b7b3ff353369 (diff) |
Add the examples from fuse_2_3_0.
-rw-r--r-- | configure.ac | 5 | ||||
-rw-r--r-- | example-23/.cvsignore | 8 | ||||
-rw-r--r-- | example-23/Makefile.am | 11 | ||||
-rw-r--r-- | example-23/fusexmp.c | 357 | ||||
-rw-r--r-- | example-23/fusexmp.c.patch | 16 | ||||
-rw-r--r-- | example-23/fusexmp_fh.c | 360 | ||||
-rw-r--r-- | example-23/fusexmp_fh.c.patch | 25 | ||||
-rw-r--r-- | example-23/hello.c | 94 | ||||
-rw-r--r-- | example-23/null.c | 78 |
9 files changed, 952 insertions, 2 deletions
diff --git a/configure.ac b/configure.ac index 912007e85..f1692ea7e 100644 --- a/configure.ac +++ b/configure.ac @@ -5,7 +5,7 @@ AC_PREREQ(2.59) AC_INIT(libfuse, 0.1, hurdextras-hackers@nongnu.org) -AC_REVISION($Revision: 1.4 $) +AC_REVISION($Revision: 1.5 $) AC_CONFIG_SRCDIR(main.c) AM_CONFIG_HEADER(config.h) @@ -39,5 +39,6 @@ CFLAGS="$CFLAGS -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -Wall -W -ggdb" # Checks for library functions. -AC_CONFIG_FILES([fuse.pc Makefile example/Makefile example-22/Makefile]) +AC_CONFIG_FILES([fuse.pc Makefile] + [example/Makefile example-22/Makefile example-23/Makefile]) AC_OUTPUT diff --git a/example-23/.cvsignore b/example-23/.cvsignore new file mode 100644 index 000000000..80eb82bdb --- /dev/null +++ b/example-23/.cvsignore @@ -0,0 +1,8 @@ +Makefile.in +Makefile +.deps +fusexmp +fusexmp_fh +null +hello +.libs diff --git a/example-23/Makefile.am b/example-23/Makefile.am new file mode 100644 index 000000000..6df9c5820 --- /dev/null +++ b/example-23/Makefile.am @@ -0,0 +1,11 @@ +## Process this file with automake to produce Makefile.in + +noinst_PROGRAMS = fusexmp fusexmp_fh null hello + +fusexmp_SOURCES = fusexmp.c +fusexmp_fh_SOURCES = fusexmp_fh.c +null_SOURCES = null.c +hello_SOURCES = hello.c + +LDADD = ../libfuse.la +AM_CPPFLAGS = -DFUSE_USE_VERSION=23 -I$(top_srcdir) diff --git a/example-23/fusexmp.c b/example-23/fusexmp.c new file mode 100644 index 000000000..f98c0a99c --- /dev/null +++ b/example-23/fusexmp.c @@ -0,0 +1,357 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +#include <config.h> + +#ifdef linux +/* For pread()/pwrite() */ +#define _XOPEN_SOURCE 500 +#endif + +#include <fuse.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <errno.h> +#include <sys/statfs.h> +#ifdef HAVE_SETXATTR +#include <sys/xattr.h> +#endif + +static int xmp_getattr(const char *path, struct stat *stbuf) +{ + int res; + + res = lstat(path, stbuf); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_readlink(const char *path, char *buf, size_t size) +{ + int res; + + res = readlink(path, buf, size - 1); + if(res == -1) + return -errno; + + buf[res] = '\0'; + return 0; +} + + +static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + DIR *dp; + struct dirent *de; + + (void) offset; + (void) fi; + + dp = opendir(path); + if(dp == NULL) + return -errno; + + while((de = readdir(dp)) != NULL) { + struct stat st; + memset(&st, 0, sizeof(st)); + st.st_ino = de->d_ino; + st.st_mode = de->d_type << 12; + if (filler(buf, de->d_name, &st, 0)) + break; + } + + closedir(dp); + return 0; +} + +static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) +{ + int res; + + /* On the Hurd we must not use mknod() to create files, but creat() */ + if(mode & S_IFREG) + res = creat(path, mode & ALLPERMS); + else + res = mknod(path, mode, rdev); + + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_mkdir(const char *path, mode_t mode) +{ + int res; + + res = mkdir(path, mode); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_unlink(const char *path) +{ + int res; + + res = unlink(path); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_rmdir(const char *path) +{ + int res; + + res = rmdir(path); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_symlink(const char *from, const char *to) +{ + int res; + + res = symlink(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_rename(const char *from, const char *to) +{ + int res; + + res = rename(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_link(const char *from, const char *to) +{ + int res; + + res = link(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_chmod(const char *path, mode_t mode) +{ + int res; + + res = chmod(path, mode); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_chown(const char *path, uid_t uid, gid_t gid) +{ + int res; + + res = lchown(path, uid, gid); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_truncate(const char *path, off_t size) +{ + int res; + + res = truncate(path, size); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_utime(const char *path, struct utimbuf *buf) +{ + int res; + + res = utime(path, buf); + if(res == -1) + return -errno; + + return 0; +} + + +static int xmp_open(const char *path, struct fuse_file_info *fi) +{ + int res; + + res = open(path, fi->flags); + if(res == -1) + return -errno; + + close(res); + return 0; +} + +static int xmp_read(const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + int fd; + int res; + + (void) fi; + fd = open(path, O_RDONLY); + if(fd == -1) + return -errno; + + res = pread(fd, buf, size, offset); + if(res == -1) + res = -errno; + + close(fd); + return res; +} + +static int xmp_write(const char *path, const char *buf, size_t size, + off_t offset, struct fuse_file_info *fi) +{ + int fd; + int res; + + (void) fi; + fd = open(path, O_WRONLY); + if(fd == -1) + return -errno; + + res = pwrite(fd, buf, size, offset); + if(res == -1) + res = -errno; + + close(fd); + return res; +} + +static int xmp_statfs(const char *path, struct statfs *stbuf) +{ + int res; + + res = statfs(path, stbuf); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_release(const char *path, struct fuse_file_info *fi) +{ + /* Just a stub. This method is optional and can safely be left + unimplemented */ + + (void) path; + (void) fi; + return 0; +} + +static int xmp_fsync(const char *path, int isdatasync, + struct fuse_file_info *fi) +{ + /* Just a stub. This method is optional and can safely be left + unimplemented */ + + (void) path; + (void) isdatasync; + (void) fi; + return 0; +} + +#ifdef HAVE_SETXATTR +/* xattr operations are optional and can safely be left unimplemented */ +static int xmp_setxattr(const char *path, const char *name, const char *value, + size_t size, int flags) +{ + int res = lsetxattr(path, name, value, size, flags); + if(res == -1) + return -errno; + return 0; +} + +static int xmp_getxattr(const char *path, const char *name, char *value, + size_t size) +{ + int res = lgetxattr(path, name, value, size); + if(res == -1) + return -errno; + return res; +} + +static int xmp_listxattr(const char *path, char *list, size_t size) +{ + int res = llistxattr(path, list, size); + if(res == -1) + return -errno; + return res; +} + +static int xmp_removexattr(const char *path, const char *name) +{ + int res = lremovexattr(path, name); + if(res == -1) + return -errno; + return 0; +} +#endif /* HAVE_SETXATTR */ + +static struct fuse_operations xmp_oper = { + .getattr = xmp_getattr, + .readlink = xmp_readlink, + .readdir = xmp_readdir, + .mknod = xmp_mknod, + .mkdir = xmp_mkdir, + .symlink = xmp_symlink, + .unlink = xmp_unlink, + .rmdir = xmp_rmdir, + .rename = xmp_rename, + .link = xmp_link, + .chmod = xmp_chmod, + .chown = xmp_chown, + .truncate = xmp_truncate, + .utime = xmp_utime, + .open = xmp_open, + .read = xmp_read, + .write = xmp_write, + .statfs = xmp_statfs, + .release = xmp_release, + .fsync = xmp_fsync, +#ifdef HAVE_SETXATTR + .setxattr = xmp_setxattr, + .getxattr = xmp_getxattr, + .listxattr = xmp_listxattr, + .removexattr= xmp_removexattr, +#endif +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &xmp_oper); +} diff --git a/example-23/fusexmp.c.patch b/example-23/fusexmp.c.patch new file mode 100644 index 000000000..2d0054b1f --- /dev/null +++ b/example-23/fusexmp.c.patch @@ -0,0 +1,16 @@ +--- fusexmp.c.orig 2005-12-02 01:47:51.000000000 +0100 ++++ fusexmp.c 2005-12-02 01:51:26.000000000 +0100 +@@ -79,7 +79,12 @@ + { + int res; + +- res = mknod(path, mode, rdev); ++ /* On the Hurd we must not use mknod() to create files, but creat() */ ++ if(mode & S_IFREG) ++ res = creat(path, mode & ALLPERMS); ++ else ++ res = mknod(path, mode, rdev); ++ + if(res == -1) + return -errno; + diff --git a/example-23/fusexmp_fh.c b/example-23/fusexmp_fh.c new file mode 100644 index 000000000..6538d271f --- /dev/null +++ b/example-23/fusexmp_fh.c @@ -0,0 +1,360 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +#include <config.h> + +#ifdef linux +/* For pread()/pwrite() */ +#define _XOPEN_SOURCE 500 +#endif + +#include <fuse.h> +#include <stdio.h> +#include <string.h> +#include <unistd.h> +#include <fcntl.h> +#include <dirent.h> +#include <errno.h> +#include <sys/statfs.h> +#ifdef HAVE_SETXATTR +#include <sys/xattr.h> +#endif + +static int xmp_getattr(const char *path, struct stat *stbuf) +{ + int res; + + res = lstat(path, stbuf); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_readlink(const char *path, char *buf, size_t size) +{ + int res; + + res = readlink(path, buf, size - 1); + if(res == -1) + return -errno; + + buf[res] = '\0'; + return 0; +} + +static int xmp_opendir(const char *path, struct fuse_file_info *fi) +{ + DIR *dp = opendir(path); + if (dp == NULL) + return -errno; + + fi->fh = (unsigned long) dp; + return 0; +} + +static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + DIR *dp = (DIR *) fi->fh; + struct dirent *de; + + (void) path; + seekdir(dp, offset); + while ((de = readdir(dp)) != NULL) { + struct stat st; + memset(&st, 0, sizeof(st)); + st.st_ino = de->d_ino; + st.st_mode = de->d_type << 12; + if (filler(buf, de->d_name, &st, telldir(dp))) + break; + } + + return 0; +} + +static int xmp_releasedir(const char *path, struct fuse_file_info *fi) +{ + DIR *dp = (DIR *) fi->fh; + (void) path; + closedir(dp); + return 0; +} + +static int xmp_mknod(const char *path, mode_t mode, dev_t rdev) +{ + int res; + + /* On the Hurd we must not use mknod() to create files, but creat() */ + if(mode & S_IFREG) + res = creat(path, mode & ALLPERMS); + else + res = mknod(path, mode, rdev); + + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_mkdir(const char *path, mode_t mode) +{ + int res; + + res = mkdir(path, mode); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_unlink(const char *path) +{ + int res; + + res = unlink(path); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_rmdir(const char *path) +{ + int res; + + res = rmdir(path); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_symlink(const char *from, const char *to) +{ + int res; + + res = symlink(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_rename(const char *from, const char *to) +{ + int res; + + res = rename(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_link(const char *from, const char *to) +{ + int res; + + res = link(from, to); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_chmod(const char *path, mode_t mode) +{ + int res; + + res = chmod(path, mode); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_chown(const char *path, uid_t uid, gid_t gid) +{ + int res; + + res = lchown(path, uid, gid); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_truncate(const char *path, off_t size) +{ + int res; + + res = truncate(path, size); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_utime(const char *path, struct utimbuf *buf) +{ + int res; + + res = utime(path, buf); + if(res == -1) + return -errno; + + return 0; +} + + +static int xmp_open(const char *path, struct fuse_file_info *fi) +{ + int res; + + res = open(path, fi->flags); + if(res == -1) + return -errno; + + fi->fh = res; + return 0; +} + +static int xmp_read(const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + int res; + + (void) path; + res = pread(fi->fh, buf, size, offset); + if(res == -1) + res = -errno; + + return res; +} + +static int xmp_write(const char *path, const char *buf, size_t size, + off_t offset, struct fuse_file_info *fi) +{ + int res; + + (void) path; + res = pwrite(fi->fh, buf, size, offset); + if(res == -1) + res = -errno; + + return res; +} + +static int xmp_statfs(const char *path, struct statfs *stbuf) +{ + int res; + + res = statfs(path, stbuf); + if(res == -1) + return -errno; + + return 0; +} + +static int xmp_release(const char *path, struct fuse_file_info *fi) +{ + (void) path; + close(fi->fh); + + return 0; +} + +static int xmp_fsync(const char *path, int isdatasync, + struct fuse_file_info *fi) +{ + int res; + (void) path; + + if (isdatasync) + res = fdatasync(fi->fh); + else + res = fsync(fi->fh); + if(res == -1) + return -errno; + + return 0; +} + +#ifdef HAVE_SETXATTR +/* xattr operations are optional and can safely be left unimplemented */ +static int xmp_setxattr(const char *path, const char *name, const char *value, + size_t size, int flags) +{ + int res = lsetxattr(path, name, value, size, flags); + if(res == -1) + return -errno; + return 0; +} + +static int xmp_getxattr(const char *path, const char *name, char *value, + size_t size) +{ + int res = lgetxattr(path, name, value, size); + if(res == -1) + return -errno; + return res; +} + +static int xmp_listxattr(const char *path, char *list, size_t size) +{ + int res = llistxattr(path, list, size); + if(res == -1) + return -errno; + return res; +} + +static int xmp_removexattr(const char *path, const char *name) +{ + int res = lremovexattr(path, name); + if(res == -1) + return -errno; + return 0; +} +#endif /* HAVE_SETXATTR */ + +static struct fuse_operations xmp_oper = { + .getattr = xmp_getattr, + .readlink = xmp_readlink, + .opendir = xmp_opendir, + .readdir = xmp_readdir, + .releasedir = xmp_releasedir, + .mknod = xmp_mknod, + .mkdir = xmp_mkdir, + .symlink = xmp_symlink, + .unlink = xmp_unlink, + .rmdir = xmp_rmdir, + .rename = xmp_rename, + .link = xmp_link, + .chmod = xmp_chmod, + .chown = xmp_chown, + .truncate = xmp_truncate, + .utime = xmp_utime, + .open = xmp_open, + .read = xmp_read, + .write = xmp_write, + .statfs = xmp_statfs, + .release = xmp_release, + .fsync = xmp_fsync, +#ifdef HAVE_SETXATTR + .setxattr = xmp_setxattr, + .getxattr = xmp_getxattr, + .listxattr = xmp_listxattr, + .removexattr= xmp_removexattr, +#endif +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &xmp_oper); +} diff --git a/example-23/fusexmp_fh.c.patch b/example-23/fusexmp_fh.c.patch new file mode 100644 index 000000000..c45ece0c6 --- /dev/null +++ b/example-23/fusexmp_fh.c.patch @@ -0,0 +1,25 @@ +--- fusexmp_fh.c.orig 2005-12-02 01:47:51.000000000 +0100 ++++ fusexmp_fh.c 2005-12-02 01:58:16.000000000 +0100 +@@ -71,7 +71,7 @@ + memset(&st, 0, sizeof(st)); + st.st_ino = de->d_ino; + st.st_mode = de->d_type << 12; +- if (filler(buf, de->d_name, &st, de->d_off)) ++ if (filler(buf, de->d_name, &st, telldir(dp))) + break; + } + +@@ -90,7 +90,12 @@ + { + int res; + +- res = mknod(path, mode, rdev); ++ /* On the Hurd we must not use mknod() to create files, but creat() */ ++ if(mode & S_IFREG) ++ res = creat(path, mode & ALLPERMS); ++ else ++ res = mknod(path, mode, rdev); ++ + if(res == -1) + return -errno; + diff --git a/example-23/hello.c b/example-23/hello.c new file mode 100644 index 000000000..b71bcd645 --- /dev/null +++ b/example-23/hello.c @@ -0,0 +1,94 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +#include <fuse.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> +#include <fcntl.h> + +static const char *hello_str = "Hello World!\n"; +static const char *hello_path = "/hello"; + +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_path) == 0) { + stbuf->st_mode = S_IFREG | 0444; + stbuf->st_nlink = 1; + stbuf->st_size = strlen(hello_str); + } + else + res = -ENOENT; + + return res; +} + +static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler, + off_t offset, struct fuse_file_info *fi) +{ + (void) offset; + (void) fi; + + if(strcmp(path, "/") != 0) + return -ENOENT; + + filler(buf, ".", NULL, 0); + filler(buf, "..", NULL, 0); + filler(buf, hello_path + 1, NULL, 0); + + return 0; +} + +static int hello_open(const char *path, struct fuse_file_info *fi) +{ + if(strcmp(path, hello_path) != 0) + return -ENOENT; + + if((fi->flags & 3) != O_RDONLY) + return -EACCES; + + return 0; +} + +static int hello_read(const char *path, char *buf, size_t size, off_t offset, + struct fuse_file_info *fi) +{ + 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; +} + +static struct fuse_operations hello_oper = { + .getattr = hello_getattr, + .readdir = hello_readdir, + .open = hello_open, + .read = hello_read, +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &hello_oper); +} diff --git a/example-23/null.c b/example-23/null.c new file mode 100644 index 000000000..d58feabb2 --- /dev/null +++ b/example-23/null.c @@ -0,0 +1,78 @@ +/* + FUSE: Filesystem in Userspace + Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> + + This program can be distributed under the terms of the GNU GPL. + See the file COPYING. +*/ + +#include <fuse.h> +#include <string.h> +#include <unistd.h> +#include <time.h> +#include <errno.h> + +#define UNUSED(x) x __attribute__((unused)) + +static int null_getattr(const char *path, struct stat *stbuf) +{ + if(strcmp(path, "/") != 0) + return -ENOENT; + + stbuf->st_mode = S_IFREG | 0644; + stbuf->st_nlink = 1; + stbuf->st_uid = getuid(); + stbuf->st_gid = getgid(); + stbuf->st_size = (1ULL << 32); /* 4G */ + stbuf->st_blocks = 0; + stbuf->st_atime = stbuf->st_mtime = stbuf->st_ctime = time(NULL); + + return 0; +} + +static int null_truncate(const char *path, off_t UNUSED(size)) +{ + if(strcmp(path, "/") != 0) + return -ENOENT; + + return 0; +} + +static int null_open(const char *path, struct fuse_file_info *UNUSED(fi)) +{ + if(strcmp(path, "/") != 0) + return -ENOENT; + + return 0; +} + +static int null_read(const char *path, char *UNUSED(buf), size_t size, + off_t UNUSED(offset), struct fuse_file_info *UNUSED(fi)) +{ + if(strcmp(path, "/") != 0) + return -ENOENT; + + return size; +} + +static int null_write(const char *path, const char *UNUSED(buf), size_t size, + off_t UNUSED(offset), struct fuse_file_info *UNUSED(fi)) +{ + if(strcmp(path, "/") != 0) + return -ENOENT; + + return size; +} + +static struct fuse_operations null_oper = { + .getattr = null_getattr, + .truncate = null_truncate, + .open = null_open, + .read = null_read, + .write = null_write, +}; + +int main(int argc, char *argv[]) +{ + return fuse_main(argc, argv, &null_oper); +} |