team-ljyzipelo

공개
220줄9.0 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;
19static int snapshot_fd = -1;
20
21__attribute__((constructor)) static void initialize(void) {
22 real_write = dlsym(RTLD_NEXT, "write");
23 real_openat = dlsym(RTLD_NEXT, "openat");
24 real_open64 = dlsym(RTLD_NEXT, "open64");
25 real_fsync = dlsym(RTLD_NEXT, "fsync");
26 real_fstatvfs = dlsym(RTLD_NEXT, "fstatvfs");
27}
28
29static int output_file(int fd) {
30 char link[64], path[4096];
31 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
32 ssize_t count = readlink(link, path, sizeof(path) - 1);
33 if (count < 0) return 0;
34 path[count] = 0;
35 const char *fault = getenv("ZIPELO_TEST_FAULT");
36 if (fault && (!strcmp(fault, "stage-full") || !strcmp(fault, "codec-full"))) return strstr(path, "/#") && strstr(path, "(deleted)");
37 return strstr(path, "/.zipelo-") != NULL;
38}
39
40int openat(int fd, const char *path, int flags, ...) {
41 mode_t mode = 0;
42 if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) {
43 va_list args;
44 va_start(args, flags);
45 mode = va_arg(args, unsigned int);
46 va_end(args);
47 }
48 const char *fault = getenv("ZIPELO_TEST_FAULT");
49 if (fault && (!strcmp(fault, "create-full") || !strcmp(fault, "create-denied"))
50 && !strncmp(path, ".zipelo-", 8)) {
51 errno = !strcmp(fault, "create-full") ? ENOSPC : EACCES;
52 return -1;
53 }
54 /* 해제 작업의 원본 사본과 단계 임시 파일은 출력 폴더 핸들 기준 O_TMPFILE로 만듦. */
55 if ((flags & O_TMPFILE) == O_TMPFILE) {
56 if (fault && !strcmp(fault, "temp-full")) {
57 errno = ENOSPC;
58 return -1;
59 }
60 if (fault && !strcmp(fault, "fallback")) {
61 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
62 if (marker >= 0) close(marker);
63 errno = EOPNOTSUPP;
64 return -1;
65 }
66 }
67 return real_openat(fd, path, flags, mode);
68}
69
70int open64(const char *path, int flags, ...) {
71 mode_t mode = 0;
72 if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) {
73 va_list args;
74 va_start(args, flags);
75 mode = va_arg(args, unsigned int);
76 va_end(args);
77 }
78 const char *fault = getenv("ZIPELO_TEST_FAULT");
79 const char *name = strrchr(path, '/');
80 name = name ? name + 1 : path;
81 int named_temp = (flags & O_CREAT) && !strncmp(name, "zipelo-", 7);
82 if (fault && !strcmp(fault, "temp-full")
83 && (((flags & O_TMPFILE) == O_TMPFILE) || named_temp)) {
84 errno = ENOSPC;
85 return -1;
86 }
87 if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) {
88 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
89 if (marker >= 0) close(marker);
90 errno = EOPNOTSUPP;
91 return -1;
92 }
93 return real_open64(path, flags, mode);
94}
95
96ssize_t write(int fd, const void *bytes, size_t count) {
97 const char *fault = getenv("ZIPELO_TEST_FAULT");
98 if (!fault || !output_file(fd)) return real_write(fd, bytes, count);
99 if (!strcmp(fault, "codec-full")) {
100 if (snapshot_fd == -1) snapshot_fd = fd;
101 if (fd == snapshot_fd) return real_write(fd, bytes, count);
102 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
103 errno = ENOSPC;
104 return -1;
105 }
106 if (!strcmp(fault, "write-io")) { errno = EIO; return -1; }
107 if (!strcmp(fault, "full") || !strcmp(fault, "stage-full") || !strcmp(fault, "full-cleanup-fail")) {
108 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
109 errno = ENOSPC;
110 return -1;
111 }
112 if (!strcmp(fault, "pause") && !started++) {
113 const char *marker = getenv("ZIPELO_TEST_MARKER");
114 const char *release = getenv("ZIPELO_TEST_RELEASE");
115 int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600);
116 if (signal_fd < 0) { errno = EIO; return -1; }
117 close(signal_fd);
118 for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000);
119 if (access(release, F_OK)) { errno = EIO; return -1; }
120 }
121 return real_write(fd, bytes, count);
122}
123
124int fstatvfs(int fd, struct statvfs *value) {
125 int result = real_fstatvfs(fd, value);
126 const char *fault = getenv("ZIPELO_TEST_FAULT");
127 if (result || !fault || strncmp(fault, "space-", 6)) return result;
128 struct stat st;
129 if (fstat(fd, &st)) return -1;
130 int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined")
131 || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode))
132 || ((!strcmp(fault, "space-destination") || !strcmp(fault, "space-one-output")) && S_ISDIR(st.st_mode));
133 if (!affected) return result;
134 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
135 if (marker >= 0) close(marker);
136 if (!strcmp(fault, "space-error")) { errno = EIO; return -1; }
137 if (!strcmp(fault, "space-one-output")) {
138 value->f_frsize = 1;
139 value->f_bavail = 64;
140 return 0;
141 }
142 value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0;
143 return 0;
144}
145
146int fsync(int fd) {
147 const char *fault = getenv("ZIPELO_TEST_FAULT");
148 struct stat st;
149 if (fault && !strcmp(fault, "move-on-sync") && output_file(fd)) {
150 char link[64], path[4096], moved[4112];
151 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
152 ssize_t count = readlink(link, path, sizeof(path) - 1);
153 if (count < 0) { errno = EIO; return -1; }
154 path[count] = 0;
155 snprintf(moved, sizeof(moved), "%s.owned", path);
156 if (rename(path, moved)) { errno = EIO; return -1; }
157 return real_fsync(fd);
158 }
159 int directory_full = fault && !strcmp(fault, "dir-sync-full")
160 && !fstat(fd, &st) && S_ISDIR(st.st_mode);
161 if (directory_full || (fault && !strcmp(fault, "sync-full") && output_file(fd))) {
162 errno = ENOSPC;
163 return -1;
164 }
165 if (fault && output_file(fd) && (!strcmp(fault, "sync-io") || !strcmp(fault, "sync-cleanup-fail") || !strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish"))) {
166 if (!strcmp(fault, "swap-sync") || !strcmp(fault, "swap-publish")) {
167 char link[64], path[4096], moved[4112];
168 snprintf(link, sizeof(link), "/proc/self/fd/%d", fd);
169 ssize_t count = readlink(link, path, sizeof(path) - 1);
170 if (count < 0) { errno = EIO; return -1; }
171 path[count] = 0;
172 snprintf(moved, sizeof(moved), "%s.owned", path);
173 if (rename(path, moved)) { errno = EIO; return -1; }
174 int replacement = real_openat(AT_FDCWD, path, O_CREAT | O_EXCL | O_WRONLY, 0600);
175 if (replacement < 0) { errno = EIO; return -1; }
176 real_write(replacement, "unrelated replacement", 21);
177 close(replacement);
178 if (!strcmp(fault, "swap-publish")) return real_fsync(fd);
179 }
180 errno = EIO;
181 return -1;
182 }
183 if (fault && !strcmp(fault, "dir-sync-io") && !fstat(fd, &st) && S_ISDIR(st.st_mode)) {
184 errno = EIO;
185 return -1;
186 }
187 return real_fsync(fd);
188}
189
190int unlinkat(int fd, const char *path, int flags) {
191 const char *fault = getenv("ZIPELO_TEST_FAULT");
192 if (fault && strstr(fault, "cleanup-fail") && !strncmp(path, ".zipelo-", 8)) {
193 errno = EACCES;
194 return -1;
195 }
196 int (*real_unlinkat)(int, const char *, int) = dlsym(RTLD_NEXT, "unlinkat");
197 return real_unlinkat(fd, path, flags);
198}
199
200int linkat(int oldfd, const char *oldname, int newfd, const char *newname, int flags) {
201 int (*real_linkat)(int, const char *, int, const char *, int) = dlsym(RTLD_NEXT, "linkat");
202 const char *fault = getenv("ZIPELO_TEST_FAULT");
203 if (fault && !strcmp(fault, "swap-on-link") && !strncmp(oldname, ".zipelo-", 8)) {
204 char moved[512];
205 snprintf(moved, sizeof(moved), "%s.owned", oldname);
206 if (renameat(oldfd, oldname, oldfd, moved)) return -1;
207 int replacement = real_openat(oldfd, oldname, O_CREAT | O_EXCL | O_WRONLY, 0600);
208 if (replacement < 0) return -1;
209 real_write(replacement, "unrelated replacement", 21);
210 close(replacement);
211 }
212 return real_linkat(oldfd, oldname, newfd, newname, flags);
213}
214
215int renameat(int oldfd, const char *oldname, int newfd, const char *newname) {
216 const char *fault = getenv("ZIPELO_TEST_FAULT");
217 if (fault && !strcmp(fault, "replace-fail")) { errno = EIO; return -1; }
218 int (*real_renameat)(int, const char *, int, const char *) = dlsym(RTLD_NEXT, "renameat");
219 return real_renameat(oldfd, oldname, newfd, newname);
220}