#include "auto.h"
#define _DEBUG
int main( int argc, char **argv )
{
int i;
if ( argc == 2 && strcmp( argv[1], "-v" ) == 0 )
{
printf( "Current Version:1.0.1\n" );
printf( "Last Update Time:2004-07-07\n" );
return 0;
}
if ( argc != 3 )
{
printf("usage:AutoClean2 tracefile envfile!\n");
return -1;
}
if ( ( ffp = fopen( argv[1], "a+" ) ) == NULL )
{
printf( "open file %s err!\n", argv[1] );
return ( -1);
}
sprintf( tmpss, "*******Current Version:1.0.1(Clean Old Files...)*******" );
PTrace( DEBUG, tmpss );
if ( Get_Env( argv[2] ) )
{
sprintf( tmpss, "Get Env File %s Err!", argv[2] );
PTrace( TIMEDEBUG, tmpss );
return ( -2);
}
while ( 1 )
{
for ( i = 0;i < nDeletePaths;i++ )
{
if ( Deal_Proc( DDir[i], i ) < 0 )
{
sprintf( tmpss, "Deal Proc Err!" );
PTrace( TIMEDEBUG, tmpss );
return ( -3);
}
}
sleep( STime );
}
return (0);
}
int Get_Env( char *envfile )
{
char ss[5][800], buf[800];
char *pch;
FILE *fp;
int i, j, k;
i = j = 0;
fp = NULL;
sprintf( tmpss, "*******Begin Get Env ...*******" );
PTrace( DEBUG, tmpss );
if ((fp = fopen(envfile, "r")) == NULL)
{
sprintf( tmpss, "open File %s %s Err!", envfile );
PTrace( TIMEDEBUG, tmpss );
}
while ( fgets( buf, 800, fp ) != NULL )
{
DeleteSpace( buf );
if (strlen(buf) < 3)
continue;
if ( buf[0] == '!' || buf[0] == '%' || buf[0] == '#' )
continue;
sscanf( buf, "%s %s %s %s", ss[1], ss[2], ss[3], ss[4]);
if ( !strcmp( ss[1], "SLEEPTIME") )
{
STime = atoi(ss[2]);
sprintf( tmpss, "***SLEEPTIME:%d ***", STime );
PTrace( DEBUG, tmpss );
continue;
}
else if ( !strcmp( ss[1], "CLEANTIME") )
{
CTime = atoi( ss[2] ) * 24 * 60 * 60;
sprintf( tmpss, "***CLEANTIME:%s ***", ss[2] );
PTrace( DEBUG, tmpss );
continue;
}
else if ( !strcmp( ss[1], "DELETEPATH") )
{
//DELETEPATH /home/szcj/data/LC/mq:Q11*,Q02*,Q06*
pch = strchr( ss[2], ':' );
*pch = 0;
strcpy( DDir[i], ss[2] );
k = strlen( DDir[i] );
if ( DDir[i][k - 1] != '/' )
{
DDir[i][k] = '/';
DDir[i][k + 1] = 0;
}
sprintf( tmpss, "***%02d:DELETEPATH:%s ***", i, DDir[i] );
PTrace( DEBUG, tmpss );
SplitString( pch + 1, ',' );
S_DirAndModes[i].nModeNum = SCounts;
for ( j = 0;j < SCounts;j++ )
{
sprintf( tmpss, " *****Modes:%d:%s *****", j, Strings[j] );
PTrace( DEBUG, tmpss );
strcpy( S_DirAndModes[i].FSuffixMode[j], Strings[j] );
}
i++;
continue;
}
}
nDeletePaths = i;
fclose(fp);
sprintf( tmpss, "*******End Get Env !!!*******" );
PTrace( DEBUG, tmpss );
return 0;
}
//递归返回值:
// 0 : can delete the whole directory
// >0: some files in the dir is not deleted ,so cann't delete the directory
// <0: occur errors during the process
int Deal_Proc( char *_dir, int pos )
{
int i, j, k, Len1, Len2, Len3, f1, f2, f3;
int flag1, flag2;
time_t timer;
int num_empty = 0;
int num_child = 0;
struct Dirbuf Dir1[MAX_FILE + 10];
char ss1[400], ss2[400], tt[80], ss[80];
if ( ( f1 = ReadSortDir( _dir, MAX_FILE, READFILE, pos ) ) < 0 )
{
sprintf( tmpss, "Read Sort File %s Err!", _dir );
PTrace( ERRDEBUG, tmpss );
return ( -1);
}
// sprintf( tmpss,"file=%s,f1=%d=",_dir,f1 ); PTrace( TIMEDEBUG,tmpss );
i = CleanFile( _dir, f1 );
if ( i < 0 )
{
sprintf( tmpss, "Clean File in %s Err!=%d=", _dir, i );
PTrace( TIMEDEBUG, tmpss );
return ( -1);
}
if ( i < f1 )
num_empty += 1; //说明该目录下有些文件没有删除,因此该目录不能删
if ( ( Len1 = ReadSortDir( _dir, MAX_FILE, READDIR, pos ) ) < 0 )
{
sprintf( tmpss, "Read Sort Dir %s Err!", _dir );
PTrace( ERRDEBUG, tmpss );
return ( -1);
}
for ( i = 0;i < Len1;i++ )
sprintf( Dir1[i].fname, "%s%s/", _dir, DirBuf[i].fname );
for ( i = 0;i < Len1;i++ )
{
num_child = Deal_Proc( Dir1[i].fname, pos );
if ( num_child == 0 )
{
if ( rmdir( Dir1[i].fname ) )
{
sprintf( tmpss, "rmdir %s Err!", Dir1[i].fname );
PTrace( ERRDEBUG, tmpss );
return -1;
}
sprintf( tmpss, "rmdir %s End!", Dir1[i].fname );
PTrace( TIMEDEBUG, tmpss );
}
else if ( num_child < 0)
{
sprintf( tmpss, "deal dir %s Err!", Dir1[i].fname );
PTrace( ERRDEBUG, tmpss );
return -1;
}
else if ( num_child > 0 )
{
num_empty += num_child;
}
}
return num_empty;
}
int CleanFile( char *_dir, int Len )
{
int i, j, k;
time_t timer;
char ss[400];
k = 0;
for ( i = 0;i < Len;i++ )
{
time( &timer );
j = timer - FileBuf[i].ftime;
if ( j < CTime )
continue;
sprintf( ss, "%s%s", _dir, FileBuf[i].fname );
unlink( ss );
k++;
}
return (k);
}
int GetTime( int num, char *rtime )
{
time_t timer;
struct tm nowtimer;
time( &timer );
timer -= num;
nowtimer = *localtime ( &timer );
nowtimer.tm_mon++;
sprintf( rtime, "%04u%02u%02u-%02u%02u%02u",
nowtimer.tm_year + 1900, nowtimer.tm_mon, nowtimer.tm_mday,
nowtimer.tm_hour, nowtimer.tm_min, nowtimer.tm_sec );
return (0);
}
int DeleteSpace( char *ss )
{
int i;
i = strlen(ss) - 1;
while ( i && ss[i] == ' ' )
i--;
ss[i + 1] = 0;
return (0);
}
int DeleteHSpace( char *ss )
{
int i;
char ss1[80];
strcpy( ss1, ss );
i = 0;
while ( ss1[i] == ' ' )
i++;
strcpy( ss, ss1 + i );
return (0);
}
int SplitString( char *ss, char chSplit )
{
int i;
char *ss1;
i = 0;
while ( ( ss1 = strchr( ss, chSplit ) ) != NULL )
{
*ss1 = 0;
strcpy( Strings[i], ss );
ss = ss1 + 1;
i++;
}
strcpy( Strings[i], ss );
i++;
SCounts = i;
return (0);
}
int _DirCmpProc( const void *s1, const void *s2 )
{
int i;
i = ((struct Dirbuf *)s1)->ftime - ((struct Dirbuf *)s2)->ftime;
return ( i );
}
int ReadSortDir( char *dirname, int nums, int mark, int pos )
{
int i, j, ll, k, m, nMatch;
DIR *dirfp;
struct dirent *sdir;
struct stat ustat;
char fname[200];
if ( (dirfp = opendir( dirname )) == NULL)
return ( -1 );
i = 0;
while ( (sdir = readdir(dirfp)) != NULL )
{
if ( !sdir->d_ino )
continue;
sprintf(fname, "%s/%s", dirname, sdir->d_name);
if ( stat(fname, &ustat) )
{
closedir(dirfp);
return ( -2 );
}
if ( mark == READFILE )
{
if ( ustat.st_mode & 0040000 )
continue;
nMatch = 0;
for ( m = 0;m < S_DirAndModes[pos].nModeNum;m++ )
{
if ( Compare( sdir->d_name, S_
没有合适的资源?快使用搜索试试~ 我知道了~
自动递归删除文件的工具程序.rar
共3个文件
h:2个
cpp:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 112 浏览量
2023-03-01
15:36:00
上传
评论
收藏 5KB RAR 举报
温馨提示
自动递归删除文件的工具程序.rar
资源推荐
资源详情
资源评论
收起资源包目录
自动递归删除文件的工具程序.rar (3个子文件)
auto.h 2KB
com1_.h 1021B
AutoClean.cpp 16KB
共 3 条
- 1
资源评论
GZM888888
- 粉丝: 176
- 资源: 2943
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功