file属性读取 Struct dirent struct stat

来源:互联网 发布:培训课程网络促销方案 编辑:程序博客网 时间:2024/05/23 07:24

文件信息获取操作

Struct __dirstream

{

void *__fd

char *__data;

int __entry_data;

char *__ptr;

int __entry_ptr;

size_t __allocation;

size_t __size;

__libc_lock_define(,__lock);

}

typedef struct __dirstream DIR;

Struct dirent

{

long d_ino; //inode number

off_t d_off; //offset to this dirent

unsigned short d_reclen; //length of this d_name

unsigned char d_type; //the type of d_name

char d_name[NAME_MAX+1]; //file name

//具体定义在/usr/include/bits/stat.h stat64和stat有点区别

struct stat {

dev_t         st_dev;      /* device */

ino_t         st_ino;      /* inode */

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 type (if inode device) */

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;    /* time of last access */

time_t        st_mtime;    /* time of last modification */

time_t        st_ctime;    /* time of last status change */

};

其中,st_mode的类型 mode_t.

mode_t其实就是普通的unsigned int.

目前,st_mode使用了其低19bit. 0170000 => 1+ 3*5 = 16.

其中,最低的9位(0-8)是权限,9-11是id,12-15是类型。

具体定义如下:

S_IFMT     0170000   bitmask for the file type bitfields

S_IFSOCK   0140000   socket

S_IFLNK    0120000   symbolic link

S_IFREG    0100000   regular file

S_IFBLK    0060000   block device

S_IFDIR    0040000   directory

S_IFCHR    0020000   character device

S_IFIFO    0010000   fifo

S_ISUID    0004000   set UID bit

S_ISGID    0002000   set GID bit (see below)

S_ISVTX    0001000   sticky bit (see below)

S_IRWXU    00700     mask for file owner permissions

S_IRUSR    00400     owner has read permission

S_IWUSR    00200     owner has write permission

S_IXUSR    00100     owner has execute permission

S_IRWXG    00070     mask for group permissions

S_IRGRP    00040     group has read permission

S_IWGRP    00020     group has write permission

S_IXGRP    00010     group has execute permission

S_IRWXO    00007     mask for permissions for others (not in group)

S_IROTH    00004     others have read permission

S_IWOTH    00002     others have write permisson

S_IXOTH    00001     others have execute permission

文件类型:

d目录文件         - 普通文件

l 链接             b 块设备文件 

p 管道文件         c 字符设备文件   

s 套接口文件

文件权限:

[0-2]所有者权限,[3-5]所有者同一组用户权,[6-8]其它用户权限

DIR* opendir (const char * path ); //get a dir handler

struct dirent* readdir(DIR* dir_handle);//read dir



代码:

/****************************************************************************
*create time :2014-11-26
*function :use for read file or dir info
*author: JHK
*version: 0.0.01
******************************************************************************/
#include<stdio.h>
#include<dirent.h>
#include<sys/stat.h>
#include<error.h>
#include<errno.h>
#include<string.h>
#include<time.h>

#include"prin.h"  //实现了prin_info函数

#define PATH "/jhk/test"

typedef struct
{
 char f_mode;
 char limits[10];
 int own;
 int group;
 long f_len;
 char l_modify[32];
 char f_name[32];
}FileInfo;

static int analysis_stmode(unsigned int mode,FileInfo *pf_info);
int main(void)
{
    DIR *dir=NULL;
 struct dirent *pdirent;
 struct stat64 StatBuf;
 char filepath[255];
 FileInfo pf_info;
 struct tm * t;
 
    dir = opendir(PATH);  //get dir handler  
    if(dir == NULL)
    {
        prin_info("opendir error:%s",strerror(errno));
  return ERR;
    }
 
 while((pdirent = readdir(dir))!=NULL)//read dir
 {
  if( strcmp( pdirent->d_name, "." ) == 0  ||
      strcmp( pdirent->d_name, ".." ) == 0 )
  {
   continue;
  }
  sprintf(filepath,"%s/%s",PATH,pdirent->d_name);
  if(stat64(filepath,&StatBuf) !=0 )
  {
   prin_info("stat failed :%s",strerror(errno));
   return ERR;
  }
  analysis_stmode(StatBuf.st_mode,&pf_info);
  //ID  /etc/group
  pf_info.own=StatBuf.st_uid;
  pf_info.group=StatBuf.st_gid;
  
  //file length
  pf_info.f_len=StatBuf.st_size;
  
  //last modify time
  t = localtime ( &StatBuf.st_mtime );
  sprintf(pf_info.l_modify,"%d-%d-%d %d:%d:%d",t->tm_year+1900,t->tm_mon,t->tm_mday,
                t->tm_hour,t->tm_min,t->tm_sec);
  //file name
  sprintf(pf_info.f_name,"%s",pdirent->d_name);
  prin_info("%c%s%3d%3d %10d %s %s",pf_info.f_mode,pf_info.limits,pf_info.own,pf_info.group,
           pf_info.f_len,pf_info.l_modify,pf_info.f_name);
  
 }

    return 0;
}

static int analysis_stmode(unsigned int mode,FileInfo *pf_info)
{
 int i=0;
 /*file  mode&0x0040000==1 id a directory*/
  if(S_ISREG(mode)) //S_IFMT     0170000   bitmask for the file type bitfields
  pf_info->f_mode = '-';
  else if(S_ISDIR(mode))  //S_IFDIR    0040000   directory
   pf_info->f_mode = 'd';
  else if(S_ISCHR(mode)) // S_IFCHR    0020000   character device
   pf_info->f_mode = 'c';
  else if(S_ISBLK(mode)) //S_IFBLK    0060000   block device  
   pf_info->f_mode = 'b';
  else if(S_ISFIFO(mode))//S_IFREG    0100000   regular file 
   pf_info->f_mode = 'p';
  else if(S_ISLNK(mode)) //S_IFLNK    0120000   symbolic link  
   pf_info->f_mode = 'l';
  else if(S_ISSOCK(mode))//S_IFSOCK   0140000   socket 
   pf_info->f_mode = 's';
  else  
  prin_info("f_mode error!");

  /*file limits[0-2]own,[3-5]own's group,[6-8]others*/
  strcpy(pf_info->limits,"rwxrwxrwx");
  for(i=8;i>=0;i--)
  {
  if((mode&(1<<i))==0)
   pf_info->limits[8-i]='-';
  }
    return OK;
}

0 0
原创粉丝点击