tests.py: Check xattr module version. Fix to work with version xattr 0.9.1
[rrq/fuse_xattrs.git] / utils.c
diff --git a/utils.c b/utils.c
index f245e7e3e84849b0a62d57a9c5dd09ff872749bb..2f935ab83c02b4b326dcdcf491bdc47eeb570ece 100644 (file)
--- a/utils.c
+++ b/utils.c
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/stat.h>
 
 #include "utils.h"
 #include "fuse_xattrs_config.h"
+#include "xattrs_config.h"
 
-char *prepend_source_directory(const char *a, const char *b) {
-    size_t len = strlen(a) + strlen(b) + 1;
-    char *dst = (char*) malloc(sizeof(char) * len);
-    sprintf(dst, "%s%s", a, b);
+/* TODO: re-use memory to avoid calling malloc every time */
+char *prepend_source_directory(const char *b) {
+    const size_t b_size = strlen(b);
+    const size_t dst_len = xattrs_config.source_dir_size + b_size + 1;
+    char *dst = (char*) malloc(sizeof(char) * dst_len);
+
+    memcpy(dst, xattrs_config.source_dir, xattrs_config.source_dir_size);
+    memcpy(dst+xattrs_config.source_dir_size, b, b_size + 1); // include '\0'
+    //sprintf(dst, "%s%s", a, b);
 
     return dst;
 }
 
+int is_directory(const char *path) {
+    struct stat statbuf;
+    if (stat(path, &statbuf) != 0) {
+        fprintf(stderr, "cannot get source directory status: %s\n", path);
+        return -1;
+    }
+
+    if (!S_ISDIR(statbuf.st_mode)) {
+        fprintf(stderr, "source directory must be a directory: %s\n", path);
+        return -1;
+    }
+
+    return 1;
+}
+
+int is_regular_file(const char *path) {
+    struct stat statbuf;
+    if (stat(path, &statbuf) != 0) {
+        return -1;
+    }
+
+    if (!S_ISREG(statbuf.st_mode)) {
+        return -1;
+    }
+
+    return 1;
+}
+
 char *get_sidecar_path(const char *path)
 {
     const size_t path_len = strlen(path);