Browse Source

move rgpgfs_mkdirs() to new src/fs.c

Fabian Peter Hammerle 5 years ago
parent
commit
6ad87a5e29
5 changed files with 57 additions and 33 deletions
  1. 1 0
      .gitignore
  2. 15 7
      Makefile
  3. 12 26
      rgpgfs.c
  4. 26 0
      src/fs.c
  5. 3 0
      src/fs.h

+ 1 - 0
.gitignore

@@ -1 +1,2 @@
+*.o
 /rgpgfs

+ 15 - 7
Makefile

@@ -1,11 +1,19 @@
-SOURCES = *.c
+.PHONY = default format
 
-.PHONY = format
+default : rgpgfs
 
-rgpgfs : $(SOURCES)
-	gcc -Wall -Werror $^ -o $@ \
-		$(shell pkg-config fuse3 --cflags --libs) \
-		$(shell gpgme-config --cflags --libs)
+src/fs.o : src/fs.c src/fs.h
+	gcc -Wall -Werror -I"$(CURDIR)" -c $< -o $@
 
-format : $(SOURCES)
+rgpgfs.o : rgpgfs.c src/fs.h
+	gcc -Wall -Werror -c $< -o $@ \
+		$(shell pkg-config fuse3 --cflags) \
+		$(shell gpgme-config --cflags)
+
+rgpgfs : src/fs.o rgpgfs.o
+	gcc $^ -o $@ \
+		$(shell pkg-config fuse3 --libs) \
+		$(shell gpgme-config --libs)
+
+format : src/*.c rgpgfs.c
 	clang-format -i -verbose $^

+ 12 - 26
rgpgfs.c

@@ -1,7 +1,10 @@
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include "src/fs.h"
+
+// http://libfuse.github.io/doxygen/globals.html
+#define FUSE_USE_VERSION 31
+#include <fuse.h>
+// https://www.gnupg.org/documentation/manuals/gpgme/Function-and-Data-Index.html
+#include <gpgme.h>
 
 // posix
 #include <dirent.h>
@@ -9,11 +12,10 @@
 #include <sys/types.h>
 #include <unistd.h>
 
-// http://libfuse.github.io/doxygen/globals.html
-#define FUSE_USE_VERSION 31
-#include <fuse.h>
-// https://www.gnupg.org/documentation/manuals/gpgme/Function-and-Data-Index.html
-#include <gpgme.h>
+#include <errno.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
 
 #define FUSE_PATH_BUF_LEN 256
 static char cache_dir[] = "/tmp/rgpgfs-cache-XXXXXX";
@@ -24,22 +26,6 @@ static const char gpgme_recip_fpr[] =
     "1234567890ABCDEF1234567890ABCDEF12345678";
 static gpgme_key_t gpgme_recip_key;
 
-static int rgpgfs_mkdirs(char *path) {
-  char *delimiter = strrchr(path, '/');
-  if (delimiter == NULL) {
-    errno = ENOTSUP;
-    return 1;
-  }
-  *delimiter = '\0';
-  struct stat statbuf;
-  if (lstat(path, &statbuf) && (rgpgfs_mkdirs(path) || mkdir(path, S_IRWXU))) {
-    *delimiter = '/';
-    return 1;
-  }
-  *delimiter = '/';
-  return 0;
-}
-
 static int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data) {
   if (gpgme_data_seek(data, 0, SEEK_SET) != 0) {
     perror("rgpgfs_gpgme_data_to_file: failed to seek");
@@ -90,7 +76,7 @@ static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
   struct stat cache_stat;
   if (lstat(cache_path, &cache_stat) ||
       source_stat.st_mtim.tv_sec > cache_stat.st_mtim.tv_sec) {
-    if (rgpgfs_mkdirs(cache_path)) {
+    if (rgpgfs_fs_mkdirs(cache_path)) {
       perror("rgpgfs_encrypt: failed to create dirs");
       return 1;
     }

+ 26 - 0
src/fs.c

@@ -0,0 +1,26 @@
+#include "src/fs.h"
+
+// posix
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
+
+#include <errno.h>
+#include <string.h>
+
+int rgpgfs_fs_mkdirs(char *path) {
+  char *delimiter = strrchr(path, '/');
+  if (delimiter == NULL) {
+    errno = ENOTSUP;
+    return 1;
+  }
+  *delimiter = '\0';
+  struct stat statbuf;
+  if (lstat(path, &statbuf) &&
+      (rgpgfs_fs_mkdirs(path) || mkdir(path, S_IRWXU))) {
+    *delimiter = '/';
+    return 1;
+  }
+  *delimiter = '/';
+  return 0;
+}

+ 3 - 0
src/fs.h

@@ -0,0 +1,3 @@
+#pragma once
+
+int rgpgfs_fs_mkdirs(char *path);