1.新建文件,设置文件权限屏蔽字为0;
2.建立该文件的硬链接文件,打印硬链接文件的inode节点号和文件大小;
3.建立该文件的软链接文件,打印软链接文件的inode节点号和文件大小;打印软链接文件中的内容;
4.打印源文件的inode节点号,文件大小和链接数目;
5.调用unlink对源文件进行操作,打印源文件链接数目;
源代码:
#include <fcntl.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
{
int fd;
if((fd=open("process",O_RDWR|O_CREAT))<0)
{
perror("open");
exit(-1);
}
fchmod(fd,S_IRWXU|S_IRWXG|S_IRWXO);
struct stat statbuf,statbuf1,statbuf2,statbuf3;
fstat(fd,&statbuf);
umask(0);
close(fd);
if(link("process","process1")<0)
{
perror("link");
exit(-1);
}
stat("process1",&statbuf1);
printf("link:inodenum:%ld\tsize is :%ld\n",statbuf1.st_ino,statbuf1.st_size);
stat("process",&statbuf3);
if(symlink("process","process2")<0)
{
perror("symlink");
exit(-1);
}
lstat("process2",&statbuf2);
char str[1024];
if(readlink("process2",str,1024)<0)
{
perror("read");
exit(-1);
}
printf("symlink_inode:inodenum:%ld\tsize is :%ld\n",statbuf2.st_ino,statbuf2.st_size);
printf("read num is :%s\n",str);
printf("the father file inode is %ld,size is %ld,linknum is %ld\n",statbuf3.st_ino,statbuf3.st_size,statbuf3.st_nlink);
if(unlink("process")<0)
{
perror("error");
exit(-1);
}
printf("after unlink , tempfile link num is :%ld",statbuf3.st_nlink);
return 0;
}
umask()系统调用
功能
为进程设置文件存取权限屏蔽字,并返回以前的值
头文件
#include <sys/types.h>
#include <sys/stat.h>
函数原型
mode_t umask(mode_t mask);
chmod()/fchmod()系统调用
功能
更改文件权限
头文件
#include <sys/types.h>
#include <sys/stat.h>
函数原型
int chmod(const char *path, mode_t mode);
int fchmod(int fildes, mode_t mode);
返回值
成功时返回0
失败返回-1
link()/unlink()系统调用
功能
创建/删除一个硬链接
头文件
#include <unistd.h>
函数原型
int link(const char *oldpath, const char *newpath);
int unlink(const char *pathname);
删除一个文件的目录项并减少它的链接数
返回值
成功时返回0
失败返回-1
symlink()/readlink()系统调用
头文件
#include <unistd.h>
函数原型
int symlink(const char *oldpath, const char *newpath);
功能:用来创建符号链接文件。
返回值
成功时返回0
否则-1
int readlink(const char *path, char *buf, size_t bufsiz);
功能:readlink 函数用来读取链接文件本身的内容(也就是符号链接指向的文件的文件名)。
返回值
成功时返回放置在buffer中的字符数
否则-1