0

I have a strange scenario where the stat function is providing the file size always as 4096. Below is the code snippet:

#include <iostream>
#include <sys/stat.h>
using namespace std;

int main()
{
        struct stat st;

        cout << "stat call "<< stat("a.txt", &st)<< endl;
        cout << "./a.txt File size " << st.st_size << endl;
        FILE *fp = fopen("a.txt", "rb");
        struct stat finfo;
        if (fstat(fileno(fp), &finfo)) cout << "fail"<< endl;
        cout << "./a.txt File ftst size " << finfo.st_size << endl;
        return 0;
}

Output:

stat call 0
./a.txt File size 4096
./a.txt File ftst size 4096
root@sv3aggr005:/home/akumar/extract# ll a.txt
-rw-r--r-- 1 root root 2 Apr 25 09:08 a.txt
root@sv3aggr005:/home/akumar/extract#

The actual file size is 2 bytes. I created this just for test. However, the stat function gives proper result:

root@sv3aggr005:/home/akumar/extract# stat a.txt
  File: a.txt
  Size: 2               Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d    Inode: 1742602     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)

I used gdb and printed the stat structure and found the below result:

Breakpoint 1, main () at teststat_dup.cpp:9
warning: Source file is more recent than executable.
9               cout << "stat call "<< stat("a.txt", &st)<< endl;
(gdb) gdb st
Undefined command: "gdb".  Try "help".
(gdb) print st
$1 = {st_dev = 281474842417696, st_ino = 281474842419680, st_mode = 4158107648, st_nlink = 0, st_uid = 4294964472, st_gid = 65535, st_rdev = 187649984503048, __pad1 = 0,
  st_size = 281474842189824, st_blksize = 281470681743361, __pad2 = 0, st_blocks = 281474842458968, st_atim = {tv_sec = 281470681743360, tv_nsec = 281474842487496}, st_mtim = {
    tv_sec = 281474840607424, tv_nsec = 3}, st_ctim = {tv_sec = 281474976707360, tv_nsec = 281474840597192}, __glibc_reserved = {-134512544, 65535}}
(gdb) s
__GI___stat64 (file=0xaaaaaaaa0ec8 "a.txt", buf=0xfffffffff248) at ../sysdeps/unix/sysv/linux/stat64.c:29
29      ../sysdeps/unix/sysv/linux/stat64.c: No such file or directory.
(gdb) s
__GI___fstatat64 (fd=-100, file=0xaaaaaaaa0ec8 "a.txt", buf=0xfffffffff248, flag=0) at ../sysdeps/unix/sysv/linux/fstatat64.c:163
163     ../sysdeps/unix/sysv/linux/fstatat64.c: No such file or directory.
(gdb) s
fstatat64_time64_stat (flag=0, buf=0xfffffffff248, file=0xaaaaaaaa0ec8 "a.txt", fd=-100) at ../sysdeps/unix/sysv/linux/fstatat64.c:98
98      in ../sysdeps/unix/sysv/linux/fstatat64.c
(gdb) s
__GI___fstatat64 (fd=<optimized out>, file=0xaaaaaaaa0ec8 "a.txt", buf=0xfffffffff248, flag=0) at ../sysdeps/unix/sysv/linux/fstatat64.c:166
166     in ../sysdeps/unix/sysv/linux/fstatat64.c
(gdb) s
stat call 0
main () at teststat_dup.cpp:10
10              cout << "./a.txt File size " << st.st_size << endl;
(gdb) print st
$2 = {st_dev = 64769, st_ino = 1742602, st_mode = 33188, st_nlink = 0, st_uid = 0, st_gid = 0, st_rdev = 0, __pad1 = 2, st_size = 4096, st_blksize = 8, __pad2 = 1682438907,
  st_blocks = 857538058, st_atim = {tv_sec = 1682438907, tv_nsec = 857538058}, st_mtim = {tv_sec = 1682438907, tv_nsec = 857538058}, st_ctim = {tv_sec = 0, tv_nsec = 281474840597192},
  __glibc_reserved = {-134512544, 65535}}

I am using aarc64 machine.

I expect that st_size should have a value of 2 instead of 4096

  • What are the contents of `a.txt` file? – Gaurav Pathak Apr 26 '23 at 04:33
  • @GauravPathak a.txt is just a dummy file with one character present. ```root@sv3aggr005:/home/akumar/extract# cat a.txt a ``` – user2456835 Apr 26 '23 at 06:03
  • Does your compiler support the OS version? It seems that the header file does not match the OS layout. If the structure elements are different (for instance, no padding), then you will have this issue. Do you have kernel user space headers to use directly generated by the kernel image in use? – artless noise Apr 26 '23 at 17:32
  • This looks like either a broken installation or a libc bug. What OS, compiler, distribution is this, and what exact versions? How did you install your development tools? – Nate Eldredge Apr 28 '23 at 14:40
  • I can't reproduce this on stock Ubuntu 23.04 aarch64. Both lines print `2` as expected. – Nate Eldredge May 02 '23 at 05:03

1 Answers1

0

I was using a common machine and someone had manually overwritten typesizes.h from a x86_64 Linux machine which led to this issue. I replaced it with the aarch64 typesizes.h and the issue was resolved.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 04 '23 at 12:55