Ver código fonte

rgpgfs_gpgme_data_to_file: fix missing fclose()

Fabian Peter Hammerle 5 anos atrás
pai
commit
03985fed0f
1 arquivos alterados com 18 adições e 16 exclusões
  1. 18 16
      src/gpgme.c

+ 18 - 16
src/gpgme.c

@@ -20,34 +20,36 @@ int rgpgfs_gpgme_get_encrypt_key(gpgme_ctx_t gpgme_ctx, const char *key_name,
   return 0;
 }
 
-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");
-    return 1;
-  }
-
-  FILE *file = fopen(path, "wb");
-  if (file == NULL) {
-    perror("rgpgfs_gpgme_data_to_file: failed to open file");
-    return 1;
-  }
-
+static int _rgpgfs_gpgme_write_data_to_file(FILE *file, gpgme_data_t data) {
   ssize_t count;
   char buf[BUFSIZ];
   while ((count = gpgme_data_read(data, buf, BUFSIZ)) > 0) {
     if (fwrite(buf, 1, count, file) != count) {
-      fprintf(stderr,
-              "rgpgfs_gpgme_data_to_file: failed to write data to file");
+      fprintf(stderr, "%s: failed to write data to file", __func__);
       return 1;
     }
   }
   if (count != 0) {
-    perror("rgpgfs_gpgme_data_to_file: failed to load data into buffer");
+    perror("_rgpgfs_gpgme_write_data_to_file: failed to load data into buffer");
+    return 1;
+  }
+  return 0;
+}
+
+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");
     return 1;
   }
 
+  FILE *file = fopen(path, "wb");
+  if (file == NULL) {
+    perror("rgpgfs_gpgme_data_to_file: failed to open file");
+    return 1;
+  }
+  int res = _rgpgfs_gpgme_write_data_to_file(file, data);
   fclose(file);
-  return 0;
+  return res;
 }
 
 int rgpgfs_gpgme_encrypt_data_to_file(gpgme_ctx_t gpgme_ctx,