Chinaunix首页 | 论坛 | 博客
  • 博客访问: 583805
  • 博文数量: 92
  • 博客积分: 5026
  • 博客等级: 大校
  • 技术积分: 1321
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-28 11:04
文章分类

全部博文(92)

文章存档

2011年(9)

2010年(17)

2009年(12)

2008年(54)

我的朋友

分类: LINUX

2008-03-04 10:32:29

Linux文件和目录
文件的组成:数据块 和 inode(文件类型,属主关系,访问权限,时间戳)
inode: 存放再 struct stat 中
函数;  int stat(const char* pathname, struct stat* buf)  //获取inode通过文件名称      
    int fstat(int fd, struct stat* buf)               //获取inode通过文件描述字
    int lstat(const char* pathname, struct stat* buf) //类似stat, 当文件类型为软连接时,返回软连接本身的inode,所以要监测各种不同的文件类型,这个比较可靠
    struct stat:
    mode_t st_mode 文件类型和访问权限
    ino_t st_ino 文件inode号
    nlink_t st_nlink 文件硬连接的数目
    uid_t st_uid             gid_t st_gid     
    off_t st_size //文件大小
    。。。
测试文件类型:
    宏:S_ISREG(mode_t) S_ISDIR(mode_t) S_ISLNK(mode_t) S_ISSOCK(mode_t) S_ISCHR(mode_t) S_ISBLK(mode_t) S_ISFIFO(mode_t)
读取文件大小,将文件放入内存
     int fd = open(filename, O_RDONLY, 0);
     struct stat* statbuf;
     stat(filename, statbuf);
     char* buf  = new char[statbuf.st_size+1];
     read(fd, buf, statbuf.st_size);
     buf[statbuf.st_size] = 0;
     close(fd);
     ...
目录文件:
   表现为一张目录登记表(inode号 和 文件名)
   123   ..
   232   dir
   465   fileb               //都是文件的硬链接
   465  fileb_2          //都是文件的硬连接
改变文件用户id和组id
    chown(const char* pathname, uid_t uid, gid_t gid)  fchown lchown
改变文件访问权限:
   chmod(const char* pathname, mode_t mode) fchmod
access函数:
   access(const char* pathname, R_OK | W_OK |X_OK | F_OK)测试当前的实际用户,对该文件的访问权限,成功0 否则-1
文件大小:
   对普通文件和符号链接文件才有意义(链接的大小是所指向的文件名的长度)
   st_blocks(文件占用磁盘块数) * st_blksize(磁盘块大小) = size文件实际的存储空间大小
   当 size 远远小于 st_size的时候,说明该文件是一个稀疏文件(文件有许多null组成,但是磁盘不存储他们)例如,fseek到文件尾后一段距离,就产生了一段空洞,该文件就是稀疏文件!但是如果拷贝该文件,null字符也将存储在磁盘中(系统无法知道一个文件是否是稀疏文件)
获取工作目录:
      char* getwd(char* buf);  char* getcwd(char* buf, int size); //获取当前的工作目录
      int chdir(const char* pathname)  int fchdir(int fd) //设置当前工作目录
创建目录: int mkdir(const char* pathname, mode_t mode);
读取目录流:
      struct DIR          //目录登记表
      struct dirent       //目录登记项
      example:
      DIR* dir = opendir(dirname);
      dirent* item;
      while((item = readdir(dir)) != 0) {
       item->d_ino      //文件inode号       
       item->d_name     //文件名称
      }   
阅读(1138) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~