Skip to content

Commit

Permalink
lua: filecmp: Fix for symlink
Browse files Browse the repository at this point in the history
When doing filecmp on symlink(s) we should compare the actual symlinked file
so do not use AT_SYMLINK_NOFOLLOW otherwise we will compare the symlink and not
the symlinked file.
This mimic cmp(1)

Add tests cases to be sure that this doesn't regress in the futur.
  • Loading branch information
evadot committed Feb 18, 2021
1 parent 1b7f519 commit 240e1aa
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 6 deletions.
8 changes: 2 additions & 6 deletions libpkg/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,14 @@ lua_pkg_filecmp(lua_State *L)
lua_getglobal(L, "package");
struct pkg *pkg = lua_touserdata(L, -1);

if (fstatat(pkg->rootfd, RELATIVE_PATH(file1), &s1, AT_SYMLINK_NOFOLLOW) == -1) {
if (fstatat(pkg->rootfd, RELATIVE_PATH(file1), &s1, 0) == -1) {
lua_pushinteger(L, 2);
return (1);
}
if (fstatat(pkg->rootfd, RELATIVE_PATH(file2), &s2, AT_SYMLINK_NOFOLLOW) == -1) {
if (fstatat(pkg->rootfd, RELATIVE_PATH(file2), &s2, 0) == -1) {
lua_pushinteger(L, 2);
return (1);
}
if (!S_ISREG(s1.st_mode) || !S_ISREG(s2.st_mode)) {
lua_pushinteger(L, -1);
return (1);
}
if (s1.st_size != s2.st_size) {
lua_pushinteger(L, 1);
return (1);
Expand Down
86 changes: 86 additions & 0 deletions tests/frontend/lua.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ tests_init \
script_rename \
script_upgrade \
script_filecmp \
script_filecmp_symlink \
script_sample_not_exists \
script_sample_exists \
script_stat \
Expand Down Expand Up @@ -362,6 +363,91 @@ EOF
pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
}

script_filecmp_symlink_body() {
echo "sametext" > a
echo "sametext" > b
ln -s a c
ln -s b d
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
cat << EOF >> test.ucl
files: {
${TMPDIR}/a: ""
${TMPDIR}/b: ""
${TMPDIR}/c: ""
${TMPDIR}/d: ""
}
lua_scripts: {
post-install: [ <<EOS
if pkg.filecmp("${TMPDIR}/c", "${TMPDIR}/d") == 0 then
pkg.print_msg("same")
else
pkg.print_msg("different")
end
EOS
, ]
}
EOF

atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg create -M test.ucl

mkdir ${TMPDIR}/target
atf_check \
-o inline:"same\n" \
-e empty \
-s exit:0 \
pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz

# Cleanup
atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target delete -qfy test-1
rm -rf ${TMPDIR}/target
rm a b c d

echo "sametext" > a
echo "differenttext" > b
ln -s a c
ln -s b d
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
cat << EOF >> test.ucl
files: {
${TMPDIR}/a: ""
${TMPDIR}/b: ""
${TMPDIR}/c: ""
${TMPDIR}/d: ""
}
lua_scripts: {
post-install: [ <<EOS
if pkg.filecmp("${TMPDIR}/c", "${TMPDIR}/d") == 0 then
pkg.print_msg("same")
else
pkg.print_msg("different")
end
EOS
, ]
}
EOF

atf_check \
-o empty \
-e empty \
-s exit:0 \
pkg create -M test.ucl

mkdir ${TMPDIR}/target
atf_check \
-o inline:"different\n" \
-e empty \
-s exit:0 \
pkg -o REPOS_DIR=/dev/null -r ${TMPDIR}/target install -qfy ${TMPDIR}/test-1.txz
}

script_sample_not_exists_body() {
echo "sample text" > a.sample
atf_check -s exit:0 sh ${RESOURCEDIR}/test_subr.sh new_pkg "test" "test" "1"
Expand Down

0 comments on commit 240e1aa

Please sign in to comment.