-O ftrunc=tail: trim unused tail after processing actual data.
authorAndrey Rys <rys@lynxlynx.ru>
Fri, 13 Dec 2019 23:46:30 +0000 (00:46 +0100)
committerAndrey Rys <rys@lynxlynx.ru>
Fri, 13 Dec 2019 23:46:30 +0000 (00:46 +0100)
This is a hack actually, it's not much useful in everyday life.
It is for compressing files in place, exploiting stream compressors like this:

        xz -9c -T 4 < file.tar | tfcrypt -P -O ftrunc=tail - file.tar

, or more safer way (since tfcrypt does not buffer):

        tfcrypt -P file.tar | xz -9c -T 4 | tfcrypt -P -O ftrunc=tail - file.tar

VERSION
tfc_error.c
tfc_vars.c
tfcrypt.c
tfcrypt.h

diff --git a/VERSION b/VERSION
index a7873645902455c63d166fdcaa4b2fe565f6de7d..8f92bfdd49766b1907d4aec8d3b0f9ed6129d0e6 100644 (file)
--- a/VERSION
+++ b/VERSION
@@ -1 +1 @@
-34
+35
index 905ae40213bb501be0089218718f38ed261cb0af..a40ab0ae9bfed2aa8a2a9bc56eefc126444fe0a1 100644 (file)
@@ -266,6 +266,7 @@ void usage(void)
        tfc_say("    oseek=val: seek destination file/device by these val bytes.");
        tfc_say("    count=val: process only these val bytes, both input and output.");
        tfc_say("    ftrunc=val: truncate output file to these val bytes before closing it.");
+       tfc_say("    ftrunc=tail: truncate output's tail, leaving only processed data.");
        tfc_say("    xkey=val: take only val bytes from user keyfile.");
        tfc_say("    okey=val: seek the key before reading it (usually a device).");
        tfc_say("    xctr=val: specify size in bytes of initial counter prepended or read.");
index 4ca378554eaf522a5df4c90437abfbc80a03caa4..1a65ac331968261d16e3bfe983bcda068823090b 100644 (file)
@@ -62,7 +62,7 @@ size_t sksum_turns;
 int do_edcrypt = TFC_DO_ENCRYPT, do_stop, quiet, error_action;
 int counter_opt, mackey_opt, do_mac, do_outfmt = TFC_OUTFMT_B64, rawkey;
 int idx, write_flags;
-tfc_yesno catch_all_errors, ignore_seek_errors, password, overwrite_source, do_fsync, do_pad;
+tfc_yesno catch_all_errors, ignore_seek_errors, password, overwrite_source, do_fsync, do_pad, do_ftrunc = TFC_NO_FTRUNC;
 tfc_yesno do_preserve_time, do_stats_in_gibs, do_statline_dynamic = YES, do_less_stats;
 tfc_yesno no_repeat, do_full_hexdump = YES, verbose, statline_was_shown, show_secrets;
 char *srcfname = TFC_STDIN_NAME, *dstfname = TFC_STDOUT_NAME, *do_mac_file, *counter_file, *sksum_hashlist_file;
index d673d386e7757cf739ffc35fbf35283476b39e3b..fb3dfb9cb44a7e0826dc059c13c555ef7ecbc761 100644 (file)
--- a/tfcrypt.c
+++ b/tfcrypt.c
@@ -425,14 +425,21 @@ _baddfname:
                                        }
                                        else if (!strncmp(s, "ftrunc", 6) && *(s+6) == '=') {
                                                s += 7;
-                                               ftrunc_dfd = tfc_humanfsize(s, &stoi);
-                                               if (!str_empty(stoi)) {
-                                                       ftrunc_dfd = tfc_fnamesize(s, YES);
-                                                       ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
-                                                       if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
-                                                       "%s: invalid ftrunc value", s);
+                                               if (!strcmp(s, "tail")) {
+                                                       do_ftrunc = TFC_FTRUNC_TAIL;
+                                                       ftrunc_dfd = NOFSIZE;
+                                               }
+                                               else {
+                                                       do_ftrunc = TFC_DO_FTRUNC;
+                                                       ftrunc_dfd = tfc_humanfsize(s, &stoi);
+                                                       if (!str_empty(stoi)) {
+                                                               ftrunc_dfd = tfc_fnamesize(s, YES);
+                                                               ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
+                                                               if (ftrunc_dfd == NOFSIZE) xerror(NO, YES, YES,
+                                                               "%s: invalid ftrunc value", s);
+                                                       }
+                                                       else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
                                                }
-                                               else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':'));
                                        }
                                        else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') {
                                                s += 5;
@@ -1333,7 +1340,10 @@ _nomac:
 
        if (do_preserve_time) fcopy_matime(dfd, &s_stat);
        xclose(sfd);
-       if (ftrunc_dfd != NOFSIZE) if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);
+       if (do_ftrunc > TFC_NO_FTRUNC) {
+               if (do_ftrunc == TFC_FTRUNC_TAIL) ftrunc_dfd = total_processed_dst;
+               if (ftruncate(dfd, (off_t)ftrunc_dfd) == -1) xerror(YES, NO, YES, "ftruncate(%d)", dfd);
+       }
        xclose(dfd);
 
        xexit(exitcode);
index a313b33d142fe8b5629f669aa4161acc0d0b2110..85ed79a5890f4f142d2a828f607586ebe68394b8 100644 (file)
--- a/tfcrypt.h
+++ b/tfcrypt.h
@@ -155,7 +155,7 @@ extern size_t sksum_turns;
 extern int do_edcrypt, do_stop, quiet, error_action;
 extern int counter_opt, mackey_opt, do_mac, do_outfmt, rawkey;
 extern int idx, write_flags;
-extern tfc_yesno catch_all_errors, ignore_seek_errors, password, overwrite_source, do_fsync, do_pad;
+extern tfc_yesno catch_all_errors, ignore_seek_errors, password, overwrite_source, do_fsync, do_pad, do_ftrunc;
 extern tfc_yesno do_preserve_time, do_stats_in_gibs, do_statline_dynamic, do_less_stats;
 extern tfc_yesno no_repeat, do_full_hexdump, verbose, statline_was_shown, show_secrets;
 extern char *srcfname, *dstfname, *do_mac_file, *counter_file, *sksum_hashlist_file;
@@ -229,5 +229,6 @@ enum {
        TFC_MODE_STREAM, TFC_MODE_XTS, TFC_MODE_ECB, TFC_MODE_CBC, TFC_MODE_OCB
 };
 enum { TFC_CTR_SHOW = 1, TFC_CTR_HEAD, TFC_CTR_RAND, TFC_CTR_ZERO, TFC_CTR_SSET };
+enum { TFC_NO_FTRUNC, TFC_DO_FTRUNC, TFC_FTRUNC_TAIL };
 
 #endif