Quellcode durchsuchen

refactor: added rgpgfs_gpgme_encrypt_file_to_file()

Fabian Peter Hammerle vor 6 Jahren
Ursprung
Commit
d54e33b953
3 geänderte Dateien mit 29 neuen und 20 gelöschten Zeilen
  1. 20 0
      src/gpgme.c
  2. 5 0
      src/gpgme.h
  3. 4 20
      src/main.c

+ 20 - 0
src/gpgme.c

@@ -47,6 +47,8 @@ int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,
 
   int result = 0;
 
+  // list of recipients may implicitly include the default recipient
+  // (GPGME_ENCRYPT_NO_ENCRYPT_TO)
   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");
@@ -60,3 +62,21 @@ int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,
   gpgme_data_release(cipher_data);
   return result;
 }
+
+int rgpgfs_gpgme_encrypt_file_to_file(gpgme_ctx_t gpgme_ctx,
+                                      gpgme_key_t recip_keys[],
+                                      const char *plain_path,
+                                      const char *cipher_path) {
+  gpgme_data_t plain_data;
+  gpgme_error_t gpgme_read_err =
+      gpgme_data_new_from_file(&plain_data, plain_path, 1);
+  if (gpgme_read_err != GPG_ERR_NO_ERROR) {
+    fprintf(stderr, "%s: failed to read file %s: %s (%d)\n", __func__,
+            plain_path, gpg_strerror(gpgme_read_err), gpgme_read_err);
+    return 1;
+  }
+  int result = rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx, recip_keys,
+                                                 plain_data, cipher_path);
+  gpgme_data_release(plain_data);
+  return result;
+}

+ 5 - 0
src/gpgme.h

@@ -6,3 +6,8 @@ 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);
+
+int rgpgfs_gpgme_encrypt_file_to_file(gpgme_ctx_t gpgme_ctx,
+                                      gpgme_key_t recip_keys[],
+                                      const char *plain_path,
+                                      const char *cipher_path);

+ 4 - 20
src/main.c

@@ -52,31 +52,15 @@ static int rgpgfs_encrypt(const char *source_path, char *cache_path) {
       return 1;
     }
 
-    gpgme_data_t plain_data;
-    gpgme_error_t gpgme_source_read_err =
-        gpgme_data_new_from_file(&plain_data, source_path, 1);
-    if (gpgme_source_read_err != GPG_ERR_NO_ERROR) {
-      fprintf(stderr,
-              "rgpgfs_encrypt: failed to read source file %s: %s (%d)\n",
-              source_path, gpg_strerror(gpgme_source_read_err),
-              gpgme_source_read_err);
-      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};
-    int result = 0;
-    if (rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx, recip_keys, plain_data,
+    if (rgpgfs_gpgme_encrypt_file_to_file(gpgme_ctx, recip_keys, source_path,
                                           cache_path)) {
-      fprintf(stderr,
-              "rgpgfs_encrypt: failed to create encrypted cache of %s\n",
+      fprintf(stderr, "%s: failed to create encrypted cache of %s\n", __func__,
               source_path);
-      result = 1;
-    } else {
-      printf("encrypted %s\n", source_path);
+      return 1;
     }
-    gpgme_data_release(plain_data);
-    return result;
+    printf("encrypted %s\n", source_path);
   }
 
   return 0;