-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathclean.c
117 lines (100 loc) · 2.69 KB
/
clean.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Cleans up plashs internal data.
// - Removes all broken links in $PLASH_DATA/index
// - Removes all broken links in $PLASH_DATA/map
// - Removes unused temporary directories in $PLASH_DATA/tmp
#define USAGE "usage: plash clean\n"
#include <dirent.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <plash.h>
size_t remove_broken_links_here() {
size_t removed = 0;
DIR *dir = opendir(".");
if (dir == NULL)
pl_fatal("opendir");
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_LNK) {
if (access(entry->d_name, F_OK) == -1) {
if (unlink(entry->d_name) != -1)
removed++;
}
}
}
closedir(dir);
return removed;
}
int is_process_still_running(pid_t pid, pid_t sid) {
if (getsid(pid) != sid) {
return EXIT_SUCCESS;
}
if (kill(pid, 0) == 0)
return EXIT_FAILURE;
return EXIT_SUCCESS;
}
size_t delete_unused_tmpdirs_here() {
size_t count = 0;
char *dirname_copy, *pid, *sid;
DIR *dir = opendir(".");
if (dir == NULL)
pl_fatal("opendir");
struct dirent *entry;
while ((entry = readdir(dir)) != NULL) {
if (entry->d_type == DT_DIR) {
// parse sid and pid from the dir name
if ((dirname_copy = strdup(entry->d_name)) == NULL)
pl_fatal("strdup");
strtok(dirname_copy, "_");
if ((pid = strtok(NULL, "_")) == NULL)
continue;
if ((sid = strtok(NULL, "_")) == NULL)
continue;
// delete temporary directory if the process that created it already died.
if (!is_process_still_running(atoll(pid), atoll(sid))) {
pl_run("rm", "-rf", entry->d_name);
count++;
}
free(dirname_copy);
}
}
closedir(dir);
return count;
}
int clean_main(int argc, char *argv[]) {
size_t count;
pl_unshare_user();
char *pid, *sid;
char *plash_data = plash("data");
// cd index
if (chdir(plash_data) == -1)
pl_fatal("chdir");
if (chdir("index") == -1)
pl_fatal("chdir");
// remove broken indexes
fprintf(stderr, "unlinked indexes: ");
count = remove_broken_links_here();
fprintf(stderr, "%ld\n", count);
// cd map
if (chdir(plash_data) == -1)
pl_fatal("chdir");
if (chdir("map") == -1)
pl_fatal("chdir");
// remove broken maps
fprintf(stderr, "unlinked maps: ");
count = remove_broken_links_here();
fprintf(stderr, "%ld\n", count);
// cd tmp
if (chdir(plash_data) == -1)
pl_fatal("chdir");
if (chdir("tmp") == -1)
pl_fatal("chdir");
// remove unused tmp dirs
fprintf(stderr, "removed tmpdirs: ");
count = delete_unused_tmpdirs_here();
fprintf(stderr, "%ld\n", count);
return EXIT_SUCCESS;
}