Advertisement
Guest User

fuserace.c

a guest
Jan 22nd, 2012
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #define _XOPEN_SOURCE 500
  2. #define _GNU_SOURCE
  3. #define FUSE_USE_VERSION 26
  4.  
  5. #include <stdio.h>
  6. #include <fuse.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12.  
  13. #define PATH_MAX 1024
  14. #define BASEDIR "/tmp/RACE"
  15.  
  16. void get_path(char *fpath, const char *path) {
  17.   snprintf(fpath, PATH_MAX, "%s%s.%d", BASEDIR, path, fuse_get_context()->uid);  
  18. }
  19.  
  20. int r_open(const char *path, struct fuse_file_info *fi)
  21. {
  22.     char *fpath = malloc(PATH_MAX);
  23.     get_path(fpath, path);
  24.     fi->fh = open(fpath, fi->flags);
  25.     free(fpath);
  26.     if (fi->fh < 0) {
  27.       return -errno;
  28.     } else {
  29.       return 0;
  30.     }
  31. }
  32.  
  33. int r_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
  34.   return pread(fi->fh, buf, size, offset);
  35. }
  36.  
  37. int r_release(const char *path, struct fuse_file_info *fi) {
  38.   return close(fi->fh);
  39. }
  40.  
  41. int r_getattr(const char *path, struct stat *statbuf) {
  42.   int retstat = 0;
  43.   if (strcmp(path, "/") == 0) {
  44.     statbuf->st_mode = S_IFDIR | 0755;
  45.     statbuf->st_nlink = 2;
  46.   } else if (strcmp(path, "/a") == 0) {
  47.     char *fpath = malloc(PATH_MAX);
  48.     get_path(fpath, path);
  49.     retstat = lstat(fpath, statbuf);
  50.     free(fpath);
  51.   }
  52.   return retstat;
  53. }
  54.  
  55. struct fuse_operations r_oper = {
  56.   .getattr = r_getattr,
  57.   .open = r_open,
  58.   .read = r_read,
  59.   .release = r_release,
  60. };
  61.  
  62. int main(int argc, char *argv[]) {
  63.   return fuse_main(argc, argv, &r_oper, NULL);
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement