#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);