"Fossies" - the Fresh Open Source Software Archive

Member "fuse-3.2.3/example/null.c" (11 May 2018, 2734 Bytes) of package /linux/misc/fuse-3.2.3.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "null.c": 3.2.0_vs_3.2.1.

    1 /*
    2   FUSE: Filesystem in Userspace
    3   Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
    4 
    5   This program can be distributed under the terms of the GNU GPL.
    6   See the file COPYING.
    7 */
    8 
    9 /** @file
   10  *
   11  * This "filesystem" provides only a single file. The mountpoint
   12  * needs to be a file rather than a directory. All writes to the
   13  * file will be discarded, and reading the file always returns
   14  * \0.
   15  *
   16  * Compile with:
   17  *
   18  *     gcc -Wall null.c `pkg-config fuse3 --cflags --libs` -o null
   19  *
   20  * ## Source code ##
   21  * \include passthrough_fh.c
   22  */
   23 
   24 
   25 #define FUSE_USE_VERSION 31
   26 
   27 #include <fuse.h>
   28 #include <fuse_lowlevel.h>
   29 #include <stdio.h>
   30 #include <string.h>
   31 #include <unistd.h>
   32 #include <time.h>
   33 #include <errno.h>
   34 
   35 static int null_getattr(const char *path, struct stat *stbuf,
   36             struct fuse_file_info *fi)
   37 {
   38     (void) fi;
   39 
   40     if(strcmp(path, "/") != 0)
   41         return -ENOENT;
   42 
   43     stbuf->st_mode = S_IFREG | 0644;
   44     stbuf->st_nlink = 1;
   45     stbuf->st_uid = getuid();
   46     stbuf->st_gid = getgid();
   47     stbuf->st_size = (1ULL << 32); /* 4G */
   48     stbuf->st_blocks = 0;
   49     stbuf->st_atime = stbuf->st_mtime = stbuf->st_ctime = time(NULL);
   50 
   51     return 0;
   52 }
   53 
   54 static int null_truncate(const char *path, off_t size,
   55              struct fuse_file_info *fi)
   56 {
   57     (void) size;
   58     (void) fi;
   59 
   60     if(strcmp(path, "/") != 0)
   61         return -ENOENT;
   62 
   63     return 0;
   64 }
   65 
   66 static int null_open(const char *path, struct fuse_file_info *fi)
   67 {
   68     (void) fi;
   69 
   70     if(strcmp(path, "/") != 0)
   71         return -ENOENT;
   72 
   73     return 0;
   74 }
   75 
   76 static int null_read(const char *path, char *buf, size_t size,
   77              off_t offset, struct fuse_file_info *fi)
   78 {
   79     (void) buf;
   80     (void) offset;
   81     (void) fi;
   82 
   83     if(strcmp(path, "/") != 0)
   84         return -ENOENT;
   85 
   86     if (offset >= (1ULL << 32))
   87         return 0;
   88 
   89     memset(buf, 0, size);
   90     return size;
   91 }
   92 
   93 static int null_write(const char *path, const char *buf, size_t size,
   94               off_t offset, struct fuse_file_info *fi)
   95 {
   96     (void) buf;
   97     (void) offset;
   98     (void) fi;
   99 
  100     if(strcmp(path, "/") != 0)
  101         return -ENOENT;
  102 
  103     return size;
  104 }
  105 
  106 static struct fuse_operations null_oper = {
  107     .getattr    = null_getattr,
  108     .truncate   = null_truncate,
  109     .open       = null_open,
  110     .read       = null_read,
  111     .write      = null_write,
  112 };
  113 
  114 int main(int argc, char *argv[])
  115 {
  116     struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  117     struct fuse_cmdline_opts opts;
  118     struct stat stbuf;
  119 
  120     if (fuse_parse_cmdline(&args, &opts) != 0)
  121         return 1;
  122 
  123     if (!opts.mountpoint) {
  124         fprintf(stderr, "missing mountpoint parameter\n");
  125         return 1;
  126     }
  127 
  128     if (stat(opts.mountpoint, &stbuf) == -1) {
  129         fprintf(stderr ,"failed to access mountpoint %s: %s\n",
  130             opts.mountpoint, strerror(errno));
  131         return 1;
  132     }
  133     if (!S_ISREG(stbuf.st_mode)) {
  134         fprintf(stderr, "mountpoint is not a regular file\n");
  135         return 1;
  136     }
  137 
  138     return fuse_main(argc, argv, &null_oper, NULL);
  139 }