Operation of temporary files using C language on Linux systems

Source: Internet
Author: User
Tags linux

Sometimes a program needs to store a lot of data, or exchange data between several processes, when you might consider using a temporary file. There are several issues to consider when using temporary files:

1, to ensure that the file name between temporary documents conflict.

2, to ensure that the contents of the temporary files are not other users or hackers peek, delete and modify.

So there are functions that specialize in temporary files under Linux

mkstemp function

The Mkstemp function creates and opens a file with a unique filename in the system, and only the current user has access to the temporary file, and the current user can open and read and write to the temporary file. The Mkstemp function has only one argument, which is a non-empty string that ends with "XXXXXX". The Mkstemp function replaces "XXXXXX" with a randomly generated string, guaranteeing the uniqueness of the filename. function returns a file descriptor that returns 1 if execution fails. The access rights to this file in glibc 2.0.6 and earlier glibc libraries are 0666, and the access rights for this file in glibc 2.0.7 libraries are 0600.

When the temporary file completes her mission if you don't clean it up or the program exits before the temporary file is cleared, the directory where the temporary file is located will be stuffed with garbage. Because the temporary files created by the Mkstemp function cannot be deleted automatically (refer to the Tmpfile function below). To call the unlink function after executing the MKSTEMP function, the unlink function deletes the directory entry of the file, so the temporary file can also be accessed through the file descriptor until the last open process closes the file operator, or the temporary file is automatically deleted after the program exits.

Routines:

Direct use of advanced Linux programming routines, just translate the annotations

  #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 is a handle to the temporary file, in this case only a file descriptor/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. */* Writes the length byte data to the temporary file in this function from the buffer. Temporary files are deleted as soon as they are created. function returns a handle to the temporary file. * * Temp_file_handle write_temp_file (char* buffer, size_t length) {/* Create the filename and file. The XXXXXX is replaced with characters, and make the filename unique. * * New file name and file, xxxxxx in filename will be replaced by random strings, to ensure that the file name in the system unique/char temp_filename[] = "/tmp/temp_file.
  XXXXXX ";
  int fd = mkstemp (temp_filename); /* Unlink The file immediately, so it'll be removed when the file descriptor is closed.
  * * * file is unlink immediately, so long as the file descriptor a close file will be automatically deleted/unlink (temp_filename); /* Write the number of bytes to the file a. *//////////////////
  Write (fd, &length, sizeof (length)); /* Now write the data itself.
  * * * Write the data itself */write (fd, buffer, length); /* Use the file descriptor as the handle for the temporary file.
  ////////////function returns the file descriptor, as a temporary file handle/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 and deallocate with free. *length is set to the size of the contents, in bytes. The temporary file is removed. *///////////* read data from temporary files created by Write_temp_file.
  The return value is a newly-requested block of memory that contains the contents of the file, which should be freed by invoking the Read_temp_file. *length is the length of the contents of the temporary file body.
  After performing the Read_temp_file function, the temporary file is completely removed * * * 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 is a file descriptor for accessing temporary files/int fd = Temp_file; /* Rewind to the beginning of the file.
  * * * To the file pointer to the beginning of the file * * Lseek (FD, 0, Seek_set); /* Read the size of the data in the TemporaRy file.
  * * * Get temporary file body length * * (fd, length, sizeof (*length)); /* Allocate A buffer and read the data.
  * * * ALLOCATE memory block, read data/buffer = (char*) malloc (*length);
  Read (fd, buffer, *length); /* Close the file descriptor, which'll cause the temporary file to go away.
  *//////////* Close file descriptor, temporary file is completely removed * * (FD);
  return buffer; }

Tmpfile function

If you use the C library I/O function and no other program uses the temporary file (note: As I understand it is in the same process or in a process group with a parent-child relationship), there is a more concise function--tmpfile. The Tmpfile function creates and opens a temporary file, and automatically executes the temporary file unlink. The Tmpfile function returns a file descriptor that returns null if execution fails. The resource is freed when the program executes a fclose or exits.

Linux systems also provide functions such as mktemp, Tmpnam, and Tempnam, but they are not recommended for robustness and security reasons.

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.