Skip to content

Commit 096398f

Browse files
committed
fix: detect logged-in user's home directory when running as root
When asimov runs as root (via brew services or sudo), ~ expands to /var/root. Now detects the console user via stat/dscl and uses their home directory instead. Addresses stevegrunwell#72.
1 parent 64834be commit 096398f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
3030
* Added `scripts/uninstall.sh` to cleanly remove Asimov and its launchd schedule ([#35], props @sylver)
3131
* Added common interval reference comments to `com.stevegrunwell.asimov.plist` ([#35], props @sylver)
3232

33+
### Fixed
34+
35+
* Detect the logged-in user's home directory when running as root, fixing `brew services` and `sudo` invocations that would search `/var/root` instead ([stevegrunwell/asimov#72])
36+
3337
### Changed
3438

3539
* Migrated test suite from PHP/PHPUnit to [Bats](https://github.com/bats-core/bats-core) (Bash Automated Testing System), removing the PHP dependency for contributors
@@ -117,3 +121,4 @@ Initial public release.
117121
[#55]: https://github.com/stevegrunwell/asimov/pull/55
118122
[#35]: https://github.com/stevegrunwell/asimov/pull/35
119123
[#56]: https://github.com/stevegrunwell/asimov/pull/56
124+
[stevegrunwell/asimov#72]: https://github.com/stevegrunwell/asimov/issues/72

asimov

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,19 @@ set -Eeu -o pipefail
1717
# @author Steve Grunwell
1818
# @license MIT
1919

20-
readonly ASIMOV_ROOT=~
20+
# When running as root (e.g. via launchd or sudo), determine the logged-in
21+
# user's home directory instead of using /var/root.
22+
if [[ "${EUID:-$(id -u)}" -eq 0 ]]; then
23+
_console_user="$(stat -f '%Su' /dev/console 2>/dev/null || echo '')"
24+
if [[ -n "$_console_user" && "$_console_user" != "root" ]]; then
25+
_root_dir="$(dscl . -read "/Users/${_console_user}" NFSHomeDirectory 2>/dev/null | awk '{print $2}')"
26+
readonly ASIMOV_ROOT="$_root_dir"
27+
else
28+
readonly ASIMOV_ROOT=~
29+
fi
30+
else
31+
readonly ASIMOV_ROOT=~
32+
fi
2133

2234
# Paths to unconditionally skip over. This prevents Asimov from modifying the
2335
# Time Machine exclusions for these paths (and decendents). It has an important

tests/behavior.bats

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,17 @@ load test_helper
117117
[[ "$(count_exclusions)" -eq 1 ]]
118118
}
119119

120+
# =============================================================================
121+
# ASIMOV_ROOT detection
122+
# =============================================================================
123+
124+
@test "uses HOME as root directory when not running as root" {
125+
create_project "Code/My-Project" "package.json" "node_modules"
126+
run_asimov
127+
assert_excluded "${HOME}/Code/My-Project/node_modules"
128+
[[ "$(count_exclusions)" -eq 1 ]]
129+
}
130+
120131
@test "does not descend into excluded dependency directories" {
121132
# A node_modules inside another node_modules should not be separately excluded
122133
create_project "Code/My-Project" "package.json" "node_modules"

0 commit comments

Comments
 (0)