简体   繁体   English

为什么 Linux 中的 stat 函数返回的结果与 C 中的 stat function 不同

[英]Why stat funcion in Linux return different result than stat function in C

Sorry for this totally amateur question, but I can't understand one difference between the native Linux stat program and the stat function in C抱歉这个完全业余的问题,但我无法理解原生 Linux stat 程序和 C 中的 stat function 之间的区别

The point is, when I use a stat program in the example directory: "/ usr" its modification time is as follows:重点是,当我使用示例目录中的stat程序时:“/usr”它的修改时间如下:

  File: /usr
Access: 2022-01-08 17:28:57.400521375 -0600
Modify: 2021-12-22 15:45:08.584831000 -0600
Change: 2021-12-22 15:45:08.584831000 -0600


But when I execute the following code但是当我执行以下代码时

int getDirContent(){

    DIR *dir;
    struct dirent *entry;
    struct stat pFilestat;
    if ((dir = opendir("/")) == NULL)
        perror("opendir() error");
    else {
        puts("contents of root:");
    
    /* Read directory entries */
    while ((entry = readdir(dir)) != NULL){
         /* Extract Filename */
        stat(entry->d_name, &pFilestat);
        printf("%s\n", entry->d_name);

        /* Extract Create time */
        if (S_ISDIR(pFilestat.st_mode)){
            printf("Access: %s",ctime(&pFilestat.st_atime));
            printf("Modify: %s",ctime(&pFilestat.st_mtime));
            printf("Change: %s",ctime(&pFilestat.st_ctime));


        }
        }

    closedir(dir);
  }
}

the result is as follows结果如下

contents of root:
[...]
usr
Access: Sun Jan  9 16:08:20 2022
Modify: Sun Jan  9 16:09:10 2022
Change: Sun Jan  9 16:09:10 2022

My system:我的系统:

OS: Linux debian x86_64 GNU/Linux
compiler version:
Thread model: posix
gcc version 8.3.0 (Debian 8.3.0-6)

[user@debia]:~$ timedatectl
               Local time: Sun 2022-01-09 16:05:35 CET
           Universal time: Sun 2022-01-09 15:05:35 UTC
                 RTC time: Sun 2022-01-09 15:05:36
                Time zone: Europe/(CET, +0100)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no

if the question has already been asked, please send me a link.如果问题已经被问过,请给我一个链接。 Unfortunately, I tried to find the answer, but I can't ask a good question on google不幸的是,我试图找到答案,但我无法在 google 上提出一个好的问题

EDIT:编辑:

Using of getDirContent: main.c getDirContent的使用:main.c

include <string.h>
#include <stdio.h>

#include "header/integrity_monitoring.h"

int main() {


    const char * pPath_array[] = {"/etc","/home", NULL}; // array of pointers
    /*
    For the sake of simplicity, the code removes the possibility for functions to accept arguments in the form of an array. Everything happens from inside the getDirContent function*/

    getDirContent();
    
    return 0;
}

wants to go through the directories indicated in the pointer table想通过指针表中指示的目录 go

Maybe the problem is that stat is not acessing the same files readdir is...也许问题是stat没有访问相同的文件readdir是......

The value in entry->d_name does not include the directory nor it is the full path to the entry. entry->d_name中的值不包括目录,也不是条目的完整路径。 It is just the file name (eg: "url" , not "/url" ).它只是文件名(例如: "url" ,而不是"/url" )。

You could try and concatenate the directory ( "/" ) with the entry->d_name like this:您可以尝试将目录( "/" )与entry->d_name连接起来,如下所示:

char fullFilepath[PATH_MAX + 1];
snprintf(fullFilepath, PATH_MAX, "/%s", entry->d_name);

/* Extract Filename */
stat(fullFilepath, &pFilestat);
printf("%s\n", fullFilepath);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 linux stat函数中struct stat * buffer和&buffer有什么区别 - what is the different between struct stat * buffer and &buffer in linux stat function 为什么统计返回错误的结果? - Why does stat return wrong result? 在C中重现Linux stat命令 - Reproducing Linux stat command in C stat() function 的最大文件名长度是否与操作系统接受的不同? - Is there a different maximum file name length for stat() function than the OS accepts? Linux C stat()八进制权限掩码,连接发送到函数? - Linux C stat() octal permissions masks, concatenate to send to function? 在 Linux(RedHat) 中,C function malloc_stats() 显示与 /proc/ 不同的值<pid> /stat 常驻 memory 大小</pid> - In Linux(RedHat) , C function malloc_stats() shows different values compared to /proc/<pid>/stat resident memory size linux stat参数指向未初始化的字节? - c linux stat param points to uninitialised bytes? Linux上POSIX函数“ stat”的定义在哪里? - Where is the definition of the POSIX function “stat” on Linux? [C] [Stat] [Fileinfo]当我使用stat()调用返回结构时,为什么将st_mode定义为不在结构中的东西? - [C][Stat][Fileinfo] Why is st_mode defined as something not in structure, when I use the stat() call to return a structure? 为什么stat和fstat返回st_size == 0? - Why stat and fstat return the st_size == 0?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM