team-ljyzipelo

공개
135줄5.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) {
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 return real_openat(fd, path, flags, mode);
55}
56
57int open64(const char *path, int flags, ...) {
58 mode_t mode = 0;
59 if ((flags & O_CREAT) || ((flags & O_TMPFILE) == O_TMPFILE)) {
60 va_list args;
61 va_start(args, flags);
62 mode = va_arg(args, unsigned int);
63 va_end(args);
64 }
65 const char *fault = getenv("ZIPELO_TEST_FAULT");
66 if (fault && !strcmp(fault, "temp-full")
67 && (((flags & O_TMPFILE) == O_TMPFILE) || strstr(path, "/zipelo-"))) {
68 errno = ENOSPC;
69 return -1;
70 }
71 if (fault && !strcmp(fault, "fallback") && ((flags & O_TMPFILE) == O_TMPFILE)) {
72 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
73 if (marker >= 0) close(marker);
74 errno = EOPNOTSUPP;
75 return -1;
76 }
77 return real_open64(path, flags, mode);
78}
79
80ssize_t write(int fd, const void *bytes, size_t count) {
81 const char *fault = getenv("ZIPELO_TEST_FAULT");
82 if (!fault || !output_file(fd)) return real_write(fd, bytes, count);
83 if (!strcmp(fault, "codec-full")) {
84 if (snapshot_fd == -1) snapshot_fd = fd;
85 if (fd == snapshot_fd) return real_write(fd, bytes, count);
86 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
87 errno = ENOSPC;
88 return -1;
89 }
90 if (!strcmp(fault, "write-io")) { errno = EIO; return -1; }
91 if (!strcmp(fault, "full") || !strcmp(fault, "stage-full")) {
92 if (!started++) return real_write(fd, bytes, count < 7 ? count : 7);
93 errno = ENOSPC;
94 return -1;
95 }
96 if (!strcmp(fault, "pause") && !started++) {
97 const char *marker = getenv("ZIPELO_TEST_MARKER");
98 const char *release = getenv("ZIPELO_TEST_RELEASE");
99 int signal_fd = real_openat(AT_FDCWD, marker, O_CREAT | O_EXCL | O_WRONLY, 0600);
100 if (signal_fd < 0) { errno = EIO; return -1; }
101 close(signal_fd);
102 for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000);
103 if (access(release, F_OK)) { errno = EIO; return -1; }
104 }
105 return real_write(fd, bytes, count);
106}
107
108int fstatvfs(int fd, struct statvfs *value) {
109 int result = real_fstatvfs(fd, value);
110 const char *fault = getenv("ZIPELO_TEST_FAULT");
111 if (result || !fault || strncmp(fault, "space-", 6)) return result;
112 struct stat st;
113 if (fstat(fd, &st)) return -1;
114 int affected = !strcmp(fault, "space-error") || !strcmp(fault, "space-combined")
115 || (!strcmp(fault, "space-stage") && S_ISREG(st.st_mode))
116 || (!strcmp(fault, "space-destination") && S_ISDIR(st.st_mode));
117 if (!affected) return result;
118 int marker = real_openat(AT_FDCWD, getenv("ZIPELO_TEST_MARKER"), O_CREAT | O_WRONLY, 0600);
119 if (marker >= 0) close(marker);
120 if (!strcmp(fault, "space-error")) { errno = EIO; return -1; }
121 value->f_bavail = !strcmp(fault, "space-combined") ? (1536 * 1024) / value->f_frsize : 0;
122 return 0;
123}
124
125int fsync(int fd) {
126 const char *fault = getenv("ZIPELO_TEST_FAULT");
127 struct stat st;
128 int directory_full = fault && !strcmp(fault, "dir-sync-full")
129 && !fstat(fd, &st) && S_ISDIR(st.st_mode);
130 if (directory_full || (fault && !strcmp(fault, "sync-full") && output_file(fd))) {
131 errno = ENOSPC;
132 return -1;
133 }
134 return real_fsync(fd);
135}