Advertisement
CSenshi

OS - FUSE(access)

Jul 20th, 2019
375
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.83 KB | None | 0 0
  1. int FS_access(const char *path, int mask)
  2. {
  3.     _debug_print_FS("\n%d FS : Called access  | path : %s\n", FS_COUNT++, path);
  4.  
  5.     struct fuse_context *context = (struct fuse_context *)fuse_get_context();
  6.     struct memcached *m = (struct memcached *)(context->private_data);
  7.  
  8.     mm_data_info info;
  9.     memcached_get(m, path, &info);
  10.     if (info.value == NULL)
  11.         return -ENOENT;
  12.  
  13.     gid_t cur_gid;
  14.     uid_t cur_uid;
  15.     mode_t mode;
  16.  
  17.     if (info.flags & MM_DIR) // check if directory
  18.     {
  19.         dir d;
  20.         memcpy(&d, info.value, sizeof(struct dir));
  21.         mode = d.mode;
  22.         cur_gid = d.gid;
  23.         cur_uid = d.uid;
  24.     }
  25.     else if (info.flags & MM_FIL) // check if file
  26.     {
  27.         file f;
  28.         memcpy(&f, info.value, sizeof(struct file));
  29.         mode = f.mode;
  30.         cur_gid = f.gid;
  31.         cur_uid = f.uid;
  32.     }
  33.     else //error
  34.         return -ENOENT;
  35.  
  36.     int R_PERM, W_PERM, X_PERM;
  37.  
  38.     if (cur_uid == context->uid) // is user
  39.     {
  40.         R_PERM = S_IRUSR; /* R for owner */
  41.         W_PERM = S_IWUSR; /* W for owner */
  42.         X_PERM = S_IXUSR; /* X for owner */
  43.     }
  44.     else if (cur_gid == context->gid) // is gid
  45.     {
  46.         R_PERM = S_IRGRP; /* R for group */
  47.         W_PERM = S_IWGRP; /* W for group */
  48.         X_PERM = S_IXGRP; /* X for group */
  49.     }
  50.     else // is other
  51.     {
  52.         R_PERM = S_IROTH; /* R for other */
  53.         W_PERM = S_IWOTH; /* W for other */
  54.         X_PERM = S_IXOTH; /* X for other */
  55.     }
  56.  
  57.     if ((mask & R_OK) && (mode & R_PERM)) /* (wants to write) and (has permission) */
  58.         return 0;
  59.  
  60.     if ((mask & W_OK) && (mode & W_PERM)) /* (wants to read) and (has permission) */
  61.         return 0;
  62.  
  63.     if ((mask & X_OK) && (mode & X_PERM)) /* (wants to execute) and (has permission) */
  64.         return 0;
  65.  
  66.     return -EACCES;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement