mirror of
https://gitlab.alpinelinux.org/alpine/abuild.git
synced 2026-11-20 17:23:06 +03:00
The current chmod-clean logic is not sufficient as it only covers srcdir. Notably go frequently writes read-only files/directories to tmpdir: https://gitlab.alpinelinux.org/alpine/aports/-/issues/17999 Since all of these directories are "owned" by abuild (as in abuild creates and cleans them) we know that read-only files should always be deleted. That means the "chmod-clean" option can be implied which simplifies APKBUILDs. It may be slow to always use `chmod -R +w "$srcdir"` even when it is not needed in most cases. By implementing a custom rm in C we only need to recurse once and can re-use the already required fstatat syscall to check if a fchmodat syscall is needed. abuild-rm acts similar to busybox rm -rf with the following differences: - tries to use fchmodat - does not follow symlinks that replaced directories while recursing https://lists.busybox.net/pipermail/busybox/2026-February/091908.html
53 lines
1.2 KiB
C
53 lines
1.2 KiB
C
#include <dirent.h>
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <unistd.h>
|
|
|
|
static int recursive_clean(int dirfd, const char *dirname) {
|
|
struct stat sb;
|
|
DIR *dir;
|
|
struct dirent *dirent;
|
|
int fd;
|
|
|
|
if (fstatat(dirfd, dirname, &sb, AT_SYMLINK_NOFOLLOW) < 0)
|
|
return errno != ENOTDIR && errno != ENOENT ? -1 : 0;
|
|
|
|
if (!S_ISDIR(sb.st_mode))
|
|
return unlinkat(dirfd, dirname, 0);
|
|
|
|
if ((sb.st_mode & S_IRWXU) != S_IRWXU)
|
|
fchmodat(dirfd, dirname, S_IRWXU, AT_SYMLINK_NOFOLLOW);
|
|
|
|
fd = openat(dirfd, dirname, O_RDONLY|O_DIRECTORY|O_NOFOLLOW);
|
|
if (fd < 0)
|
|
return -1;
|
|
dir = fdopendir(fd);
|
|
if (dir == NULL) {
|
|
close(fd);
|
|
return -1;
|
|
}
|
|
|
|
while ((dirent = readdir(dir)) != NULL) {
|
|
if (dirent->d_name[0] == '.' && (dirent->d_name[1] == '\0' || (dirent->d_name[1] == '.' && dirent->d_name[2] == '\0')))
|
|
continue;
|
|
|
|
if (recursive_clean(fd, dirent->d_name) < 0)
|
|
break;
|
|
}
|
|
closedir(dir);
|
|
|
|
return unlinkat(dirfd, dirname, AT_REMOVEDIR);
|
|
}
|
|
|
|
int main(int argc, char **argv) {
|
|
if (argc != 3 || strcmp(argv[1], "-rf")) {
|
|
fprintf(stderr, "Usage: abuild-rm -rf <path>\n");
|
|
return 1;
|
|
}
|
|
|
|
return recursive_clean(AT_FDCWD, argv[2]) == -1 ? 1 : 0;
|
|
}
|