`
DiaoCow
  • 浏览: 243702 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

【读书笔记】《Unix环境高级编程》第四章 Files and Directories

阅读更多
看完了该章,觉得最重要的是需要了解以下几个方面:
a.stat结构
b.进程权限及作用
c.文件相关属性(访问权限,文件时间等等)
d.unix文件系统(inode和block关系)


现在我就以流水账^_^的形式对上述几个方面做个总结:

---------------------------------------------------------------华丽风格线---------------------------------------------------------------

1.获取指定文件的stat结构
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);

这三个方法基本相同,但是略有区别:
a.fstat函数是通过文件描述符来获取指定文件的stat结构;
b.当传递给lstat函数的文件是一个符号链接时,返回的是该符号链接的stat结构,而不是其引用文件;

现在我们来看下stat结构的大致定义:
struct stat {
       mode_t    st_mode;      /* file type & mode (permissions) */
       ino_t     st_ino;       /* i-node number (serial number) */
       dev_t     st_dev;       /* device number (file system) */
       dev_t     st_rdev;      /* device number for special files */
       nlink_t   st_nlink;     /* number of links */
       uid_t     st_uid;       /* user ID of owner */
       gid_t     st_gid;       /* group ID of owner */
       off_t     st_size;      /* size in bytes, for regular files */
       time_t    st_atime;     /* time of last access */
       time_t    st_mtime;     /* time of last modification */
       time_t    st_ctime;     /* time of last file status change */
       blksize_t st_blksize;   /* best I/O block size */
       blkcnt_t  st_blocks;    /* number of disk blocks allocated */
     };

后面的很多操作函数(譬如chown, chmod),实际是对该结构的修改

2.每一个进程都有以下6个权限: 

其中,real user ID 和 real group ID就是我们当前登录的那个用户,并且effective user ID 和 effective group ID的值通常就是real user ID 和 real group ID,但是我们依然有能力改变这一默认值:

譬如,我们可以使用passwd命令去修改自己账户的登录密码,而修改密码的实质其实就是修改/etc/passwd或者/etc/shadow文件,但是如果你仔细的化会发现,这两个文件权限实际上都是只读文件,那么当我们一个普通用户执行passwd命令时是如何修改成功的呢?


原因就在于passwd文件设置了set-user-ID位,当一个普通用户进程执行该文件时,该进程的effective user ID被设置为文件的user ID(即root),然后它就有权限去读写/etc/passwd或者/etc/shadow文件了(set-user-ID标记位存在于stat结构的st_mode字段)


3.每一个文件都有它自己的权限标记位-rw-rw-rw-(这些标记位同样也是存放于stat结构的st_mode字段),这些标记位控制着你是否能够读或是写又或是执行文件,对于普通文件,我们很好理解,但是对于目录,这些权限由意味着什么呢(譬如一个目录的x权限)?

a.如果我们想进入一个目录cd ~/test,则必须有该目录的执行权限,譬如:当我们希望读取/test/out.tmp文件,我们不仅仅要有out.tmp的读取权限,还必须有根目录/,test目录的可执行权限

b.若我们想在某个目录下执行ls命令,则仅仅有该目录的执行权限还不够,还必须有该目录的可读权限

c.当我们想删除目录下的某个文件时,则需要改目录的执行和写权限,而不需要关于删除文件的任何权限(如果你了解unix的文件系统,删除一个文件,实际上是删除该目录对应block中中的某些数据)

4.理解好该章的一个很大前提是了解unix文件系统的布局(特别是inode和block的关系),关于这方面知识,鸟哥的私房菜是个很不错的学习资料:http://vbird.dic.ksu.edu.tw/linux_basic/0230filesystem.php#harddisk-inode,当我们了解indoe和block,你就可以知道:
a.为什么删除一个目录下某个文件,只需要该目录的相关权限,而不需要文件本身的相关权限;
b.硬链接和符号链接的区别是什么,为什么创建硬链接不增加inode数;
c.为什么修改文件的权限或者拥有者,只改变了文件c_time而未改变文件的m_time;
d. ... 还有很多问题,不再赘述;

5.文件时间,一个文件有三个时间:st_atime,st_mtime,st_ctime(对应stat结构中的三个字段),其中
st_atime表示文件内容的最后访问时间(block);
st_mtime表示文件内容的最后修改时间(block);
st_ctime表示文件相关属性(譬如权限)最后修改时间(inode);  <-- 这个点原来是我最迷糊的地方

如果我们对文件系统中的inode和block有个比较熟悉的了解,就很容易区分这三种时间的不同


好了,流水账就记到这里






  • 大小: 29.7 KB
  • 大小: 13.8 KB
  • 大小: 10.3 KB
分享到:
评论

相关推荐

    UNIX环境高级编程英文第三版+源码

    1.4 Files and Directories 4 1.5 Input and Output 8 1.6 Programs and Processes 10 1.7 Error Handling 14 1.8 User Identification 16 1.9 Signals 18 1.10 Time Values 20 1.11 System Calls and Librar y ...

    UNIX环境高级编程(第二版,英文版)

    此书对于linux环境编程的同志应该是必读的书,目录如下: Copyright Praise for Advanced Programming in the UNIX® Environment, Second Edition Praise for the First Edition Addison-Wesley ...

    VM虚拟机Ubuntu系统打开报错A start job is running for Create Volatile Files and Directories(no limit)解决方案

    报错是因为虚拟机的磁盘用完了,想要正常启动需要进入...4. 按 ctrl+x或者F10 进入单用户模式,当前用户即为root。这时候可以修改删除文件。修改完毕后重启即可。 回车,然后就可以看到下图界面 5.附录:虚拟机可以

    unix环境高级编程[第二版]

    Building on Stevens' work, he begins with basic topics such as files, directories, and processes, carefully laying the groundwork for understanding more advanced techniques, such as signal handling ...

    UNIX环境高级编程第二版.chm 英文版 Advanced Programming in the UNIX® Environment: Second Edition

    Building on Stevens' work, he begins with basic topics such as files, directories, and processes, carefully laying the groundwork for understanding more advanced techniques, such as signal handling ...

    Reading and sorting directories with C on Linux and Unix

    Directories - Reading and sorting directories. readdir.c - Reading a directory (readdir). dirsortsize.c - Sort a directory by file size (scandir). dirsortalpha.c - Sort a directory alphabetically ...

    KDiff3 is a program that compares two or three text input files or directories, shows the differences line by line and character by character, provides an automatic merge facility and an integrated editor for comfortable solving of merge conflicts,

    KDiff3 is a program that compares two or three text input files or directories, shows the differences line by line and character by character, provides an automatic merge facility and an integrated ...

    京峰教育Linux Shell高级编程

    京峰教育 Linux Shell 高级编程 本文档是京峰教育 shell 高级编程文档,内部私密文档(vip 内部文档),主要介绍 Linux shell 高级编程的实战经验和技术要点。下面是本文档的知识点概括: 1. Shell 编程实战系统...

    go系统编程(英文版)

    Chapter 5, Files and Directories, is the first chapter of this book that deals with a systems programming topic, which is the handling of files, symbolic links, and directories. In this chapter, you ...

    opengl编程环境配置

    OpenGL编程环境配置主要涉及到在两种不同的开发环境中设置OpenGL库,以便能够编写和运行OpenGL图形程序。本文将详细讲解如何在Microsoft Visual C++ 6.0(VC6.0)和Visual Studio .NET环境下配置OpenGL。 首先,...

    Linux系统编程

    files and directories. Chapter 9, Memory Management This chapter covers memory management. It begins by introducing Unix concepts of memory, such as the process address space and the page, and ...

    Unix shell programming in 24 hours.pdf

    - **File Types** Unix distinguishes between regular files, directories, symbolic links, character devices, block devices, and named pipes (FIFOs). - **Owners, Groups, and Permissions** Each file has ...

    unix power tools 3ed.pdf

    #### 三、文件和目录操作 (Part III: Working with Files and Directories) **3.1 目录组织 (Chapter 7: Directory Organization)** - **文件系统结构**: 如Linux的`/bin`, `/usr`, `/etc`等目录的作用。 - **最佳...

    unix系统命令

    以上只是Unix系统命令的冰山一角,掌握这些命令将极大地提升在Unix环境下的工作效率。Unix的强大之处不仅在于其命令的多样性和深度,还在于这些命令之间的无缝协作能力,使得自动化脚本和复杂任务的执行变得简单而...

    UNIX下常用命令

    文件与目录(Files and Directories) - `ls`:列出目录内容。 - `cd`:改变当前工作目录。 - `mkdir`:创建新目录。 - `rmdir`:删除空目录。 - `rm`:删除文件或目录(需小心使用)。 - `mv`:移动或重命名文件/...

    2009 达内Unix学习笔记

    集合了 所有的 Unix命令大全 登陆服务器时输入 公帐号 openlab-open123 telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss 老师测评网址 http://172.16.0.198:8080/poll/ 各个 ...

    File and Directories

    Linux文件系统是操作系统的一个重要组成部分,它是Unix系统的一个演变,为用户提供了一个虚拟存储的抽象,主要包括文件和目录的概念。文件是存储在Linux文件系统中最基本的单位,它是一个字节序列,这些字节可以通过...

    VC6.0__directories_默认设置.doc

    4. **Source Directories**:虽然不是标准的directories设置,但源代码路径可能包含自定义的类库或者项目源码,如: - `C:\PROGRAM FILES\MICROSOFT VISUAL STUDIO\VC98\MFC\SRC` MFC的源代码。 - `C:\Program ...

Global site tag (gtag.js) - Google Analytics