You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

567 lines
9.8 KiB

  1. /*
  2. FUSE: Filesystem in Userspace
  3. Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
  4. Copyright (C) 2011 Sebastian Pipping <sebastian@pipping.org>
  5. This program can be distributed under the terms of the GNU GPL.
  6. See the file COPYING.
  7. gcc -Wall fusexmp_fh.c `pkg-config fuse --cflags --libs` -lulockmgr -o fusexmp_fh
  8. */
  9. #define FUSE_USE_VERSION 26
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <fuse.h>
  15. #include <ulockmgr.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <unistd.h>
  20. #include <fcntl.h>
  21. #include <sys/stat.h>
  22. #include <dirent.h>
  23. #include <errno.h>
  24. #include <sys/time.h>
  25. #ifdef HAVE_SETXATTR
  26. #include <sys/xattr.h>
  27. #endif
  28. #include <sys/file.h> /* flock(2) */
  29. static int xmp_getattr(const char *path, struct stat *stbuf)
  30. {
  31. int res;
  32. res = lstat(path, stbuf);
  33. if (res == -1)
  34. return -errno;
  35. return 0;
  36. }
  37. static int xmp_fgetattr(const char *path, struct stat *stbuf,
  38. struct fuse_file_info *fi)
  39. {
  40. int res;
  41. (void) path;
  42. res = fstat(fi->fh, stbuf);
  43. if (res == -1)
  44. return -errno;
  45. return 0;
  46. }
  47. static int xmp_access(const char *path, int mask)
  48. {
  49. int res;
  50. res = access(path, mask);
  51. if (res == -1)
  52. return -errno;
  53. return 0;
  54. }
  55. static int xmp_readlink(const char *path, char *buf, size_t size)
  56. {
  57. int res;
  58. res = readlink(path, buf, size - 1);
  59. if (res == -1)
  60. return -errno;
  61. buf[res] = '\0';
  62. return 0;
  63. }
  64. struct xmp_dirp {
  65. DIR *dp;
  66. struct dirent *entry;
  67. off_t offset;
  68. };
  69. static int xmp_opendir(const char *path, struct fuse_file_info *fi)
  70. {
  71. int res;
  72. struct xmp_dirp *d = malloc(sizeof(struct xmp_dirp));
  73. if (d == NULL)
  74. return -ENOMEM;
  75. d->dp = opendir(path);
  76. if (d->dp == NULL) {
  77. res = -errno;
  78. free(d);
  79. return res;
  80. }
  81. d->offset = 0;
  82. d->entry = NULL;
  83. fi->fh = (unsigned long) d;
  84. return 0;
  85. }
  86. static inline struct xmp_dirp *get_dirp(struct fuse_file_info *fi)
  87. {
  88. return (struct xmp_dirp *) (uintptr_t) fi->fh;
  89. }
  90. static int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
  91. off_t offset, struct fuse_file_info *fi)
  92. {
  93. struct xmp_dirp *d = get_dirp(fi);
  94. (void) path;
  95. if (offset != d->offset) {
  96. seekdir(d->dp, offset);
  97. d->entry = NULL;
  98. d->offset = offset;
  99. }
  100. while (1) {
  101. struct stat st;
  102. off_t nextoff;
  103. if (!d->entry) {
  104. d->entry = readdir(d->dp);
  105. if (!d->entry)
  106. break;
  107. }
  108. memset(&st, 0, sizeof(st));
  109. st.st_ino = d->entry->d_ino;
  110. st.st_mode = d->entry->d_type << 12;
  111. nextoff = telldir(d->dp);
  112. if (filler(buf, d->entry->d_name, &st, nextoff))
  113. break;
  114. d->entry = NULL;
  115. d->offset = nextoff;
  116. }
  117. return 0;
  118. }
  119. static int xmp_releasedir(const char *path, struct fuse_file_info *fi)
  120. {
  121. struct xmp_dirp *d = get_dirp(fi);
  122. (void) path;
  123. closedir(d->dp);
  124. free(d);
  125. return 0;
  126. }
  127. static int xmp_mknod(const char *path, mode_t mode, dev_t rdev)
  128. {
  129. int res;
  130. if (S_ISFIFO(mode))
  131. res = mkfifo(path, mode);
  132. else
  133. res = mknod(path, mode, rdev);
  134. if (res == -1)
  135. return -errno;
  136. return 0;
  137. }
  138. static int xmp_mkdir(const char *path, mode_t mode)
  139. {
  140. int res;
  141. res = mkdir(path, mode);
  142. if (res == -1)
  143. return -errno;
  144. return 0;
  145. }
  146. static int xmp_unlink(const char *path)
  147. {
  148. int res;
  149. res = unlink(path);
  150. if (res == -1)
  151. return -errno;
  152. return 0;
  153. }
  154. static int xmp_rmdir(const char *path)
  155. {
  156. int res;
  157. res = rmdir(path);
  158. if (res == -1)
  159. return -errno;
  160. return 0;
  161. }
  162. static int xmp_symlink(const char *from, const char *to)
  163. {
  164. int res;
  165. res = symlink(from, to);
  166. if (res == -1)
  167. return -errno;
  168. return 0;
  169. }
  170. static int xmp_rename(const char *from, const char *to)
  171. {
  172. int res;
  173. res = rename(from, to);
  174. if (res == -1)
  175. return -errno;
  176. return 0;
  177. }
  178. static int xmp_link(const char *from, const char *to)
  179. {
  180. int res;
  181. res = link(from, to);
  182. if (res == -1)
  183. return -errno;
  184. return 0;
  185. }
  186. static int xmp_chmod(const char *path, mode_t mode)
  187. {
  188. int res;
  189. res = chmod(path, mode);
  190. if (res == -1)
  191. return -errno;
  192. return 0;
  193. }
  194. static int xmp_chown(const char *path, uid_t uid, gid_t gid)
  195. {
  196. int res;
  197. res = lchown(path, uid, gid);
  198. if (res == -1)
  199. return -errno;
  200. return 0;
  201. }
  202. static int xmp_truncate(const char *path, off_t size)
  203. {
  204. int res;
  205. res = truncate(path, size);
  206. if (res == -1)
  207. return -errno;
  208. return 0;
  209. }
  210. static int xmp_ftruncate(const char *path, off_t size,
  211. struct fuse_file_info *fi)
  212. {
  213. int res;
  214. (void) path;
  215. res = ftruncate(fi->fh, size);
  216. if (res == -1)
  217. return -errno;
  218. return 0;
  219. }
  220. #ifdef HAVE_UTIMENSAT
  221. static int xmp_utimens(const char *path, const struct timespec ts[2])
  222. {
  223. int res;
  224. /* don't use utime/utimes since they follow symlinks */
  225. res = utimensat(0, path, ts, AT_SYMLINK_NOFOLLOW);
  226. if (res == -1)
  227. return -errno;
  228. return 0;
  229. }
  230. #endif
  231. static int xmp_create(const char *path, mode_t mode, struct fuse_file_info *fi)
  232. {
  233. int fd;
  234. fd = open(path, fi->flags, mode);
  235. if (fd == -1)
  236. return -errno;
  237. fi->fh = fd;
  238. return 0;
  239. }
  240. static int xmp_open(const char *path, struct fuse_file_info *fi)
  241. {
  242. int fd;
  243. fd = open(path, fi->flags);
  244. if (fd == -1)
  245. return -errno;
  246. fi->fh = fd;
  247. return 0;
  248. }
  249. static int xmp_read(const char *path, char *buf, size_t size, off_t offset,
  250. struct fuse_file_info *fi)
  251. {
  252. int res;
  253. (void) path;
  254. res = pread(fi->fh, buf, size, offset);
  255. if (res == -1)
  256. res = -errno;
  257. return res;
  258. }
  259. static int xmp_read_buf(const char *path, struct fuse_bufvec **bufp,
  260. size_t size, off_t offset, struct fuse_file_info *fi)
  261. {
  262. struct fuse_bufvec *src;
  263. (void) path;
  264. src = malloc(sizeof(struct fuse_bufvec));
  265. if (src == NULL)
  266. return -ENOMEM;
  267. *src = FUSE_BUFVEC_INIT(size);
  268. src->buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
  269. src->buf[0].fd = fi->fh;
  270. src->buf[0].pos = offset;
  271. *bufp = src;
  272. return 0;
  273. }
  274. static int xmp_write(const char *path, const char *buf, size_t size,
  275. off_t offset, struct fuse_file_info *fi)
  276. {
  277. int res;
  278. (void) path;
  279. res = pwrite(fi->fh, buf, size, offset);
  280. if (res == -1)
  281. res = -errno;
  282. return res;
  283. }
  284. static int xmp_write_buf(const char *path, struct fuse_bufvec *buf,
  285. off_t offset, struct fuse_file_info *fi)
  286. {
  287. struct fuse_bufvec dst = FUSE_BUFVEC_INIT(fuse_buf_size(buf));
  288. (void) path;
  289. dst.buf[0].flags = FUSE_BUF_IS_FD | FUSE_BUF_FD_SEEK;
  290. dst.buf[0].fd = fi->fh;
  291. dst.buf[0].pos = offset;
  292. return fuse_buf_copy(&dst, buf, FUSE_BUF_SPLICE_NONBLOCK);
  293. }
  294. static int xmp_statfs(const char *path, struct statvfs *stbuf)
  295. {
  296. int res;
  297. res = statvfs(path, stbuf);
  298. if (res == -1)
  299. return -errno;
  300. return 0;
  301. }
  302. static int xmp_flush(const char *path, struct fuse_file_info *fi)
  303. {
  304. int res;
  305. (void) path;
  306. /* This is called from every close on an open file, so call the
  307. close on the underlying filesystem. But since flush may be
  308. called multiple times for an open file, this must not really
  309. close the file. This is important if used on a network
  310. filesystem like NFS which flush the data/metadata on close() */
  311. res = close(dup(fi->fh));
  312. if (res == -1)
  313. return -errno;
  314. return 0;
  315. }
  316. static int xmp_release(const char *path, struct fuse_file_info *fi)
  317. {
  318. (void) path;
  319. close(fi->fh);
  320. return 0;
  321. }
  322. static int xmp_fsync(const char *path, int isdatasync,
  323. struct fuse_file_info *fi)
  324. {
  325. int res;
  326. (void) path;
  327. #ifndef HAVE_FDATASYNC
  328. (void) isdatasync;
  329. #else
  330. if (isdatasync)
  331. res = fdatasync(fi->fh);
  332. else
  333. #endif
  334. res = fsync(fi->fh);
  335. if (res == -1)
  336. return -errno;
  337. return 0;
  338. }
  339. #ifdef HAVE_POSIX_FALLOCATE
  340. static int xmp_fallocate(const char *path, int mode,
  341. off_t offset, off_t length, struct fuse_file_info *fi)
  342. {
  343. (void) path;
  344. if (mode)
  345. return -EOPNOTSUPP;
  346. return -posix_fallocate(fi->fh, offset, length);
  347. }
  348. #endif
  349. #ifdef HAVE_SETXATTR
  350. /* xattr operations are optional and can safely be left unimplemented */
  351. static int xmp_setxattr(const char *path, const char *name, const char *value,
  352. size_t size, int flags)
  353. {
  354. int res = lsetxattr(path, name, value, size, flags);
  355. if (res == -1)
  356. return -errno;
  357. return 0;
  358. }
  359. static int xmp_getxattr(const char *path, const char *name, char *value,
  360. size_t size)
  361. {
  362. int res = lgetxattr(path, name, value, size);
  363. if (res == -1)
  364. return -errno;
  365. return res;
  366. }
  367. static int xmp_listxattr(const char *path, char *list, size_t size)
  368. {
  369. int res = llistxattr(path, list, size);
  370. if (res == -1)
  371. return -errno;
  372. return res;
  373. }
  374. static int xmp_removexattr(const char *path, const char *name)
  375. {
  376. int res = lremovexattr(path, name);
  377. if (res == -1)
  378. return -errno;
  379. return 0;
  380. }
  381. #endif /* HAVE_SETXATTR */
  382. static int xmp_lock(const char *path, struct fuse_file_info *fi, int cmd,
  383. struct flock *lock)
  384. {
  385. (void) path;
  386. return ulockmgr_op(fi->fh, cmd, lock, &fi->lock_owner,
  387. sizeof(fi->lock_owner));
  388. }
  389. static int xmp_flock(const char *path, struct fuse_file_info *fi, int op)
  390. {
  391. int res;
  392. (void) path;
  393. res = flock(fi->fh, op);
  394. if (res == -1)
  395. return -errno;
  396. return 0;
  397. }
  398. static struct fuse_operations xmp_oper = {
  399. .getattr = xmp_getattr,
  400. .fgetattr = xmp_fgetattr,
  401. .access = xmp_access,
  402. .readlink = xmp_readlink,
  403. .opendir = xmp_opendir,
  404. .readdir = xmp_readdir,
  405. .releasedir = xmp_releasedir,
  406. .mknod = xmp_mknod,
  407. .mkdir = xmp_mkdir,
  408. .symlink = xmp_symlink,
  409. .unlink = xmp_unlink,
  410. .rmdir = xmp_rmdir,
  411. .rename = xmp_rename,
  412. .link = xmp_link,
  413. .chmod = xmp_chmod,
  414. .chown = xmp_chown,
  415. .truncate = xmp_truncate,
  416. .ftruncate = xmp_ftruncate,
  417. #ifdef HAVE_UTIMENSAT
  418. .utimens = xmp_utimens,
  419. #endif
  420. .create = xmp_create,
  421. .open = xmp_open,
  422. .read = xmp_read,
  423. .read_buf = xmp_read_buf,
  424. .write = xmp_write,
  425. .write_buf = xmp_write_buf,
  426. .statfs = xmp_statfs,
  427. .flush = xmp_flush,
  428. .release = xmp_release,
  429. .fsync = xmp_fsync,
  430. #ifdef HAVE_POSIX_FALLOCATE
  431. .fallocate = xmp_fallocate,
  432. #endif
  433. #ifdef HAVE_SETXATTR
  434. .setxattr = xmp_setxattr,
  435. .getxattr = xmp_getxattr,
  436. .listxattr = xmp_listxattr,
  437. .removexattr = xmp_removexattr,
  438. #endif
  439. .lock = xmp_lock,
  440. .flock = xmp_flock,
  441. .flag_nullpath_ok = 1,
  442. #if HAVE_UTIMENSAT
  443. .flag_utime_omit_ok = 1,
  444. #endif
  445. };
  446. int main(int argc, char *argv[])
  447. {
  448. umask(0);
  449. return fuse_main(argc, argv, &xmp_oper, NULL);
  450. }