Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scripts/launchd/backup-daily.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,10 @@ set -euo pipefail

export PATH="/opt/homebrew/bin:/usr/local/bin:/usr/bin:/bin:$HOME/.local/bin"
export PYTHONUNBUFFERED=1
BRAINLAYER_DIR="${BRAINLAYER_DIR:-__BRAINLAYER_DIR_VALUE__}"
case "$BRAINLAYER_DIR" in
__BRAINLAYER_DIR_*) BRAINLAYER_DIR="$HOME/Gits/brainlayer" ;;
esac
export PYTHONPATH="$BRAINLAYER_DIR/src${PYTHONPATH:+:$PYTHONPATH}"

exec "${BRAINLAYER_PYTHON:-python3}" -m brainlayer.backup_daily
6 changes: 5 additions & 1 deletion scripts/launchd/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ install_plist() {
install_backup_script() {
local src="$SCRIPT_DIR/backup-daily.sh"
local dst="$BRAINLAYER_LIB_DIR/backup-daily.sh"
local escaped_brainlayer_dir

if [ ! -f "$src" ]; then
echo "ERROR: $src not found"
return 1
fi

cp "$src" "$dst"
escaped_brainlayer_dir="$(printf '%s' "$BRAINLAYER_DIR" | sed 's/[\\&|]/\\&/g')"
sed \
-e "s|__BRAINLAYER_DIR_VALUE__|$escaped_brainlayer_dir|g" \
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Quote the embedded repository path for the shell

When the checkout path contains shell syntax characters such as ", `, or $(, this writes the raw path into the generated backup-daily.sh assignment; sed escaping only protects the sed replacement, not the shell source that will be parsed later. For example a repo under a directory containing " produces an unterminated BRAINLAYER_DIR="${BRAINLAYER_DIR:-...}" line, so manual and launchd backup runs fail before importing brainlayer; use shell-safe quoting/escaping for the value before embedding it.

Useful? React with 👍 / 👎.

"$src" > "$dst"
chmod 755 "$dst"
echo "Installed: $dst"
}
Expand Down
7 changes: 7 additions & 0 deletions tests/test_backup_daily.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,16 +100,23 @@ def files(self):

def test_launchd_installer_knows_backup_target():
install_path = Path("scripts/launchd/install.sh")
wrapper_path = Path("scripts/launchd/backup-daily.sh")
plist_path = Path("scripts/launchd/com.brainlayer.backup-daily.plist")

assert install_path.is_file(), f"Installer not found at {install_path}; check test working directory"
assert wrapper_path.is_file(), f"Backup wrapper not found at {wrapper_path}; check launchd wrapper is committed"
assert plist_path.is_file(), f"Backup plist not found at {plist_path}; check launchd template is committed"

install = install_path.read_text()
wrapper = wrapper_path.read_text()
plist = plist_path.read_text()

assert "backup-daily" in install
assert "install_backup_script" in install
assert "escaped_brainlayer_dir" in install
assert "__BRAINLAYER_DIR_VALUE__" in install
assert "PYTHONPATH" in wrapper
assert "__BRAINLAYER_DIR_VALUE__" in wrapper
assert "<string>com.brainlayer.backup-daily</string>" in plist
assert "<integer>3</integer>" in plist
assert "<integer>17</integer>" in plist
Loading