diff options
| author | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-15 14:48:29 +0200 | 
|---|---|---|
| committer | David Oberhollenzer <david.oberhollenzer@sigma-star.at> | 2019-06-15 14:49:58 +0200 | 
| commit | 868843fbf76dd67e804bc8a02121f5b56c560621 (patch) | |
| tree | 0ccdd3a107c0e6f35175712d88c37de32be107e1 | |
| parent | 083fc146266a9eeb2dd407546c86c3bf725330c1 (diff) | |
Move function to padd a file with 0 bytes to libutil
Signed-off-by: David Oberhollenzer <david.oberhollenzer@sigma-star.at>
| -rw-r--r-- | include/util.h | 7 | ||||
| -rw-r--r-- | lib/Makemodule.am | 2 | ||||
| -rw-r--r-- | lib/util/padd_file.c | 41 | ||||
| -rw-r--r-- | mkfs/mkfs.c | 37 | 
4 files changed, 50 insertions, 37 deletions
diff --git a/include/util.h b/include/util.h index 8e54cb4..28d21bc 100644 --- a/include/util.h +++ b/include/util.h @@ -3,6 +3,7 @@  #define UTIL_H  #include <sys/types.h> +#include <stdint.h>  /*    Removes all preceeding and trailing slashes, shortens all sequences of @@ -50,4 +51,10 @@ int pushdn(const char *path, size_t len);  /* Returns 0 on success. On failure, prints error message to stderr. */  int popd(void); +/* +  Write zero bytes to an output file to padd it to specified block size. +  Returns 0 on success. On failure, prints error message to stderr. +*/ +int padd_file(int outfd, uint64_t size, size_t blocksize); +  #endif /* UTIL_H */ diff --git a/lib/Makemodule.am b/lib/Makemodule.am index ddc78f6..a676291 100644 --- a/lib/Makemodule.am +++ b/lib/Makemodule.am @@ -30,7 +30,7 @@ libutil_a_SOURCES = lib/util/canonicalize_name.c lib/util/write_retry.c  libutil_a_SOURCES += lib/util/read_retry.c include/util.h  libutil_a_SOURCES += lib/util/print_version.c lib/util/mkdir_p.c  libutil_a_SOURCES += lib/util/str_table.c include/str_table.h -libutil_a_SOURCES += lib/util/dirstack.c +libutil_a_SOURCES += lib/util/dirstack.c lib/util/padd_file.c  if WITH_GZIP  libcompress_a_SOURCES += lib/comp/gzip.c diff --git a/lib/util/padd_file.c b/lib/util/padd_file.c new file mode 100644 index 0000000..8598b8b --- /dev/null +++ b/lib/util/padd_file.c @@ -0,0 +1,41 @@ +/* SPDX-License-Identifier: GPL-3.0-or-later */ +#include "util.h" + +#include <stdlib.h> +#include <stdio.h> + +int padd_file(int outfd, uint64_t size, size_t blocksize) +{ +	size_t padd_sz = size % blocksize; +	int status = -1; +	uint8_t *buffer; +	ssize_t ret; + +	if (padd_sz == 0) +		return 0; + +	padd_sz = blocksize - padd_sz; + +	buffer = calloc(1, padd_sz); +	if (buffer == NULL) +		goto fail_errno; + +	ret = write_retry(outfd, buffer, padd_sz); + +	if (ret < 0) +		goto fail_errno; + +	if ((size_t)ret < padd_sz) +		goto fail_trunc; + +	status = 0; +out: +	free(buffer); +	return status; +fail_trunc: +	fputs("padding output to block size: truncated write\n", stderr); +	goto out; +fail_errno: +	perror("padding output file to block size"); +	goto out; +} diff --git a/mkfs/mkfs.c b/mkfs/mkfs.c index 8317bd2..2219989 100644 --- a/mkfs/mkfs.c +++ b/mkfs/mkfs.c @@ -1,41 +1,6 @@  /* SPDX-License-Identifier: GPL-3.0-or-later */  #include "mkfs.h" -static int padd_file(int outfd, sqfs_super_t *super, options_t *opt) -{ -	size_t padd_sz = super->bytes_used % opt->devblksz; -	uint8_t *buffer; -	ssize_t ret; - -	if (padd_sz == 0) -		return 0; - -	padd_sz = opt->devblksz - padd_sz; - -	buffer = calloc(1, padd_sz); -	if (buffer == NULL) { -		perror("padding output file to block size"); -		return -1; -	} - -	ret = write_retry(outfd, buffer, padd_sz); - -	if (ret < 0) { -		perror("Error padding squashfs image to page size"); -		free(buffer); -		return -1; -	} - -	if ((size_t)ret < padd_sz) { -		fputs("Truncated write trying to padd squashfs image\n", -		      stderr); -		return -1; -	} - -	free(buffer); -	return 0; -} -  static int process_file(data_writer_t *data, tree_node_t *n, bool quiet)  {  	int ret, infd; @@ -198,7 +163,7 @@ int main(int argc, char **argv)  	if (sqfs_super_write(&super, outfd))  		goto out_data; -	if (padd_file(outfd, &super, &opt)) +	if (padd_file(outfd, super.bytes_used, opt.devblksz))  		goto out_data;  	status = EXIT_SUCCESS;  | 
