zoukankan      html  css  js  c++  java
  • Linux Opendir and readdir will not update along with directory changing

    Linux Opendir and readdir will not update along with directory changing

    1 Result

    According to my experiment, opendir() will just record a static state of opened directory. During the readdir() operation, if you add/remove a file to the directory opened by opendir(), readdir() knows nothing about it.

    2 temporary fix method

    use access() to judge if some file existed.

    3 Test

    • touch files

    ~ddd$ touch a.tmp b.tmp c.tmp a.aa b.aa

    • test

    ./readdir-test ddd

    *******************************
    a.aa  a.tmp  b.aa  b.tmp  c.tmp
    *******************************
    [0], full name is ddd/a.aa
    [1], full name is ddd/..
    [2], full name is ddd/c.tmp
    =====================================
    all .aa deleted.
    a.tmp  b.tmp  c.tmp
    =====================================
    [3], full name is ddd/b.aa // shouldn't show b.aa now
    ddd/b.aa not exist.
    [4], full name is ddd/b.tmp
    rm: cannot remove `ddd/*.aa': No such file or directory
    =====================================
    all .aa deleted.
    a.tmp  b.tmp  c.tmp
    =====================================
    [5], full name is ddd/.
    [6], full name is ddd/a.tmp
    rm: cannot remove `ddd/*.aa': No such file or directory
    =====================================
    all .aa deleted.
    a.tmp  b.tmp  c.tmp
    =====================================
    
    • code as below
    #include <stdio.h>
    #include <sys/types.h>
    #include <string.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <dirent.h>
    
    int main(int argc, char *argv[])
    {
        DIR *pdir = NULL;
        struct dirent *pdirent = NULL;
        char fname[256];
        char cmd[256];
        static int i = 0;
    
        if (argc != 2) {
            printf("Usage: %s dir
    ", argv[0]);
            return -1;
        }
    
        pdir = opendir(argv[1]);
        if (pdir == NULL)
            return -1;
    
        printf("*******************************
    ");
        sprintf(cmd, "ls %s", argv[1]);
        system(cmd);
        printf("*******************************
    ");
    
        while ((pdirent = readdir(pdir)) != NULL) {
    
            sprintf(fname, "%s/%s", argv[1], pdirent->d_name);
            printf("[%d], full name is %s
    ", i++, fname);
    
            if (access(fname, F_OK) != 0) {
                printf("%s not exist.
    ", fname);
                continue;
            }
    
            if (strncmp(fname+strlen(fname)-4, ".tmp", 4) == 0) {
    
                sprintf(cmd, "rm %s/*.aa", argv[1]);
                system(cmd);
    
                printf("=====================================
    ");
                printf("all .aa deleted.
    ");
                sprintf(cmd, "ls %s", argv[1]);
                system(cmd);
                printf("=====================================
    ");
            }
        }
        closedir(pdir);
    
        return 0;
    }
    

    Output result is

  • 相关阅读:
    java 实现大顶堆
    (转)单调栈总结
    leetcode再次总结
    (转)jvm具体gc算法介绍标记整理--标记清除算法
    (转)java 多线程 对象锁&类锁
    maven 上传 jar 到本地私服
    记录一下faster rcnn
    yolov2训练ICDAR2011数据集
    MSE,ks,mAP,weight decay等名词解释
    python爬虫爬取指定用户微博图片及内容,并进行微博分类及使用习惯分析,生成可视化图表
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4777260.html
Copyright © 2011-2022 走看看