Update recyclix-flow.svg
[hband-ld-preload-libs.git] / src / saveunlinkedinode.c
blob91b173d6362d5f34cddfa23a688610a4f3aa126d
2 #include <sys/stat.h>
3 #include <bits/posix1_lim.h>
4 #include <fcntl.h>
5 #include <time.h>
6 #include <stdlib.h>
7 #include <stdio.h>
8 #include <dlfcn.h>
9 #include <unistd.h>
11 int unlinkat(int dirfd, const char *pathname, int flags)
13 int (*real_unlinkat)(int dirfd, const char *pathname, int flags) = dlsym(RTLD_NEXT, "unlinkat");
14 int retval;
16 char fdlinkname[_POSIX_PATH_MAX];
17 char atdir[_POSIX_PATH_MAX];
18 char bkfilename[_POSIX_PATH_MAX];
19 char timebuf[20];
20 time_t timer;
21 struct tm* tm_info;
22 struct stat st;
23 FILE *fd;
25 atdir[0] = 0;
28 if(fstatat(dirfd, pathname, &st, flags) == -1) {
29 perror("fstatat");
30 goto do_unlink;
33 if(dirfd != AT_FDCWD) {
34 sprintf(fdlinkname, "/proc/self/fd/%d", dirfd);
35 if(readlink(fdlinkname, atdir, _POSIX_PATH_MAX) == -1) {
36 perror("readlink");
37 goto do_unlink;
40 else {
41 if(getcwd(atdir, _POSIX_PATH_MAX) == NULL) {
42 perror("getcwd");
43 goto do_unlink;
47 time(&timer);
48 tm_info = localtime(&timer);
49 strftime(timebuf, 20, "%Y-%m-%d %H:%M:%S", tm_info);
52 do_unlink:
53 retval = real_unlinkat(dirfd, pathname, flags);
55 if(retval == 0 && atdir[0] != 0) {
56 sprintf(bkfilename, "%s/%s", getenv("HOME"), ".unlinked_inodes");
57 fd = fopen(bkfilename, "a");
58 if(fd != NULL) {
59 fprintf(fd, "%s %ld %s/%s\n", timebuf, (long)st.st_ino, atdir, pathname);
60 fclose(fd);
62 else {
63 perror("fopen");
67 return retval;
70 // Fordítás: gcc -s -x c saveunlinkedinode.c -fPIC -shared -ldl -o saveunlinkedinode.so
71 // Futtatás: LD_PRELOAD=./saveunlinkedinode.so rm somefile