Unix环境之文件和目录

文件结构stat——

下面三个函数可以获取文件的状态信息:

#include <sys/stat.h>
int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

stat函数指定文件路径,fstat指定文件描述符,lstat类似于stat,但对于符号链接文件来说,lstat获取的是符号链接文件本身的状态信息而非引用的那个文件的状态信息,stat是个结构体,如下:

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

文件类型——

UNIX系统的文件类型包括普通文件、目录文件、块特殊文件、字符特殊文件、FIFO(命名管道)、套接字、符号链接,其中最主要的文件类型是普通文件。

文件权限——

每个文件都有9个访问权限位,分别是用户、组和其它的读、写和执行权限。与每个进程相关联的ID有诸多个,分别是实际用户ID、实际组ID、有效用户ID、有效组ID、附加组ID、保存的设置用户ID、保存的设置组ID。创建一个新文件时,新文件的ID与当前进程的ID有关。下面的access函数是按实际用户ID和实际组ID进行访问权限测试的:

#include <unistd.h>
int access(const char *pathname, int mode);

创建文件时可以指定文件模式,与这个文件模式相关的有个屏蔽字,umask函数可以指定屏蔽字:

#include <sys/stat.h>
mode_t umask(mode_t cmask);

更改现有文件的访问权限可以使用下面两个函数:

#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);

更改现有文件的用户ID、组ID可以使用下面几个函数:

#include <unistd.h>
int chown(const char *path, uid_t owner, gid_t group);
int fchown(int fd, uid_t owner, gid_t group);
int lchown(const char *path, uid_t owner, git_t group);

文件截短——

在创建文件时可以指定O_TRUNC将文件截短,下面的函数也可以做到:

#include <unistd.h>
int truncate(const char *path, off_t length);
int ftruncate(int fd, off_t length);

粘住位——

S_ISVTX位被称为粘住位或保存文本位,如果一个可执行程序文件的这一位被设置了,那么在该程序第一次被执行并结束时,其程序正文部分的一个副本仍被保存在交换区,这使得下次执行该程序时能较快地将其装入内存区。其原因是交换区占用连续磁盘空间,可将它视为连续文件,而且一个程序的正文部分在交换区也是连续存放的,而在一般的UNIX文件系统中,文件的各数据块很可能是随机存放的,对于常用的应用程序,例如文本编辑器和C编辑器,我们常常设置它们所在文件的粘住位。现近较新的UNIX系统大多数都配置有虚拟存储系统以及快速文件系统,所以不再需要使用这种技术。

文件系统——

以传统的基于BSD的UNIX文件系统UFS为例,我们可以把一个磁盘分成一个或多个分区,每个分区可以包含一个文件系统。一个很重要的概念就是i节点,它包含了大多数与文件有关的信息:文件类型、文件访问权限位、文件长度和指向该文件所占用的数据块的指针等等。stat结构中的大多数信息都取自i节点。只有两项数据存放在目录项中:文件名和i节点编号。

文件链接——

链接分为符号链接和硬链接,前者是指向一个文件的间接指针,而后者直接指向文件的i节点。引入符号链接的原因是为了避开硬链接的一些限制:硬链接通常要求链接和文件位于同一文件系统中;只有超级用户才能创建指向目录的硬链接。既然符号链接可以指向目录,有可能引入循环,使用时这点需要注意。

任何一个文件可以有多个目录项指向i节点,创建一个指向现有文件的链接的方法是使用link函数:

#include <unistd.h>
int link(const char *oldpath, const char *newpath);

为了删除一个 现有的目录项,可以调用unlink函数:

#include <unistd.h>
int unlink(const char *pathname);

解除对一个文件或目录的链接也可以用remove函数:

#include <stdio.h>
int remove(const char *pathname);

文件名或目录用rename函数更名:

#include <stdio.h>
int rename(const char *oldname, const char *newname);

symlink函数创建一个符号链接:

#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);

readlink函数打开符号链接并读取链接中的名字:

#include <unistd.h>
ssize_t readlink(const char *path, char *buf, size_t bufsize);

文件时间——

对每个文件保持有三个时间段,分别是文件数据的最后访问时间st_atime、文件数据的最后修改时间st_mtime和i节点状态的最后更改时间st_ctime。一个文件的访问和修改时间可以用utime函数更改:

#include <utime.h>
int utime(const char *filename, const struct utimbuf *times);

utimbuf结构如下:

struct utimbuf {
        time_t actime;       /* access time */
        time_t modtime;      /* modification time */
};

目录操作——

mkdir函数创建目录,rmdir函数删除空目录:

#include <syt/stat.h>
int mkdir(const char *pathname, mode_t mde);

#include <unistd.h>
int rmdir(const char *pathname);

读取目录可以使用下列函数:

#include <dirent.h>
DIR* opendir(const char *name);
DIR* fdopendir(int fd);
struct dirent* readdir(DIR *dirp);
void rewinddir(DIR *dirp);
int closedir(DIR *dirp);
int telldir(DIR *dirp);
void seekdir(DIR *dirp, long offset);

