team-ljyzipelo

공개
29줄1.1 KB
1#define _GNU_SOURCE
2#include <dlfcn.h>
3#include <errno.h>
4#include <fcntl.h>
5#include <stdlib.h>
6#include <string.h>
7#include <sys/stat.h>
8#include <sys/statvfs.h>
9#include <unistd.h>
10
11static int regular_calls;
12
13int fstatvfs(int fd, struct statvfs *value) {
14 int (*original)(int, struct statvfs *) = dlsym(RTLD_NEXT, "fstatvfs");
15 int result = original(fd, value);
16 const char *mode = getenv("ZIPELO_COMPRESSED_FAULT");
17 struct stat info;
18 if (result || !mode || fstat(fd, &info) || !S_ISREG(info.st_mode)) return result;
19 if (++regular_calls != 2) return result;
20 int marker = open(getenv("ZIPELO_COMPRESSED_MARKER"), O_CREAT | O_EXCL | O_WRONLY, 0600);
21 if (marker < 0) { errno = EIO; return -1; }
22 close(marker);
23 if (!strcmp(mode, "full")) { value->f_bavail = 0; return 0; }
24 if (!strcmp(mode, "io")) { errno = EIO; return -1; }
25 const char *release = getenv("ZIPELO_COMPRESSED_RELEASE");
26 for (int i = 0; i < 1000 && access(release, F_OK); i++) usleep(10000);
27 if (access(release, F_OK)) { errno = EIO; return -1; }
28 return result;
29}