#include <unistd.h>
int link(const char *oldpath, const char *newpath);
示例:传入现有的文件名并为该文件建立硬链接
/************************************************************
>File Name : link_test.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月16日 星期一 15时03分15秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not found filename\n");
return -1;
}
link(argv[1], "./linkdir/link_test.c.hard");
return 0;
}
在执行程序时,参数oldpath和newpath不仅可以是文件名,也可以是带有路径的文件名。
#include <unistd.h>
int symlink(const char *oldpath, const char *newpath);
示例:传入现有的文件名并为该文件建立符号链接
/************************************************************
>File Name : symlink_test.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月16日 星期一 15时25分50秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not found filename\n");
return -1;
}
symlink(argv[1], "symlink_test.c.soft");
return 0;
}
#include <unistd.h>
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
/************************************************************
>File Name : readlink_test.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月16日 星期一 16时01分26秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not found filename\n");
return -1;
}
char buf[10] = {0};
ssize_t ret = readlink(argv[1], buf, sizeof(buf));
printf("buf = %s, buflen = %ld, read size = %ld\n", buf, sizeof(buf), ret);
return 0;
}
#include <unistd.h>
int unlink(const char *pathname);
示例1:unlink()删除文件、软链接、硬链接
/************************************************************
>File Name : unlink_test.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月16日 星期一 16时57分59秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
if(argc < 2)
{
printf("not found filename\n");
return -1;
}
unlink(argv[1]);
return 0;
}
示例2:write()写入unlink()删除的文件
/************************************************************
>File Name : unlink_test2.c
>Author : QQ
>Company : QQ
>Create Time: 2022年05月16日 星期一 17时13分37秒
************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
int fd = open("hello.txt", O_WRONLY | O_CREAT, 0664);
unlink("hello.txt");
int ret = write(fd, "hello", sizeof("hello"));
if(ret < 0)
{
perror("write err");
}
else if(ret == 0)
{
printf("not write\n");
}
else
{
printf("write %d byte\n", ret);
}
return 0;
}
在这个实验中看到,虽然并没有hello.txt这个文件,但是write()函数返回了成功,也就是写入成功了,那么内容写到哪去了呢。实际上,unlink函数删除硬链接计数时,如果有进程在引用这个文件,那么将暂时先不删除文件,等进程退出后,再删除文件。实际上这个文件已经写入成功了,只不过在后面又被删除了,这有点像我们在网上听音乐或看视频时的缓存,也就是一个比实际进度更快的一个进度条,它会先把要看的内容缓存在一个临时文件(以便于看的时候更流畅),在看完后自动删除。