Skip to content

Commit 0cf3fb1

Browse files
committed
base-files: board_detect: Make robust against power-cuts
If board_detect is interrupted by cutting power on first boot, board.json might only be half-way written and the file will not be written again correctly on subsequent boots. Write to a temporary file first, then rename. Since a rename on the same file system is an atomic operation, it ensures that either /etc/board.json does not exist or that the complete file exists. Signed-off-by: Andreas Gnau <andreas.gnau@iopsys.eu>
1 parent dc79d0a commit 0cf3fb1

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,22 @@
11
#!/bin/sh
22

3-
CFG=$1
3+
REAL_CFG=$1
44

5-
[ -n "$CFG" ] || CFG=/etc/board.json
5+
[ -n "$REAL_CFG" ] || REAL_CFG=/etc/board.json
66

7-
if [ -d "/etc/board.d/" ] && [ ! -s "$CFG" ]; then
7+
if [ -d "/etc/board.d/" -a ! -s "$REAL_CFG" ]; then
8+
# Use temp file to prevent incomplete file on power-cut, CFG is used by the sourced to read/write the file
9+
CFG="$(mktemp -p "$(dirname "$REAL_CFG")" "~$(basename "$REAL_CFG").XXXXXX")"
10+
[ -n "$CFG" ] || return
811
for a in $(ls /etc/board.d/*); do
912
[ -s "$a" ] || continue
1013
(. "$a")
1114
done
1215
fi
1316

14-
[ -s "$CFG" ] || return 1
17+
if [ -s "$CFG" ]; then
18+
mv "$CFG" "$REAL_CFG" || return
19+
else
20+
rm -f "$CFG"
21+
return 1
22+
fi

0 commit comments

Comments
 (0)