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