2 fuse_xattrs - Add xattrs support using sidecar files
4 Copyright (C) 2016 Felipe Barriga Richards <felipe {at} felipebarriga.cl>
6 Based on passthrough.c (libfuse example)
8 This program can be distributed under the terms of the GNU GPL.
12 #define FUSE_USE_VERSION 30
14 /* For pread()/pwrite()/utimensat() */
15 #define _XOPEN_SOURCE 700
25 #include "fuse_xattrs_config.h"
28 int xmp_getattr(const char *path, struct stat *stbuf) {
31 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
32 res = lstat(_path, stbuf);
41 int xmp_access(const char *path, int mask) {
44 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
45 res = access(_path, mask);
54 int xmp_readlink(const char *path, char *buf, size_t size) {
57 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
58 res = readlink(_path, buf, size - 1);
68 int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
69 off_t offset, struct fuse_file_info *fi)
77 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
84 while ((de = readdir(dp)) != NULL) {
86 memset(&st, 0, sizeof(st));
87 st.st_ino = de->d_ino;
88 st.st_mode = de->d_type << 12;
89 if (filler(buf, de->d_name, &st, 0))
97 int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
99 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
101 /* On Linux this could just be 'mknod(path, mode, rdev)' but this
104 res = open(_path, O_CREAT | O_EXCL | O_WRONLY, mode);
107 } else if (S_ISFIFO(mode))
108 res = mkfifo(_path, mode);
110 res = mknod(_path, mode, rdev);
119 int xmp_mkdir(const char *path, mode_t mode) {
122 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
123 res = mkdir(_path, mode);
132 int xmp_unlink(const char *path) {
135 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
145 int xmp_rmdir(const char *path) {
148 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
158 int xmp_symlink(const char *from, const char *to) {
161 char *_to = prepend_source_directory(xattrs_config.source_dir, to);
162 res = symlink(from, _to);
171 int xmp_rename(const char *from, const char *to) {
174 char *_from = prepend_source_directory(xattrs_config.source_dir, from);
175 char *_to = prepend_source_directory(xattrs_config.source_dir, to);
176 res = rename(_from, _to);
186 int xmp_link(const char *from, const char *to) {
189 char *_from = prepend_source_directory(xattrs_config.source_dir, from);
190 char *_to = prepend_source_directory(xattrs_config.source_dir, to);
191 res = link(_from, _to);
201 int xmp_chmod(const char *path, mode_t mode) {
204 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
205 res = chmod(_path, mode);
214 int xmp_chown(const char *path, uid_t uid, gid_t gid) {
217 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
218 res = lchown(_path, uid, gid);
227 int xmp_truncate(const char *path, off_t size) {
230 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
231 res = truncate(_path, size);
240 #ifdef HAVE_UTIMENSAT
241 int xmp_utimens(const char *path, const struct timespec ts[2],
242 struct fuse_file_info *fi)
247 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
248 /* don't use utime/utimes since they follow symlinks */
249 res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW);
258 int xmp_open(const char *path, struct fuse_file_info *fi) {
261 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
262 res = open(_path, fi->flags);
272 int xmp_read(const char *path, char *buf, size_t size, off_t offset,
273 struct fuse_file_info *fi) {
278 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
279 fd = open(_path, O_RDONLY);
285 res = pread(fd, buf, size, offset);
293 int xmp_write(const char *path, const char *buf, size_t size,
294 off_t offset, struct fuse_file_info *fi) {
299 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
300 fd = open(_path, O_WRONLY);
306 res = pwrite(fd, buf, size, offset);
314 int xmp_statfs(const char *path, struct statvfs *stbuf) {
317 char *_path = prepend_source_directory(xattrs_config.source_dir, path);
318 res = statvfs(_path, stbuf);
327 int xmp_release(const char *path, struct fuse_file_info *fi) {
328 /* Just a stub. This method is optional and can safely be left
336 int xmp_fsync(const char *path, int isdatasync,
337 struct fuse_file_info *fi) {
338 /* Just a stub. This method is optional and can safely be left
347 #ifdef HAVE_POSIX_FALLOCATE
348 int xmp_fallocate(const char *path, int mode,
349 off_t offset, off_t length, struct fuse_file_info *fi)
359 char *_path = concat(xattrs_config.source_dir, path);
360 fd = open(_path, O_WRONLY);
366 res = -posix_fallocate(fd, offset, length);