Linux系统调用八、link系列API函数详解


在这里插入图片描述


🚀1. link函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int link(const char *oldpath, const char *newpath);
  • 函数功能

    link() creates a new link (also known as a hard link) to an existing file. 为现有的文件建立一个硬链接。

  • 函数参数

    • oldpath:源文件名(路径)
    • newpath:硬链接文件名(路径)
  • 函数返回值

    • 成功返回0。On success, zero is returned.
    • 失败返回-1并设置errno。On error, -1 is returned, and errno is set appropriately.

示例:传入现有的文件名并为该文件建立硬链接

/************************************************************
  >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不仅可以是文件名,也可以是带有路径的文件名。
在这里插入图片描述

🚀2. symlink函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int symlink(const char *oldpath, const char *newpath);
  • 函数功能

    symlink() creates a symbolic link named newpath which contains the string oldpath. 创建一个符号链接,也就是软链接。

  • 函数参数

    • oldpath:源文件名(路径)
    • newpath:符号链接文件名(路径)
  • 函数返回值

    • 成功返回0。On success, zero is returned.
    • 失败返回-1,并设置errno。On error, -1 is returned, and errno is set appropriately.

示例:传入现有的文件名并为该文件建立符号链接

/************************************************************
  >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;
}

在这里插入图片描述

🚀3. readlink函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
ssize_t readlink(const char *path, char *buf, size_t bufsiz);
  • 函数功能

    readlink() places the contents of the symbolic link path in the buffer buf, which has size bufsiz. readlink() does not append a null byte to buf. It will truncate the contents (to a length of bufsiz characters), in case the buffer is too small to hold all of the contents. 读取软链接,这个函数只能读取软链接,不能读取硬链接。

  • 函数参数

    • path:连接名(路径)
    • buf:缓冲区(缓存读出的数据)
    • bufsiz:缓冲区大小
  • 函数返回值

    • 成功返回缓冲区被填充的大小。On success, readlink() returns the number of bytes placed in buf.
    • 失败返回-1且设置errno。On error, -1 is returned and errno is set to indicate the error.
/************************************************************
  >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;
}

在这里插入图片描述

🚀4. unlink函数

  • 包含头文件
#include <unistd.h>
  • 函数原型
int unlink(const char *pathname);
  • 函数功能

    unlink() deletes a name from the file system. 删除软链接、硬链接、文件。(注意同名命令unlink,查询函数man手册时要加章节2)

  • 函数参数

    • pathname:链接名,也可以是文件名
  • 函数返回值

    • 成功返回0。On success, zero is returned.
    • 失败返回-1且设置errno。On error, -1 is returned, and errno is set appropriately.

示例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函数删除硬链接计数时,如果有进程在引用这个文件,那么将暂时先不删除文件,等进程退出后,再删除文件。实际上这个文件已经写入成功了,只不过在后面又被删除了,这有点像我们在网上听音乐或看视频时的缓存,也就是一个比实际进度更快的一个进度条,它会先把要看的内容缓存在一个临时文件(以便于看的时候更流畅),在看完后自动删除。


在这里插入图片描述
在这里插入图片描述


posted @ 2022-05-25 16:01  Mindtechnist  阅读(121)  评论(0编辑  收藏  举报  来源