summaryrefslogtreecommitdiffstats
path: root/util/mdclean.c
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-04-16 04:07:51 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-04-16 04:07:51 +0800
commitc0bdb92ced5b2911d800fb3920e252a50e25dc9d (patch)
treea1528e1e553154a2fe0cb0147bc040c89c0a8920 /util/mdclean.c
parentd10177be1eb9a86b468387498fe81e979e1638cb (diff)
downloadpttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar.gz
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar.bz2
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar.lz
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar.xz
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.tar.zst
pttbbs-c0bdb92ced5b2911d800fb3920e252a50e25dc9d.zip
mdclean
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@113 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
Diffstat (limited to 'util/mdclean.c')
-rw-r--r--util/mdclean.c81
1 files changed, 81 insertions, 0 deletions
diff --git a/util/mdclean.c b/util/mdclean.c
new file mode 100644
index 00000000..f8f58728
--- /dev/null
+++ b/util/mdclean.c
@@ -0,0 +1,81 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include "config.h"
+#include <sys/types.h>
+#include <dirent.h>
+#include <unistd.h>
+#include <err.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/param.h>
+#include <sys/mount.h>
+
+#define MAXDSs 1048576
+#define LEAVE 10
+#define CLEAN 10
+typedef struct {
+ time_t atime;
+ char name[60];
+} DS;
+
+DS ds[MAXDSs];
+int nDSs;
+
+int compar(const void *a, const void *b)
+{
+ return ((DS *)a)->atime - ((DS *)b)->atime;
+}
+
+int main(int argc, char **argv)
+{
+ DIR *dirp;
+ struct dirent *dp;
+ struct stat sb;
+ struct statfs sf;
+ char *fn;
+ time_t now;
+ if( chdir(BBSHOME "/cache") < 0 )
+ err(1, "chdir");
+
+ if( (dirp = opendir(".")) == NULL )
+ err(1, "opendir");
+
+ statfs(".", &sf);
+ if( sf.f_bfree * 100 / sf.f_blocks > LEAVE )
+ return 0;
+
+ now = time(NULL);
+ while( (dp = readdir(dirp)) != NULL ){
+ fn = dp->d_name;
+ if( fn[0] != 'e' && fn[0] != 'b' )
+ continue;
+ if( stat(fn, &sb) < 0 )
+ continue;
+ if( sb.st_atime < now - 1800 ){
+ printf("atime: %s\n", fn);
+ unlink(fn);
+ }
+ else if( sb.st_mtime < now - 7200 ){
+ printf("mtime: %s\n", fn);
+ unlink(fn);
+ }
+ else{
+ if( nDSs != MAXDSs ){
+ strcpy(ds[nDSs].name, fn);
+ ds[nDSs].atime = sb.st_atime;
+ ++nDSs;
+ }
+ }
+ }
+
+ statfs(".", &sf);
+ if( sf.f_bfree * 100 / sf.f_blocks <= LEAVE ){
+ qsort(ds, nDSs, sizeof(DS), compar);
+ nDSs = nDSs * CLEAN / 100;
+ while( nDSs-- ){
+ printf("%s\n", ds[nDSs].name);
+ unlink(ds[nDSs].name);
+ }
+ }
+ return 0;
+}