用unlink函数来清除临时文件(转)
来源:互联网 发布:js点击图片显示大图 编辑:程序博客网 时间:2024/06/05 12:41
/******************************************************************
*本文首发于bbs.bluegem.org的CWorld区
*本人email:chenfei@sohu.com
*如转载本文,请保留首发地和本人联络方式,以方便交流,谢谢!
******************************************************************/
有时程序需要
存储
很大量的数据,或者在几个进程间
交换
数据,这时您可能考虑到使用临时文件。使用临时文件要考虑几个问题:
1、保证临时文件间的文件名不互助冲突。
2、保证临时文件中内容不被其他用户或者黑客偷看、删除和修改。
所以在
linux
下有专门处理临时文件的函数
mkstemp函数
mkstemp函数将在系统中以独一无二的文件名创建一个文件并打开,而且只有当前用户才有访问这个临时文件的权限,当前用户对这个临时文件可以打开并进行读、写操作。mkstemp函数只有一个参数,这个参数是个以“XXXXXX”结尾的非空字符串。mkstemp函数会用随机产生的字符串替换“XXXXXX”,保证了文件名的唯一性。函数返回一个文件描述符,如果执行失败返回-1。在glibc 2.0.6 以及更早的glibc库中这个文件的访问权限是0666 ,glibc 2.0.7 以后的库这个文件的访问权限是0600。
当临时文件完成她的使命如果不把它清除干净把或者程序由于意外在临时文件被清除前就已经退出,临时文件所在的目录会塞满垃圾。由于mkstemp函数创建的临时文件不能自动删除(请参考下文中的tmpfile函数)。执行完mkstemp函数后要调用unlink函数,unlink函数删除文件的目录入口,所以临时文件还可以通过文件描述符进行访问,直到最后一个打开的进程关闭文件操作符,或者程序退出后临时文件被自动彻底地删除。
例程:
直接使用advanced
linux
programming的例程,只把注释翻译一下
#include
#include
/* A handle for a temporary file created with write_temp_file. In
this implementation, it’s just a file descriptor. */
/*write_temp_file是个操作临时文件的句柄,本例中只是个文件描述符*/
typedef int temp_file_handle;
/* Writes LENGTH bytes from BUFFER into a temporary file. The
temporary file is immediately unlinked. Returns a handle to the
temporary file. */
/*在这函数从BUFFER中向临时文件写入LENGTH字节数据。临时文件在刚一创建就被删除掉。函数会返回临时文件的句柄。*/
temp_file_handle write_temp_file (char* buffer, size_t length)
{
/* Create the filename and file. The XXXXXX will be replaced with
characters that make the filename unique. */
/*新建文件名和文件,文件名中的XXXXXX将被随机字符串代替,以保证文件名在系统中的唯一性*/
char temp_filename[] = “/tmp/temp_file.XXXXXX”;
int fd = mkstemp (temp_filename);
/* Unlink the file immediately, so that it will be removed when the
file descriptor is closed. */
/*文件立刻被unlink,这样只要文件描述符一关闭文件就会被自动删除*/
unlink (temp_filename);
/* Write the number of bytes to the file first. */
/*首先写入即将写入数据的长度*/
write (fd, &length, sizeof (length));
/* Now write the data itself. */
/*写入数据本身*/
write (fd, buffer, length);
/* Use the file descriptor as the handle for the temporary file. */
/*函数返回文件描述符,作为临时文件的句柄*/
return fd;
}
/* Reads the contents of a temporary file TEMP_FILE created with
write_temp_file. The return value is a newly allocated buffer of
those contents, which the caller must deallocate with free.
*LENGTH is set to the size of the contents, in bytes. The
temporary file is removed. */
/*从被write_temp_file创建的临时文件中读取数据。返回值是含有文件内容的新申请到的内存块,这块内存应该又调用read_temp_file者释放。
*length是临时文件正文内容的长度。执行完read_temp_file函数后临时文件被彻底删除*/
char* read_temp_file (temp_file_handle temp_file, size_t* length)
{
char* buffer;
/* The TEMP_FILE handle is a file descriptor to the temporary file. */
/*fd是访问临时文件的文件描述符*/
int fd = temp_file;
/* Rewind to the beginning of the file. */
/*把文件指针指向文件开头*/
lseek (fd, 0, SEEK_SET);
/* Read the size of the data in the temporary file. */
/*获得临时文件正文长度*/
read (fd, length, sizeof (*length));
/* Allocate a buffer and read the data. */
/*分配内存块,读取数据*/
buffer = (char*) malloc (*length);
read (fd, buffer, *length);
/* Close the file descriptor, which will cause the temporary file to
go away. */
/*关闭文件描述符,临时文件被彻底删除*/
close (fd);
return buffer;
}
tmpfile函数
如果您使用C library I/O函数,并且并没有另一个程序使用这个临时文件(笔者注:按我的理解是在同一进程或具有父子关系的进程组中),有个更简洁的函数——tmpfile。tmpfile函数创建并打开一个临时文件,并且自动执行了unlink了这个临时文件。tmpfile函数返回一个文件描述符,如果执行失败返回NULL。当程序执行了fclose或者退出时,资源被释放。
linux系统中还提供mktemp、 tmpnam、 和tempnam等函数,但是由于健壮性和
安全
方面理由不建议使用他们。
unlink函数的另一个用途就是用来创建临时文件,如果在程序中使用open创建了一个文件后,然后立即使用 unlink 函数删除文件,由于此时进程正在打开该文件,所以系统并不会释放该文件的 inode 节点,而只是删除其目录项。当进程退出时,该inode节点将会立即被释放。
临时文件可以用在进程间通信中的 有名管道 通信中。
*本文首发于bbs.bluegem.org的CWorld区
*本人email:chenfei@sohu.com
*如转载本文,请保留首发地和本人联络方式,以方便交流,谢谢!
******************************************************************/
有时程序需要
存储
很大量的数据,或者在几个进程间
交换
数据,这时您可能考虑到使用临时文件。使用临时文件要考虑几个问题:
1、保证临时文件间的文件名不互助冲突。
2、保证临时文件中内容不被其他用户或者黑客偷看、删除和修改。
所以在
linux
下有专门处理临时文件的函数
mkstemp函数
mkstemp函数将在系统中以独一无二的文件名创建一个文件并打开,而且只有当前用户才有访问这个临时文件的权限,当前用户对这个临时文件可以打开并进行读、写操作。mkstemp函数只有一个参数,这个参数是个以“XXXXXX”结尾的非空字符串。mkstemp函数会用随机产生的字符串替换“XXXXXX”,保证了文件名的唯一性。函数返回一个文件描述符,如果执行失败返回-1。在glibc 2.0.6 以及更早的glibc库中这个文件的访问权限是0666 ,glibc 2.0.7 以后的库这个文件的访问权限是0600。
当临时文件完成她的使命如果不把它清除干净把或者程序由于意外在临时文件被清除前就已经退出,临时文件所在的目录会塞满垃圾。由于mkstemp函数创建的临时文件不能自动删除(请参考下文中的tmpfile函数)。执行完mkstemp函数后要调用unlink函数,unlink函数删除文件的目录入口,所以临时文件还可以通过文件描述符进行访问,直到最后一个打开的进程关闭文件操作符,或者程序退出后临时文件被自动彻底地删除。
例程:
直接使用advanced
linux
programming的例程,只把注释翻译一下
#include
#include
/* A handle for a temporary file created with write_temp_file. In
this implementation, it’s just a file descriptor. */
/*write_temp_file是个操作临时文件的句柄,本例中只是个文件描述符*/
typedef int temp_file_handle;
/* Writes LENGTH bytes from BUFFER into a temporary file. The
temporary file is immediately unlinked. Returns a handle to the
temporary file. */
/*在这函数从BUFFER中向临时文件写入LENGTH字节数据。临时文件在刚一创建就被删除掉。函数会返回临时文件的句柄。*/
temp_file_handle write_temp_file (char* buffer, size_t length)
{
/* Create the filename and file. The XXXXXX will be replaced with
characters that make the filename unique. */
/*新建文件名和文件,文件名中的XXXXXX将被随机字符串代替,以保证文件名在系统中的唯一性*/
char temp_filename[] = “/tmp/temp_file.XXXXXX”;
int fd = mkstemp (temp_filename);
/* Unlink the file immediately, so that it will be removed when the
file descriptor is closed. */
/*文件立刻被unlink,这样只要文件描述符一关闭文件就会被自动删除*/
unlink (temp_filename);
/* Write the number of bytes to the file first. */
/*首先写入即将写入数据的长度*/
write (fd, &length, sizeof (length));
/* Now write the data itself. */
/*写入数据本身*/
write (fd, buffer, length);
/* Use the file descriptor as the handle for the temporary file. */
/*函数返回文件描述符,作为临时文件的句柄*/
return fd;
}
/* Reads the contents of a temporary file TEMP_FILE created with
write_temp_file. The return value is a newly allocated buffer of
those contents, which the caller must deallocate with free.
*LENGTH is set to the size of the contents, in bytes. The
temporary file is removed. */
/*从被write_temp_file创建的临时文件中读取数据。返回值是含有文件内容的新申请到的内存块,这块内存应该又调用read_temp_file者释放。
*length是临时文件正文内容的长度。执行完read_temp_file函数后临时文件被彻底删除*/
char* read_temp_file (temp_file_handle temp_file, size_t* length)
{
char* buffer;
/* The TEMP_FILE handle is a file descriptor to the temporary file. */
/*fd是访问临时文件的文件描述符*/
int fd = temp_file;
/* Rewind to the beginning of the file. */
/*把文件指针指向文件开头*/
lseek (fd, 0, SEEK_SET);
/* Read the size of the data in the temporary file. */
/*获得临时文件正文长度*/
read (fd, length, sizeof (*length));
/* Allocate a buffer and read the data. */
/*分配内存块,读取数据*/
buffer = (char*) malloc (*length);
read (fd, buffer, *length);
/* Close the file descriptor, which will cause the temporary file to
go away. */
/*关闭文件描述符,临时文件被彻底删除*/
close (fd);
return buffer;
}
tmpfile函数
如果您使用C library I/O函数,并且并没有另一个程序使用这个临时文件(笔者注:按我的理解是在同一进程或具有父子关系的进程组中),有个更简洁的函数——tmpfile。tmpfile函数创建并打开一个临时文件,并且自动执行了unlink了这个临时文件。tmpfile函数返回一个文件描述符,如果执行失败返回NULL。当程序执行了fclose或者退出时,资源被释放。
linux系统中还提供mktemp、 tmpnam、 和tempnam等函数,但是由于健壮性和
安全
方面理由不建议使用他们。
unlink函数的另一个用途就是用来创建临时文件,如果在程序中使用open创建了一个文件后,然后立即使用 unlink 函数删除文件,由于此时进程正在打开该文件,所以系统并不会释放该文件的 inode 节点,而只是删除其目录项。当进程退出时,该inode节点将会立即被释放。
临时文件可以用在进程间通信中的 有名管道 通信中。
阅读全文
0 0
- 用unlink函数来清除临时文件(转)
- 用unlink函数来清除临时文件(转)
- 用unlink函数来清除临时文件(转)
- 清除临时文件
- unlink函数
- unlink 函数
- unlink函数
- unlink()函数
- unlink函数
- unlink()函数
- 创建临时文件 tmpnam tmpfile tempnam mkstemp unlink
- 创建临时文件 tmpnam tmpfile tempnam mkstemp unlink
- 创建临时文件 tmpnam tmpfile tempnam mkstemp unlink
- 通过unlink创建安全的临时文件
- 清除系统的临时文件
- C#清除IE临时文件
- 批清除VC临时文件
- C#清除IE临时文件
- struts2
- 内联函数
- 秒杀多线程第七篇 经典线程同步 互斥量Mutex
- BZOJ 1231: [Usaco2008 Nov]mixup2 混乱的奶牛 状压dp
- Vue学习之源码分析--Vue.js响应式原理(一)
- 用unlink函数来清除临时文件(转)
- linux下配置nginx多域名
- 一脸懵逼搭建Zookeeper分布式集群
- 项目在intellij idea里配置tomcat可以启动并访问, 打成war包丢到tomcat webapps下能启动却访问不了相关的接口
- Codeforces830A【二分+贪心】
- C#调用C++ DLL动态库的两种方式
- java基础(十)捕获异常还是抛出异常
- ES 创建索引设置(setting)基础
- 基于Directshow的USB视频捕获Delphi篇(二)