main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. /*
  2. main.c (01.09.09)
  3. FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
  4. Copyright (C) 2010-2013 Andrew Nayenko
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #define FUSE_USE_VERSION 26
  17. #include <fuse.h>
  18. #include <errno.h>
  19. #include <fcntl.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <exfat.h>
  24. #include <inttypes.h>
  25. #include <limits.h>
  26. #include <sys/types.h>
  27. #include <pwd.h>
  28. #include <unistd.h>
  29. #define exfat_debug(format, ...)
  30. #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
  31. #error FUSE 2.6 or later is required
  32. #endif
  33. const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
  34. "defer_permissions";
  35. struct exfat ef;
  36. static struct exfat_node* get_node(const struct fuse_file_info* fi)
  37. {
  38. return (struct exfat_node*) (size_t) fi->fh;
  39. }
  40. static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
  41. {
  42. fi->fh = (uint64_t) (size_t) node;
  43. }
  44. static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
  45. {
  46. struct exfat_node* node;
  47. int rc;
  48. exfat_debug("[%s] %s", __func__, path);
  49. rc = exfat_lookup(&ef, &node, path);
  50. if (rc != 0)
  51. return rc;
  52. exfat_stat(&ef, node, stbuf);
  53. exfat_put_node(&ef, node);
  54. return 0;
  55. }
  56. static int fuse_exfat_truncate(const char* path, off_t size)
  57. {
  58. struct exfat_node* node;
  59. int rc;
  60. exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
  61. rc = exfat_lookup(&ef, &node, path);
  62. if (rc != 0)
  63. return rc;
  64. rc = exfat_truncate(&ef, node, size);
  65. exfat_put_node(&ef, node);
  66. return rc;
  67. }
  68. static int fuse_exfat_readdir(const char* path, void* buffer,
  69. fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
  70. {
  71. struct exfat_node* parent;
  72. struct exfat_node* node;
  73. struct exfat_iterator it;
  74. int rc;
  75. char name[EXFAT_NAME_MAX + 1];
  76. exfat_debug("[%s] %s", __func__, path);
  77. rc = exfat_lookup(&ef, &parent, path);
  78. if (rc != 0)
  79. return rc;
  80. if (!(parent->flags & EXFAT_ATTRIB_DIR))
  81. {
  82. exfat_put_node(&ef, parent);
  83. exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
  84. return -ENOTDIR;
  85. }
  86. filler(buffer, ".", NULL, 0);
  87. filler(buffer, "..", NULL, 0);
  88. rc = exfat_opendir(&ef, parent, &it);
  89. if (rc != 0)
  90. {
  91. exfat_put_node(&ef, parent);
  92. exfat_error("failed to open directory `%s'", path);
  93. return rc;
  94. }
  95. while ((node = exfat_readdir(&ef, &it)))
  96. {
  97. exfat_get_name(node, name, EXFAT_NAME_MAX);
  98. exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
  99. name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
  100. node->size, node->start_cluster);
  101. filler(buffer, name, NULL, 0);
  102. exfat_put_node(&ef, node);
  103. }
  104. exfat_closedir(&ef, &it);
  105. exfat_put_node(&ef, parent);
  106. return 0;
  107. }
  108. static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
  109. {
  110. struct exfat_node* node;
  111. int rc;
  112. exfat_debug("[%s] %s", __func__, path);
  113. rc = exfat_lookup(&ef, &node, path);
  114. if (rc != 0)
  115. return rc;
  116. set_node(fi, node);
  117. fi->keep_cache = 1;
  118. return 0;
  119. }
  120. static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
  121. {
  122. exfat_debug("[%s] %s", __func__, path);
  123. exfat_put_node(&ef, get_node(fi));
  124. return 0;
  125. }
  126. static int fuse_exfat_read(const char* path, char* buffer, size_t size,
  127. off_t offset, struct fuse_file_info* fi)
  128. {
  129. ssize_t ret;
  130. exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
  131. ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
  132. if (ret < 0)
  133. return -EIO;
  134. return ret;
  135. }
  136. static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
  137. off_t offset, struct fuse_file_info* fi)
  138. {
  139. ssize_t ret;
  140. exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
  141. ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
  142. if (ret < 0)
  143. return -EIO;
  144. return ret;
  145. }
  146. static int fuse_exfat_unlink(const char* path)
  147. {
  148. struct exfat_node* node;
  149. int rc;
  150. exfat_debug("[%s] %s", __func__, path);
  151. rc = exfat_lookup(&ef, &node, path);
  152. if (rc != 0)
  153. return rc;
  154. rc = exfat_unlink(&ef, node);
  155. exfat_put_node(&ef, node);
  156. return rc;
  157. }
  158. static int fuse_exfat_rmdir(const char* path)
  159. {
  160. struct exfat_node* node;
  161. int rc;
  162. exfat_debug("[%s] %s", __func__, path);
  163. rc = exfat_lookup(&ef, &node, path);
  164. if (rc != 0)
  165. return rc;
  166. rc = exfat_rmdir(&ef, node);
  167. exfat_put_node(&ef, node);
  168. return rc;
  169. }
  170. static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
  171. {
  172. exfat_debug("[%s] %s 0%ho", __func__, path, mode);
  173. return exfat_mknod(&ef, path);
  174. }
  175. static int fuse_exfat_mkdir(const char* path, mode_t mode)
  176. {
  177. exfat_debug("[%s] %s 0%ho", __func__, path, mode);
  178. return exfat_mkdir(&ef, path);
  179. }
  180. static int fuse_exfat_rename(const char* old_path, const char* new_path)
  181. {
  182. exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
  183. return exfat_rename(&ef, old_path, new_path);
  184. }
  185. static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
  186. {
  187. struct exfat_node* node;
  188. int rc;
  189. exfat_debug("[%s] %s", __func__, path);
  190. rc = exfat_lookup(&ef, &node, path);
  191. if (rc != 0)
  192. return rc;
  193. exfat_utimes(node, tv);
  194. exfat_put_node(&ef, node);
  195. return 0;
  196. }
  197. #ifdef __APPLE__
  198. static int fuse_exfat_chmod(const char* path, mode_t mode)
  199. {
  200. exfat_debug("[%s] %s 0%ho", __func__, path, mode);
  201. /* make OS X utilities happy */
  202. return 0;
  203. }
  204. #endif
  205. static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
  206. {
  207. exfat_debug("[%s]", __func__);
  208. sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
  209. sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
  210. sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
  211. sfs->f_bavail = exfat_count_free_clusters(&ef);
  212. sfs->f_bfree = sfs->f_bavail;
  213. sfs->f_namemax = EXFAT_NAME_MAX;
  214. /*
  215. Below are fake values because in exFAT there is
  216. a) no simple way to count files;
  217. b) no such thing as inode;
  218. So here we assume that inode = cluster.
  219. */
  220. sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
  221. sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
  222. sfs->f_ffree = sfs->f_bavail;
  223. return 0;
  224. }
  225. static void* fuse_exfat_init(struct fuse_conn_info* fci)
  226. {
  227. exfat_debug("[%s]", __func__);
  228. #ifdef FUSE_CAP_BIG_WRITES
  229. fci->want |= FUSE_CAP_BIG_WRITES;
  230. #endif
  231. return NULL;
  232. }
  233. static void fuse_exfat_destroy(void* unused)
  234. {
  235. exfat_debug("[%s]", __func__);
  236. exfat_unmount(&ef);
  237. }
  238. static void usage(const char* prog)
  239. {
  240. fprintf(stderr, "Usage: %s [-d] [-o options] [-v] <device> <dir>\n", prog);
  241. exit(1);
  242. }
  243. static struct fuse_operations fuse_exfat_ops =
  244. {
  245. .getattr = fuse_exfat_getattr,
  246. .truncate = fuse_exfat_truncate,
  247. .readdir = fuse_exfat_readdir,
  248. .open = fuse_exfat_open,
  249. .release = fuse_exfat_release,
  250. .read = fuse_exfat_read,
  251. .write = fuse_exfat_write,
  252. .unlink = fuse_exfat_unlink,
  253. .rmdir = fuse_exfat_rmdir,
  254. .mknod = fuse_exfat_mknod,
  255. .mkdir = fuse_exfat_mkdir,
  256. .rename = fuse_exfat_rename,
  257. .utimens = fuse_exfat_utimens,
  258. #ifdef __APPLE__
  259. .chmod = fuse_exfat_chmod,
  260. #endif
  261. .statfs = fuse_exfat_statfs,
  262. .init = fuse_exfat_init,
  263. .destroy = fuse_exfat_destroy,
  264. };
  265. static char* add_option(char* options, const char* name, const char* value)
  266. {
  267. size_t size;
  268. if (value)
  269. size = strlen(options) + strlen(name) + strlen(value) + 3;
  270. else
  271. size = strlen(options) + strlen(name) + 2;
  272. options = realloc(options, size);
  273. if (options == NULL)
  274. {
  275. exfat_error("failed to reallocate options string");
  276. return NULL;
  277. }
  278. strcat(options, ",");
  279. strcat(options, name);
  280. if (value)
  281. {
  282. strcat(options, "=");
  283. strcat(options, value);
  284. }
  285. return options;
  286. }
  287. static char* add_fsname_option(char* options, const char* spec)
  288. {
  289. char* spec_abs = realpath(spec, NULL);
  290. if (spec_abs == NULL)
  291. {
  292. free(options);
  293. exfat_error("failed to get absolute path for `%s'", spec);
  294. return NULL;
  295. }
  296. options = add_option(options, "fsname", spec_abs);
  297. free(spec_abs);
  298. return options;
  299. }
  300. static char* add_user_option(char* options)
  301. {
  302. struct passwd* pw;
  303. if (getuid() == 0)
  304. return options;
  305. pw = getpwuid(getuid());
  306. if (pw == NULL || pw->pw_name == NULL)
  307. {
  308. free(options);
  309. exfat_error("failed to determine username");
  310. return NULL;
  311. }
  312. return add_option(options, "user", pw->pw_name);
  313. }
  314. static char* add_blksize_option(char* options, long cluster_size)
  315. {
  316. long page_size = sysconf(_SC_PAGESIZE);
  317. char blksize[20];
  318. if (page_size < 1)
  319. page_size = 0x1000;
  320. snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
  321. return add_option(options, "blksize", blksize);
  322. }
  323. static char* add_fuse_options(char* options, const char* spec)
  324. {
  325. options = add_fsname_option(options, spec);
  326. if (options == NULL)
  327. return NULL;
  328. options = add_user_option(options);
  329. if (options == NULL)
  330. return NULL;
  331. options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
  332. if (options == NULL)
  333. return NULL;
  334. return options;
  335. }
  336. int main(int argc, char* argv[])
  337. {
  338. struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
  339. struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
  340. const char* spec = NULL;
  341. const char* mount_point = NULL;
  342. char* mount_options;
  343. int debug = 0;
  344. struct fuse_chan* fc = NULL;
  345. struct fuse* fh = NULL;
  346. char** pp;
  347. printf("FUSE exfat %u.%u.%u\n",
  348. EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
  349. mount_options = strdup(default_options);
  350. if (mount_options == NULL)
  351. {
  352. exfat_error("failed to allocate options string");
  353. return 1;
  354. }
  355. for (pp = argv + 1; *pp; pp++)
  356. {
  357. if (strcmp(*pp, "-o") == 0)
  358. {
  359. pp++;
  360. if (*pp == NULL)
  361. usage(argv[0]);
  362. mount_options = add_option(mount_options, *pp, NULL);
  363. if (mount_options == NULL)
  364. return 1;
  365. }
  366. else if (strcmp(*pp, "-d") == 0)
  367. debug = 1;
  368. else if (strcmp(*pp, "-v") == 0)
  369. {
  370. free(mount_options);
  371. puts("Copyright (C) 2010-2013 Andrew Nayenko");
  372. return 0;
  373. }
  374. else if (spec == NULL)
  375. spec = *pp;
  376. else if (mount_point == NULL)
  377. mount_point = *pp;
  378. else
  379. {
  380. free(mount_options);
  381. usage(argv[0]);
  382. }
  383. }
  384. if (spec == NULL || mount_point == NULL)
  385. {
  386. free(mount_options);
  387. usage(argv[0]);
  388. }
  389. if (exfat_mount(&ef, spec, mount_options) != 0)
  390. {
  391. free(mount_options);
  392. return 1;
  393. }
  394. if (ef.ro == -1) /* read-only fallback was used */
  395. {
  396. mount_options = add_option(mount_options, "ro", NULL);
  397. if (mount_options == NULL)
  398. {
  399. exfat_unmount(&ef);
  400. return 1;
  401. }
  402. }
  403. mount_options = add_fuse_options(mount_options, spec);
  404. if (mount_options == NULL)
  405. {
  406. exfat_unmount(&ef);
  407. return 1;
  408. }
  409. /* create arguments for fuse_mount() */
  410. if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
  411. fuse_opt_add_arg(&mount_args, "-o") != 0 ||
  412. fuse_opt_add_arg(&mount_args, mount_options) != 0)
  413. {
  414. exfat_unmount(&ef);
  415. free(mount_options);
  416. return 1;
  417. }
  418. free(mount_options);
  419. /* create FUSE mount point */
  420. fc = fuse_mount(mount_point, &mount_args);
  421. fuse_opt_free_args(&mount_args);
  422. if (fc == NULL)
  423. {
  424. exfat_unmount(&ef);
  425. return 1;
  426. }
  427. /* create arguments for fuse_new() */
  428. if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
  429. (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
  430. {
  431. fuse_unmount(mount_point, fc);
  432. exfat_unmount(&ef);
  433. return 1;
  434. }
  435. /* create new FUSE file system */
  436. fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
  437. sizeof(struct fuse_operations), NULL);
  438. fuse_opt_free_args(&newfs_args);
  439. if (fh == NULL)
  440. {
  441. fuse_unmount(mount_point, fc);
  442. exfat_unmount(&ef);
  443. return 1;
  444. }
  445. /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
  446. if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
  447. {
  448. fuse_unmount(mount_point, fc);
  449. fuse_destroy(fh);
  450. exfat_unmount(&ef);
  451. exfat_error("failed to set signal handlers");
  452. return 1;
  453. }
  454. /* go to background (unless "-d" option is passed) and run FUSE
  455. main loop */
  456. if (fuse_daemonize(debug) == 0)
  457. {
  458. if (fuse_loop(fh) != 0)
  459. exfat_error("FUSE loop failure");
  460. }
  461. else
  462. exfat_error("failed to daemonize");
  463. fuse_remove_signal_handlers(fuse_get_session(fh));
  464. /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
  465. fuse_unmount(mount_point, fc);
  466. fuse_destroy(fh);
  467. return 0;
  468. }