⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 autoclean.cpp

📁 自动递归删除文件的工具程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:


#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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -