Linux System Programming 学习笔记(八) 文件和目录管理
1. 文件和元数据
/* 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. 目录
/* 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. 链接
/* creates a new link under the path newpath for the existing file oldpath */
#include <unistd.h>
int link (const char *oldpath, const char *newpath);
/* 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);
4. Copying and Moving Files
). 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. 块设备
Linux System Programming 学习笔记(八) 文件和目录管理的更多相关文章
- 【Linux】Shell学习笔记之四——文件和目录管理(硬连接和软连接)
在这节将要学习linux的连接档,在之前用"ls -l" 查看文件属性的命令时, 其中第二个属性是连接数.那么这个连接数是干什么的?这就要理解inode. 先说一下文件是怎么存储的 ...
- Linux System Programming 学习笔记(二) 文件I/O
1.每个Linux进程都有一个最大打开文件数,默认情况下,最大值是1024 文件描述符不仅可以引用普通文件,也可以引用套接字socket,目录,管道(everything is a file) 默认情 ...
- CentOS学习笔记--基本命令--文件与目录管理
Linux基本命令--文件与目录管理 本节节选自鸟哥的 Linux 私房菜 -- 基础学习篇目录 第七章.Linux 文件与目录管理 ls(文件与目录的检视) ls命令就是list的缩写,ls可以 ...
- Linux System Programming 学习笔记(一) 介绍
1. Linux系统编程的三大基石:系统调用.C语言库.C编译器 系统调用:内核向用户级程序提供服务的唯一接口.在i386中,用户级程序执行软件中断指令 INT n 之后切换至内核空间 用户程序通过寄 ...
- 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 ...
- Linux System Programming 学习笔记(十一) 时间
1. 内核提供三种不同的方式来记录时间 Wall time (or real time):actual time and date in the real world Process time:the ...
- Linux System Programming 学习笔记(九) 内存管理
1. 进程地址空间 Linux中,进程并不是直接操作物理内存地址,而是每个进程关联一个虚拟地址空间 内存页是memory management unit (MMU) 可以管理的最小地址单元 机器的体系 ...
- Linux System Programming 学习笔记(七) 线程
1. Threading is the creation and management of multiple units of execution within a single process 二 ...
- Linux System Programming 学习笔记(六) 进程调度
1. 进程调度 the process scheduler is the component of a kernel that selects which process to run next. 进 ...
随机推荐
- 解决TS报错Property 'style' does not exist on type 'Element'
在使用queryselector获取一个dom元素,编译时却报错说property 'style' does not exist on type 'element'. 原因:这是typescript的 ...
- 【转】C++后台开发应该读的书
转载自http://www.cnblogs.com/balloonwj/articles/9094905.html 作者 左雪菲 根据我的经验来谈一谈,先介绍一下我的情况,坐标上海,后台开发(也带团队 ...
- 洛谷 1571 眼红的Medusa
洛谷 1571 眼红的Medusa 虽说这道题标签里写明了二分,然而我还是首先想到了map......毕竟map真的是简单好写. map做法 #include<bits/stdc++.h> ...
- python入门:while 循环的基本用法
#!/usr/bin/env python # -*- coding:utf-8 -*- #while 循环的作用 import time while True: ") time.sleep ...
- python导出开发环境
1.导出开发环境的依赖包 本地开发完后,再把代码给别人之前,需要 pip freeze > pip123.txt 2.其他环境安装依赖包 pip install -r pip123.txt 其他 ...
- Thinkphp5 获取执行的sql语句
获取最后执行的sql语句 $str_order_action = db('order_action')->getLastSql(); //获取最后执行的sql语句 获取执行的sql语句 $ord ...
- 分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN
分享一个编程学习网站:https://github.com/justjavac/free-programming-books-zh_CN
- MySQL迁移至MariaDB
为什么要用MariaDB来代替MySQL MariaDB是MySQL社区开发的分支,也是一个增强型的替代品.它由MySQL前开发者们带头组织的基金会开发,使用起来和MySQL完全一样.自从Oracle ...
- bash 统计在线时长最长的十个玩/统计一天内一直处于不活跃状态的玩家的百分比
1.某游戏的客户端每隔5分钟会向服务端报告一次玩家的账户积分,如果两次报告的时间间隔不大于5分钟,认为该玩家在这5分钟内在线,假设报告数据的格式如下: IP Dat ...
- 有限状态机(FSM)的设计与实现
有限状态机(FSM)是表示有限个状态及在这些状态之间的转移和动作等行为的数学模型,在计算机领域有着广泛的应用.通常FSM包含几个要素:状态的管理.状态的监控.状态的触发.状态触发后引发的动作.本文主要 ...