stat function not returning proper result in st_size

149 Views Asked by At

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

1

There are 1 best solutions below

1
user2456835 On

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.