-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdiskQuota.c
89 lines (81 loc) · 2 KB
/
diskQuota.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include <time.h>
#include <unistd.h>
#include "lib/sqlite3.c"
#include "lib/sqlite3.h"
#include "config.h"
#include "diskQuota.h"
#include "db_config.h"
#include "config.c"
#include "db_config.c"
int statFile(char *directory, Config *config) {
struct stat st;
struct timespec t = {0, 0};
if(stat(directory,&st) == 0)
{
t.tv_sec = st.st_mtime;
} else return 666; //failure status
if(difftime(t.tv_sec,(time(NULL)-(config->old))) < 0)
{
return unlink(directory);
} else {
return 111; //no need to delete it
}
}
void findFiles(char *directory, Config *config)
{
DIR *d;
struct dirent *dir;
d= opendir(directory);
char *name;
if(d)
{
while( ( dir=readdir(d) ) != NULL)
{
name = dir->d_name;
if(strcmp(name,".") == 0) continue; //dont check this dir again
if(strcmp(name,"..") == 0) continue; //also dont go up that would be dumb as well
char type = dir->d_type;
if(type == DT_DIR) //test directory
{ //is directory
char *deeperDirectory=(char*)malloc(sizeof(char)*256);
strcpy(deeperDirectory,directory);
strcat(deeperDirectory, "/");
strcat(deeperDirectory, name);
findFiles(deeperDirectory, config);
free(deeperDirectory);
} else if(type == DT_REG) { //is file
char *toStat=(char*)malloc(sizeof(char)*256);
strcpy(toStat, directory);
strcat(toStat,"/");
strcat(toStat,name);
statFile(toStat, config);
free(toStat);
} else {
printf("dirent type error");
continue;
}
}
}
closedir(d);
return;
}
int main()
{
Config *config = malloc(sizeof(Config));
if (access("development.sqlite3", F_OK) != -1) {
dbConfig(config, "development.sqlite3");
} else {
readConf(config);
}
while(1) {
findFiles(config->directory,config);
sleep(config->scan_interval);
}
return 0;
}