Skip to content

Commit 9b5052f

Browse files
andy5995claude
andcommitted
fix: strip trailing slash in resolve_path to fix wrong restore destination
g_path_get_dirname("foo/") returns "foo" rather than "." (unlike POSIX dirname), causing rmw dir/ to restore into dir/dir. Strip the trailing slash from the input before calling g_path_get_dirname. Add regression tests for both trailing-slash-on-directory (restore path) and trailing-slash-on-regular-file (must be rejected without moving). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 2bca4c6 commit 9b5052f

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/utils.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ resolve_path(const char *file, const char *b)
227227
char tmp[req_len];
228228
strcpy(tmp, file);
229229

230+
/* g_path_get_dirname("foo/") returns "foo", not "." — strip trailing slash
231+
so it behaves like POSIX dirname */
232+
size_t tlen = strlen(tmp);
233+
if (tlen > 1 && tmp[tlen - 1] == '/')
234+
tmp[tlen - 1] = '\0';
235+
230236
gchar *dir = g_path_get_dirname(tmp);
231237
char *orig_dirname = realpath(dir, NULL);
232238
g_free(dir);

test/test_restore.sh

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,26 @@ if [ -n "$(command -v Xvfb)" ] && ! grep -q "DISABLE_CURSES" "$MESON_BUILD_ROOT/
146146
fi
147147
fi
148148

149+
# Regression test: rmw file/ (trailing slash on regular file) must not move it
150+
echo "$SEPARATOR"
151+
echo "Trailing slash on regular file: must be rejected cleanly"
152+
cd "${RMW_FAKE_HOME}"
153+
touch trailing_slash_file.txt
154+
${RMW_TEST_CMD_STRING} trailing_slash_file.txt/ || true
155+
test -f "${RMW_FAKE_HOME}/trailing_slash_file.txt"
156+
157+
# Regression test: rmw dir/ (trailing slash) then restore must not create dir/dir
158+
echo "$SEPARATOR"
159+
echo "Trailing slash: rmw dir/ then restore"
160+
cd "${RMW_FAKE_HOME}"
161+
mkdir -p trailing_slash_test
162+
touch trailing_slash_test/canary
163+
${RMW_TEST_CMD_STRING} trailing_slash_test/
164+
test ! -d trailing_slash_test
165+
test -d "${PRIMARY_WASTE_DIR}/files/trailing_slash_test"
166+
${RMW_TEST_CMD_STRING} -u
167+
test -d "${RMW_FAKE_HOME}/trailing_slash_test"
168+
test -f "${RMW_FAKE_HOME}/trailing_slash_test/canary"
169+
test ! -d "${RMW_FAKE_HOME}/trailing_slash_test/trailing_slash_test"
170+
149171
exit 0

0 commit comments

Comments
 (0)