Răsfoiți Sursa

refactor: create rgpgfs_gpgme_encrypt_data_to_file()

Fabian Peter Hammerle 6 ani în urmă
părinte
comite
090c09a2b5
4 a modificat fișierele cu 42 adăugiri și 18 ștergeri
  1. 1 1
      Makefile
  2. 27 0
      src/gpgme.c
  3. 5 0
      src/gpgme.h
  4. 9 17
      src/main.c

+ 1 - 1
Makefile

@@ -24,5 +24,5 @@ src/main.o : src/main.c src/fs.h src/gpgme.h
 rgpgfs : src/*.o
 	$(LD) $^ -o $@ $(LIBS)
 
-format : src/*.c
+format : src/*.h src/*.c
 	clang-format -i -verbose $^

+ 27 - 0
src/gpgme.c

@@ -33,3 +33,30 @@ int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data) {
   fclose(file);
   return 0;
 }
+
+int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,
+                                      gpgme_key_t recip_keys[],
+                                      gpgme_data_t plain_data,
+                                      const char *cipher_path) {
+  gpgme_data_t cipher_data;
+  if (gpgme_data_new(&cipher_data) != GPG_ERR_NO_ERROR) {
+    fprintf(stderr, "rgpgfs_gpgme_encrypt_data_to_file: failed to prepare "
+                    "cipher data container\n");
+    return 1;
+  }
+
+  int result = 0;
+
+  if (gpgme_op_encrypt(gpgme_ctx, recip_keys, 0, plain_data, cipher_data) !=
+      GPG_ERR_NO_ERROR) {
+    fprintf(stderr, "rgpgfs_gpgme_encrypt_data_to_file: failed to encrypt\n");
+    result = 1;
+  } else if (rgpgfs_gpgme_data_to_file(cipher_path, cipher_data)) {
+    fprintf(stderr, "rgpgfs_gpgme_encrypt_data_to_file: failed to write cipher "
+                    "data to disk\n");
+    result = 1;
+  }
+
+  gpgme_data_release(cipher_data);
+  return result;
+}

+ 5 - 0
src/gpgme.h

@@ -1,3 +1,8 @@
 #include <gpgme.h>
 
 int rgpgfs_gpgme_data_to_file(const char *path, gpgme_data_t data);
+
+int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,
+                                      gpgme_key_t recip_keys[],
+                                      gpgme_data_t plain_data,
+                                      const char *cipher_path);

+ 9 - 17
src/main.c

@@ -62,29 +62,21 @@ static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
               gpgme_source_read_err);
       return 1;
     }
-    gpgme_data_t cipher_data;
-    if (gpgme_data_new(&cipher_data) != GPG_ERR_NO_ERROR) {
-      fprintf(stderr,
-              "rgpgfs_encrypt: failed to prepare cipher data container\n");
-      gpgme_data_release(plain_data);
-      return 1;
-    }
     // list of recipients may implicitly include the default recipient
     // (GPGME_ENCRYPT_NO_ENCRYPT_TO)
     gpgme_key_t recip_keys[] = {gpgme_recip_key, NULL};
-    if (gpgme_op_encrypt(gpgme_ctx, recip_keys, 0, plain_data, cipher_data) ==
-        GPG_ERR_NO_ERROR) {
-      if (rgpgfs_gpgme_data_to_file(cache_path, cipher_data)) {
-        fprintf(stderr,
-                "rgpgfs_encrypt: failed to write cipher data to disk\n");
-      } else {
-        printf("encrypted %s\n", source_path);
-      }
+    int result = 0;
+    if (rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx, recip_keys, plain_data,
+                                          cache_path)) {
+      fprintf(stderr,
+              "rgpgfs_encrypt: failed to create encrypted cache of %s\n",
+              source_path);
+      result = 1;
     } else {
-      fprintf(stderr, "rgpgfs_encrypt: failed to encrypt %s\n", source_path);
+      printf("encrypted %s\n", source_path);
     }
-    gpgme_data_release(cipher_data);
     gpgme_data_release(plain_data);
+    return result;
   }
 
   return 0;