0

I'm encountering a situation where I think the mtime returned by ls on the command line differs from the mtime returned by calling stat in my C++ program. Does anyone know why this might be the case?

Because this is a university assignment, I can't share my exact code with you. However, this is essentially what I am doing in my program:

    DIR *dr;
    struct dirent *mydirent;
    dr = opendir(somepath);
    if (dr) {
        while((mydirent = readdir(dr)) != NULL){
            struct stat sb;
            stat(mydirent->d_name, &sb);
            fprintf(stdout, "%s %d\n", mydirent->d_name, (int)sb.st_mtime);
            
        }//while
    }//if
    closedir(dr);

Here's some sample output from my program. I'm focused mainly on the mtime of the file test.txt.

vagrant@vagrant:~/gios-spr-22-pr4$ ls --time-style='+%s' -l ./mnt/server
total 876
-rw-rw-r-- 1 vagrant vagrant 886443 1649448731 gt-einsteing2.jpg
drwxrwxr-x 2 vagrant vagrant   4096 1649535341 sample-files
-rw-rw-r-- 1 vagrant vagrant   1024 **1649535794 test.txt**


vagrant@vagrant:~/gios-spr-22-pr4$ ./bin/dfs-server-p1 
-- SYSINFO: DFSServerNode server listening on 0.0.0.0:42001
.. 1649616999 
**test.txt 1649535767** 
.gitignore 1648919848 
. 1649535767 

In short, why is ls and stat returning different mtimes for test.txt?

Thanks, Ben

Ben
  • 21
  • 3
  • *Because this is a university assignment, I can't share my exact code with you.* Not an issue. You should be providing a [mre] and they rarely look much like the the code they're summarizing. – user4581301 Apr 10 '22 at 21:52
  • Why don't you try to actually check the returned value from `stat` call, to see if the `stat()` call succeeded. It's almost a given that it fails, because of a very common bug that results from not using `stat` correctly after `readdir`, but you're blissfully unaware of the fact that it fails, and therefore the resulting output is complete garbage. If so, then the lesson to learm from this: don't assume that any system call succeeds. – Sam Varshavchik Apr 10 '22 at 21:55
  • user4581301, I actually made a better example, should I change my original post? I don't think the bug will occur if I do, though. Sam Varshavchik, I started checking the return value of stat. As it turns out, for test.txt, stat was failing. Only the stat calls on "." and ".." were succeeding. Can you explain to me why this is the case? – Ben Apr 10 '22 at 22:37
  • Sam Varshavchik was right! I found this post: https://stackoverflow.com/questions/34168266/why-does-stat-fail-using-a-name-from-readdir?msclkid=eb51eceab91e11ecb106e9e4190b2ed7 and that saves the day. I was using the combination of readdir and stat incorrectly. Thanks! – Ben Apr 10 '22 at 22:55
  • My problem was that I was using the combination of readdir and stat incorrectly. This article showed me what to do: https://stackoverflow.com/questions/34168266/why-does-stat-fail-using-a-name-from-readdir?msclkid=eb51eceab91e11ecb106e9e4190b2ed7 – Ben Apr 10 '22 at 22:56
  • fwiw you should use `std::filesystem` if available. And don't prefix `struct` in c++ – apple apple Apr 11 '22 at 07:55

0 Answers0