Guest User

cloudfuse-edit

a guest
Feb 19th, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 13.81 KB | None | 0 0
  1. #define FUSE_USE_VERSION 26
  2. #include <fuse.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include <strings.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <pthread.h>
  12. #include <pwd.h>
  13. #include <fcntl.h>
  14. #include <signal.h>
  15. #include <stdint.h>
  16. #include <stddef.h>
  17. #include "cloudfsapi.h"
  18. #include "config.h"
  19.  
  20.  
  21. #define OPTION_SIZE 1024
  22.  
  23. static int cache_timeout;
  24.  
  25. typedef struct dir_cache
  26. {
  27.   char *path;
  28.   dir_entry *entries;
  29.   time_t cached;
  30.   struct dir_cache *next, *prev;
  31. } dir_cache;
  32. static dir_cache *dcache;
  33. static pthread_mutex_t dmut;
  34.  
  35. typedef struct
  36. {
  37.   int fd;
  38.   int flags;
  39. } openfile;
  40.  
  41.  
  42. static void dir_for(const char *path, char *dir)
  43. {
  44.   strncpy(dir, path, MAX_PATH_SIZE);
  45.   char *slash = strrchr(dir, '/');
  46.   if (slash)
  47.     *slash = '\0';
  48. }
  49.  
  50. static dir_cache *new_cache(const char *path)
  51. {
  52.   dir_cache *cw = (dir_cache *)calloc(sizeof(dir_cache), 1);
  53.   cw->path = strdup(path);
  54.   cw->prev = NULL;
  55.   cw->entries = NULL;
  56.   cw->cached = time(NULL);
  57.   if (dcache)
  58.     dcache->prev = cw;
  59.   cw->next = dcache;
  60.   return (dcache = cw);
  61. }
  62.  
  63. static int caching_list_directory(const char *path, dir_entry **list)
  64. {
  65.   pthread_mutex_lock(&dmut);
  66.   if (!strcmp(path, "/"))
  67.     path = "";
  68.   dir_cache *cw;
  69.   for (cw = dcache; cw; cw = cw->next)
  70.     if (!strcmp(cw->path, path))
  71.       break;
  72.   if (!cw)
  73.   {
  74.     if (!cloudfs_list_directory(path, list))
  75.       return  0;
  76.     cw = new_cache(path);
  77.   }
  78.   else if (cache_timeout > 0 && (time(NULL) - cw->cached > cache_timeout))
  79.   {
  80.     if (!cloudfs_list_directory(path, list))
  81.       return  0;
  82.     cloudfs_free_dir_list(cw->entries);
  83.     cw->cached = time(NULL);
  84.   }
  85.   else
  86.     *list = cw->entries;
  87.   cw->entries = *list;
  88.   pthread_mutex_unlock(&dmut);
  89.   return 1;
  90. }
  91.  
  92. static void update_dir_cache(const char *path, off_t size, int isdir)
  93. {
  94.   pthread_mutex_lock(&dmut);
  95.   dir_cache *cw;
  96.   dir_entry *de;
  97.   char dir[MAX_PATH_SIZE];
  98.   dir_for(path, dir);
  99.   for (cw = dcache; cw; cw = cw->next)
  100.   {
  101.     if (!strcmp(cw->path, dir))
  102.     {
  103.       for (de = cw->entries; de; de = de->next)
  104.       {
  105.         if (!strcmp(de->full_name, path))
  106.         {
  107.           de->size = size;
  108.           pthread_mutex_unlock(&dmut);
  109.           return;
  110.         }
  111.       }
  112.       de = (dir_entry *)malloc(sizeof(dir_entry));
  113.       de->size = size;
  114.       de->isdir = isdir;
  115.       de->name = strdup(&path[strlen(cw->path)+1]);
  116.       de->full_name = strdup(path);
  117.       de->content_type = strdup(isdir ? "application/directory" : "application/octet-stream");
  118.       de->last_modified = time(NULL);
  119.       de->next = cw->entries;
  120.       cw->entries = de;
  121.       if (isdir)
  122.         new_cache(path);
  123.       break;
  124.     }
  125.   }
  126.   pthread_mutex_unlock(&dmut);
  127. }
  128.  
  129. static void dir_decache(const char *path)
  130. {
  131.   dir_cache *cw;
  132.   pthread_mutex_lock(&dmut);
  133.   dir_entry *de, *tmpde;
  134.   char dir[MAX_PATH_SIZE];
  135.   dir_for(path, dir);
  136.   for (cw = dcache; cw; cw = cw->next)
  137.   {
  138.     if (!strcmp(cw->path, path))
  139.     {
  140.       if (cw == dcache)
  141.         dcache = cw->next;
  142.       if (cw->prev)
  143.         cw->prev->next = cw->next;
  144.       if (cw->next)
  145.         cw->next->prev = cw->prev;
  146.       cloudfs_free_dir_list(cw->entries);
  147.       free(cw->path);
  148.       free(cw);
  149.     }
  150.     else if (cw->entries && !strcmp(dir, cw->path))
  151.     {
  152.       if (!strcmp(cw->entries->full_name, path))
  153.       {
  154.         de = cw->entries;
  155.         cw->entries = de->next;
  156.         de->next = NULL;
  157.         cloudfs_free_dir_list(de);
  158.       }
  159.       else for (de = cw->entries; de->next; de = de->next)
  160.       {
  161.         if (!strcmp(de->next->full_name, path))
  162.         {
  163.           tmpde = de->next;
  164.           de->next = de->next->next;
  165.           tmpde->next = NULL;
  166.           cloudfs_free_dir_list(tmpde);
  167.           break;
  168.         }
  169.       }
  170.     }
  171.   }
  172.   pthread_mutex_unlock(&dmut);
  173. }
  174.  
  175. static dir_entry *path_info(const char *path)
  176. {
  177.   char dir[MAX_PATH_SIZE];
  178.   dir_for(path, dir);
  179.   dir_entry *tmp;
  180.   if (!caching_list_directory(dir, &tmp))
  181.     return NULL;
  182.   for (; tmp; tmp = tmp->next)
  183.   {
  184.     if (!strcmp(tmp->full_name, path))
  185.       return tmp;
  186.   }
  187.   return NULL;
  188. }
  189.  
  190. static int cfs_getattr(const char *path, struct stat *stbuf)
  191. {
  192.   stbuf->st_uid = geteuid();
  193.   stbuf->st_gid = getegid();
  194.   if (!strcmp(path, "/"))
  195.   {
  196.     stbuf->st_mode = S_IFDIR | 0755;
  197.     stbuf->st_nlink = 2;
  198.     return 0;
  199.   }
  200.   dir_entry *de = path_info(path);
  201.   if (!de)
  202.     return -ENOENT;
  203.   stbuf->st_ctime = stbuf->st_mtime = de->last_modified;
  204.   if (de->isdir)
  205.   {
  206.     stbuf->st_size = 0;
  207.     stbuf->st_mode = S_IFDIR | 0755;
  208.     stbuf->st_nlink = 2;
  209.   }
  210.   else
  211.   {
  212.     stbuf->st_size = de->size;
  213.     /* calc. blocks as if 4K blocksize filesystem; stat uses units of 512B */
  214.     stbuf->st_blocks = ((4095 + de->size) / 4096) * 8;
  215.     stbuf->st_mode = S_IFREG | 0666;
  216.     stbuf->st_nlink = 1;
  217.   }
  218.   return 0;
  219. }
  220.  
  221. static int cfs_fgetattr(const char *path, struct stat *stbuf, struct fuse_file_info *info)
  222. {
  223.   openfile *of = (openfile *)(uintptr_t)info->fh;
  224.   if (of)
  225.   {
  226.     stbuf->st_size = cloudfs_file_size(of->fd);
  227.     stbuf->st_mode = S_IFREG | 0666;
  228.     stbuf->st_nlink = 1;
  229.     return 0;
  230.   }
  231.   return -ENOENT;
  232. }
  233.  
  234. static int cfs_readdir(const char *path, void *buf, fuse_fill_dir_t filldir, off_t offset, struct fuse_file_info *info)
  235. {
  236.   dir_entry *de;
  237.   if (!caching_list_directory(path, &de))
  238.     return -ENOLINK;
  239.   filldir(buf, ".", NULL, 0);
  240.   filldir(buf, "..", NULL, 0);
  241.   for (; de; de = de->next)
  242.     filldir(buf, de->name, NULL, 0);
  243.   return 0;
  244. }
  245.  
  246. static int cfs_mkdir(const char *path, mode_t mode)
  247. {
  248.   if (cloudfs_create_directory(path))
  249.   {
  250.     update_dir_cache(path, 0, 1);
  251.     return 0;
  252.   }
  253.   return -ENOENT;
  254. }
  255.  
  256. static int cfs_create(const char *path, mode_t mode, struct fuse_file_info *info)
  257. {
  258.  
  259.   FILE *temp_file;
  260.   setenv("TMPDIR", "/cftmp/", 1);
  261.   temp_file = tmpfile();
  262.  
  263.   openfile *of = (openfile *)malloc(sizeof(openfile));
  264.   of->fd = dup(fileno(temp_file));
  265.   fclose(temp_file);
  266.   of->flags = info->flags;
  267.   info->fh = (uintptr_t)of;
  268.   update_dir_cache(path, 0, 0);
  269.   info->direct_io = 1;
  270.   return 0;
  271. }
  272.  
  273. static int cfs_open(const char *path, struct fuse_file_info *info)
  274. {
  275.  
  276.   FILE *temp_file;
  277.   setenv("TMPDIR", "/cftmp/", 1);
  278.   temp_file = tmpfile();
  279.  
  280.   dir_entry *de = path_info(path);
  281.   if (!(info->flags & O_WRONLY))
  282.   {
  283.     if (!cloudfs_object_write_fp(path, temp_file))
  284.     {
  285.       fclose(temp_file);
  286.       return -ENOENT;
  287.     }
  288.     update_dir_cache(path, (de ? de->size : 0), 0);
  289.   }
  290.   openfile *of = (openfile *)malloc(sizeof(openfile));
  291.   of->fd = dup(fileno(temp_file));
  292.   fclose(temp_file);
  293.   of->flags = info->flags;
  294.   info->fh = (uintptr_t)of;
  295.   info->direct_io = 1;
  296.   return 0;
  297. }
  298.  
  299. static int cfs_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *info)
  300. {
  301.   return pread(((openfile *)(uintptr_t)info->fh)->fd, buf, size, offset);
  302. }
  303.  
  304. static int cfs_flush(const char *path, struct fuse_file_info *info)
  305. {
  306.   openfile *of = (openfile *)(uintptr_t)info->fh;
  307.   if (of)
  308.   {
  309.     update_dir_cache(path, cloudfs_file_size(of->fd), 0);
  310.     if (of->flags & O_RDWR || of->flags & O_WRONLY)
  311.     {
  312.       FILE *fp = fdopen(dup(of->fd), "r");
  313.       rewind(fp);
  314.       if (!cloudfs_object_read_fp(path, fp))
  315.       {
  316.         fclose(fp);
  317.         return -ENOENT;
  318.       }
  319.       fclose(fp);
  320.     }
  321.   }
  322.   return 0;
  323. }
  324.  
  325. static int cfs_release(const char *path, struct fuse_file_info *info)
  326. {
  327.   close(((openfile *)(uintptr_t)info->fh)->fd);
  328.   return 0;
  329. }
  330.  
  331. static int cfs_rmdir(const char *path)
  332. {
  333.   if (cloudfs_delete_object(path))
  334.   {
  335.     dir_decache(path);
  336.     return 0;
  337.   }
  338.   return -ENOENT;
  339. }
  340.  
  341. static int cfs_ftruncate(const char *path, off_t size, struct fuse_file_info *info)
  342. {
  343.   openfile *of = (openfile *)(uintptr_t)info->fh;
  344.   if (ftruncate(of->fd, size))
  345.     return -errno;
  346.   lseek(of->fd, 0, SEEK_SET);
  347.   update_dir_cache(path, size, 0);
  348.   return 0;
  349. }
  350.  
  351. static int cfs_write(const char *path, const char *buf, size_t length, off_t offset, struct fuse_file_info *info)
  352. {
  353.   update_dir_cache(path, offset + length, 0);
  354.   return pwrite(((openfile *)(uintptr_t)info->fh)->fd, buf, length, offset);
  355. }
  356.  
  357. static int cfs_unlink(const char *path)
  358. {
  359.   if (cloudfs_delete_object(path))
  360.   {
  361.     dir_decache(path);
  362.     return 0;
  363.   }
  364.   return -ENOENT;
  365. }
  366.  
  367. static int cfs_fsync(const char *path, int idunno, struct fuse_file_info *info)
  368. {
  369.   return 0;
  370. }
  371.  
  372. static int cfs_truncate(const char *path, off_t size)
  373. {
  374.   cloudfs_object_truncate(path, size);
  375.   return 0;
  376. }
  377.  
  378. static int cfs_statfs(const char *path, struct statvfs *stat)
  379. {
  380.   stat->f_bsize = 4096;
  381.   stat->f_frsize = 4096;
  382.   stat->f_blocks = INT_MAX;
  383.   stat->f_bfree = stat->f_blocks;
  384.   stat->f_bavail = stat->f_blocks;
  385.   stat->f_files = INT_MAX;
  386.   stat->f_ffree = INT_MAX;
  387.   stat->f_favail = INT_MAX;
  388.   stat->f_namemax = INT_MAX;
  389.   return 0;
  390. }
  391.  
  392. static int cfs_chown(const char *path, uid_t uid, gid_t gid)
  393. {
  394.   return 0;
  395. }
  396.  
  397. static int cfs_chmod(const char *path, mode_t mode)
  398. {
  399.   return 0;
  400. }
  401.  
  402. static int cfs_rename(const char *src, const char *dst)
  403. {
  404.   dir_entry *src_de = path_info(src);
  405.   if (!src_de)
  406.       return -ENOENT;
  407.   if (src_de->isdir)
  408.     return -EISDIR;
  409.   if (cloudfs_copy_object(src, dst))
  410.   {
  411.     /* FIXME this isn't quite right as doesn't preserve last modified */
  412.     update_dir_cache(dst, src_de->size, 0);
  413.     return cfs_unlink(src);
  414.   }
  415.   return -EIO;
  416. }
  417.  
  418. static void *cfs_init(struct fuse_conn_info *conn)
  419. {
  420.   signal(SIGPIPE, SIG_IGN);
  421.   return NULL;
  422. }
  423.  
  424. char *get_home_dir()
  425. {
  426.   char *home;
  427.   if ((home = getenv("HOME")) && !access(home, R_OK))
  428.     return home;
  429.   struct passwd *pwd = getpwuid(geteuid());
  430.   if ((home = pwd->pw_dir) && !access(home, R_OK))
  431.     return home;
  432.   return "~";
  433. }
  434.  
  435. static struct options {
  436.     char username[OPTION_SIZE];
  437.     char tenant[OPTION_SIZE];
  438.     char password[OPTION_SIZE];
  439.     char cache_timeout[OPTION_SIZE];
  440.     char authurl[OPTION_SIZE];
  441.     char region[OPTION_SIZE];
  442.     char use_snet[OPTION_SIZE];
  443.     char verify_ssl[OPTION_SIZE];
  444. } options = {
  445.     .username = "",
  446.     .password = "",
  447.     .tenant = "",
  448.     .cache_timeout = "600",
  449.     .authurl = "https://identity.api.rackspacecloud.com/v2.0/",
  450.     .region = "",
  451.     .use_snet = "false",
  452.     .verify_ssl = "true",
  453. };
  454.  
  455. int parse_option(void *data, const char *arg, int key, struct fuse_args *outargs)
  456. {
  457.   if (sscanf(arg, " username = %[^\r\n ]", options.username) ||
  458.       sscanf(arg, " tenant = %[^\r\n ]", options.tenant) ||
  459.       sscanf(arg, " api_key = %[^\r\n ]", options.password) ||
  460.       sscanf(arg, " password = %[^\r\n ]", options.password) ||
  461.       sscanf(arg, " cache_timeout = %[^\r\n ]", options.cache_timeout) ||
  462.       sscanf(arg, " authurl = %[^\r\n ]", options.authurl) ||
  463.       sscanf(arg, " region = %[^\r\n ]", options.region) ||
  464.       sscanf(arg, " use_snet = %[^\r\n ]", options.use_snet) ||
  465.       sscanf(arg, " verify_ssl = %[^\r\n ]", options.verify_ssl))
  466.     return 0;
  467.   if (!strcmp(arg, "-f") || !strcmp(arg, "-d") || !strcmp(arg, "debug"))
  468.     cloudfs_debug(1);
  469.   return 1;
  470. }
  471.  
  472. int main(int argc, char **argv)
  473. {
  474.   char settings_filename[MAX_PATH_SIZE] = "";
  475.   FILE *settings;
  476.   struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
  477.  
  478.   snprintf(settings_filename, sizeof(settings_filename), "%s/.cloudfuse", get_home_dir());
  479.   if ((settings = fopen(settings_filename, "r")))
  480.   {
  481.     char line[OPTION_SIZE];
  482.     while (fgets(line, sizeof(line), settings))
  483.       parse_option(NULL, line, -1, &args);
  484.     fclose(settings);
  485.   }
  486.  
  487.   fuse_opt_parse(&args, &options, NULL, parse_option);
  488.  
  489.   cache_timeout = atoi(options.cache_timeout);
  490.  
  491.   if (!*options.username || !*options.password)
  492.   {
  493.     fprintf(stderr, "Unable to determine username and API key.\n\n");
  494.     fprintf(stderr, "These can be set either as mount options or in"
  495.                     "a file named %s\n\n", settings_filename);
  496.     fprintf(stderr, "  username=[Account username]\n");
  497.     fprintf(stderr, "  api_key=[API key (or password for Keystone API)]\n\n");
  498.     fprintf(stderr, "The following settings are optional:\n\n");
  499.     fprintf(stderr, "  authurl=[Authentication url - connect to non-Rackspace Swift]\n");
  500.     fprintf(stderr, "  tenant=[Tenant for authentication with Keystone]\n");
  501.     fprintf(stderr, "  password=[Password for authentication with Keystone]\n");
  502.     fprintf(stderr, "  use_snet=[True to use Rackspace ServiceNet for connections]\n");
  503.     fprintf(stderr, "  cache_timeout=[Seconds for directory caching, default 600]\n");
  504.     fprintf(stderr, "  verify_ssl=[False to disable SSL cert verification]\n");
  505.  
  506.     return 1;
  507.   }
  508.  
  509.   cloudfs_init();
  510.  
  511.   cloudfs_verify_ssl(!strcasecmp(options.verify_ssl, "true"));
  512.  
  513.   cloudfs_set_credentials(options.username, options.tenant, options.password,
  514.                           options.authurl, options.region,
  515.                           !strcasecmp(options.use_snet, "true"));
  516.   if (!cloudfs_connect())
  517.   {
  518.     fprintf(stderr, "Failed to authenticate.\n");
  519.     return 1;
  520.   }
  521.  
  522.   #ifndef HAVE_OPENSSL
  523.   #warning Compiling without libssl, will run single-threaded.
  524.   fuse_opt_add_arg(&args, "-s");
  525.   #endif
  526.  
  527.   struct fuse_operations cfs_oper = {
  528.     .readdir = cfs_readdir,
  529.     .mkdir = cfs_mkdir,
  530.     .read = cfs_read,
  531.     .create = cfs_create,
  532.     .open = cfs_open,
  533.     .fgetattr = cfs_fgetattr,
  534.     .getattr = cfs_getattr,
  535.     .flush = cfs_flush,
  536.     .release = cfs_release,
  537.     .rmdir = cfs_rmdir,
  538.     .ftruncate = cfs_ftruncate,
  539.     .truncate = cfs_truncate,
  540.     .write = cfs_write,
  541.     .unlink = cfs_unlink,
  542.     .fsync = cfs_fsync,
  543.     .statfs = cfs_statfs,
  544.     .chmod = cfs_chmod,
  545.     .chown = cfs_chown,
  546.     .rename = cfs_rename,
  547.     .init = cfs_init,
  548.   };
  549.  
  550.   pthread_mutex_init(&dmut, NULL);
  551.   return fuse_main(args.argc, args.argv, &cfs_oper, &options);
  552. }
Advertisement
Add Comment
Please, Sign In to add comment