rm_r.c (f94594b37a145b9b3e9ff31af2cd1dc3de8aa4d4) rm_r.c (1d981d8ef5ad488747e2499adba87b8cafdb1830)
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 36 unchanged lines hidden (view full) ---

45 DIR *d;
46 struct dirent *e;
47 struct stat st;
48
49 if (*path == '/')
50 path++;
51
52 dirfd = openat(rootfd, path, O_DIRECTORY);
1/*-
2 * Copyright (C) 1996
3 * David L. Nugent. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 36 unchanged lines hidden (view full) ---

45 DIR *d;
46 struct dirent *e;
47 struct stat st;
48
49 if (*path == '/')
50 path++;
51
52 dirfd = openat(rootfd, path, O_DIRECTORY);
53 if (dirfd == -1) {
54 return;
55 }
53
54 d = fdopendir(dirfd);
55 while ((e = readdir(d)) != NULL) {
56 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
57 continue;
58
59 if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
60 continue;
61 if (S_ISDIR(st.st_mode))
62 rm_r(dirfd, e->d_name, uid);
63 else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
64 unlinkat(dirfd, e->d_name, 0);
65 }
66 closedir(d);
67 if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
68 return;
69 unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0);
70}
56
57 d = fdopendir(dirfd);
58 while ((e = readdir(d)) != NULL) {
59 if (strcmp(e->d_name, ".") == 0 || strcmp(e->d_name, "..") == 0)
60 continue;
61
62 if (fstatat(dirfd, e->d_name, &st, AT_SYMLINK_NOFOLLOW) != 0)
63 continue;
64 if (S_ISDIR(st.st_mode))
65 rm_r(dirfd, e->d_name, uid);
66 else if (S_ISLNK(st.st_mode) || st.st_uid == uid)
67 unlinkat(dirfd, e->d_name, 0);
68 }
69 closedir(d);
70 if (fstatat(rootfd, path, &st, AT_SYMLINK_NOFOLLOW) != 0)
71 return;
72 unlinkat(rootfd, path, S_ISDIR(st.st_mode) ? AT_REMOVEDIR : 0);
73}