1. 文件和元数据

每个文件都是通过inode引用,每个inode索引节点都具有文件系统中唯一的inode number
一个inode索引节点是存储在Linux文件系统的磁盘介质上的物理对象,也是LInux内核通过数据结构表示的实体
inode存储相关联文件的元数据
 
ls -i 命令获取文件的inode number
 
/* obtaining the metadata of a file */
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat (const char *path, struct stat *buf);
int fstat (int fd, struct stat *buf);
int lstat (const char *path, struct stat *buf);

注意:lstat函数可以获取 符号链接的文件元数据,lstat() returns information about the link itself and not the target file

struct stat {
dev_t st_dev; /* ID of device containing file */
ino_t st_ino; /* inode number */
mode_t st_mode; /* permissions */
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; /* blocksize for filesystem I/O */
blkcnt_t st_blocks; /* number of blocks allocated */
time_t st_atime; /* last access time */
time_t st_mtime; /* last modification time */
time_t st_ctime; /* last status change time */
};

2. 目录

a directory contains a list of filenames, each of which maps to an inode number. Each name is called a directory entry, and each name-to-inode mapping is called a link
当打开给定目录中的文件,内核查询相应的inode编号,然后将inode编号传递给文件系统,文件系统使用inode编号来寻找相应的存储在物理介质中的文件
 
Reading a Directory’s Contents : 
/* creates a directory stream representing the directory given by name */
#include <sys/types.h>
#include <dirent.h>
DIR * opendir (const char *name);
/* returns the next entry in the directory represented by dir */
#include <sys/types.h>
#include <dirent.h>
struct dirent * readdir (DIR *dir);
/* closes the directory stream represented by dir */
#include <sys/types.h>
#include <dirent.h>
int closedir (DIR *dir);
/*
* find_file_in_dir - searches the directory 'path' for a
* file named 'file'.
*
* Returns 0 if 'file' exists in 'path' and a nonzero
* value otherwise.
*/
int find_file_in_dir (const char *path, const char *file)
{
struct dirent *entry;
int ret = ;
DIR *dir;
dir = opendir (path);
errno = ;
while ((entry = readdir (dir)) != NULL) {
if (strcmp(entry->d_name, file) == ) {
ret = ;
break;
}
}
if (errno && !entry)
perror ("readdir");
closedir (dir);
return ret;
}

3. 链接

each name-to-inode mapping in a directory is called a link.
Most files have a link count of 1,that is, they are pointed at by a single directory entry
当一个文件的链接计数减为0时,文件被标记为free,如果存在进程仍在使用此文件,则该文件将保留在文件系统中
 
Linux内核使用 a link count and a usage count 实现,一个文件只有其link count和usage count都为0时,才会从文件系统移除
 
硬链接:
/* creates a new link under the path newpath for the existing file oldpath */
#include <unistd.h>
int link (const char *oldpath, const char *newpath);
成功调用之后,oldpath and newpath refer to the same file
 
符号链接(软链接):
软链接可以跨文件系统,链接到任何文件
/* creates the symbolic link newpath pointing at the target oldpath */
#include <unistd.h>
int symlink (const char *oldpath, const char *newpath);

解链:

#include <unistd.h>
int unlink (const char *pathname);
Once no process has the file open, it is deleted
 

4. Copying and Moving Files

 
Unix没有提供可以直接复制文件和目录的系统调用
 
 copying a file src to a file named dst:
). Open src.
). Open dst, creating it if it does not exist, and truncating it to zero length if it does exist.
). Read a chunk of src into memory.
). Write the chunk to dst.
). Continue until all of src has been read and written to dst.
). Close dst.
). Close src.
 

5. 块设备

The null device lives at /dev/null. The kernel silently discards all write requests to the device. All read requests to the file return end-of-file (EOF).
The zero device lives at /dev/zero.  读此设备返回null字符,写此设备被丢弃
The full device lives at /dev/full.  读此设备返回null字符,写此设备将触发错误表示设备已满
The kernel's random number generators live at /dev/random. 

