文件存储

Inode

  • 本质为结构体,存储文件的属性信息,例如权限、类型、大小、时间等等
  • 也称为文件属性管理结构,大多数inode都存储在磁盘上
  • 少量、近期使用的inode会被缓存到内存中

dentry

  • dentry是一个内存实体,其中的d_inode成员指向对应的inode
  • dentry里面也存有文件名、硬链接名
  • 也称目录项

stat函数

  • 用于获取文件属性,从inode结构体中获取

  • lstat函数与stat函数没什么区别,但lstat不会穿透链接,stat会

  • 函数原型

    int stat(const char *path, struct stat *buf);
    
  • path文件名路径

  • buf:存放文件属性

  • 返回值:

    • 成功返回0
    • 失败返回-1
  • 代码示例

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h> 
#include <sys/stat.h>    
int main(int argc, char *argv[])
{
    struct stat sbuf;
    int ret =  stat(argv[1], &sbuf);
    if(ret==-1)
    {   
        perror("stat error");
        exit(1);
    }   
    printf("file size %ld\n",sbuf.st_size);                                                       
    return 0;
}

link函数

Ulink函数

  • 作用删除文件,也就是删除目录项
  • 如果文件的硬链接数为0,系统不会马上释放该文件,而是等到所有进程关闭该文件,系统才会挑时间释放掉该文件

dup和dup2

复制文件描述符

int dup(int oldfd);
//oldfd:已有的文件描述符
//return:新文件描述符

//文件描述符复制和文件描述符重定向
int dup2(int oldfd, int newfd);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Mr_Csyn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值