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);
170 char *sidecar_path = get_sidecar_path(_path);
171 if (is_regular_file(sidecar_path)) {
172 if (unlink(sidecar_path) == -1) {
173 error_print("Error removing sidecar file: %s\n", sidecar_path);
182 // FIXME: remove sidecar
183 int xmp_rmdir(const char *path) {
185 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
189 char *_path = prepend_source_directory(path);
199 int xmp_symlink(const char *from, const char *to) {
201 if (xattrs_config.show_sidecar == 0) {
202 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
207 char *_to = prepend_source_directory(to);
208 res = symlink(from, _to);
217 int xmp_rename(const char *from, const char *to) {
219 if (xattrs_config.show_sidecar == 0) {
220 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
225 char *_from = prepend_source_directory(from);
226 char *_to = prepend_source_directory(to);
227 res = rename(_from, _to);
235 char *from_sidecar_path = get_sidecar_path(_from);
236 char *to_sidecar_path = get_sidecar_path(_to);
238 // FIXME: Remove to_sidecar_path if it exists ?
239 if (is_regular_file(from_sidecar_path)) {
240 if (rename(from_sidecar_path, to_sidecar_path) == -1) {
241 error_print("Error renaming sidecar. from: %s to: %s\n", from_sidecar_path, to_sidecar_path);
244 free(from_sidecar_path);
245 free(to_sidecar_path);
253 // TODO: handle sidecar file ?
254 int xmp_link(const char *from, const char *to) {
256 if (xattrs_config.show_sidecar == 0) {
257 if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
262 char *_from = prepend_source_directory(from);
263 char *_to = prepend_source_directory(to);
264 res = link(_from, _to);
274 int xmp_chmod(const char *path, mode_t mode) {
276 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
280 char *_path = prepend_source_directory(path);
281 res = chmod(_path, mode);
290 int xmp_chown(const char *path, uid_t uid, gid_t gid) {
292 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
296 char *_path = prepend_source_directory(path);
297 res = lchown(_path, uid, gid);
306 int xmp_truncate(const char *path, off_t size) {
308 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
312 char *_path = prepend_source_directory(path);
313 res = truncate(_path, size);
322 #ifdef HAVE_UTIMENSAT
323 int xmp_utimens(const char *path, const struct timespec ts[2],
324 struct fuse_file_info *fi)
326 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
333 char *_path = prepend_source_directory(path);
334 /* don't use utime/utimes since they follow symlinks */
335 res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW);
344 int xmp_open(const char *path, struct fuse_file_info *fi) {
346 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
350 char *_path = prepend_source_directory(path);
351 fd = open(_path, fi->flags);
361 int xmp_read(const char *path, char *buf, size_t size, off_t offset,
362 struct fuse_file_info *fi)
365 if (fi == NULL || fi->fh == 0) {
369 int res = pread(fi->fh, buf, size, offset);
376 int xmp_write(const char *path, const char *buf, size_t size,
377 off_t offset, struct fuse_file_info *fi)
380 if (fi == NULL || fi->fh == 0) {
384 int res = pwrite(fi->fh, buf, size, offset);
391 int xmp_statfs(const char *path, struct statvfs *stbuf) {
393 if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1) {
397 char *_path = prepend_source_directory(path);
398 res = statvfs(_path, stbuf);
407 int xmp_release(const char *path, struct fuse_file_info *fi) {
409 return close(fi->fh);
412 int xmp_fsync(const char *path, int isdatasync,
413 struct fuse_file_info *fi) {
414 /* Just a stub. This method is optional and can safely be left
423 #ifdef HAVE_POSIX_FALLOCATE
424 int xmp_fallocate(const char *path, int mode,
425 off_t offset, off_t length, struct fuse_file_info *fi)
428 if (fi == NULL || fi->fh == 0) {
436 res = -posix_fallocate(fi->fh, offset, length);