forked from llm-y2k/tinycore-redpill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.sh
332 lines (277 loc) · 8.36 KB
/
file.sh
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#!/usr/bin/env bash
set -u
# Resolves array relative path to a file
#
# Args:
# $1 raw file path
# $2 reference to a K=>V map of variables; it should contain "@@@_DEF_@@@" entry for non-variabled paths
brp_expand_var_path()
{
local -n __vars_map=$2
local file_path="${1}"
if [[ "${1}" == /* ]]; then
: #noop, absolute paths don't need any modifications
elif [[ "${1}" != @@@* ]]; then
# since path doesn't begin with a variable we just assume the default
file_path="${__vars_map[@@@_DEF_@@@]}/${file_path}"
else
local var_value
for var_name in "${!__vars_map[@]}"
do
var_value="${__vars_map[$var_name]}"
file_path="${file_path/${var_name}/${var_value}}"
done
fi
pr_dbg "Resolved path '${1}' to '${file_path}'"
echo "${file_path}"
}
# Computes SHA-256 hash for a file and returns it
#
# Args: $1 file path
rpt_get_file_sha256()
{
pr_dbg "Generating SHA-256 for %s" "${1}"
local hash;
local hash_res;
hash=$("${SHA256SUM_PATH}" "${1}" | cut -d ' ' -f1)
hash_res=$?
echo "${hash}"
return $hash_res
}
# Validates file
#
# Args: $1 file path | $2 expected checksum | $3 make failure non-critical [default=0]
brp_verify_file_sha256()
{
pr_process "Verifying %s file" "${1}"
local hash;
hash=$(rpt_get_file_sha256 "${1}")
if [ $? -ne 0 ]; then
pr_process_err
if [[ "${3:-1}" -eq 1 ]]; then
pr_err "Failed to generate checksum for file\n\n%s" "${hash}"
else
pr_crit "Failed to generate checksum for file\n\n%s" "${hash}"
fi
fi
if [ "$2" != "$hash" ]; then
pr_process_err
if [[ "${3:-1}" -ne 1 ]]; then
pr_err "Checksum mismatch - expected %s but computed %s" "$2" "$hash"
else
pr_crit "Checksum mismatch - expected %s but computed %s" "$2" "$hash"
fi
fi
pr_process_ok
}
# Unpacks tar-like file
#
# Args: $1 file path | $2 directory to unpack (must exist) | $3 should hard fail on error? [default=1]
brp_unpack_tar()
{
pr_process "Unpacking %s file to %s" "${1}" "${2}"
local output;
output=$("${TAR_PATH}" -xf "${1}" -C "${2}" 2>&1)
if [ $? -ne 0 ]; then
pr_process_err
if [[ "${3:-1}" -ne 1 ]]; then
pr_err "Failed to unpack tar\n\n%s" "${output}"
return 1
else
pr_crit "Failed to unpack tar\n\n%s" "${output}"
fi
fi
pr_process_ok
}
# Unpacks tar-like file without any folder structure
#
# Args: $1 file path | $2 directory to unpack (must exist) | $3 should hard fail on error? [default=1]
brp_unpack_tar_flat()
{
pr_process "Unpacking files from %s to %s" "${1}" "${2}"
local output;
# Lovely hack by ford: https://stackoverflow.com/a/14295908
#todo: some older tar versions leave empty directories... there's no elegant way to get rid of them ;<
output=$("${TAR_PATH}" -xf "${1}" --transform='s/.*\///' -C "${2}" 2>&1)
if [ $? -ne 0 ]; then
pr_process_err
if [[ "${3:-1}" -ne 1 ]]; then
pr_err "Failed to unpack tar\n\n%s" "${output}"
return 1
else
pr_crit "Failed to unpack tar\n\n%s" "${output}"
fi
fi
pr_process_ok
}
# Unpacks compressed ramdisks
#
# Args: $1 file path | $2 directory to unpack (must exist)
brp_unpack_zrd()
{
pr_process "Unpacking %s file to %s" "${1}" "${2}"
local output;
output=$(cd "${2}" && "${XZ_PATH}" -dc < "${1}" 2>/dev/null | "${CPIO_PATH}" -idm 2>&1)
# Sadly we cannot check exit code of the unpacking as xz will always error out array as ramdisks have appended
# checksum, so we can check if something unpacked instead
if [ "$(ls -A ${2})" ]; then
pr_process_ok
return
fi
pr_process_err
pr_crit "Failed to unpack compressed ramdisk\n\n%s" "${output}"
}
# Repacks ramdisk into compressed archive
#
# Args: $1 output ramdisk path | $2 directory to repack
brp_pack_zrd()
{
pr_dbg "Repacking %s to LZ %s" "${2}" "${1}"
local output;
output=$(cd "${2}" && "${FIND_PATH}" . 2>/dev/null | \
cpio -o -H newc -R root:root 2>/dev/null | "${XZ_PATH}" -9 --format=lzma 2>/dev/null 1> "${1}")
if [ $? -ne 0 ]; then
pr_process_err
pr_crit "Failed to repack compressed ramdisk\n\n%s" "${output}"
fi
}
# Repacks ramdisk into flat CPIO archive
#
# This one is useful for kernels with broken unpacking routines <points fingers>
#
# Args: $1 output ramdisk path | $2 directory to repack
brp_pack_cpiord()
{
pr_dbg "Repacking %s to CPIO %s" "${2}" "${1}"
local output;
output=$(cd "${2}" && "${FIND_PATH}" . 2>/dev/null | \
cpio -o -H newc -R root:root 2>/dev/null 1> "${1}")
if [ $? -ne 0 ]; then
pr_crit "Failed to repack flat ramdisk\n\n%s" "${output}"
fi
}
# Unpacks compressed (or not) linux kernel image
#
# Args: $1 packed file | $2 destination
brp_unpack_zimage()
{
pr_process "Unpacking %s file to %s" "${1}" "${2}"
local output;
local log_file="${2}.log";
output=$("${EXTRACT_VMLINUX_PATH}" "${1}" 1> "${2}" 2>"${log_file}")
if [ $? -ne 0 ]; then
pr_process_err
pr_crit "Failed to unpack zImage\n\n%s" "$(cat \"${2}.log\")"
fi
pr_process_ok
pr_dbg "Log file saved to %s" "${log_file}"
}
# Repacks uncompressed vmlinux image into zImage
#
# Args: $1 kernel sources | $2 unpacked vmlinux file | $3 destination for zImage
brp_repack_zimage()
{
pr_info "Repacking \"%s\" to \"%s\" within \"%s\"" "${2}" "${3}" "${1}"
# Make file more anonymous
export KBUILD_BUILD_TIMESTAMP="1970/1/1 00:00:00"
export KBUILD_BUILD_USER="root"
export KBUILD_BUILD_HOST="localhost"
export KBUILD_BUILD_VERSION=0
"${REBUILD_KERNEL_PATH}" "${1}" "${2}" "${3}"
if [ $? -ne 0 ]; then
pr_crit "Failed to rebuild zImage, see log above"
fi
pr_info "Successfully created \"%s\"" "${3}"
}
# Decompresses a single gzipped file without removing the original
#
# Args: $1 source.gz | $2 destination.plain
brp_unpack_single_gz()
{
pr_dbg "Unpacking %s to %s" "${1}" "${2}"
"${GZIP_PATH}" --decompress --stdout "${1}" > "${2}"
if [ $? -ne 0 ]; then
pr_crit "Failed to unpack %s to %s" "${1}" "${2}"
fi
}
brp_mkdir()
{
if [[ -d "${1}" ]]; then return 0; fi
pr_dbg "Creating directory \"%s\"" "${1}"
"${MKDIR_PATH}" -p "${1}" || pr_crit "Failed to create \"%s\" directory" "${1}"
}
rpt_make_executable()
{
"${CHMOD_PATH}" +x "${1}" || pr_crit "Failed to make \"%s\" executable" "${1}"
}
# Copies files while resolving all symlinks
#
# Args: $1 source file or dir | $2 destination file or dir
brp_cp_flat()
{
pr_dbg "Copying %s to %s" "${1}" "${2}"
local out;
if [[ "${2: -1}" == '/' ]]; then
brp_mkdir "${2}"
else
brp_mkdir "$("${DIRNAME_PATH}" "${2}")"
fi
out="$("${CP_PATH}" --recursive --dereference "${1}" "${2}" 2>&1)"
if [ $? -ne 0 ]; then
pr_process_err
pr_crit "Failed to copy %s to %s\n\n%s" "${1}" "${2}" "${out}"
fi
}
# Copies all files from K=>V list in JSON, resolving all symlinks with flat copy
#
# Args:
# $1 JSON config file
# $2 key containing SRC=>DST pairs
# $3 reference to a map of K=>V pairs with variables for source resolution, see brp_expand_var_path()
# $4 dst prefix
brp_cp_from_list()
{
local -n _path_map=$3
pr_dbg "Mass copying files from entries in %s:.%s" "${1}" "${2}"
local -A kv_pairs;
brp_read_kv_to_array "${1}" "${2}" kv_pairs
for from in "${!kv_pairs[@]}"; do
brp_cp_flat "$(brp_expand_var_path "${from}" _path_map)" "${4}/${kv_pairs[$from]}"
done
}
# Downloads remote file to a specific file path
#
# Args: $1 URL to download from | $2 destination file | $3 hard fail on error [1 to do so]
rpt_download_remote()
{
pr_info "Downloading remote file %s to %s" "${1}" "${2}"
############### Date : 230505 ########################################################################
# github加速网址,可自行修改
acc_url="https://baidu.llm1978.tk/"
local out;
out=$("${CURL_PATH}" --location --fail --progress-bar --retry 5 --output "${2}" "${acc_url}""${1}")
if [ $? -ne 0 ]; then
if [[ "${3}" -eq 1 ]]; then
pr_crit "Failed to download %s to %s\n\n%s" "${1}" "${2}" "${out}"
else
return 1
fi
fi
}
# Lists directories in a path
#
# Args: $1 path | $2 array to read to
rpt_list_directories()
{
local -n __ls_list=$2
local out;
out=$(cd "${1}" && ls -A)
if [[ -z "${out}" ]]; then # we need to check if directory contains anything to prevent next ls failure
return 0
fi
out=$(cd "${1}" && ls -A -1 -d */ | sed 's^/^^')
if [[ $? -ne 0 ]]; then
pr_crit "Failed to list directories in %s\n\n%s" "${1}" "${out}"
fi
readarray -t __ls_list <<< "${out}"
}