apue第四章(一)

我的目标是:“让家人生活得更好”

1、文件的属性是存放在一个结构中的。

   这个结构式 struct stat 在文件 bits/stat.h

下面黑色部分是主要的结构:

struct stat

  {

    __dev_t st_dev;        /* Device.  */

#if __WORDSIZE == 32

    unsigned short int __pad1;

#endif

#if __WORDSIZE == 64 || !defined __USE_FILE_OFFSET64

    __ino_t st_ino;          /* File serial number.  */

#else

    __ino_t __st_ino;                    /* 32bit file serial number. */

#endif

#if __WORDSIZE == 32

    __mode_t st_mode;                /* File mode.  */

    __nlink_t st_nlink;                 /* Link count.  */

#else

    __nlink_t st_nlink;           /* Link count.  */

    __mode_t st_mode;         /* File mode.  */

#endif

    __uid_t st_uid;         /* User ID of the file's owner. */

    __gid_t st_gid;          /* Group ID of the file's group.*/

#if __WORDSIZE == 64

    int __pad0;

#endif

    __dev_t st_rdev;             /* Device number, if device.  */

#if __WORDSIZE == 32

    unsigned short int __pad2;

#endif

#if __WORDSIZE == 64 || !defined __USE_FILE_OFFSET64

    __off_t st_size;                /* Size of file, in bytes.  */

#else

    __off64_t st_size;                   /* Size of file, in bytes.  */

#endif

    __blksize_t st_blksize;     /* Optimal block size for I/O.  */

#if __WORDSIZE == 64 || !defined __USE_FILE_OFFSET64

    __blkcnt_t st_blocks;             /* Number 512-byte blocks allocated. */

#else

    __blkcnt64_t st_blocks;          /* Number 512-byte blocks allocated. */

#endif

#ifdef __USE_MISC

    /* Nanosecond resolution timestamps are stored in a format

       equivalent to 'struct timespec'.  This is the type used

       whenever possible but the Unix namespace rules do not allow the

       identifier 'timespec' to appear in the <sys/stat.h> header.

       Therefore we have to handle the use of this header in strictly

       standard-compliant sources special.  */

    struct timespec st_atim;          /* Time of last access.  */

    struct timespec st_mtim;         /* Time of last modification.  */

    struct timespec st_ctim;          /* Time of last status change.  */

 …………………………….

…………………………….

  };

 

 

 

 

2、可以使用三个系统调用来取得这个结构

 int stat(constr char * pathname, struct stat * stat)

第一个参数是文件的文件名

 int fstat(int fid, struct stat * stat)

第一个参数是打开的文件描述符

int lstat(constr char * pathname, struct stat * stat)

第一个参数是符号链接的文件名 返回的 是符号链接指向的文件的文件状态,而不是这个符号链接文件本身

成功返回0 否则返回-1

 

3unix 的文件类型

1>普通文件,包含了某种类型的数据,可以是二进制的也可以是文本,具体类容由相应的程序解释

2>目录文件,目录文件时包含了其他文件的文件名和链接的文件。

3>字符设备文件,对文件的读写以字符为单位的,没哟缓冲区

4>块设备文件,对文件的读写以某个大小的块为单位,有缓冲区,写入与读出都是从缓冲区的读写,例如磁盘 文件爱你

5>Fifo文件 也就是命名管道文件

6>符号连结:指向另一个文件的文件

 

4、识别unix的文件类别:

 

   可以使用stat结构中的st_mode来识别文件类型。

现在一般的unix系列的系统都有一些宏来识别文件:

stat.h中有 S_ISXXX来识别,S就是stat的意识,is就是判断的意思

S_ISDIR()

S_ISREG()

S_ISCHR()

S_ISBLK()

S_ISLNK()

在老的系统中没有提供这些宏

是采用 S_ISREG(stat.st_mode) == ((stat.st_mode) & S_IFMT == S_IFDIR)

S_IFMT 是一个屏蔽字,通过与其与操作,然后再对比。

例如

 

#include<sys/stat.h>

#include<stdio.h>

 

int main(int argc,char * argv[])

{

     struct stat stat;

     int status = -1;

     if((status = lstat(argv[1],&stat)) < 0)

            return 0;

 

     if(S_ISDIR(stat.st_mode)) printf("dir/n");

     if(S_ISREG(stat.st_mode)) printf("reg/n");

     if(S_ISBLK(stat.st_mode)) printf("blk/n");

     if(S_ISCHR(stat.st_mode)) printf("char/n");

     if(S_ISFIFO(stat.st_mode)) printf("fifo/n");

}

 

 

4unix进程的权限:设置用户id 设置组id

实际用户(组)id: Unix的进程有实际用户id和实际组id,它们用于表示这个进程对应程序的用户所有者和组所有者。

 

有效用户()id:除了这个,进程还有有效用户id和有效组id。它们用于表示进程的访问权限。

比如一个程序的拥有者是white,表示它的实际用户idwhite,而它以root权限打开,表示这个程序的有效用户idroot

 

设置用户(组)id:在文件方式字中有一个bit为可疑用来表示设置用户id,这个位的作用是:当这个位置位后,运行这个程序的用户可以得到这个程序所有者的权限。

例如:我是whitepasswd的所有者是root,按理说我不能运行这个程序,但是它的设置用户id置位了,所以white运行passwd程序拥有了passwd所有者root的权限。

 

5Unix的文件操作的权限

1、目录的执行权限:也就是目录的通过权限,例如/home/junjun/words.doc

如果要对文件words.doc进行操作,需要对 /home/home/junjun有可执行权限。

2、删除某一个文件,必须有对这个文件所在的目录有可执行和可写权限,

  对文件本身不需要有读写权限,(因为文件夹也是一个特殊的文件,他有到其所包含文件的连接,删除的是这个连接,而对这个连接指向的文件没有关系。)

 

Access函数

Int access(char * pathname, int mode)

记住Access函数测试的是文件的实际用户()id,而不是有效用户id和组id,这个会出现这种有趣的情况:

使用access函数测试某一个文件不能读的。但是比如这个文件的所有者是root,但是却能通过open函数打开它。因为我们可以通过一个设置用户id的程序打开这个文件。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值