Normalize the root path

This makes sure there's never any confusion about whether it is an
absolute path or has symlinks or whatever.  Add e2e cases to cover.
This commit is contained in:
Tim Hockin 2020-11-07 11:38:48 -08:00
parent 93e74c2527
commit 24c06a5b48
2 changed files with 88 additions and 5 deletions

View File

@ -398,6 +398,16 @@ func main() {
} }
} }
if err := os.MkdirAll(*flRoot, 0700); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't make root dir: %v", err)
os.Exit(1)
}
absRoot, err := normalizePath(*flRoot)
if err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't normalize root path: %v", err)
os.Exit(1)
}
if *flAddUser { if *flAddUser {
if err := addUser(); err != nil { if err := addUser(); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: can't write to /etc/passwd: %v\n", err) fmt.Fprintf(os.Stderr, "ERROR: can't write to /etc/passwd: %v\n", err)
@ -496,7 +506,7 @@ func main() {
for { for {
start := time.Now() start := time.Now()
ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout) ctx, cancel := context.WithTimeout(context.Background(), *flSyncTimeout)
if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, *flRoot, *flLink, *flAskPassURL, *flSubmodules); err != nil { if changed, hash, err := syncRepo(ctx, *flRepo, *flBranch, *flRev, *flDepth, absRoot, *flLink, *flAskPassURL, *flSubmodules); err != nil {
updateSyncMetrics(metricKeyError, start) updateSyncMetrics(metricKeyError, start)
if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures { if *flMaxSyncFailures != -1 && failCount >= *flMaxSyncFailures {
// Exit after too many retries, maybe the error is not recoverable. // Exit after too many retries, maybe the error is not recoverable.
@ -523,7 +533,7 @@ func main() {
if *flOneTime { if *flOneTime {
os.Exit(0) os.Exit(0)
} }
if isHash, err := revIsHash(ctx, *flRev, *flRoot); err != nil { if isHash, err := revIsHash(ctx, *flRev, absRoot); err != nil {
log.Error(err, "can't tell if rev is a git hash, exiting", "rev", *flRev) log.Error(err, "can't tell if rev is a git hash, exiting", "rev", *flRev)
os.Exit(1) os.Exit(1)
} else if isHash { } else if isHash {
@ -540,6 +550,18 @@ func main() {
} }
} }
func normalizePath(path string) (string, error) {
delinked, err := filepath.EvalSymlinks(path)
if err != nil {
return "", err
}
abs, err := filepath.Abs(delinked)
if err != nil {
return "", err
}
return abs, nil
}
func updateSyncMetrics(key string, start time.Time) { func updateSyncMetrics(key string, start time.Time) {
syncDuration.WithLabelValues(key).Observe(time.Since(start).Seconds()) syncDuration.WithLabelValues(key).Observe(time.Since(start).Seconds())
syncCount.WithLabelValues(key).Inc() syncCount.WithLabelValues(key).Inc()

View File

@ -144,10 +144,30 @@ function remove_containers() {
} }
############################################## ##############################################
# Test HEAD one-time # Test HEAD one-time when root doesn't exist
############################################## ##############################################
testcase "head-once" testcase "head-once-root-doesnt-exist"
# First sync echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
rm -rf "$ROOT" # remove the root to test
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="$ROOT" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass
##############################################
# Test HEAD one-time when root exists
##############################################
testcase "head-once-root-exists"
echo "$TESTCASE" > "$REPO"/file echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE" git -C "$REPO" commit -qam "$TESTCASE"
GIT_SYNC \ GIT_SYNC \
@ -164,6 +184,47 @@ assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up # Wrap up
pass pass
##############################################
# Test HEAD one-time with a weird --root flag
##############################################
testcase "head-once-root-flag-is-weird"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="../../../../../$ROOT/../../../../../../$ROOT" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass
##############################################
# Test HEAD one-time with a symlink in --root
##############################################
testcase "head-once-root-flag-has-symlink"
echo "$TESTCASE" > "$REPO"/file
git -C "$REPO" commit -qam "$TESTCASE"
ln -s "$ROOT" "$DIR/rootlink" # symlink to test
GIT_SYNC \
--one-time \
--repo="file://$REPO" \
--branch=master \
--rev=HEAD \
--root="$DIR/rootlink" \
--link="link" \
> "$DIR"/log."$TESTCASE" 2>&1
assert_link_exists "$ROOT"/link
assert_file_exists "$ROOT"/link/file
assert_file_eq "$ROOT"/link/file "$TESTCASE"
# Wrap up
pass
############################################## ##############################################
# Test default syncing # Test default syncing
############################################## ##############################################