文件属性获取及open函数

43 篇文章 1 订阅

int open(const char*pathname,int flags);

int open(const char*pathname,int flags,mode_t mode);

参数说明:

1.pathname

要打开或创建的目标文件

2.flags

打开文件时,可以传入多个参数选项,用下面的

一个或者多个常量进行“或”运算,构成falgs

参数:

O_RDONLY: 只读打开

O_WRONLY: 只写打开

O_RDWR: 读,写打开

这三个常量,必须制定一个且只能指定一个

O_CREAT: 若文件不存在,则创建它,需要使

用mode选项。来指明新文件的访问权限

O_APPEND: 追加写,如果文件已经有内容,这次打开文件所

写的数据附加到文件的末尾而不覆盖原来的内容

3.mode

一个文件权限如果为“-rwxr-x---”,那么其文件模式为0750,计算过程为:0400 0200 0100 0040 0010=0750。例如常见的/tmp目录的权限就为1777,表示任何用户都可以在该目录下创建文件,但是用户不能删除其它用户的文件。0666(默认的文件建立模式).

struct stat结构体:获取指定路径的文件或者文件夹的信息

_stat结构体是文件(夹)信息的结构体,定义如下:以上信息就是可以通过_stat函数获取的所有相关信息,一般情况下,我们关心文件大小和创建时间、访问时间、修改时间。

1、通过路径获取:

int stat(const char *path, struct stat *struct_stat);

int lstat(const char *path,struct stat *struct_stat);

这两个方法区别在于stat没有处理字符链接(软链接)的能力,如果一个文件是符号链接,stat会直接返回它所指向的文件的属性;而lstat返回的就是这个符号链接的内容。这里需要说明一下的是软链接和硬链接的含义。我们知道目录在linux中也是一个文件,文件的内容就是这这个目录下面所有文件与inode的对应关系。那么所谓的硬链接就是在某一个目录下面将一个文件名与一个inode关联起来,其实就是添加一条记录!而软链接也叫符号链接更加简单了,这个文件的内容就是一个字符串,这个字符串就是它所链接的文件的绝对或者相对地址。

2、通过文件描述符:

int fstat(int fdp, struct stat *struct_stat);

struct stat

{

dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/

ino_t st_ino; /* inode number -inode节点号*/

mode_t st_mode; /* protection -保护模式?*/

nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/

uid_t st_uid; /* user ID of owner -user id*/

gid_t st_gid; /* group ID of owner - group id*/

dev_t st_rdev; /* device ID (if special file) -设备号,针对设备文件*/

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 - *文件状态改变时间/

};
int index = 0;
    char filePath[256] = {0,};
    if(pFileLog->index == 0)
    {
        for(int i=0; i<BSLOG_REDIRECT_FILE_CNT; i++)
        {
            //拼接文件路径
            snprintf(filePath, sizeof(filePath), "%s%s.%d", pFileLog->fileName, BSLOG_EXTN_REDIRECT, i + 1);
            struct stat statbuf;//文件属性的结构体
            if(!stat(filePath, &statbuf))//通过文件路径获取文件对应的属性。
            {
                if(statbuf.st_size < pFileLog->logSizeBytes)
                {
                    pFileLog->index = i + 1;
                    break;
                }
            }
        }

        if(pFileLog->index == 0)
        {
            snprintf(filePath, sizeof(filePath), "%s%s.%d", pFileLog->fileName, BSLOG_EXTN_REDIRECT, 1);
            unlink(filePath); //删除文件
            pFileLog->index = 1;
        }

        index = pFileLog->index;
    }
    else
    {
        index = (pFileLog->index % BSLOG_REDIRECT_FILE_CNT) + 1;

        snprintf(filePath, sizeof(filePath), "%s%s.%d", pFileLog->fileName, BSLOG_EXTN_REDIRECT, index);
        unlink(filePath);

        pFileLog->index ++;
    }

    snprintf(filePath, sizeof(filePath), "%s%s.%d", pFileLog->fileName, BSLOG_EXTN_REDIRECT, index);
    //打开文件
    pFileLog->wrtFd = open(filePath, O_WRONLY | O_CREAT | O_APPEND, 0666);
    fprintf(stderr, "[%s] [M_LOG] filePath: %s, fd: %d\n",__FUNCTION__,filePath,pFileLog->wrtFd);

    struct stat statbuf;
    if(!stat(filePath, &statbuf))
    {
        pFileLog->fileLength = statbuf.st_size;
    }

    std::string linkpath = std::string(pFileLog->fileName) + ".REDIRECT";
    unlink(linkpath.c_str());                  // delete old one if it exists
    if (symlink(filePath, linkpath.c_str()) != 0)
    {
        // silently ignore failures
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值