【问题标题】:Get file size with stat syscall使用 stat 系统调用获取文件大小
【发布时间】:2015-01-28 18:19:09
【问题描述】:

我正在尝试使用 stat 系统调用和程序集 (nasm) 获取文件大小:

section .data
    encodeFile db "/home/user/file"

section .bss
    stat resb 64

struc STAT
    .st_dev: resd 1
    .st_ino: resd 1
    .st_mode: resw 1
    .st_nlink: resw 1
    .st_uid: resw 1
    .st_gid: resw 1
    .st_rdev: resd 1
    .st_size: resd 1
    .st_atime: resd 1
    .st_mtime: resd 1
    .st_ctime: resd 1
    .st_blksize: resd 1
    .st_blocks: resd 1
endstruc

_start:
    mov rax, 4
    mov rdi, encodeFile
    mov rsi, stat
    syscall

    mov eax, dword [stat + STAT.st_size]

系统调用执行后rax中有0,很好但是mov eax, dword [stat + STAT.st_size]之后也有0

【问题讨论】:

  • 也许文件的大小为零?

标签: assembly nasm x86-64 system-calls stat


【解决方案1】:

您似乎为 64 位 Linux 编程。从sys/stat.h 获得正确的结构有点困难。我最后为此创建了一个 C 程序:

#include <stdio.h>
#include <sys/stat.h>

int main ( void )
{
    struct stat file_stat;

    printf ("__WORDSIZE: %d\n",__WORDSIZE);
    printf ("__USE_MISC: %d\n",__USE_MISC);
    printf ("__USE_XOPEN2K8: %d\n",__USE_XOPEN2K8);

    printf ("file_stat len: %ld\n", sizeof file_stat);

    long p =  (long)(&file_stat);

    printf ("file_stat.st_dev          pos: %3ld   len: %2ld\n", (long)(&file_stat.st_dev) - p,           sizeof file_stat.st_dev);
    printf ("file_stat.st_ino          pos: %3ld   len: %2ld\n", (long)(&file_stat.st_ino) - p,           sizeof file_stat.st_ino);
    printf ("file_stat.st_nlink        pos: %3ld   len: %2ld\n", (long)(&file_stat.st_nlink) - p,         sizeof file_stat.st_nlink);
    printf ("file_stat.st_mode         pos: %3ld   len: %2ld\n", (long)(&file_stat.st_mode) - p,          sizeof file_stat.st_mode);
    printf ("file_stat.st_uid          pos: %3ld   len: %2ld\n", (long)(&file_stat.st_uid) - p,           sizeof file_stat.st_uid);
    printf ("file_stat.st_gid          pos: %3ld   len: %2ld\n", (long)(&file_stat.st_gid) - p,           sizeof file_stat.st_gid);
    printf ("file_stat.__pad0          pos: %3ld   len: %2ld\n", (long)(&file_stat.__pad0) - p,           sizeof file_stat.__pad0);
    printf ("file_stat.st_rdev         pos: %3ld   len: %2ld\n", (long)(&file_stat.st_rdev) - p,          sizeof file_stat.st_rdev);
    printf ("file_stat.st_size         pos: %3ld   len: %2ld\n", (long)(&file_stat.st_size) - p,          sizeof file_stat.st_size);
    printf ("file_stat.st_blksize      pos: %3ld   len: %2ld\n", (long)(&file_stat.st_blksize) - p,       sizeof file_stat.st_blksize);
    printf ("file_stat.st_blocks       pos: %3ld   len: %2ld\n", (long)(&file_stat.st_blocks) - p,        sizeof file_stat.st_blocks);
    printf ("file_stat.st_atim.tv_sec  pos: %3ld   len: %2ld\n", (long)(&file_stat.st_atim.tv_sec) - p,   sizeof file_stat.st_atim.tv_sec);
    printf ("file_stat.st_atim.tv_nsec pos: %3ld   len: %2ld\n", (long)(&file_stat.st_atim.tv_nsec) - p,  sizeof file_stat.st_atim.tv_nsec);
    printf ("file_stat.st_mtim.tv_sec  pos: %3ld   len: %2ld\n", (long)(&file_stat.st_mtim.tv_sec) - p,   sizeof file_stat.st_mtim.tv_sec);
    printf ("file_stat.st_mtim.tv_nsec pos: %3ld   len: %2ld\n", (long)(&file_stat.st_mtim.tv_nsec) - p,  sizeof file_stat.st_mtim.tv_nsec);
    printf ("file_stat.st_ctim.tv_sec  pos: %3ld   len: %2ld\n", (long)(&file_stat.st_ctim.tv_sec) - p,   sizeof file_stat.st_ctim.tv_sec);
    printf ("file_stat.st_ctim.tv_nsec pos: %3ld   len: %2ld\n", (long)(&file_stat.st_ctim.tv_nsec) - p,  sizeof file_stat.st_ctim.tv_nsec);
    printf ("file_stat.__unused        pos: %3ld   len: %2ld\n", (long)(&file_stat.__unused) - p,         sizeof file_stat.__unused);

    return 0;
}

