Temporary file operations in Linux: mkstemp

Source: Internet
Author: User
There are several considerations for using temporary files: ensure that the file names of temporary files do not conflict with each other. Ensure that the contents of temporary files are not peeked, deleted, or modified by other users or hackers. Mk is provided in Linux.

To use temporary files, consider the following:
Ensure that the file names of temporary files do not conflict with each other.
Ensure that the contents of temporary files are not peeked, deleted, or modified by other users or hackers.
The mkstemp and tmpfile functions are provided in Linux to process temporary files.
Mkstemp function
Int mkstemp (char * template );
The mkstemp function creates and opens a file with a unique file name in the system. only the current user can access this temporary file and perform read and write operations. The mkstemp function has only one parameter. this parameter is a non-null string ending with "XXXXXX. The mkstemp function replaces "XXXXXX" with a random string to ensure the uniqueness of the file name. The function returns a file descriptor. if the execution fails,-1 is returned. In glibc 2.0.6 and earlier glibc libraries, the access permission for this file is 0666, and in libraries later than glibc 2.0.7, the access permission for this file is 0600.
The temporary files should be deleted in time after use; otherwise, the Directory of the temporary files will be full of garbage. The temporary files created by the mkstemp function cannot be automatically deleted. Therefore, after the mkstemp function is executed, you must call the unlink function. The unlink function deletes the directory entry of the file, however, temporary files can be accessed through file descriptors until the last opened process closes the file operator, or the temporary files are automatically and permanently deleted after the program exits.

#include 
   
    #include 
    
     #include 
     
      #include 
      
       int write_temp_file(char* buffer,size_t length) {    int len=length;    char filename_template[]="/tmp/temp_file.XXXXXX";    int fd=mkstemp(filename_template);    unlink(filename_template);//Unlink the file, so it'll be removed when close    printf("Template file name:%s\n",filename_template);    write(fd,&len,sizeof(len));    write(fd,buffer,len);    return fd;}char* read_temp_file(int fd, size_t* length) {    char* buffer;    lseek(fd,0,SEEK_SET);    read(fd,length,sizeof(size_t));    buffer=(char*)malloc(*length);    read(fd,buffer,*length);    close(fd); // Temp file will be deleted    return buffer;}int main(int argc, char** argv) {    char buffer[]="Test template files";    int fd=write_temp_file(buffer,strlen(buffer));    int len=0;    char* result=read_temp_file(fd,&len);    printf("Len:%d\nContent:%s\n",len,result);    free(result);    return 0;}
      
     
    
   


Tmpfile function
If you use the C library I/O function and no other program uses this temporary file, there is a more concise function-tmpfile. The tmpfile function creates and opens a temporary file and automatically runs unlink. The tmpfile function returns a file descriptor. if execution fails, NULL is returned. When the program executes fclose or exits, the resource is released.
In addition, mktemp, tmpnam, tempnam, and other functions are provided in linux. However, due to robustness and security issues, it is not recommended.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.