dirent结构如下:

struct dirent {
    ino_t          d_ino;       /* inode number */
    off_t          d_off;       /* offset to the next dirent */
    unsigned short d_reclen;    /* length of this record */
    unsigned char  d_type;      /* type of file; not supported
                                   by all file system types */
    char           d_name[256]; /* filename */
};

下面两个函数可以更改当前工作目录:

#include <unistd.h>
int chdir(const char *path);
int fchdir(int fd);

getcwd获取当前工作目录:

#include <unistd.h>
char* getcwd(char *buf, size_t size);
时间: 2024-08-24 22:35:16

Unix环境之文件和目录的相关文章

unix环境编程 文件操作

1.  文件的打开读写 关闭 int open(const char *pathname,int flag,-)   打开一个文件 成功则返回文件描述符,若出现则返回-1 flag可以取下面的常量 O_RDONLY  只读打开 O_WRONLY  只写打开 O_RDWR    读写打开 其它的常量 O_APPEND,O_CREAT,O_EXCL,O_TRUNC,O_NOCTTY,O_NONBLOCK是可选的. int create(const char *pathname,mode_t mode

Unix环境之文件IO

Unix系统中的大多数文件IO只需用到5个函数:open.read.write.lseek以及close.这些函数是不带缓冲的IO,不带缓冲指的是每个read和write都调用内核中的一个系统调用,它们不是ISO/C的组成部分,而是POSIX和SUS的东西. 文件描述符-- 对于内核而言,所有打开的文件都通过文件描述符引用.文件描述符是一个非负整数,按照惯例,文件描述符0.1.2分别表示的是标准输入.标准输出.标准出错,在依从POSIX的应用程序中,这3个幻数应当替换为符号常量STDIN_FIL

Unix 环境高级编程

1 UNIX 环境高级编程 2 目录 3 1.Unix基础知识 4 2.Unix标准化及实现 5 3.文件I/O 6 4.文件盒目录 7 5.标准I/O库 8 6.系统数据文件和信息 9 7.进程环境 10 8.进程控制 11 9.进程关系 12 10.信号 13 11.线程 14 12.线程控制 15 13.守护线程 16 14.高级I/O 17 15.进程间通信 18 16.网络IPC:套接字 19 17.高级进程间通信 20 18.终端I/O 21 19.伪终端 22 20.数据库函数库

UNIX 环境高级编程 文件和目录

函数stat , fstat , fstatat , lstat stat函数返回与此文件有关的信息结构. fstat函数使用已打开的文件描述符(而stat则使用文件名) fstatat函数 为一个相对于当前打开目录的路径名返回文件信息. lstat函数返回该符号链接的有关信息,而不是该符号链接引用的文件的信息. 使用stat最多的地方可能就是 ls -l 命令. st_mode  与 S_IFMT 进行 与 运算 在与 S_IFXXX常量相比较,来判断类型. 文件类型 1.普通文件: 无论是文

unix环境高级编程笔记(4)—— 文件和目录(1)

1 引言 本文将描述文件系统的一些特征和文件的性质,从stat函数开始,逐个讲解stat结构的成员以了解文件的属性. 2 stat,fstat 和 lstat函数 #include <sys/stat.h> int stat(const char *restrict pathname,struct stat *restrict buf); int fstat(int filedes,struct stat *restrict buf); int lstat(const char *restri

《UNIX环境高级编程》---4文件和目录

文件和目录 一. stat 结构和权限相关 四个stat函数:返回文件或者目录的信息结构: #include<sys/stat.h> int stat(const char * restrict pathname, struct stat*restrict buf); int fstat(int fd, struct stat* buf); int lstat(const char* restrict pathname,struct stat *restrict buf); int fstat

Unix环境高级编程(二)文件和目录

本章主要介绍的是文件结构及目录.重点是通过stat函数获取文件的结构信息,然后是文件目录及其遍历.学完本章后,编写了一个输出给的目录下的文件信息的程序. 首先是包含在<sys/stat.h>文件下的stat.fstat.lstat三个函数,三个函数的原型如下: int stat(const char *path, struct stat *buf);int fstat(int fd, struct stat *buf);int lstat(const char *path, struct st

unix环境高级编程笔记(5)—— 文件和目录(2)

1 文件截短 有时我们需要把文件尾端处截去一些数据以缩短文件,将一个文件清0是一个特例.在打开文件时使用O_TRUNC标志可以做到这一点. #include <unistd.h> int truncate(const char *pathname,off_t length); int ftruncate(int filedes,off_t length); 成功返回0,出错返回-1. 如果length 小于文件长度,则使文件长度截断至length,剩下的不可访问.如果length大于文件长度,

Unix高级环境编程学习笔记(二):文件和目录

1 stat,fstat,lstat函数 #include<sys/stat.h> int stat (const char* restrict pathname, struct stat *restrict buf); int fstat (int filedes, struct stat * buf); int lstat (const char* restrict pathname, struct stat *restrict buf); 成功返回0,失败返回-1 stat返回pathn