使用stat syscall获取文件大小 [英] Get file size with stat syscall
本文介绍了使用stat syscall获取文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用汇编程序(nasm)获取文件大小和统计信息syscall:
I'm trying to get file size wit stat syscall with assembly (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
There is 0 in rax
after syscall executing, it's good but after mov eax, dword [stat + STAT.st_size]
there is 0 too
推荐答案
似乎您为64位Linux编程.从sys/stat.h
获取正确的结构有点困难.我为此创建了一个C程序:
It seems that you program for 64-bit Linux. It is a bit difficult to get the right structure from sys/stat.h
. I created at last a C program for that:
#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结构:
This leads to the following NASM structure:
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作为链接器对其进行了测试,并且可以正常工作.
I tested it with GCC as linker and it worked.
这篇关于使用stat syscall获取文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文