2 fuse_xattrs - Add xattrs support using sidecar files
4 Copyright (C) 2016-2017 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 "xattrs_config.h"
28 int xmp_getattr(const char *path, struct stat *stbuf) {
31 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
35 char *_path = prepend_source_directory(path);
36 res = lstat(_path, stbuf);
45 int xmp_access(const char *path, int mask) {
47 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
51 char *_path = prepend_source_directory(path);
52 res = access(_path, mask);
61 int xmp_readlink(const char *path, char *buf, size_t size) {
63 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
67 char *_path = prepend_source_directory(path);
68 res = readlink(_path, buf, size - 1);
78 int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
79 off_t offset, struct fuse_file_info *fi)
86 if (fi != NULL && fi->fh != 0) {
87 dp = fdopendir(fi->fh);
89 char *_path = prepend_source_directory(path);
97 while ((de = readdir(dp)) != NULL) {
98 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(de->d_name) == 1) {
103 memset(&st, 0, sizeof(st));
104 st.st_ino = de->d_ino;
105 st.st_mode = de->d_type << 12;
106 if (filler(buf, de->d_name, &st, 0))
114 int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
116 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
120 char *_path = prepend_source_directory(path);
122 /* On Linux this could just be 'mknod(path, mode, rdev)' but this
125 res = open(_path, O_CREAT | O_EXCL | O_WRONLY, mode);
128 } else if (S_ISFIFO(mode))
129 res = mkfifo(_path, mode);
131 res = mknod(_path, mode, rdev);
140 int xmp_mkdir(const char *path, mode_t mode) {
142 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
146 char *_path = prepend_source_directory(path);
147 res = mkdir(_path, mode);
156 int xmp_unlink(const char *path) {
158 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
162 char *_path = prepend_source_directory(path);
172 int xmp_rmdir(const char *path) {
174 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
178 char *_path = prepend_source_directory(path);
188 int xmp_symlink(const char *from, const char *to) {
190 if (xattrs_config.show_sidecar == 0) {
191 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
196 char *_to = prepend_source_directory(to);
197 res = symlink(from, _to);
206 int xmp_rename(const char *from, const char *to) {
208 if (xattrs_config.show_sidecar == 0) {
209 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
214 char *_from = prepend_source_directory(from);
215 char *_to = prepend_source_directory(to);
216 res = rename(_from, _to);
226 int xmp_link(const char *from, const char *to) {
228 if (xattrs_config.show_sidecar == 0) {
229 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
234 char *_from = prepend_source_directory(from);
235 char *_to = prepend_source_directory(to);
236 res = link(_from, _to);
246 int xmp_chmod(const char *path, mode_t mode) {
248 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
252 char *_path = prepend_source_directory(path);
253 res = chmod(_path, mode);
262 int xmp_chown(const char *path, uid_t uid, gid_t gid) {
264 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
268 char *_path = prepend_source_directory(path);
269 res = lchown(_path, uid, gid);
278 int xmp_truncate(const char *path, off_t size) {
280 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
284 char *_path = prepend_source_directory(path);
285 res = truncate(_path, size);
294 #ifdef HAVE_UTIMENSAT
295 int xmp_utimens(const char *path, const struct timespec ts[2],
296 struct fuse_file_info *fi)
298 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
305 char *_path = prepend_source_directory(path);
306 /* don't use utime/utimes since they follow symlinks */
307 res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW);
316 int xmp_open(const char *path, struct fuse_file_info *fi) {
318 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
322 char *_path = prepend_source_directory(path);
323 fd = open(_path, fi->flags);
333 int xmp_read(const char *path, char *buf, size_t size, off_t offset,
334 struct fuse_file_info *fi)
337 if (fi == NULL || fi->fh == 0) {
341 int res = pread(fi->fh, buf, size, offset);
348 int xmp_write(const char *path, const char *buf, size_t size,
349 off_t offset, struct fuse_file_info *fi)
352 if (fi == NULL || fi->fh == 0) {
356 int res = pwrite(fi->fh, buf, size, offset);
363 int xmp_statfs(const char *path, struct statvfs *stbuf) {
365 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
369 char *_path = prepend_source_directory(path);
370 res = statvfs(_path, stbuf);
379 int xmp_release(const char *path, struct fuse_file_info *fi) {
381 return close(fi->fh);
384 int xmp_fsync(const char *path, int isdatasync,
385 struct fuse_file_info *fi) {
386 /* Just a stub. This method is optional and can safely be left
395 #ifdef HAVE_POSIX_FALLOCATE
396 int xmp_fallocate(const char *path, int mode,
397 off_t offset, off_t length, struct fuse_file_info *fi)
400 if (fi == NULL || fi->fh == 0) {
408 res = -posix_fallocate(fi->fh, offset, length);