Linux Opendir and readdir will not update along with directory changing

Linux Opendir and readdir will not update along with directory changing

Table of Contents

  • 1. Result
  • 2. temporary fix method
  • 3. Test

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\n", argv[0]);
        return -1;
    }

    pdir = opendir(argv[1]);
    if (pdir == NULL)
        return -1;

    printf("*******************************\n");
    sprintf(cmd, "ls %s", argv[1]);
    system(cmd);
    printf("*******************************\n");

    while ((pdirent = readdir(pdir)) != NULL) {

        sprintf(fname, "%s/%s", argv[1], pdirent->d_name);
        printf("[%d], full name is %s\n", i++, fname);

        if (access(fname, F_OK) != 0) {
            printf("%s not exist.\n", fname);
            continue;
        }

        if (strncmp(fname+strlen(fname)-4, ".tmp", 4) == 0) {

            sprintf(cmd, "rm %s/*.aa", argv[1]);
            system(cmd);

            printf("=====================================\n");
            printf("all .aa deleted.\n");
            sprintf(cmd, "ls %s", argv[1]);
            system(cmd);
            printf("=====================================\n");
        }
    }
    closedir(pdir);

    return 0;
}

Output result is

时间: 2024-08-10 06:02:16

Linux Opendir and readdir will not update along with directory changing的相关文章

linux cd进入目录出现no such file or directory

linux cd进入目录出现no such file or directory 本人菜鸟,刚开始学,为什么我cd进任何一个目录都是no such file or directory. 你把路径弄错了,你用pwd 命令就能看到,如果我没猜错,你现在所在的路径是/home/yy/,所以你要进test目录,要么直接cd test ,要么用正确路径 cd /home/yy/test,你的根目录下没有test,所以cd /test 会遇到这个报错 原文地址:https://www.cnblogs.com/

opendir,readdir,closedir

struct dirent { ino_t d_ino; //inode number off_t d_off; //offset to the next diret unsigned short d_reclen; //length of this record unsigned short d_type; //type of file char d_name[256]; //filename } #include<diret.h> DIR* opendir(const char *path

linux之SQL语句简明教程---UPDATE DELETE FROM

我们有时候可能会需要修改表格中的资料.在这个时候,我们就需要用到 UPDATE 指令.这个指令的语法是: UPDATE "表格名" SET "栏位1" = [新值] WHERE "条件"; 最容易了解这个语法的方式是透过一个例子.假设我们有以下的表格: Store_Information 表格 Store_Name Sales Txn_Date Los Angeles 1500 05-Jan-1999 San Diego 250 07-Jan-1

kali linux 更换更新源,apt-get update报:已下载 25.9 MB,耗时 38秒 (677 kB/s) 正在读取软件包列表... 完成 W: 校验数字签名时出错。此仓库未被更新,所以仍然使用此前的索引文件。GPG 错误:http://mirrors.ustc.edu.cn/kali kali

正在读取软件包列表... 完成 W: 校验数字签名时出错.此仓库未被更新,所以仍然使用此前的索引文件.GPG 错误:http://mirrors.ustc.edu.cn/kali kali-rolling InRelease: 下列签名无效: EXPKEYSIG ED444FF07D8D0BF6 Kali Linux Repository <[email protected]> W: 无法下载 http://http.kali.org/kali/dists/kali-rolling/InRel

Linux执行.sh文件,提示No such file or directory的问题的解决方法

Linux执行.sh文件,提示No such file or directory的问题在window平台下,写好shell脚本文件,迁移到linux平台,赋过可执行权限,执行该sh文件,却提示No such file or directory.ls 了下,确实有该文件,怎么会事呢,难道是文件格式兼容性问题?用vim打开该sh文件,输入:[plain]:set ff 回车,显示fileformat=dos,重新设置下文件格式:[plain]:set ff=unix 保存退出:[plain]:wq 

每天学习一个LINUX命令:mkdir 创建目录文件夹(make directory 做活动目录)

linux mkdir 命令用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 语法参数: mkdir [参数] [目录名] 参数: -m --mode=模式,设定权限<模式> (类似 chmod),而不是 rwxrwxrwx 减 umask-p --parents 递归创建目录-v, --verbose 每次创建新目录都显示信息 用法实例: 创建一个空目录 mkdir abc1 创建多个空目录 mkdir 11 22 33 44

linux学习记录-----vsftpd文件上传(550 create directory operation failed)

1.连接条件:服务端服务开启,防火墙关闭 2.ftp服务器的路径可手动配置,默认为:var/ftp/pub,必须确保pub目录有足够的权限 3.匿名登陆的用户名为:anonymous,密码为空,当时匿名登陆时候,必须确保pub目录对匿名用户开放:chmod o+w /var/ftp/pub 4.同时更改SELinux设置:setsebool -P ftpd_full_access=on --------------顺利完成vsftp服务文件匿名上传 原文地址:https://www.cnblog

Linux无法连接上127.0.0.1,拒绝连接,更新时提示无法下载,无法正常使用apt-get update

你是否遇到过这种情况,在Linux以apt-get update 时更新的时候无法更新,提示一下内容 p { margin-bottom: 0.25cm; line-height: 120% } 错误:1 http://ppa.launchpad.net/diesch/testing/ubuntu zesty InRelease 无法连接上 127.0.0.1:42165 (127.0.0.1). - connect (111: 拒绝连接) 错误:2 http://archive.ubuntuk

Linux系统安全需要注意的一些问题

写在前面:当你部署一台服务器,第一步不应该是部署应用,安全是才是首要任务 如果某一天当你登录服务器发现 /bin/bash –i,python -c 'import pty; pty.spawn("/bin/sh")' 等命令在服务器上出现的时候,那么恭喜你,服务器被入侵了 但是入侵者都是很聪明的,首先会执行以下命令 unset HISTORY HISTFILE HISTSAVE HISTZONE HISTORY HISTLOG; export HISTFILE=/dev/null;