设置文件的属性-chmod/chown/truncate/utime/umask

设置文件属性

1.chmod/fchmod

用于修改文件的存取权限

2.chown/fchown/lchown

用于修改文件的用户id和组id

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)

chown会将参数path指定的文件所有者id变更为参数owner代表的用户id,而将该文件所有者的组id变更为参数groupid

fchownchown类似,以文件描述符为参数。

lchown:在某个文件是符号链接的情况下,lchown更改符号链接本身的所有者id,而不是该符号链接所指向的文件。

 

文件的所有者只能改变文件的组id为其所属组中的一个,超级用户才能修改文件的所有者id,并且超级用户可以任意修改文件的用户组id

如果参数ownergroup指定为-1,那么文件的用户id和组id不会被改变。

返回值:

成功:0

错误:-1

 

3.truncate/ftruncate

用于改变文件的大小

int truncate(const char *path, off_t length)

int ftruncate(int fd, off_t length)

truncate将参数path指向的文件大小改为length指定的大小。如果原来的文件大小比参数length大,则超出的部分会被删除;如果原来的文件比参数length小,则文件将被扩展,与lseek系统调用类似,文件扩展的部分将以/0填充。如果文件大小被改变了,则文件的st_mtime域和st_ctime域将会更新。

返回值:

成功:0

错误:-1

 

4.utime

用于改变任何文件的st_mtime域和st_ctime域,即存取时间和修改时间

int utime(const char *filename, structutimbuf *buf)

int utimes(char *filename, struct timeval*tvp)

struct utimbuf{

       time_tactime; //access time

       time_tmodtime;   //modification time

}

utime系统调用会把由第一个参数filename指定的文件的存取时间改为第二个参数bufactime域,把修改时间改为第二个参数bufmodtime域,如果buf是一个空指针,则将存取时间和修改时间都改为当前时间。

返回值:

成功:0

错误:-1

 

5.umask

用于设置文件创建时使用的屏蔽字,并返回以前的值

mode_t umask(mode_t mask)

调用open函数创建一个新文件,新文件的实际存取权限是modeumask按照(mode & ~umask)运算以后的结果。

代码:创建一个文件,umask设置屏蔽其他用户的所有权限,utime设置文件的存取时间为2010年,truncate设置文件大小为1Mchown修改文件的用户id和组idroot用户组

#include <sys/types.h>

#include <sys/stat.h>

#include <fcntl.h>

#include <utime.h>

#include <stdio.h>

#include <unistd.h>

#include <errno.h>

 

struct tm  {

    int tm_sec;

    int tm_min;

    int tm_hour;

    int tm_mday;

    int tm_mon;

    int tm_year;

    int tm_wday;

    int tm_yday;

    int tm_isdst;

 };

int main(int argc, char **argv)

{

       if(argc != 2)

       {

              printf("Usage:\n");

              printf("./XXX<filename>\n");

              return0;

       }

       umask(S_IRWXO);

       intfd;

       fd= open(argv[1], O_RDWR | O_CREAT);

       if(fd == -1)

       {

              printf("Error%d \n", __LINE__);

              return0;

       }

       structtm tmActime;

       tmActime.tm_year= 2015 - 1900;

       tmActime.tm_mon= 1;

       tmActime.tm_mday= 1;

       tmActime.tm_hour= 12;

       tmActime.tm_min= 0;

       tmActime.tm_sec= 0;

       time_tactime = mktime(&tmActime);

       structutimbuf timeBuf;

       timeBuf.actime= actime;

       timeBuf.modtime= actime;

       if(utime(argv[1],&timeBuf) != 0)

       {

              printf("Error%d \n", __LINE__);

              return0;

       }

       if(ftruncate(fd, 1024*1024) != 0)

       {

              printf("Error%d \n", __LINE__);

              return0;

       }

       if(chown(argv[1], (uid_t)0, (gid_t)0) != 0)

       {

              printf("Error%d \n", __LINE__);

              printf("Errno = %d\n", errno);

              return0;

       }

       printf("end\n");

       close(fd);

       return0;

}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值