Linux系统调用:link、unlink函数

函数原型

#include 
int link(const char *oldpath, const char *newpath);
int unlink(const char *pathname);

link函数

  • link - nake a new name for a file

oldpath:原始文件名

newpath:新的硬链接名

  • 函数描述

link()函数的功能是为已经存在的文件创建一个新的硬链接,功能和ln命令一样,当成功创建了一个硬链接后,inode号的数量加1。

  • 返回值

成功,返回0;失败,返回-1,并且将errno设置为以下的错误码。

<备注> errno全局变量定义在头文件。

link(2) - Linux man page

unlink函数

  • unlink - delete a name and possibly the file it refers to

pathname:被删除的文件名。

  • 函数描述

unlink()函数功能从文件系统中中删除一个名字,若这个名字是指向这个文件的最后一个链接(即inode值为1),并且没有进程处于打开这个文件的状态,则删除这个文件,释放这个文件占用的空间。

如果这个名字是指向这个文件的最后一个链接,但有某个进程处于打开这个文件的状态,则暂时不删除这个文件,要等到打开这个文件的进程关闭这个文件的文件描述符后才删除这个文件。

如果这个名字指向一个符号链接,则删除这个符号链接。

如果这个名字指向一个socket、fifo或者一个设备,则这个socket、fifo、设备的名字被删除,但是打开这些socket、fifo、设备的进程仍然可以继续使用它们,直到进程结束或者close掉对应的文件描述符。

  • 返回值

成功返回1,;否则返回-1,并将errno全局变量设置为以下的值。

unlink(2) - Linux man page

示例程序1

#include
#ifdef MINGW
#include 
#else
#include 
#endif

int main(void)
{
	FILE *fp = fopen("junk.jnk","w");
	int status;
	fprintf(fp,"junk");

	status = access("junk.jnk", F_OK);  //检查文件是否存在
	if (status == 0)
		printf("File exists\n");
	else
		printf("File doesn't exist\n");
	fclose(fp);
	unlink("junk.jnk");
	status = access("junk.jnk", F_OK);
	if (status == 0)
		printf("File exists\n");
	else
		printf("File doesn't exist\n");
	return 0;
}

编译命令:gcc unlink_test.c -o unlink_test -std=c99

运行结果:./unlink_test 
File exists
File doesn't exist

示例程序2

#include 
#include 
#include 
#include 
#include 
#include 
#ifdef MINGW
#include 
#else
#include 
#include 
#endif

int main(void)
{
	int fd; 
    char buf[256] = {0};
    if ((fd =open("tempfile.txt", O_RDWR | O_CREAT | O_TRUNC)) < 0){
        perror("open error");
		exit(-1);
	}
	fgets(buf,sizeof(buf),stdin); //从标准输入端输入字符串
	write(fd,buf,sizeof(buf));
    if (unlink("tempfile.txt") < 0){
        perror("unlink error");
		exit(-1);
	}
    printf("file unlinked\n");
    read(fd, buf, sizeof(buf)); //即使调用了unlink()函数仍可以读取文件内容,直到进程结束才删除文件
    printf("buf = %s", buf);
#ifdef MINGW
    system("Pause"); //Pause命令只在windows系统下有效
#else
	//等效于Windows下的system("Pause")
	//方式1-需要添加头文件
	// printf("Press any key to continue...\n");
	// struct termios te;
	// int ch;
	// tcgetattr( STDIN_FILENO,&te);
	// te.c_lflag &=~( ICANON|ECHO);
	// tcsetattr(STDIN_FILENO,TCSANOW,&te);
	// tcflush(STDIN_FILENO,TCIFLUSH);
	// fgetc(stdin);
	// te.c_lflag |=( ICANON|ECHO);
	// tcsetattr(STDIN_FILENO,TCSANOW,&te);
	//方式2
	setbuf(stdin, NULL); //Linux下清除标准输入缓冲区
	fgetc(stdin);  //等待输入
#endif
    printf("main() done\n");
	return 0;
}

编译命令:gcc unlink_test2.c -o unlink_test2 -std=c99

运行结果: 

./unlink_test2 
Hello, Linux!
file unlinked
buf = Hello, Linux!

main() done

程序分析:从该示例程序可以知道,即使先执行了unlink()函数也不会立马删除指定的文件,进程仍然可以访问该文件,直到进程结束后,文件才会被删除。进程结束后,可以看到当前目录下已经没有tempfile.txt了。

示例程序3

#include 
#include 
#include 
#include 
#include 
#include 
#ifdef MINGW
#include 
#else
#include 
#endif
#define DEBUG

int main()
{
    int fd;
    char buf[32] = {0};
    struct stat buff; //定义描述文件信息的结构体变量

    if((fd=open("temp.txt",O_RDWR|O_CREAT|O_TRUNC,S_IRWXU))<0){
        printf("create file error!\n");
    }
    if(stat("temp.txt",&buff)<0)
	{
		printf("#1-stat() error:%s\n",strerror(errno));
		return -1;
	}
    printf("The temp.link=%d\n",buff.st_nlink); //新建的一个文件的硬链接数目为1
    if(link("temp.txt","test.txt")<0)  //创建temp.txt的硬链接文件test.txt
	{
		printf("link() error:%s\n",strerror(errno));
		return -1;
	}
    stat("temp.txt",&buff); //通过文件名获取文件信息,并保存在buff中
    printf("after link(),the temp.link =%d\n",buff.st_nlink); //打印文件的硬链接数目
    if(unlink("test.txt")<0){  //取消一个temp.txt的硬链接文件test.txt,程序结束后test.txt文件将会被删除
        printf("unlink error !\n");
		return -1;
    }
    if(stat("temp.txt",&buff)<0)
	{
		printf("#2-stat() error:%s\n",strerror(errno));
		return -1;
	}
    printf("after unlink(),the temp.link=%d\n",buff.st_nlink);
	
	fgets(buf,sizeof(buf),stdin); //从标准输入端输入字符串
    if(write(fd,buf,sizeof(buf))<0){
        printf("write wrror!\n");
    }
	//将文件指针定位到文件头
    if((lseek(fd,0,SEEK_SET))==-1){
        printf("lseek error!\n");
    }
    if((read(fd,buf,5))<0){
        printf("read error!\n");
    }
    printf("buf=%s\n",buf); //输出文件内容
	
    return 0;
}

编译命令:gcc unlink_test3.c -o unlink_test3 -std=c99

运行结果:

./unlink_test3
The temp.link=1
after link(),the temp.link =2
after unlink(),the temp.link=1
hello,world!   //从终端输入的内容
buf=hello,world!

查看temp.txt文件内容:more temp.txt

hello,world!

程序分析:link()函数是为当前已存在的文件添加一个硬链接,当执行link()函数后,可以看到,temp.txt文件的inode值为2了,当执行unlink()函数后,inode值就减1变为1了,当程序运行结束后,temp.txt文件的硬链接文件test.txt就被删除了,而temp.txt文件仍然存在,因为此时它的inode值为1,而不是0。

 

你可能感兴趣的:(Linux_C,API函数)