nfs-ganesha 1.4

hello.c

Go to the documentation of this file.
00001 /*
00002     FUSE: Filesystem in Userspace
00003     Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
00004 
00005     This program can be distributed under the terms of the GNU GPL.
00006     See the file COPYING.
00007 
00008     gcc -Wall `pkg-config fuse --cflags --libs` hello.c -o hello
00009 */
00010 
00011 #define FUSE_USE_VERSION 26
00012 
00013 #include <ganesha_fuse_wrap.h>
00014 #include <stdio.h>
00015 #include <string.h>
00016 #include <errno.h>
00017 #include <fcntl.h>
00018 
00019 static const char *hello_str = "Hello World!\n";
00020 static const char *hello_path = "/hello";
00021 
00022 static int hello_getattr(const char *path, struct stat *stbuf)
00023 {
00024   int res = 0;
00025 
00026   memset(stbuf, 0, sizeof(struct stat));
00027   if(strcmp(path, "/") == 0)
00028     {
00029       stbuf->st_ino = 1;
00030       stbuf->st_mode = S_IFDIR | 0755;
00031       stbuf->st_nlink = 2;
00032     }
00033   else if(strcmp(path, hello_path) == 0)
00034     {
00035       stbuf->st_ino = 2;
00036       stbuf->st_mode = S_IFREG | 0444;
00037       stbuf->st_nlink = 1;
00038       stbuf->st_size = strlen(hello_str);
00039     }
00040   else
00041     res = -ENOENT;
00042 
00043   return res;
00044 }
00045 
00046 static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
00047                          off_t offset, struct fuse_file_info *fi)
00048 {
00049   (void)offset;
00050   (void)fi;
00051 
00052   if(strcmp(path, "/") != 0)
00053     return -ENOENT;
00054 
00055   filler(buf, ".", NULL, 0);
00056   filler(buf, "..", NULL, 0);
00057   filler(buf, hello_path + 1, NULL, 0);
00058 
00059   return 0;
00060 }
00061 
00062 static int hello_open(const char *path, struct fuse_file_info *fi)
00063 {
00064   if(strcmp(path, hello_path) != 0)
00065     return -ENOENT;
00066 
00067   if((fi->flags & 3) != O_RDONLY)
00068     return -EACCES;
00069 
00070   return 0;
00071 }
00072 
00073 static int hello_read(const char *path, char *buf, size_t size, off_t offset,
00074                       struct fuse_file_info *fi)
00075 {
00076   size_t len;
00077   (void)fi;
00078   if(strcmp(path, hello_path) != 0)
00079     return -ENOENT;
00080 
00081   len = strlen(hello_str);
00082   if(offset < len)
00083     {
00084       if(offset + size > len)
00085         size = len - offset;
00086       memcpy(buf, hello_str + offset, size);
00087     }
00088   else
00089     size = 0;
00090 
00091   return size;
00092 }
00093 
00094 static struct fuse_operations hello_oper = {
00095   .getattr = hello_getattr,
00096   .readdir = hello_readdir,
00097   .open = hello_open,
00098   .read = hello_read,
00099 };
00100 
00101 int main(int argc, char *argv[])
00102 {
00103   return fuse_main(argc, argv, &hello_oper, NULL);
00104 }