• 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

  • 相关阅读:
    CentOS 6的服务器后执行yum后发现出现Error: Cannot find a valid baseurl for repo: base解决办法
    C# 8字节byte数组转int
    Unity Packages 介绍
    开发笔记:服务端返回三/多级菜单数据的几种不同实现
    Grafana Azure Data Explorer Plug-In 中国区 ADX 支持
    Redash 连接中国区 Azure Data Explorer
    爆竹声中贺新年-- KEDA(Kubernetes Event-driven Autoscaling) 带你烟火秀
    生产随机码包含数字+字母
    关于串口通信发送组合键方法
    python 正则匹配一串字符串的负数和正数,合并两个列表为字典
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4777260.html
Copyright © 2020-2023  润新知