它的输出:

argv[0]: ./example_stat
__WORDSIZE: 64
__USE_MISC: 1
__USE_XOPEN2K8: 1
file_stat len: 144
file_stat.st_dev          pos:   0   len:  8
file_stat.st_ino          pos:   8   len:  8
file_stat.st_nlink        pos:  16   len:  8
file_stat.st_mode         pos:  24   len:  4
file_stat.st_uid          pos:  28   len:  4
file_stat.st_gid          pos:  32   len:  4
file_stat.__pad0          pos:  36   len:  4
file_stat.st_rdev         pos:  40   len:  8
file_stat.st_size         pos:  48   len:  8
file_stat.st_blksize      pos:  56   len:  8
file_stat.st_blocks       pos:  64   len:  8
file_stat.st_atim.tv_sec  pos:  72   len:  8
file_stat.st_atim.tv_nsec pos:  80   len:  8
file_stat.st_mtim.tv_sec  pos:  88   len:  8
file_stat.st_mtim.tv_nsec pos:  96   len:  8
file_stat.st_ctim.tv_sec  pos: 104   len:  8
file_stat.st_ctim.tv_nsec pos: 112   len:  8
file_stat.__unused        pos: 120   len: 24

这导致以下 NASM 结构:

section .bss
    stat resb 144

struc STAT
    .st_dev         resq 1
    .st_ino         resq 1
    .st_nlink       resq 1
    .st_mode        resd 1
    .st_uid         resd 1
    .st_gid         resd 1
    .pad0           resb 4
    .st_rdev        resq 1
    .st_size        resq 1
    .st_blksize     resq 1
    .st_blocks      resq 1
    .st_atime       resq 1
    .st_atime_nsec  resq 1
    .st_mtime       resq 1
    .st_mtime_nsec  resq 1
    .st_ctime       resq 1
    .st_ctime_nsec  resq 1
endstruc

我用 GCC 作为链接器对其进行了测试,它工作正常。

【讨论】:

    【解决方案2】:

    在我的参考资料(虽然它是 32 位)中,对 STAT 结构的描述略有不同。至少,您的结构的大小不同于 64。

    struct STAT
      .st_dev     dw  ?     ; ID of device containing file
      .pad1       dw  ?
      .st_ino     dd  ?     ; inode number
      .st_mode    dw  ?     ; protection
      .st_nlink   dw  ?     ; number of hard links
      .st_uid     dw  ?     ; user ID of owner
      .st_gid     dw  ?     ; group ID of owner
      .st_rdev    dw  ?     ; device ID (if special file)
      .pad2       dw  ?
      .st_size    dd  ?     ; total size, in bytes
      .st_blksize dd  ?     ; block size
      .st_blocks  dd  ?
    
      .st_atime   dd  ?     ; time of last access
      .unused1    dd  ?
    
      .st_mtime   dd  ?     ; time of last modification
      .unused2    dd  ?
    
      .st_ctime   dd  ?     ; time of last status change
      .unused3    dd  ?
      .unused4    dd  ?
      .unused5    dd  ?
    ends
    

    虽然,这些差异并不能解释为什么您的程序无法运行。 “/home/usr/file”的实际大小是多少。不是0吗?

    【讨论】:

      【解决方案3】:

      改变

      mov eax, dword [stat + STAT.st_size]
      

      mov eax, dword [STAT.st_size]
      

      【讨论】:

      • 我敢打赌一位先生会崩溃。 [stat + STAT.st_size] 应该是正确的。文件名应该以零结尾,但如果 sys_stat 返回零,那可能不是问题。不知道。代码对我来说看起来不错...
      猜你喜欢
      • 1970-01-01
      • 2021-09-13
      • 1970-01-01
      • 2011-09-25
      • 1970-01-01
      • 1970-01-01
      • 2012-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多