|
1 | 1 | #!/bin/bash |
2 | 2 |
|
3 | | -DIR="$(cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd)" |
| 3 | +DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" |
4 | 4 |
|
5 | 5 | # shellcheck source=lib/plugin.bash |
6 | 6 | . "${DIR}/plugin.bash" |
@@ -35,28 +35,68 @@ compress() { |
35 | 35 | echo "Compressing ${COMPRESSED_FILE} with ${COMPRESSION}..." |
36 | 36 |
|
37 | 37 | if [ "${COMPRESSION}" = 'tgz' ]; then |
38 | | - tar czf "${FILE}" "${COMPRESSED_FILE}" |
| 38 | + TAR_OPTS='cz' |
| 39 | + if is_absolute_path "${COMPRESSED_FILE}"; then |
| 40 | + TAR_OPTS="${TAR_OPTS}"P |
| 41 | + fi |
| 42 | + |
| 43 | + tar "${TAR_OPTS}"f "${FILE}" "${COMPRESSED_FILE}" |
39 | 44 | elif [ "${COMPRESSION}" = 'zip' ]; then |
40 | | - # because ZIP complains if the file does not end with .zip |
41 | | - zip -r "${FILE}.zip" "${COMPRESSED_FILE}" |
42 | | - mv "${FILE}.zip" "${FILE}" |
| 45 | + if is_absolute_path "${COMPRESSED_FILE}"; then |
| 46 | + local COMPRESS_DIR |
| 47 | + COMPRESS_DIR="$(dirname "${COMPRESSED_FILE}")" |
| 48 | + ( # subshell to avoid changing the working directory |
| 49 | + # shellcheck disable=SC2164 # we will exit anyway |
| 50 | + cd "${COMPRESS_DIR}" |
| 51 | + # because ZIP complains if the file does not end with .zip |
| 52 | + zip -r "${FILE}.zip" "${COMPRESSED_FILE}" |
| 53 | + mv "${FILE}.zip" "${FILE}" |
| 54 | + ) |
| 55 | + else |
| 56 | + # because ZIP complains if the file does not end with .zip |
| 57 | + zip -r "${FILE}.zip" "${COMPRESSED_FILE}" |
| 58 | + mv "${FILE}.zip" "${FILE}" |
| 59 | + fi |
43 | 60 | fi |
44 | 61 | } |
45 | 62 |
|
46 | 63 | uncompress() { |
47 | 64 | local FILE="$1" |
48 | | - local _RESTORE_PATH="$2" # pretty sure this is not necessary |
| 65 | + local RESTORE_PATH="$2" |
49 | 66 |
|
50 | 67 | local COMPRESSION='' |
51 | 68 | COMPRESSION="$(plugin_read_config COMPRESSION 'none')" |
52 | 69 |
|
53 | 70 | echo "Cache is compressed, uncompressing with ${COMPRESSION}..." |
54 | 71 |
|
55 | 72 | if [ "${COMPRESSION}" = 'tgz' ]; then |
56 | | - tar xzf "${FILE}" |
| 73 | + TAR_OPTS='xz' |
| 74 | + if is_absolute_path "${RESTORE_PATH}"; then |
| 75 | + TAR_OPTS="${TAR_OPTS}"P |
| 76 | + fi |
| 77 | + |
| 78 | + tar "${TAR_OPTS}"f "${FILE}" "${RESTORE_PATH}" |
57 | 79 | elif [ "${COMPRESSION}" = 'zip' ]; then |
58 | | - # because ZIP complains if the file does not end with .zip |
59 | | - mv "${FILE}" "${FILE}.zip" |
60 | | - unzip -o "${FILE}.zip" |
| 80 | + if is_absolute_path "${RESTORE_PATH}"; then |
| 81 | + local RESTORE_DIR |
| 82 | + RESTORE_DIR="$(dirname "${RESTORE_PATH}")" |
| 83 | + ( # subshell to avoid changing the working directory |
| 84 | + mkdir -p "${RESTORE_DIR}" |
| 85 | + # shellcheck disable=SC2164 # we will exit anyway |
| 86 | + cd "${RESTORE_DIR}" |
| 87 | + mv "${FILE}" "${RESTORE_DIR}/compressed.zip" |
| 88 | + unzip -o "compressed.zip" |
| 89 | + rm "compressed.zip" |
| 90 | + ) |
| 91 | + else |
| 92 | + # because ZIP complains if the file does not end with .zip |
| 93 | + mv "${FILE}" "${FILE}.zip" |
| 94 | + unzip -o "${FILE}.zip" |
| 95 | + fi |
61 | 96 | fi |
62 | 97 | } |
| 98 | + |
| 99 | +is_absolute_path() { |
| 100 | + local FILEPATH="${1}" |
| 101 | + [ "${FILEPATH:0:1}" = "/" ] |
| 102 | +} |
0 commit comments