How to use C language to operate temporary files under Linux

zhaozj2021-02-16  40

How to use C language to operate temporary files under Linux

/ ************************************************** ******************

* This article is first in BBS.Bluegem.org CWORLD zone

* I email: chenfei@sohu.com

* If you reprinate this article, please keep the starting ground and your contact method for easy communication, thank you!

*********************************************************** **************** /

Sometimes the program needs to store a lot of data, or exchange data between several processes, then you may take into account the use of temporary files. Use temporary documents to consider a few questions: 1, ensure that the file name between the temporary file is not mutual conflict. 2. Ensure that the content in the temporary file is not smashed, deleted, and modified by other users or hackers. So in Linux, a function MKSTEMP function MKSTEMP function is created in the system with a unique file name, and only the current user has access to this temporary file, the current user can Open and read, write. The MKSTEMP function has only one parameter, which is a non-empty string ending with "xxxxxx". The MKSTEMP function replaces "XXXXXX" with a random-generated string to ensure the uniqueness of the file name. The function returns a file descriptor if the execution fails returns -1. Access to this file in Glibc 2.0.6 and the earlier Glibc library is 0666, and the access permission of the file after GLIBC 2.0.7 is 0600. When the temporary file completes her mission If it does not clear it, it has exited it before the interim file is cleared, and the interim file is located. Since the temporary file created by the MKSTEMP function cannot be automatically deleted (see the TMPFile function below). After performing the MKSTEMP function, call the unlink function, the Unlink function deletes the file entry, so the temporary file can also be accessed through the file descriptor until the last open process turns off the file operator, or the program exits the temporary file is automatically complete. Delete. Routines: Directly use the ADVANCED Linux Programming routines, just translate the comment

#include #include / * a Handle for a Temporary File Created with write_temp_file. INTHIS IMPLEMENTATION, IT'S JUST A File Descriptor. * // * Write_temp_file is a handle of an operational temporary file, in this case just a file descriptor * / typedef int temp_file_handle;... / * Writes LENGTH bytes from BUFFER into a temporary file thetemporary file is immediately unlinked Returns a handle to thetemporary file * // * in writing this function from BUFFER into a temporary file Enter the length byte data. The temporary file is removed in just one creation. The function returns the handle of the temporary file. * / 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. * // * the new file name and file, the file name is XXXXXX will be Random string instead to ensure the uniqueness of the file name in the system * / char TEMP_FILENAME [] = "/TMP/TEMP_FILE.XXXXXX"; int fd = mkstemp (Temp_FileName); / * unlink the file immediately, so that it will Will BE Removed. * / / * The file immediately by Unlink, so as long as the file descriptor closed the file, it will be automatically deleted * / unlink (TEMP_FILENAME); / * Write the number of bytes to the file first. * / / * First, write upcoming the length of the data is about * / write (fd, & length, sizeof (length)); / * now write the data itself. * / / * Write data itself * / write (fd, buffer , length); / * Use the file descriptor as the handle for the temporary file. * / / * function Returns the file descriptor, the handle of the temporary file * / return fd;} / * reads the contents of a temporary File Temp_file created WITHWRITE_TEMP_FILE. THE RETURN VALUE IS A Newly Allocated Buffer Office Contents, Which The Caller Must Deallocate with free. * Length is set to the size of the contents, in bytes. THETES. THETEMPORAR Y File Is Removed. * // * Reads data from a temporary file created by Write_Temp_File. The return value is a newly requested memory block containing the file content, which should also call read_temp_files. * Length is the length of the textual content of the temporary file.

After performing read_temp_file function temporary file is 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 to visit temporary. File Descriptor * / INT FD = Temp_File; / * rebind to the beginning of the file. * / / * Point the file pointer to the beginning * / lseek (fd, 0, seek_set); / * read the size of the Data in the temporary file. * / / * Get a temporary file body * / read (fd, length, sizeof (* length)); / * allocate a buffer and read the data. * / / * Assign the memory block, read Data * / buffer = (char *) Malloc (* length); read (fd, buffer, * length); / * Close The File Descriptor, Which Will Cause The Temporary File To Go Away. * / / * Close the file descriptor The temporary file is completely deleted * / close (fd); return buffer;} TMPFile function If you use the C library I / O function, and no other program use this temporary file (author Note: Press my understanding is 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 this temporary file. The TMPFile function returns a file descriptor if the execution failed returns NULL. The resource is released when the program executes FCLOSE or exits. MKTEMP, TMPNAM, and TEMPNAM, etc., but it is not recommended to use them due to robust and security. Reference: Advanced Linux Programming P27 ~ 29Linux Programing Unleashed P171 ~ 172MAN Page

转载请注明原文地址:https://www.9cbs.com/read-27655.html

New Post(0)