-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivialfs_write
executable file
·105 lines (100 loc) · 2.84 KB
/
trivialfs_write
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#! /bin/sh
# Expects the output of trivialfs_file at the beginning of standard input,
# describing the allocated space of a file within a TrivialFS volume.
#
# Without further arguments, copies the remaining data from standard input as
# the new contents into the TrivialFS file. If a file or block device is
# specified as a single argument, the new contents will be copied from that
# file or block device rather than from standard input.
#
# Without the -d option, the data provided to be written must not be larger
# than the size of the trivialfs file, and its previous contents will be
# overwritten starting with the first byte of the file up to the last byte of
# input provided.
#
# When the -d option is used, dynamic file sizes up to the allocated size of
# the file are supported. In this case, the payload data overwrites the
# beginning of the allocates space as without the -d option. However, in
# addition to that, the size of the new payload data will be written after a
# newline character as an ASCII decimal number as the last bytes of the
# TrivialFS file. The new data must leave enough unused space for this purpose
# after the payload data to be written.
#
# trivialfs_write version 2016.175
#
# Copyright (c) 2016 Guenther Brunthaler. All rights reserved.
#
# This script is free software.
# Distribution is permitted under the terms of the GPLv3.
set -e
max_blocksz=`expr 4 \* 1024 \* 1024`
# a9hpguh5ddnq25ycsf7hiq5kj {
dir0=`dirname -- "$0"`; dir0=`readlink -f -- "$dir0"`; test -d "$dir0"
libdir=$dir0/sh; test -d "$libdir"
# } a9hpguh5ddnq25ycsf7hiq5kj
. "$libdir"/cleanup-37yusympssv3vb63uavsipr7u.sh
. "$libdir"/fsize-4rdwih8qv64bnb5wvizl2l61s.sh
. "$libdir"/validate_uint-r29t167e1xmvyk7trebvq0l9y.sh
raw=true
while getopts d opt
do
case $opt in
d) raw=false;;
*) false || exit
esac
done
shift `expr $OPTIND - 1 || :`
read -r file_start file_size volume_path
validate_uint "$file_start"
validate_uint "$file_size"
test -e "$volume_path"
if test $# = 0
then
test $# = 0
tfile() {
rm -- "$src"
}
src=`mktemp "${TMPDIR:-/tmp}/${0##*/}.XXXXXXXXXX"`
alloc tfile
cat > "$src"
else
src=$1; shift
test $# = 0
if test ! -f "$src"
then
test -b "$src"
fi
fi
if test -b "$src"
then
outsize=`blockdev --getsize64 "$src"`
else
outsize=`fsize "$src"`
fi
validate_uint "$outsize"
if $raw
then
szovh=0
else
szsuffix=`printf '\n%u' $outsize`
szovh=${#szsuffix}
fi
needed=`expr $outsize + $szovh || :`
if test $file_size -lt $needed
then
die "Insufficient space in allocated TrivialFS file: $needed bytes" \
"would be needed but only $file_size bytes are allocated!"
fi
"$dir0"/offset_io -M $max_blocksz \
write $outsize bytes '@' $file_start into "$volume_path" \
< "$src"
if $raw
then
:
else
printf %s "$szsuffix" \
| "$dir0"/offset_io -M $max_blocksz \
write $szovh bytes '@' `
expr $file_start + $file_size - $szovh
` into "$volume_path"
fi