summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@oper.io>2018-10-29 13:09:22 -0600
committerAaron Ball <nullspoon@oper.io>2018-10-29 13:09:22 -0600
commitbf65f7ae66eba724ceb0b3cb846b4a3674a0ca87 (patch)
tree7ed2c28ab5d67e2621c7cf1f11f8cf3d9ae6989a
parent4d11d5e2802a6074ac6ad11232ec29f28ae60297 (diff)
downloadgpgedit-bf65f7ae66eba724ceb0b3cb846b4a3674a0ca87.tar.gz
gpgedit-bf65f7ae66eba724ceb0b3cb846b4a3674a0ca87.tar.xz
Support new file creation
Now we test if the passed-in file exists. If it does, the decrypt -> edit -> encrypt process is followed. If it does not exist, the decryption process is skipped. Also added a check for an empty recipient linked list to prevent attempting to edit files when no recipients are specified.
-rw-r--r--TODO.adoc2
-rw-r--r--src/main.c64
2 files changed, 42 insertions, 24 deletions
diff --git a/TODO.adoc b/TODO.adoc
index fa8dec8..d3f69a7 100644
--- a/TODO.adoc
+++ b/TODO.adoc
@@ -12,5 +12,5 @@ Implement
* [ ] Argument parser (eg: -r,--recipient)
* [x] Recipient addition beyond the list already in the file
* [ ] Recipient removal from existing file
-* [ ] New file creation (at least one recipient required)
+* [x] New file creation (at least one recipient required)
* [x] Helptext
diff --git a/src/main.c b/src/main.c
index 2ddae06..03f4381 100644
--- a/src/main.c
+++ b/src/main.c
@@ -82,38 +82,56 @@ int main(int argc, char* argv[]) {
gpgme_error_t err;
struct gpgedit_config* config;
- if(argc == 1) {
- printf("Please specify a gpg encrypted file to edit\n");
- return 1;
- }
-
config = gpgedit_config_new();
parseargs(argc, argv, config);
- // Initialize decryption context, create tmp file to write decrypted contents
- // to, and run decryption operation.
- err = init_gpg(&decctx, GPGME_PROTOCOL_OPENPGP);
- if(err != GPG_ERR_NO_ERROR) {
- printf("Error: %s\n", gpgme_strerror(err));
+ if(config->file == NULL) {
+ printf("Please specify a gpg encrypted file to edit\n");
return 1;
}
- mkstemp(tmpfile);
- gpgme_decrypt_result_t res = gpg_decrypt_file(config->file, tmpfile, decctx);
- // Get keyids array from recipients list
- gpgedit_config_recip_from_gpgme_recip(config, res->recipients);
- // Freedom!
- gpgme_release(decctx);
- // Open the system editor
- system_edit(tmpfile);
+ // Check if fd opened. If not, the specified file doesn't exist, so we need
+ // to create it.
+ FILE* fd = fopen(config->file, "r");
+ if(!fd) {
+ printf("File '%s' does not exist. Creating.\n", config->file);
+ mkstemp(tmpfile);
+ } else {
+ // File exists, close the fd for testing existance
+ fclose(fd);
+
+ // Initialize decryption context, create tmp file to write decrypted contents
+ // to, and run decryption operation.
+ err = init_gpg(&decctx, GPGME_PROTOCOL_OPENPGP);
+ if(err != GPG_ERR_NO_ERROR) {
+ printf("Error: %s\n", gpgme_strerror(err));
+ return 1;
+ }
+
+ mkstemp(tmpfile);
+ gpgme_decrypt_result_t res = gpg_decrypt_file(config->file, tmpfile, decctx);
+ // Get keyids array from recipients list
+ gpgedit_config_recip_from_gpgme_recip(config, res->recipients);
+ // Freedom!
+ gpgme_release(decctx);
+ }
- printf("Recipients:\n");
- strll_dump(config->recipients);
+ // We can't do anything if the recipient list is empty (eg: new file but no
+ // recipients specified).
+ if(config->recipients->str == NULL) {
+ printf("Recipient list empty. Cannot proceed.\n");
+ } else {
+ // Open the system editor
+ system_edit(tmpfile);
- // Re-encrypt the plaintext tmp file
- //printf("Re-encrypting file '%s' -> '%s'\n", tmpfile, config->file);
- gpg_encrypt_file(tmpfile, config->file, config->recipients);
+ printf("Recipients:\n");
+ strll_dump(config->recipients);
+
+ // Re-encrypt the plaintext tmp file
+ //printf("Re-encrypting file '%s' -> '%s'\n", tmpfile, config->file);
+ gpg_encrypt_file(tmpfile, config->file, config->recipients);
+ }
// Clean up tmpfile
gpgedit_config_release(config);

Generated by cgit