team-ljyzipelo

공개
127줄4.7 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <stdarg.h>
6#include <stdlib.h>
7#include <string.h>
8#include <stdio.h>
9#include <unistd.h>
10#include <sys/stat.h>
11#include <sys/statvfs.h>
12
13static ssize_t (*real_write)(int, const void *, size_t);
14static int (*real_openat)(int, const char *, int, ...);
15static int (*real_open64)(const char *, int, ...);
16static int (*real_fsync)(int);
17static int (*real_fstatvfs)(int, struct statvfs *);
18static int started;
19
20__attribute__((constructor)) static void initialize(void) {
21 real_write = dlsym(RTLD_NEXT, "write");
22 real_openat = dlsym(RTLD_NEXT, "openat");
23 real_open64 = dlsym(RTLD_NEXT, "open64");
24 real_fsync = dlsym(RTLD_NEXT, "fsync");
25 real_fstatvfs = dlsym(RTLD_NEXT, "fstatvfs");
26}
27
28static int output_file(int fd) {
29 char link[64], path[4096];
30 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
31 ssize_t count = readlink(link, path, sizeof(path) - 1);
32 if (count < 0) return 0;
33 path[count] = 0;
34 const char *fault = getenv("ZIPELO_TEST_FAULT");
35 if (fault && !strcmp(fault, "stage-full")) return strstr(path, "/#") && strstr(path, "(deleted)");
36 return strstr(path, "/.zipelo-") != NULL;
37}
38
39int openat(int fd, const char *path, int flags, ...) {
40 mode_t mode = 0;
41 if (flags & O_CREAT) {
42 va_list args;
43 va_start(args, flags);
44 mode = va_arg(args, unsigned int);
45 va_end(args);
46 }
47 const char *fault = getenv("ZIPELO_TEST_FAULT");
48 if (fault && (!strcmp(fault, "create-full") || !strcmp(fault, "create-denied"))
49 && !strncmp(path, ".zipelo-", 8)) {
50 errno = !strcmp(fault, "create-full") ? ENOSPC : EACCES;
51 return -1;
52 }
53 return real_openat(fd, path, flags, mode);
54}
55
56int open64(const char *path, int flags, ...) {
57 mode_t mode = 0;
58 if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) {
59 va_list args;
60 va_start(args, flags);
61 mode = va_arg(args, unsigned int);
62 va_end(args);
63 }
64 const char *fault = getenv("ZIPELO_TEST_FAULT");
65 if (fault && !strcmp(fault, "temp-full")
66 && (((flags & O_TMPFILE) == O_TMPFILE) || strstr(path, "/zipelo-"))) {
67 errno = ENOSPC;
68 return -1;
69 }
70 if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) {
71 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
72 if (marker >= 0) close(marker);
73 errno = EOPNOTSUPP;
74 return -1;
75 }
76 return real_open64(path, flags, mode);
77}
78
79ssize_t write(int fd, const void *bytes, size_t count) {
80 const char *fault = getenv("ZIPELO_TEST_FAULT");
81 if (!fault || !output_file(fd)) return real_write(fd, bytes, count);
82 if (!strcmp(fault, "write-io")) { errno = EIO; return -1; }
83 if (!strcmp(fault, "full") || !strcmp(fault, "stage-full")) {
84 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
85 errno = ENOSPC;
86 return -1;
87 }
88 if (!strcmp(fault, "pause") && !started++) {
89 const char *marker = getenv("ZIPELO_TEST_MARKER");
90 const char *release = getenv("ZIPELO_TEST_RELEASE");
91 int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600);
92 if (signal_fd < 0) { errno = EIO; return -1; }
93 close(signal_fd);
94 for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000);
95 if (access(release, F_OK)) { errno = EIO; return -1; }
96 }
97 return real_write(fd, bytes, count);
98}
99
100int fstatvfs(int fd, struct statvfs *value) {
101 int result = real_fstatvfs(fd, value);
102 const char *fault = getenv("ZIPELO_TEST_FAULT");
103 if (result || !fault || strncmp(fault, "space-", 6)) return result;
104 struct stat st;
105 if (fstat(fd, &st)) return -1;
106 int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined")
107 || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode))
108 || (!strcmp(fault, "space-destination") && S_ISDIR(st.st_mode));
109 if (!affected) return result;
110 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
111 if (marker >= 0) close(marker);
112 if (!strcmp(fault, "space-error")) { errno = EIO; return -1; }
113 value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0;
114 return 0;
115}
116
117int fsync(int fd) {
118 const char *fault = getenv("ZIPELO_TEST_FAULT");
119 struct stat st;
120 int directory_full = fault && !strcmp(fault, "dir-sync-full")
121 && !fstat(fd, &st) && S_ISDIR(st.st_mode);
122 if (directory_full || (fault && !strcmp(fault, "sync-full") && output_file(fd))) {
123 errno = ENOSPC;
124 return -1;
125 }
126 return real_fsync(fd);
127}