team-ljyzipelo

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