-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathPadFile.sh
More file actions
executable file
·57 lines (36 loc) · 1.55 KB
/
Copy pathPadFile.sh
File metadata and controls
executable file
·57 lines (36 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
# This tool copies a file and keeps adding the given padding byte at the end
# until the specified file size has been reached.
#
# Copyright (c) 2015-2018 R. Diez - Licensed under the GNU AGPLv3
set -o errexit
set -o nounset
set -o pipefail
# set -x # Trace execution of this script.
abort ()
{
echo >&2 && echo "Error in script \"$0\": $*" >&2
exit 1
}
if [ $# -ne 4 ]; then
abort "Invalid command-line arguments. Please specify <source file name>, <destination file name>, <target size> and <padding byte value>. Ssee this script's source code for more information."
fi
# The size in bytes and the padding byte value can be given in:
# - decimal like "10" (must be WITHOUT leading zero)
# - octal like "010" (must have a leading zero)
# - hexadecimal like "0x10"
SOURCE_FILENAME="$1"
DESTINATION_FILENAME="$2"
declare -i TARGET_SIZE="$(( $3 ))"
declare -i PADDING_BYTE_VALUE="$(( $4 ))"
declare -i EXISTING_FILE_SIZE
EXISTING_FILE_SIZE="$(stat -c "%s" -- "$SOURCE_FILENAME")"
if (( EXISTING_FILE_SIZE > TARGET_SIZE )); then
abort "The file size is greater than the target size."
fi
declare -i PAD_COUNT="$(( TARGET_SIZE - EXISTING_FILE_SIZE ))"
cp -- "$SOURCE_FILENAME" "$DESTINATION_FILENAME"
# This is rather slow. We could optimise it by growing the string exponentially.
printf -v PADDING_BYTE_IN_OCTAL "%03o" "$PADDING_BYTE_VALUE"
# This is rather slow. We could optimise it by telling dd to read data in chunks.
dd if=/dev/zero ibs=1 count="$PAD_COUNT" | tr "\\000" "\\$PADDING_BYTE_IN_OCTAL" >> "$DESTINATION_FILENAME"