Linux System Programming 学习笔记(八) 文件和目录管理的更多相关文章

  1. 【Linux】Shell学习笔记之四——文件和目录管理(硬连接和软连接)

    在这节将要学习linux的连接档,在之前用"ls -l" 查看文件属性的命令时, 其中第二个属性是连接数.那么这个连接数是干什么的?这就要理解inode. 先说一下文件是怎么存储的 ...

  2. Linux System Programming 学习笔记(二) 文件I/O

    1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...

  3. CentOS学习笔记--基本命令--文件与目录管理

    Linux基本命令--文件与目录管理 本节节选自鸟哥的 Linux 私房菜 -- 基础学习篇目录  第七章.Linux 文件与目录管理  ls(文件与目录的检视) ls命令就是list的缩写,ls可以 ...

  4. Linux System Programming 学习笔记(一) 介绍

    1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...

  5. Linux System Programming 学习笔记(四) 高级I/O

    1. Scatter/Gather I/O a single system call  to  read or write data between single data stream and mu ...

  6. Linux System Programming 学习笔记(十一) 时间

    1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...

  7. Linux System Programming 学习笔记(九) 内存管理

    1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...

  8. Linux System Programming 学习笔记(七) 线程

    1. Threading is the creation and management of multiple units of execution within a single process 二 ...

  9. Linux System Programming 学习笔记(六) 进程调度

    1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...

随机推荐

  1. macbook secureCRT终端中文乱码的问题

    最近mac用crt中文总是显示的是一串串问号, 而用自带的终端软件就不会出现乱码, 经过一番折腾暂时解决了这一问题, 方法如下: 1. 打开终端操作 sudo vim /etc/profile 在最后 ...

  2. QT5:介绍

    一.简介 QT是一个跨平台的C++开发库,主要用来开发图形用户界面(Graphical User Interface,GUI) QT除了可以绘制漂亮的界面(包括控件/布局/交互),还可以多线程/访问数 ...

  3. mysql中影响数据库性能的因素讲解

    mysql中影响数据库性能的因素讲解 在本篇文章中我们给大家讲述了mysql中影响性能的因素以及相关知识点内容,有兴趣的朋友参考下 关于数据库性能的故事 面试时多多少少会讲到数据库上的事情,“你对数据 ...

  4. poj1654 Area

    题目描述: vjudge POJ 题解: 本以为是水题结果是神题 计算几何求多边形面积. 考虑到结果一定是整数或者整数/2,我们应该用long long 来存…… 用double会死…… 还有日常只能 ...

  5. 控制nginx并发链接数量和客户端请求nginx的速率

    一.控制nginx并发链接数 ngx_http_limit_conn_module这个模块用于限制每个定义的key值的链接数,特别是单IP的链接数. 不是所有的链接数都会被计数,一个符合计数要求的连接 ...

  6. python3 输入某年某月某日,判断这一天是这一年的第几天?

    题目 输入某年某月某日,判断这一天是这一年的第几天? 程序分析 特殊情况,闰年时需考虑二月多加一天. 代码: import calendar year = int(input("Year:& ...

  7. HMAC(Hash-based Message Authentication Code)实现原理

    1.HMAC 概念 HMAC(Hash-based Message Authentication Code)基于 hash 的消息验证码,是 安全通信中必要的组成部件. 主要是 防止消息被篡改,和对称 ...

  8. 【HIHOCODER 1182】欧拉路·三

    描述 小Hi和小Ho破解了一道又一道难题,终于来到了最后一关.只要打开眼前的宝箱就可以通关这个游戏了. 宝箱被一种奇怪的机关锁住: 这个机关是一个圆环,一共有2^N个区域,每个区域都可以改变颜色,在黑 ...

  9. zoj 4054

    #define ll long long ; int t; ll ans,tmp; char s[N]; int main() { scanf("%d",&t); whil ...

  10. SSAS——MDX基础

    一.基本概念 MDX:一种查询语言,从多维的数据集单元格中检索数据.支持两种不同的模式: 1.表达式语言:定义和操纵Analysis Services对象和数据以计算值 2.查询语言:从Analysi ...