From 10d07f6813a5909bb9ae957bc7424b3ad3099b93 Mon Sep 17 00:00:00 2001 From: Andrey Rys Date: Sat, 6 Apr 2019 18:49:54 +0700 Subject: [PATCH] "-O ftrunc=": implement file truncations (without sanity checks). --- VERSION | 2 +- tfc_error.c | 1 + tfc_vars.c | 2 +- tfcrypt.c | 12 ++++++++++++ tfcrypt.h | 2 +- 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/VERSION b/VERSION index 7273c0f..6f4247a 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -25 +26 diff --git a/tfc_error.c b/tfc_error.c index 11c22bb..c4e51f1 100644 --- a/tfc_error.c +++ b/tfc_error.c @@ -256,6 +256,7 @@ void usage(void) tfc_say(" Internally this number is translated into number of %u byte blocks.", TFC_U(TF_BLOCK_SIZE)); 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(" 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."); diff --git a/tfc_vars.c b/tfc_vars.c index 15a0320..f629264 100644 --- a/tfc_vars.c +++ b/tfc_vars.c @@ -41,7 +41,7 @@ tfc_byte tmpdata[TFC_TMPSIZE]; char *randsource = TFC_DEFAULT_RANDSOURCE; -tfc_fsize iseek_blocks, iseek, oseek, maxlen = NOFSIZE; +tfc_fsize iseek_blocks, iseek, oseek, maxlen = NOFSIZE, ftrunc_dfd = NOFSIZE; tfc_fsize total_processed_src, total_processed_dst; tfc_fsize delta_processed; tfc_fsize genrandom_nr_bytes, genzero_nr_bytes; diff --git a/tfcrypt.c b/tfcrypt.c index 5ed5bbf..3534bb8 100644 --- a/tfcrypt.c +++ b/tfcrypt.c @@ -376,6 +376,17 @@ _baddfname: if (counter_opt == TFC_CTR_HEAD) maxlen += TF_BLOCK_SIZE; } + 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); + } + else ftrunc_dfd = tfc_modifysize(ftrunc_dfd, strchr(s, ':')); + } else if (!strncmp(s, "xkey", 4) && *(s+4) == '=') { s += 5; maxkeylen = tfc_humanfsize(s, &stoi); @@ -1277,6 +1288,7 @@ _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); xclose(dfd); xexit(exitcode); diff --git a/tfcrypt.h b/tfcrypt.h index f2cb89c..49073c9 100644 --- a/tfcrypt.h +++ b/tfcrypt.h @@ -138,7 +138,7 @@ extern tfc_byte srcblk[TFC_BLKSIZE], dstblk[TFC_BLKSIZE], *pblk; extern tfc_byte macvrfy[SKEIN_DIGEST_SIZE], macresult[SKEIN_DIGEST_SIZE]; extern tfc_byte tmpdata[TFC_TMPSIZE]; extern char *randsource; -extern tfc_fsize iseek_blocks, iseek, oseek, maxlen; +extern tfc_fsize iseek_blocks, iseek, oseek, maxlen, ftrunc_dfd; extern tfc_fsize total_processed_src, total_processed_dst; extern tfc_fsize delta_processed; extern tfc_fsize genrandom_nr_bytes, genzero_nr_bytes; -- 2.31.1