Skip to content

Commit 1e5bfa2

Browse files
committed
fs-util: drop chmod_and_chown_unsafe() which is unused now
1 parent 62c0339 commit 1e5bfa2

File tree

3 files changed

+0
-92
lines changed

3 files changed

+0
-92
lines changed

src/basic/fs-util.c

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -280,52 +280,6 @@ int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid) {
280280
return do_chown || do_chmod;
281281
}
282282

283-
int chmod_and_chown_unsafe(const char *path, mode_t mode, uid_t uid, gid_t gid) {
284-
bool do_chown, do_chmod;
285-
struct stat st;
286-
287-
assert(path);
288-
289-
/* Change ownership and access mode of the specified path, see description of fchmod_and_chown().
290-
* Should only be used on trusted paths. */
291-
292-
if (lstat(path, &st) < 0)
293-
return -errno;
294-
295-
do_chown =
296-
(uid != UID_INVALID && st.st_uid != uid) ||
297-
(gid != GID_INVALID && st.st_gid != gid);
298-
299-
do_chmod =
300-
!S_ISLNK(st.st_mode) && /* chmod is not defined on symlinks */
301-
((mode != MODE_INVALID && ((st.st_mode ^ mode) & 07777) != 0) ||
302-
do_chown); /* If we change ownership, make sure we reset the mode afterwards, since chown()
303-
* modifies the access mode too */
304-
305-
if (mode == MODE_INVALID)
306-
mode = st.st_mode; /* If we only shall do a chown(), save original mode, since chown() might break it. */
307-
else if ((mode & S_IFMT) != 0 && ((mode ^ st.st_mode) & S_IFMT) != 0)
308-
return -EINVAL; /* insist on the right file type if it was specified */
309-
310-
if (do_chown && do_chmod) {
311-
mode_t minimal = st.st_mode & mode; /* the subset of the old and the new mask */
312-
313-
if (((minimal ^ st.st_mode) & 07777) != 0)
314-
if (chmod(path, minimal & 07777) < 0)
315-
return -errno;
316-
}
317-
318-
if (do_chown)
319-
if (lchown(path, uid, gid) < 0)
320-
return -errno;
321-
322-
if (do_chmod)
323-
if (chmod(path, mode & 07777) < 0)
324-
return -errno;
325-
326-
return do_chown || do_chmod;
327-
}
328-
329283
int fchmod_umask(int fd, mode_t m) {
330284
mode_t u;
331285
int r;

src/basic/fs-util.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ int readlink_and_make_absolute(const char *p, char **r);
3434

3535
int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
3636
int fchmod_and_chown(int fd, mode_t mode, uid_t uid, gid_t gid);
37-
int chmod_and_chown_unsafe(const char *path, mode_t mode, uid_t uid, gid_t gid);
3837

3938
int fchmod_umask(int fd, mode_t mode);
4039
int fchmod_opath(int fd, mode_t m);

src/test/test-fs-util.c

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -802,50 +802,6 @@ static void test_chmod_and_chown(void) {
802802
assert_se(S_ISLNK(st.st_mode));
803803
}
804804

805-
static void test_chmod_and_chown_unsafe(void) {
806-
_cleanup_(rm_rf_physical_and_freep) char *d = NULL;
807-
_unused_ _cleanup_umask_ mode_t u = umask(0000);
808-
struct stat st;
809-
const char *p;
810-
811-
if (geteuid() != 0)
812-
return;
813-
814-
log_info("/* %s */", __func__);
815-
816-
assert_se(mkdtemp_malloc(NULL, &d) >= 0);
817-
818-
p = strjoina(d, "/reg");
819-
assert_se(mknod(p, S_IFREG | 0123, 0) >= 0);
820-
821-
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0321, 1, 2) >= 0);
822-
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
823-
824-
assert_se(lstat(p, &st) >= 0);
825-
assert_se(S_ISREG(st.st_mode));
826-
assert_se((st.st_mode & 07777) == 0321);
827-
828-
p = strjoina(d, "/dir");
829-
assert_se(mkdir(p, 0123) >= 0);
830-
831-
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0321, 1, 2) >= 0);
832-
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0555, 3, 4) == -EINVAL);
833-
834-
assert_se(lstat(p, &st) >= 0);
835-
assert_se(S_ISDIR(st.st_mode));
836-
assert_se((st.st_mode & 07777) == 0321);
837-
838-
p = strjoina(d, "/lnk");
839-
assert_se(symlink("idontexist", p) >= 0);
840-
841-
assert_se(chmod_and_chown_unsafe(p, S_IFLNK | 0321, 1, 2) >= 0);
842-
assert_se(chmod_and_chown_unsafe(p, S_IFREG | 0555, 3, 4) == -EINVAL);
843-
assert_se(chmod_and_chown_unsafe(p, S_IFDIR | 0555, 3, 4) == -EINVAL);
844-
845-
assert_se(lstat(p, &st) >= 0);
846-
assert_se(S_ISLNK(st.st_mode));
847-
}
848-
849805
static void test_path_is_encrypted_one(const char *p, int expect) {
850806
int r;
851807

@@ -895,7 +851,6 @@ int main(int argc, char *argv[]) {
895851
test_fsync_directory_of_file();
896852
test_rename_noreplace();
897853
test_chmod_and_chown();
898-
test_chmod_and_chown_unsafe();
899854
test_path_is_encrypted();
900855

901856
return 0;

0 commit comments

Comments
 (0)