Skip to content

Commit 750012c

Browse files
authored
Add fchmodat() as an option to set symlink Unix access permissions (#1108)
* Add fchmodat() as an option to set symlink Unix access permissions syscall.c: Add fchmodat(), if it exists on the system, as a last-resort attempt to change the Unix access permissions for a symlink (on those systems that support it). This is done just prior to finally abandoning such an attempt and after any other available options for this, such as lchmod() or setattrlist(). This option is required to preserve symlink access permissions when making copies using rsync on OpenBSD. The patch itself is not OS-specific. testsuite: Add a test, symlink-unix-perms_test.py, to testsuite to check on the success of setting symlink access changes using rsync. The test is skipped on those systems that do not allow symlink access permission changes. The test checks at runtime whether or not the system permits symlink access changes. As with the patch, this test is not OS-specific. * testsuite: Modify the symlink-unix-perms test to conform to proper standards. 1) Set the proper python shebang 2) Remove the use of random test access values in favour of fixed values. 3) Skip the test only on "not implemented" or "not supported" exceptions. All other unexpected errors will fail the test. 4) Test using symlinks to a specifically created directory or file as opposed to the '.' directory. The latter could potentially lead to cyclical paths. Some code tweaking has also been done for readability. * symlink-unix-perms_test.py: Perform some useful cleanup. 1) Remove unnecessary import of "random". 2) Consolidate some unnecessarily verbose and inefficient code. * testsuite: expect unsupported symlink permission skips
1 parent 40c9a02 commit 750012c

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

‎syscall.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1809,6 +1809,8 @@ int do_chmod(const char *path, mode_t mode)
18091809
break;
18101810
if (errno == ENOTSUP)
18111811
code = 1;
1812+
# elif defined AT_FDCWD && defined AT_SYMLINK_NOFOLLOW
1813+
code = fchmodat(AT_FDCWD, path, mode & CHMOD_BITS, AT_SYMLINK_NOFOLLOW);
18121814
# else
18131815
code = 1;
18141816
# endif

‎testsuite/skiplist/cygwin.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,5 @@ source-change-size-continues
6868
symlink-dest-backupdir
6969
symlink-race-dest
7070
symlink-race-relative-dest
71+
symlink-unix-perms # platform does not support changing symlink permissions
7172
temp-dir-symlink-injection

‎testsuite/skiplist/linux.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@
88
crtimes # Rsync is configured without crtimes support
99
partial-protected-regular-retry-policy # deterministic partial EACCES recovery uses dyld interposing
1010
readonly-partial-abort-mode-regression #
11+
symlink-unix-perms # platform does not support changing symlink permissions
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/usr/bin/env python3
2+
3+
import errno
4+
import os
5+
import stat
6+
import sys
7+
8+
from rsyncfns import (
9+
FROMDIR, SCRATCHDIR, TODIR,
10+
assert_mode, rmtree, run_rsync, test_fail, test_skipped
11+
)
12+
13+
# Put some error messages into variable to make later code more readable.
14+
15+
error = 'error occurred during test of symlink access change capability: '
16+
notimplemented = 'symlink Unix access change is not implemented on this system'
17+
notsupported = 'symlink Unix access change is not supported on this system'
18+
19+
# Make sure that the FROMDIR and TODIR directories exist. These are the
20+
# source and destination directories, respectively, for the test.
21+
22+
os.chdir(SCRATCHDIR)
23+
24+
FROMDIR.mkdir(parents=True, exist_ok=True)
25+
TODIR.mkdir(parents=True, exist_ok=True)
26+
27+
# Create a test directory and a test file in FROMDIR for use later as symlink
28+
# targets, saving the actual names of each in a variable that will also be
29+
# used later.
30+
31+
testdirectory = 'testdirectory'
32+
testfile = 'testfile'
33+
34+
(FROMDIR / testdirectory).mkdir(parents=True)
35+
(FROMDIR / testfile).touch()
36+
37+
# See if the system permits setting Unix access permssions on symlinks.
38+
# If not, there's no point in continuing.
39+
#
40+
# To test this, the following code creates a symlink in FROMDIR that points
41+
# to the test directory there. It then tries to change the Unix access
42+
# permission for that symlink. If the result is a "not implemented" or a
43+
# "not supported" error, the actual test is skipped. If any other error is
44+
# returned, the test reports failure.
45+
46+
os.chdir(FROMDIR)
47+
48+
os.symlink(testdirectory, 'symlink', target_is_directory=True)
49+
50+
try:
51+
os.chmod('symlink', stat.S_IREAD + stat.S_IXOTH, follow_symlinks=False)
52+
53+
except NotImplementedError:
54+
test_skipped(notimplemented)
55+
56+
except OSError as e:
57+
if (e.errno == errno.ENOTSUP):
58+
test_skipped(notsupported)
59+
test_fail('OS' + error + f'{e}')
60+
61+
except Exception as e:
62+
test_fail(error + f'{e}')
63+
64+
# The symlink access change attempt reported success. Make sure that the
65+
# Unix access permissions were actually changed. If not, the test fails.
66+
67+
assert_mode('symlink', stat.S_IREAD + stat.S_IXOTH)
68+
69+
# The system can change symlink Unix access permissions. Continue ...
70+
71+
os.remove('symlink')
72+
73+
os.chdir(SCRATCHDIR)
74+
75+
# Create a number of symlinks to use for testing rsync.
76+
#
77+
# Set the Unix access permissions for those symlinks to selected values.
78+
# The selected values have no specific agenda. However, owner read access
79+
# is always maintained to ensure that stat info is readable.
80+
81+
# The following array consists of a number of targets and associated Unix
82+
# access permissions that will be used in the actual test. The symlink
83+
# names will be of the form "symlink-{n}" where "{n}" is the array index
84+
# for each symlink created.
85+
86+
symlinks = [
87+
[ testdirectory, 0o770 ],
88+
[ testfile, 0o0444 ],
89+
[ testfile, 0o0711 ],
90+
[ testfile, 0o0777 ],
91+
[ testdirectory, 0o7777 ],
92+
[ testfile, 0o0400 ],
93+
[ testdirectory, 0o1407 ],
94+
[ testdirectory, 0o2470 ],
95+
[ testdirectory, 0o4777 ],
96+
[ testfile, 0o7777 ],
97+
]
98+
99+
# Actually create the test symlinks in the source directory.
100+
101+
for n, symlink in enumerate(symlinks):
102+
target, access = symlink
103+
104+
os.symlink(target, FROMDIR / f'symlink-{n}', target_is_directory=(target == testdirectory))
105+
os.chmod(FROMDIR / f'symlink-{n}', access, follow_symlinks=False)
106+
107+
# Now, use rsync to copy the source directory to the destination.
108+
109+
run_rsync('-av', f'{FROMDIR}/', f'{TODIR}/')
110+
111+
# Finally, check to make sure that the Unix access permissions on the
112+
# symlinks in the destination match those in the source. The test fail
113+
# if any one doesn't match.
114+
115+
for n, symlink in enumerate(symlinks):
116+
expected = stat.S_IMODE(os.stat(FROMDIR / f'symlink-{n}', follow_symlinks=False).st_mode)
117+
assert_mode(TODIR / f'symlink-{n}', expected)

0 commit comments

Comments
 (0)