aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorTristan Riehs <tristan.riehs@inria.fr>2026-01-03 22:11:10 +0100
committerTristan Riehs <tristan.riehs@inria.fr>2026-01-03 22:11:10 +0100
commit098611d8b23ab846338f97748325eb7fc8b53892 (patch)
tree6c9a82ae35ceedcdf6fc3b952a826b847cd1b2cc /src
parent691018dae4f760b711cbca5246f4ac1db397b464 (diff)
Rework encryption function
Prepare for the decryption when using "ftag export".
Diffstat (limited to 'src')
-rw-r--r--src/main.c25
1 files changed, 19 insertions, 6 deletions
diff --git a/src/main.c b/src/main.c
index 51d84e1..1a2eac8 100644
--- a/src/main.c
+++ b/src/main.c
@@ -14,6 +14,13 @@
#define DATABASE_PATH (FTAG_ROOT "/ftag.sqlite3")
+/* Used when encrypting or decrpyting a file, see the copy_encrypted_file
+ * function. */
+enum encrypt {
+ ENCRYPT,
+ DECRYPT,
+};
+
/* TODO: read the configuration from a file
This would allow working with different databases with the same ftag
@@ -117,17 +124,23 @@ static void copy_file(const char *in, const char *out)
/* Like copy_file, but OUT is an encrypted version of IN. Encryption is done
* using GPG. */
-static void copy_and_encrypt_file(const char *in, const char *out)
+static void
+copy_file_with_encryption(const char *in, const char *out, enum encrypt encrypt)
{
int rc = fork();
+ char *crypt_param;
+ assert(encrypt == ENCRYPT || encrypt == DECRYPT);
+ if (encrypt == ENCRYPT)
+ crypt_param = "--encrypt";
+ else
+ crypt_param = "--decrypt";
if (rc == 0) {
execlp("gpg",
"gpg",
"--output", out,
- /* do not ask for overwriting files, maybe dangerous if
- * GPG asks security questions */
- "--yes",
- "--encrypt", in,
+ "--yes", /* do not ask for overwriting files, maybe
+ * dangerous if GPG asks security questions */
+ crypt_param, in,
NULL);
fprintf(stderr, "exec: gpg:");
perror("");
@@ -612,7 +625,7 @@ ftag_add_one_file(sqlite3 *db, int *next_id, const char *file, uint32_t file_sum
char new_path[512];
if (encrypt) {
strbuild(new_path, "%s/files/%s.gpg", FTAG_ROOT, canonical_name);
- copy_and_encrypt_file(file, new_path);
+ copy_file_with_encryption(file, new_path, ENCRYPT);
}
else {
strbuild(new_path, "%s/files/%s", FTAG_ROOT, canonical_name);