前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Linux系统调用API】五、link系列函数

【Linux系统调用API】五、link系列函数

作者头像
mindtechnist
发布2024-08-08 17:08:10
540
发布2024-08-08 17:08:10
举报
文章被收录于专栏:机器和智能

1. link函数

  • 包含头文件
代码语言:javascript
复制
#include <unistd.h>
  • 函数原型
代码语言:javascript
复制
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.

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

代码语言:javascript
复制
/************************************************************
  >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函数

  • 包含头文件
代码语言:javascript
复制
#include <unistd.h>
  • 函数原型
代码语言:javascript
复制
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.

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

代码语言:javascript
复制
/************************************************************
  >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函数

  • 包含头文件
代码语言:javascript
复制
#include <unistd.h>
  • 函数原型
代码语言:javascript
复制
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.
代码语言:javascript
复制
/************************************************************
  >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函数

  • 包含头文件
代码语言:javascript
复制
#include <unistd.h>
  • 函数原型
代码语言:javascript
复制
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()删除文件、软链接、硬链接

代码语言:javascript
复制
/************************************************************
  >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()删除的文件

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

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2024-01-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 机器和智能 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1. link函数
  • 2. symlink函数
  • 3. readlink函数
  • 4. unlink函数
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档