diff options
author | Luke Shumaker <lukeshu@lukeshu.com> | 2018-08-16 02:00:07 -0400 |
---|---|---|
committer | Luke Shumaker <lukeshu@lukeshu.com> | 2018-08-16 02:00:07 -0400 |
commit | 7e6b34c552b19bd8455a198eb018afb2863aeb6a (patch) | |
tree | fd02a631542c43f9b84d90f8199468b9536176de | |
parent | 4412eedf45c2c3c408ad9a235e6281683064feb3 (diff) |
osi-mk: fix
"Ignore space change" will be useful
-rwxr-xr-x | osi-mk | 74 |
1 files changed, 39 insertions, 35 deletions
@@ -177,47 +177,51 @@ main() { # main code starts here outside) - if $arg_edit; then - if [[ -n $arg_edit_base ]]; then - # shellcheck disable=SC2059 - printf -v prefix "$(gettext -- '%s [format]')" "$NAME" - { + # shellcheck disable=SC2059 + printf -v prefix "$(gettext -- '%s [format]')" "$NAME" + { + if $arg_edit; then + if [[ -n $arg_edit_base ]]; then cp -T -- "$arg_edit_base" "$arg_file" btrfstune -fu "$arg_file" - } |& sed "s|^|${prefix} |" - fi - if [[ -n $arg_size ]]; then - # Calculate sizes in exact bytes now, to avoid any discrepancies - # between truncate(1) and btrfs-filesystem(8). - tmpfile=$(mktemp -t -- "${0##*/}.XXXXXXXXXX") - trap "rm -- ${tmpfile@Q}" EXIT - truncate --reference="$arg_file" --size="$arg_size" -- "$tmpfile" - old_size=$(stat --format='%s' -- "$arg_file") - new_size=$(stat --format='%s' -- "$tmpfile") - rm -- "$tmpfile" - trap - EXIT + fi + if [[ -n $arg_size ]]; then + # Calculate sizes in exact bytes now, to avoid any discrepancies + # between truncate(1) and btrfs-filesystem(8). + # Use truncate(1) to do this. It's a little gross, + # but because TMPDIR is likely on tmpfs, it's time-cheap, + # and because of sparse files, it's space-cheap. + tmpfile=$(mktemp -t -- "${0##*/}.XXXXXXXXXX") + trap "rm -- ${tmpfile@Q}" EXIT + case "$arg_size" in + '+'*|'-'*|'<'*|'>'*|'/'*|'%'*) + truncate --reference="$arg_file" --size="$arg_size" -- "$tmpfile";; + *) + truncate --size="$arg_size" -- "$tmpfile";; + esac + old_size=$(stat --format='%s' -- "$arg_file") + new_size=$(stat --format='%s' -- "$tmpfile") + rm -- "$tmpfile" + trap - EXIT - # Do the resize - arg_mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX") - trap "rmdir -- ${arg_mountpoint@Q}" EXIT - if (( new_size > old_size )); then - truncate --size="$new_size" -- "$arg_file" - sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize max "$arg_mountpoint" - elif (( new_size < old_size )); then - sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize "$new_size" "$arg_mountpoint" - truncate --size="$new_size" -- "$arg_file" + # Do the resize + arg_mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX") + trap "rmdir -- ${arg_mountpoint@Q}" EXIT + if (( new_size > old_size )); then + truncate --size="$new_size" -- "$arg_file" + sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize max "$arg_mountpoint" + elif (( new_size < old_size )); then + sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" btrfs filesystem resize "$new_size" "$arg_mountpoint" + truncate --size="$new_size" -- "$arg_file" + fi + rmdir -- "$arg_mountpoint" + trap - EXIT fi - rmdir -- "$arg_mountpoint" EXIT - trap - EXIT - fi - else - # shellcheck disable=SC2059 - printf -v prefix "$(gettext -- '%s [format]')" "$NAME" - { + else truncate --size="$arg_size" -- "$arg_file" mkfs.btrfs -- "$arg_file" - } |& sed "s|^|${prefix} |" - fi + fi + } |& sed "s|^|${prefix} |" arg_mountpoint=$(mktemp -dt -- "${0##*/}.XXXXXXXXXX") trap "rmdir -- ${arg_mountpoint@Q}" EXIT sudo -- ./osi-mount --root -- "$arg_file" "$arg_mountpoint" "${BASH_SOURCE[0]}" --inside="$arg_mountpoint" "${arg_orig[@]}" |