Linux系统编程(三)文件系统

本文详细介绍了Linux系统中的文件属性获取、权限管理、文件系统(如FAT、UFS)、硬链接和符号链接、目录操作、命令行参数分析、C程序存储空间布局以及库(动态库和静态库)的概念,还包括进程终止机制和函数跳转。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、目录和文件

1.1 文件属性(stat)

stat() 可以通过文件名获取文件的属性

fstat() 可以通过打开的文件描述符获取文件的属性。

lstat() 和 stat() 功能相同,有一点区别就是当 pathname 是一个符号链接文件的时候,lstat() 返回的是符号链接文件本身的属性,而不是链接文件指向的文件的属性。而 stat() 则是返回符号链接所指向文件的属性。

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *pathname, struct stat *statbuf);
int fstat(int fd, struct stat *statbuf);
int lstat(const char *pathname, struct stat *statbuf);

下面是 stat 结构体中的部分成员: 

struct stat {
               dev_t     st_dev;         /* ID of device containing file */
               ino_t     st_ino;         /* Inode number */
               mode_t    st_mode;        /* File type and mode */
               nlink_t   st_nlink;       /* Number of hard links */
               uid_t     st_uid;         /* User ID of owner */
               gid_t     st_gid;         /* Group ID of owner */
               dev_t     st_rdev;        /* Device ID (if special file) */
               off_t     st_size;        /* Total size, in bytes */
               blksize_t st_blksize;     /* Block size for filesystem I/O */
               blkcnt_t  st_blocks;      /* Number of 512B blocks allocated */

               struct timespec st_atim;  /* Time of last access */
               struct timespec st_mtim;  /* Time of last modification */
               struct timespec st_ctim;  /* Time of last status change */
}

我们可以通过 stat 结构体中的 st_size 成员获取文件长度,程序 flen.c 如下:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

off_t flen(char *path){
        struct stat fstat;
        if (stat(path, &fstat) < 0) {
                perror("stat()");
                exit(1);
        }
        return fstat.st_size;
}


int main(int argc, char * argv[])
{
        if (argc < 2) {
                perror("flen <filepath>");
                exit(1);
        }

        off_t fsize = flen(argv[1]);
        printf("file size: %lld\n", (long long)fsize);
        exit(0);
}

但 st_size 仅仅是文件的属性,实际占用磁盘的块大小和个数是 st_blksize 和 st_blocks。如下图,文件 flen.c 的 Size 为 429 B,但是占用磁盘块数为 8个,8 * 512B = 4096B:

下面是一段创建空洞文件的代码 hole_file.c,该程序会创建一个大小为 5g 的空洞文件(注意 5LL * 1024LL * 1024LL * 1024LL 一定要加单位 LL,否则计算结果会溢出): 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(int argc, char *argv[])
{
        if (argc < 2) {
                perror("hole_file <filepath>");
                exit(1);
        }

        int fd = open(argv[1], O_RDWR | O_CREAT);
        if(fd < 0){
                perror("open");
                exit(1);
        }

        /* make a 5g hole file */
        lseek(fd, 5LL * 1024LL * 1024LL * 1024LL, SEEK_SET);
        write(fd, "", 1);

        exit(0);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值