stat 函数未在 st_size 中返回正确的结果

问题描述 投票:0回答:0

我有一个奇怪的场景,stat 函数提供的文件大小始终为 4096。 下面是代码片段:

#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;
}

输出:

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#

实际文件大小为2字节。我创建这个只是为了测试。 然而,stat 函数给出了正确的结果:

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)

我使用 gdb 并打印了 stat 结构,发现以下结果:

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}}

我正在使用 aarc64 机器。

我希望 st_size 的值应该是 2 而不是 4096

linux arm arm64 stat fstat
© www.soinside.com 2019 - 2024. All rights reserved.