summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-04-20 16:06:27 +0800
committerin2 <in2@63ad8ddf-47c3-0310-b6dd-a9e9d9715204>2002-04-20 16:06:27 +0800
commitd2609595d33f457e4705a14ac937386bbf0ecb64 (patch)
treead692f3a4cbddb02bffa542a26be87eceff34bc4
parent1b62f09c249ae8b59968e3927515da1ce52c705a (diff)
downloadpttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar.gz
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar.bz2
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar.lz
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar.xz
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.tar.zst
pttbbs-d2609595d33f457e4705a14ac937386bbf0ecb64.zip
powerful mdclean
git-svn-id: http://opensvn.csie.org/pttbbs/pttbbs/trunk/pttbbs@126 63ad8ddf-47c3-0310-b6dd-a9e9d9715204
-rw-r--r--util/mdclean.c91
1 files changed, 54 insertions, 37 deletions
diff --git a/util/mdclean.c b/util/mdclean.c
index f8f58728..196fc9ee 100644
--- a/util/mdclean.c
+++ b/util/mdclean.c
@@ -11,8 +11,7 @@
#include <sys/mount.h>
#define MAXDSs 1048576
-#define LEAVE 10
-#define CLEAN 10
+
typedef struct {
time_t atime;
char name[60];
@@ -34,47 +33,65 @@ int main(int argc, char **argv)
struct statfs sf;
char *fn;
time_t now;
- if( chdir(BBSHOME "/cache") < 0 )
- err(1, "chdir");
-
- if( (dirp = opendir(".")) == NULL )
- err(1, "opendir");
+ int sleeptime, leavespace, clean;
- statfs(".", &sf);
- if( sf.f_bfree * 100 / sf.f_blocks > LEAVE )
+ if( argc != 4 ){
+ puts("usage:\tmdclean sleeptime(secs) leavespace(k) clean(%%)\n");
return 0;
+ }
+ sleeptime = (atoi(argv[1]) < 1) ? 1 : atoi(argv[1]);
+ leavespace= (atoi(argv[2]) <100)?100: atoi(argv[2]);
+ clean = (atoi(argv[3]) < 1) ? 1 : atoi(argv[3]);
- 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 )
+ if( chdir(BBSHOME "/cache") < 0 )
+ err(1, "chdir");
+
+ while( 1 ){
+ puts("sleeping...");
+ sleep(sleeptime);
+ if( (dirp = opendir(".")) == NULL )
+ err(1, "opendir");
+
+ statfs(".", &sf);
+ if( sf.f_bfree * sf.f_bsize / 1024 > leavespace )
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;
+
+ nDSs = 0;
+ now = time(NULL);
+ while( (dp = readdir(dirp)) != NULL ){
+ fn = dp->d_name;
+ if( fn[0] != 'e' && fn[0] != 'b' ){
+ unlink(fn);
+ 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 - 10800 ){
+ 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);
+ closedir(dirp);
+
+ statfs(".", &sf);
+ if( sf.f_bfree * sf.f_bsize / 1024 <= leavespace ){
+ qsort(ds, nDSs, sizeof(DS), compar);
+ nDSs = nDSs * clean / 100;
+ while( nDSs-- ){
+ printf("%s\n", ds[nDSs].name);
+ unlink(ds[nDSs].name);
+ }
}
}
return 0;