cache the size of the sidecar_dir path
[rrq/fuse_xattrs.git] / passthrough.c
index 97c178e3a7d80c05579397d396de536a3c447dce..6d00593c7993853c4840bd0d7d9e53dab17984f1 100644 (file)
@@ -1,7 +1,7 @@
 /*
   fuse_xattrs - Add xattrs support using sidecar files
 
-  Copyright (C) 2016  Felipe Barriga Richards <felipe {at} felipebarriga.cl>
+  Copyright (C) 2016-2017  Felipe Barriga Richards <felipe {at} felipebarriga.cl>
 
   Based on passthrough.c (libfuse example)
 
 /* For pread()/pwrite()/utimensat() */
 #define _XOPEN_SOURCE 700
 
-#include <fuse.h>
+#ifdef __APPLE__
+    #include <osxfuse/fuse.h>
+#else
+    #include <fuse.h>
+#endif
+
 #include <stdio.h>
 #include <string.h>
+#include <stdlib.h>
 #include <unistd.h>
 #include <dirent.h>
 #include <errno.h>
 
+#include "xattrs_config.h"
+#include "utils.h"
+#include "stringmem.h"
+
 int xmp_getattr(const char *path, struct stat *stbuf) {
     int res;
 
-    res = lstat(path, stbuf);
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = lstat(_path, stbuf);
+    strfree(_path);
+
     if (res == -1)
         return -errno;
 
@@ -33,8 +50,14 @@ int xmp_getattr(const char *path, struct stat *stbuf) {
 
 int xmp_access(const char *path, int mask) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = access(_path, mask);
+    strfree(_path);
 
-    res = access(path, mask);
     if (res == -1)
         return -errno;
 
@@ -43,8 +66,14 @@ int xmp_access(const char *path, int mask) {
 
 int xmp_readlink(const char *path, char *buf, size_t size) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = readlink(_path, buf, size - 1);
+    strfree(_path);
 
-    res = readlink(path, buf, size - 1);
     if (res == -1)
         return -errno;
 
@@ -59,13 +88,23 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
     struct dirent *de;
 
     (void) offset;
-    (void) fi;
 
-    dp = opendir(path);
+    if (fi != NULL && fi->fh != 0) {
+        dp = fdopendir(fi->fh);
+    } else {
+        char *_path = prepend_source_directory(path);
+        dp = opendir(_path);
+        strfree(_path);
+    }
+
     if (dp == NULL)
         return -errno;
 
     while ((de = readdir(dp)) != NULL) {
+        if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(de->d_name) == 1) {
+            continue;
+        }
+
         struct stat st;
         memset(&st, 0, sizeof(st));
         st.st_ino = de->d_ino;
@@ -80,17 +119,24 @@ int xmp_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
 
 int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
 
     /* On Linux this could just be 'mknod(path, mode, rdev)' but this
        is more portable */
     if (S_ISREG(mode)) {
-        res = open(path, O_CREAT | O_EXCL | O_WRONLY, mode);
+        res = open(_path, O_CREAT | O_EXCL | O_WRONLY, mode);
         if (res >= 0)
             res = close(res);
     } else if (S_ISFIFO(mode))
-        res = mkfifo(path, mode);
+        res = mkfifo(_path, mode);
     else
-        res = mknod(path, mode, rdev);
+        res = mknod(_path, mode, rdev);
+
+    strfree(_path);
     if (res == -1)
         return -errno;
 
@@ -99,8 +145,14 @@ int xmp_mknod(const char *path, mode_t mode, dev_t rdev) {
 
 int xmp_mkdir(const char *path, mode_t mode) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = mkdir(_path, mode);
+    strfree(_path);
 
-    res = mkdir(path, mode);
     if (res == -1)
         return -errno;
 
@@ -109,18 +161,41 @@ int xmp_mkdir(const char *path, mode_t mode) {
 
 int xmp_unlink(const char *path) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
 
-    res = unlink(path);
-    if (res == -1)
+    char *_path = prepend_source_directory(path);
+    res = unlink(_path);
+
+    if (res == -1) {
+        strfree(_path);
         return -errno;
+    }
+
+    char *sidecar_path = get_sidecar_path(_path);
+    if (is_regular_file(sidecar_path)) {
+        if (unlink(sidecar_path) == -1) {
+            error_print("Error removing sidecar file: %s\n", sidecar_path);
+        }
+    }
+    strfree(sidecar_path);
+    strfree(_path);
 
     return 0;
 }
 
+// FIXME: remove sidecar
 int xmp_rmdir(const char *path) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = rmdir(_path);
+    strfree(_path);
 
-    res = rmdir(path);
     if (res == -1)
         return -errno;
 
@@ -129,8 +204,16 @@ int xmp_rmdir(const char *path) {
 
 int xmp_symlink(const char *from, const char *to) {
     int res;
+    if (xattrs_config.show_sidecar == 0) {
+        if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
+            return -ENOENT;
+        }
+    }
+
+    char *_to = prepend_source_directory(to);
+    res = symlink(from, _to);
+    strfree(_to);
 
-    res = symlink(from, to);
     if (res == -1)
         return -errno;
 
@@ -139,18 +222,55 @@ int xmp_symlink(const char *from, const char *to) {
 
 int xmp_rename(const char *from, const char *to) {
     int res;
+    if (xattrs_config.show_sidecar == 0) {
+        if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
+            return -ENOENT;
+        }
+    }
 
-    res = rename(from, to);
-    if (res == -1)
+    char *_from = prepend_source_directory(from);
+    char *_to = prepend_source_directory(to);
+    res = rename(_from, _to);
+
+    if (res == -1) {
+        strfree(_from);
+        strfree(_to);
         return -errno;
+    }
+
+    char *from_sidecar_path = get_sidecar_path(_from);
+    char *to_sidecar_path = get_sidecar_path(_to);
+
+    // FIXME: Remove to_sidecar_path if it exists ?
+    if (is_regular_file(from_sidecar_path)) {
+        if (rename(from_sidecar_path, to_sidecar_path) == -1) {
+            error_print("Error renaming sidecar. from: %s to: %s\n", from_sidecar_path, to_sidecar_path);
+        }
+    }
+    strfree(from_sidecar_path);
+    strfree(to_sidecar_path);
+
+    strfree(_from);
+    strfree(_to);
 
     return 0;
 }
 
+// TODO: handle sidecar file ?
 int xmp_link(const char *from, const char *to) {
     int res;
+    if (xattrs_config.show_sidecar == 0) {
+        if (filename_is_sidecar(from) == 1 || filename_is_sidecar(to)) {
+            return -ENOENT;
+        }
+    }
+
+    char *_from = prepend_source_directory(from);
+    char *_to = prepend_source_directory(to);
+    res = link(_from, _to);
+    strfree(_from);
+    strfree(_to);
 
-    res = link(from, to);
     if (res == -1)
         return -errno;
 
@@ -159,8 +279,14 @@ int xmp_link(const char *from, const char *to) {
 
 int xmp_chmod(const char *path, mode_t mode) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = chmod(_path, mode);
+    strfree(_path);
 
-    res = chmod(path, mode);
     if (res == -1)
         return -errno;
 
@@ -169,8 +295,14 @@ int xmp_chmod(const char *path, mode_t mode) {
 
 int xmp_chown(const char *path, uid_t uid, gid_t gid) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = lchown(_path, uid, gid);
+    strfree(_path);
 
-    res = lchown(path, uid, gid);
     if (res == -1)
         return -errno;
 
@@ -179,8 +311,14 @@ int xmp_chown(const char *path, uid_t uid, gid_t gid) {
 
 int xmp_truncate(const char *path, off_t size) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = truncate(_path, size);
+    strfree(_path);
 
-    res = truncate(path, size);
     if (res == -1)
         return -errno;
 
@@ -188,14 +326,17 @@ int xmp_truncate(const char *path, off_t size) {
 }
 
 #ifdef HAVE_UTIMENSAT
-int xmp_utimens(const char *path, const struct timespec ts[2],
-struct fuse_file_info *fi)
-{
-    (void) fi;
+int xmp_utimens(const char *path, const struct timespec ts[2]) {
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
     int res;
 
+    char *_path = prepend_source_directory(path);
     /* don't use utime/utimes since they follow symlinks */
-    res = utimensat(0, path, ts, AT_SYMLINK_NOFOLLOW);
+    res = utimensat(0, _path, ts, AT_SYMLINK_NOFOLLOW);
+    strfree(_path);
     if (res == -1)
         return -errno;
 
@@ -204,56 +345,62 @@ struct fuse_file_info *fi)
 #endif
 
 int xmp_open(const char *path, struct fuse_file_info *fi) {
-    int res;
+    int fd;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
 
-    res = open(path, fi->flags);
-    if (res == -1)
+    char *_path = prepend_source_directory(path);
+    fd = open(_path, fi->flags);
+    strfree(_path);
+
+    if (fd == -1)
         return -errno;
 
-    close(res);
+    fi->fh = fd;
     return 0;
 }
 
 int xmp_read(const char *path, char *buf, size_t size, off_t offset,
-             struct fuse_file_info *fi) {
-    int fd;
-    int res;
-
-    (void) fi;
-    fd = open(path, O_RDONLY);
-    if (fd == -1)
-        return -errno;
+             struct fuse_file_info *fi)
+{
+    (void) path;
+    if (fi == NULL || fi->fh == 0) {
+        return -1;
+    }
 
-    res = pread(fd, buf, size, offset);
+    int res = pread(fi->fh, buf, size, offset);
     if (res == -1)
         res = -errno;
 
-    close(fd);
     return res;
 }
 
 int xmp_write(const char *path, const char *buf, size_t size,
-              off_t offset, struct fuse_file_info *fi) {
-    int fd;
-    int res;
-
-    (void) fi;
-    fd = open(path, O_WRONLY);
-    if (fd == -1)
-        return -errno;
+              off_t offset, struct fuse_file_info *fi)
+{
+    (void) path;
+    if (fi == NULL || fi->fh == 0) {
+        return -1;
+    }
 
-    res = pwrite(fd, buf, size, offset);
+    int res = pwrite(fi->fh, buf, size, offset);
     if (res == -1)
         res = -errno;
 
-    close(fd);
     return res;
 }
 
 int xmp_statfs(const char *path, struct statvfs *stbuf) {
     int res;
+    if (xattrs_config.show_sidecar == 0 && filename_is_sidecar(path) == 1)  {
+        return -ENOENT;
+    }
+
+    char *_path = prepend_source_directory(path);
+    res = statvfs(_path, stbuf);
+    strfree(_path);
 
-    res = statvfs(path, stbuf);
     if (res == -1)
         return -errno;
 
@@ -261,12 +408,8 @@ int xmp_statfs(const char *path, struct statvfs *stbuf) {
 }
 
 int xmp_release(const char *path, struct fuse_file_info *fi) {
-    /* Just a stub.     This method is optional and can safely be left
-       unimplemented */
-
     (void) path;
-    (void) fi;
-    return 0;
+    return close(fi->fh);
 }
 
 int xmp_fsync(const char *path, int isdatasync,
@@ -284,21 +427,16 @@ int xmp_fsync(const char *path, int isdatasync,
 int xmp_fallocate(const char *path, int mode,
                   off_t offset, off_t length, struct fuse_file_info *fi)
 {
-    int fd;
-    int res;
-
-    (void) fi;
+    (void) path;
+    if (fi == NULL || fi->fh == 0) {
+        return -1;
+    }
 
+    int res;
     if (mode)
         return -EOPNOTSUPP;
 
-    fd = open(path, O_WRONLY);
-    if (fd == -1)
-        return -errno;
-
-    res = -posix_fallocate(fd, offset, length);
-
-    close(fd);
+    res = -posix_fallocate(fi->fh, offset, length);
     return res;
 }
 #endif