zoukankan      html  css  js  c++  java
  • 遍历目录

    删除指定目录的其他文件(指定则文件保留)

    /*******************************************************************************/

    在Linux下opendir()、readdir()和closedir()这三个函数主要用来遍历目录。在使用这三个函数前必须先包括以下两个头文件:
    #include <sys/types.h>
    #include <dirent.h>

    函数原型

    DIR* opendir (const char * path );

    功能

    打开一个目录.
    参数及返回值:
    输入参数:文件或目录的绝对路径。
    输出参数:无
    返回值:在失败的时候返回一个空的指针,成功返回一个DIR*类型的指针句柄。

    函数原型

    struct dirent* readdir(DIR* dir_handle);
    函数种类: 文件存取
    返回值: dirent的结构类型,
    内容说明 本函数用来读取目录(文件)。每次返回指定目录下的一个文件或者目录的dirent结构指针。
    需要注意的是:当指定目录下的文件或者目录发生改变后,此函数会再次从头遍历目录,此点网上的函数说明还没有发现提出来的。
    dirent结构体成员如下:
    struct dirent
    {
    long d_ino; /* inode number 索引节点号 */
    off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
    unsigned short d_reclen; /* length of this d_name 文件名长 */
    unsigned char d_type; /* the type of d_name 文件类型 */
    char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
    }

    函数原型

    int closedir(DIR *dir);

    函数说明

    closedir()关闭参数dir所指的目录流。关闭成功则返回0,失败返回-1,错误原因存于errno 中。EBADF 参数dir为无效的目录流。
    注意:目录文件作为一种文件,在打开必须关闭,否则会由于文件的进程打开文件过多而不能打开新的文件。因此opendir函数和closedir函数同样是配对出现的。
    /*****************************************************************************/
    int clean_otherFile(char* pathName)
    {

    #define STORAGE_ZERO_DIR "/age/storage/storage0/"
    #define STORAGE_STARTAPP_DIR "/age/storage/storage0/startapp"
    struct dirent* ent = NULL;
    DIR *pDir;
    char dir[64];
    struct stat statbuf;
    pid_t child_pid;
    char* argv_list[5]={0};
    char argv_list0[32],argv_list1[32],argv_list2[64]="",argv_list3[32]="";
    int s32Status;

    printf("file:%s,function:%s,law:%d.chenyouda ",__FILE__,__FUNCTION__,__LINE__);
    printf("size:%d ",sizeof(name));
    printf("len:%d ",strlen(name));
    if(NULL == name) return -1;

    if(0==strlen(name))
    {
    strcpy(name,STORAGE_ZERO_DIR);
    return 0;
    }

    printf("before ");
    if(0 != access(name,R_OK | W_OK)) return -1;
    printf("strcmp into ");
    if(!strcmp(STORAGE_ZERO_DIR,name))
    {
    printf("enter one ");
    if(pDir = opendir(STORAGE_ZERO_DIR))
    { printf("enter two ");
    while((ent=readdir(pDir))!=NULL)
    { printf("enter three name:%s ",ent->d_name);

    memset(dir,0,sizeof(char)*64);
    strcpy(dir,STORAGE_ZERO_DIR);
    strcat(dir,ent->d_name);
    printf("enter dir : %s ",dir);
    lstat(dir, &statbuf);//获取其属性
    if (S_ISDIR(statbuf.st_mode)) //目录
    { printf("enter foure ");
    if(0 == strcmp(ent->d_name,".") ||
    0 ==strcmp(ent->d_name,".."))
    continue;
    if(strcmp(ent->d_name,"ServiceInfo") && strcmp(ent->d_name,"UserProperty")
    && strcmp(ent->d_name,"startapp") )
    {
    //删除其他目录
    printf("clean the dir %s ",ent->d_name);
    sprintf(argv_list2,"%s/%s",name,ent->d_name);
    strcpy(argv_list0,"rm");
    strcpy(argv_list1,"-rf");

    argv_list[0]=argv_list0;
    argv_list[1]=argv_list1;
    argv_list[2]=argv_list2;
    argv_list[3]=argv_list3;
    child_pid = fork();
    if( 0 == child_pid )
    {
    execvp((const char *)argv_list[0], (char * const*)argv_list);
    printf("on error occurred in execvp! ");
    abort();
    }
    if(-1 == child_pid )
    {
    return -1;
    }

    waitpid(child_pid, &s32Status, 0);
    if(WIFEXITED(s32Status))
    {
    printf("clean the dir have realize!!! ");
    continue;
    }

    }

    }
    else if(S_ISREG(statbuf.st_mode)) //普通文件
    {
    printf("enter file ");
    if(strcmp(ent->d_name,"LocalInformation.cfg") && strcmp(ent->d_name,"UpgradeVersion.json")
    && strcmp(ent->d_name,"localNetwork.json") && strcmp(ent->d_name,"sys_setting.dat")
    && strcmp(ent->d_name,"regionIndex.json"))
    {
    //删除其他文件
    sprintf(argv_list2,"%s/%s",STORAGE_ZERO_DIR,ent->d_name);
    strcpy(argv_list0,"rm");
    strcpy(argv_list1,"-rf");

    printf("clean the file %s ",ent->d_name);
    argv_list[0]=argv_list0;
    argv_list[1]=argv_list1;
    argv_list[2]=argv_list2;
    argv_list[3]=argv_list3;
    child_pid = fork();
    if( 0 == child_pid )
    {
    execvp((const char *)argv_list[0], (char * const*)argv_list);
    printf("on error occurred in execvp! ");
    abort();
    }
    if(-1 == child_pid )
    {
    return -1;
    }

    waitpid(child_pid, &s32Status, 0);
    if(WIFEXITED(s32Status))
    {
    printf("clean the file have realize!!! ");
    continue;
    }
    }

    }
    else
    printf("error ");
    }
    if(0 != access(STORAGE_STARTAPP_DIR,R_OK | W_OK))
    {
    if(mkdir(STORAGE_STARTAPP_DIR,S_IRWXU | S_IRWXO))
    {
    printf("creat the file %s had faile!!! ",STORAGE_STARTAPP_DIR);
    }
    printf("creat the file %s had success!!! ",STORAGE_STARTAPP_DIR);
    }

    }
    closedir(pDir);
    }
    return 0;
    }

    }
     
  • 相关阅读:
    Linux进程间通信之信号量
    C语言文件操作
    Linux多线程同步方法
    Linux进程间通信之共享内存
    关于P2P下载的思考
    学习Jxta,初窥门径
    庆祝一下,基于JXTA的P2P文件共享传输文件测试成功。
    关于java nio在windows下实现
    Java aio和JSR 203
    关于文件目录的P2P共享问题
  • 原文地址:https://www.cnblogs.com/kernel-style/p/3274343.html
Copyright © 2011-2022 走看看