team-ljyzipelo

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