-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfile_operations.sh
More file actions
executable file
·282 lines (242 loc) · 7.56 KB
/
file_operations.sh
File metadata and controls
executable file
·282 lines (242 loc) · 7.56 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
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
#!/bin/bash
#
# Copyright © 2024 Quintor B.V.
#
# BCLD is gelicentieerd onder de EUPL, versie 1.2 of
# – zodra ze zullen worden goedgekeurd door de Europese Commissie -
# latere versies van de EUPL (de "Licentie");
# U mag BCLD alleen gebruiken in overeenstemming met de licentie.
# U kunt een kopie van de licentie verkrijgen op:
#
# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
#
# Tenzij vereist door de toepasselijke wetgeving of overeengekomen in
# schrijven, wordt software onder deze licentie gedistribueerd
# gedistribueerd op een "AS IS"-basis,
# ZONDER ENIGE GARANTIES OF VOORWAARDEN, zowel
# expliciet als impliciet.
# Zie de licentie voor de specifieke taal die van toepassing is
# en de beperkingen van de licentie.
#
#
# Copyright © 2024 Quintor B.V.
#
# BCLD is licensed under the EUPL, Version 1.2 or
# – as soon they will be approved by the European Commission -
# subsequent versions of the EUPL (the "Licence");
# You may not use BCLD except in compliance with the Licence.
# You may obtain a copy of the License at:
#
# https://joinup.ec.europa.eu/collection/eupl/eupl-text-eupl-12
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the License is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the License for the specific language governing
# permissions and limitations under the License.
#
#
# BCLD File Operations
# File operations for BCLD using echo_tools.sh
# Some operations are so important, that they must fail with an exit, because we cannot continue without these operations
source './script/echo_tools.sh'
source './config/BUILD.conf'
# Return to previous directory safely
function safe_return () {
cd - &> /dev/null || exit
}
# Prepare directory
function prep_dir () {
if [[ ! -d ${1} ]]; then
list_item "${1} does not exist yet! Creating..."
/usr/bin/mkdir -p "${1}"
else
list_item "${1} already exists! Ignoring..."
fi
}
## Function to generate a soft link
# 1: Target
# 2: Link
function link_file {
if [[ -f ${1} ]]; then
list_item "Linking file $(basename "${1}") to: ${2}"
/usr/bin/ln -s "${1}" "${2}"
else
list_item "File does not exist!"
exit 1
fi
}
## Function to copy a configuration file
function copy_file {
if [[ -f ${1} ]]; then
list_item "Copying file $(/usr/bin/basename "${1}")..."
/usr/bin/cp ${1} ${2}
else
list_item "File does not exist!"
list_entry
/usr/bin/ls -alh "$(/usr/bin/dirname "${1}" )"
list_catch
list_exit
exit 1
fi
}
## Function to copy a configuration directory
function copy_directory {
if [[ -d ${1} ]]; then
list_item "Copying directory $(basename "${1}")..."
/usr/bin/cp -r ${1} ${2}
else
list_item "Directory \"${1}\" does not exist!"
exit 1
fi
}
## Function to copy a directory recursively
function copy_recursively {
if [[ -d ${1} ]]; then
list_item "Copying recursively $(basename "${1}")..."
/usr/bin/cp -r ${1}/* ${2}
else
list_item "Files do not exist!"
exit 1
fi
}
# Substitute file, overwrite
function subst_file () {
if [[ -f ${1} ]]; then
list_item "Substituting $(/usr/bin/basename "${2}")..."
/usr/bin/envsubst < "${1}" > "${2}"
else
list_item "Substitute file does not exist!"
exit 1
fi
}
# Substitute file, do not overwrite
function subst_file_add () {
if [[ -f ${1} ]]; then
list_item "Substituting $(/usr/bin/basename "${2}")..."
/usr/bin/envsubst < "${1}" >> "${2}"
else
list_item "Substitute file does not exist!"
exit 1
fi
}
# New method to add packages with feature toggle
# Usage: add_pkgs <source> <target>
# Will add package lists to an incremental list
function add_pkgs () {
pkg_file="${1}" # source file
pkg_list="${2}" # target file
file_name="$(/usr/bin/basename "${pkg_file}")" # file name
# Read each pkgs from the file
while IFS= read -r pkg; do
full_pkg="$(/usr/bin/echo "${pkg}" | /usr/bin/envsubst)"
if [[ "${full_pkg}" == \#* ]]; then
list_item_fail "Skipping package: ${full_pkg}"
else
/usr/bin/echo "${full_pkg}" >> "${pkg_list}"
fi
done < "${pkg_file}"
/usr/bin/echo >> "${pkg_list}"
list_item_pass "Succesfully added package list: ${file_name}"
}
# Function to delete file which already exists
function reset_file () {
base_name=$(basename "${1}")
if [[ -f "${1}" ]]; then
list_item "${base_name} already exists! Deleting ${base_name}..."
/usr/bin/rm -f "${1}"
fi
}
# Function to delete file if it exists with description
# 1: PATH
# 2: DESC
function delete_file () {
if [[ -f ${1} ]]; then
list_item "Deleting file: ${1} (${2})..."
/usr/bin/rm -f ${1}
else
list_item "Cannot delete ${1}: File does not exist..."
fi
}
# Function to delete existing directory if it exists with description
function delete_dir () {
if [[ -d ${1} ]]; then
list_item "Deleting directory: ${1} (${2})..."
/usr/bin/rm -rf ${1}
else
list_item "Cannot delete ${1}: Directory does not exist..."
fi
}
# Function to clean up environment
function clean_chroot () {
list_item "Cleaning up chroot..."
# Chroot
if [[ -d "${CHROOT_DIR}" ]]; then
list_item "${CHROOT_DIR} already exists, clearing contents..."
/usr/bin/rm -rf ${CHROOT_DIR}/*
else
list_item "Chroot does not exist yet, creating..."
prep_dir "${CHROOT_DIR}"
fi
}
# Function to check if ISO artifact is present
function check_iso_file () {
export ISO_NAME="bcld.iso"
if [[ -f "$(pwd)/artifacts/${ISO_NAME}" ]]; then
list_item_pass "bcld.iso found!"
else
list_item_fail "ISO-artifact missing!"
on_failure
fi
}
# Function to check size of image or fail
function check_img_size () {
list_item "Checking: ${1}..."
# Vars
IMG_THRESH=1000000 # Threshold in KB
IMG_SIZE="$(/usr/bin/du "${1}" | /usr/bin/awk ' {print $1} ')"
IMG_SIZE_HUMAN="$(/usr/bin/du -h "${1}" | /usr/bin/awk ' {print $1} ')"
# Compare
if [[ ${IMG_SIZE} -gt ${IMG_THRESH} ]]; then
list_item_pass "Size: ${IMG_SIZE_HUMAN}"
on_completion
else
list_item_fail "WARNING: SMALL BCLD-IMG (${IMG_SIZE_HUMAN})!"
on_failure
fi
}
# Function to remove old artifacts
function clean_art () {
list_item "Cleaning up old artifacts..."
# Can only work if directory exists...
if [[ -d "${ART_DIR}" ]]; then
# Scan for files inside the ART_DIR
art_count=$(/usr/bin/find "${ART_DIR}" -mindepth 1 -maxdepth 1 -type f | /usr/bin/wc -l)
if [[ "${art_count}" -gt 0 ]]; then
# If the directory exists, and isn't empty, clear it.
list_item "Removing old artifacts from ${ART_DIR}..."
list_entry
/usr/bin/rm -fv ${ART_DIR}/*
list_catch
fi
else
# If there is no match, then it means there are no artifacts yet.
list_item "No older artifacts detected."
fi
}
# Function to umount if mounted
function clear_mount () {
if [[ $(/usr/bin/mount -l | cut -d ' ' -f3 | grep -Ec "^${1}$") -gt 0 ]]; then
list_item "Unmounting ${1}..."
/usr/bin/umount -lfq "${1}"
else
list_item "Cannot unmount ${1}: not mounted..."
fi
}
# Function for cleaning up after the IMG generation
function clear_loop_devs () {
list_item "Detaching loop devices..."
/usr/sbin/losetup --detach-all
}