Chinaunix首页 | 论坛 | 博客
  • 博客访问: 906500
  • 博文数量: 380
  • 博客积分: 3495
  • 博客等级: 中校
  • 技术积分: 3996
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-02 09:35
文章分类

全部博文(380)

文章存档

2015年(2)

2014年(5)

2013年(9)

2012年(9)

2011年(67)

2010年(103)

2009年(182)

2008年(3)

我的朋友

分类: LINUX

2009-04-19 11:45:04

Linux的metadata是可以由一个函数得到的,这个函数就是stat.
现阶段Linux有三个函数可用.
#include
#include
#include

int stat(const char *path, struct stat *buf);  //应用文件路径
int fstat(int fd, struct stat *buf);           //应用文件FD
int lstat(const char *path, struct stat *buf); //得到是链接文件的,如果是链接文件的话.

设定文件的权限
#include
#include
int chmod (const char *path, mode_t mode);     //正常使用文件PATH
int fchmod(int fd, mode_t mode);               //使用文件FD
int lchmod(const char *path, mode_t mode);      //链接文件对应的,

设定文件属主
#include
#include
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, gid_t group);

当然了,我们还可以设定文件的扩展属性.
得文件扩展属性
#include
#include
ssize_t getxattr(const char *path, const char *key,
                 void *value, size_t size);
ssize_t lgetxattr(const char *path, const char *key,
                 void *value, size_t size);
ssize_t fgetxattr(const char *path, const char *key,
                 void *value, size_t size);
设定文件扩展属性
#include
#include
int setxattr(const char *path, const char *key,
             const void *value, size_t size, int flags);
int lsetxattr(const char *path, const char *key,
             const void *value, size_t size, int flags);
int fsetxattr(int fd, const char *key,
             const void *value, size_t size, int flags);
得到一个文件的所有扩展属性
#include
#include
ssize_t listxattr(const char *path, char *list, size_t size);
ssize_t llistxattr(const char *path, char *list, size_t size);
ssize_t flistxattr(int fd, char *list, size_t size);

删除一个文件的扩展属性
int removexattr(const char *path, const char *key);
int lremovexattr(const char *path, const char *key);
int fremovexattr(int fd, const char *key);

目录的操作函数如下:

得到当前的工作目录
#include
char *getcwd(char *buf, size_t size);
char *get_current_dir_name(void);

改变当前的工作目录
int chdir(const char *path);
int fchdir(int fd);

创建目录
int mkdir(const char *path, mode_t mode);

删除目录
int rmdir(const char *path);

读取一个目录的内容

#include
#include

DIR *opendir(const char *name);      //打开目录
struct dirent *readdir(DIR *dir);    //读取目录的一个内容,再调得下一个内容
int closedir(DIR *dir);

创建一个链接
#include
int link(const char *oldpath, const char *newpath); //hard link
int symlink(const char *oldpath, const char *newpath); //symbolic link
int unlink(const char *pathname);//删除一个link

移动一个文件
#include
int rename(const char *path, const char *newpath);

特殊设备文件操作
int ioctl(int fd, int request, ...);

文件事件监听函数
#include
int notify_init(void);   //初始化
int inotify_add_watch(int fd, const char *path, uint32_t mask);
int inotify_rm_watch(int fd, uint32_t wd);

例子如下
#include
#include
#include

int main(void )
{

   int fd;
   fd = inotify_init();
   if(fd == -1)
   {
       perror("inotify_init");
       exit(EXIT_FAILURE);
   }

   if(inotify_add_watch(fd, "./tmp.c", IN_ACCESS | IN_MODIFY) == -1)
   {
       perror("inotify_add_watch");
       exit(EXIT_FAILURE);
   }

   while(1)
   {
       sleep(10);
       #define BUF_LEN 256*256
       char buf[BUF_LEN];
       ssize_t len, i = 0;
       len = read(fd, buf, BUF_LEN);
       while( i < len) {
           struct inotify_event *event = (struct inotify_event *)&buf[i];
           printf("wd=%d mask=%d cookie=%d len=%d dir=%s\n",
                   event->wd, event->mask,
                   event->cookie, event->len,
                   (event->mask &IN_ISDIR)?"yes":"no");
            if(event->len)
              printf("name=%s\n", event->name);
            i+=sizeof(struct inotify_event)+event->len;
       }
   }
   close(fd);
 
return 0;
}



阅读(1551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~