Linux temporary file operations

Source: Internet
Author: User
Unlink unlink (delete file)
Related functions link, rename, remove
Header file # include <unistd. h>
Define the function int unlink (const char * pathname );
Function Description unlink () deletes the file specified by pathname. If the file name is the last connection point, but other processes open the file, it will be deleted after all the file descriptions about the file are closed. If the pathname parameter is a symbolic connection, the connection is deleted.
If the return value is successful, 0 is returned. If the return value is failed,-1 is returned. The cause of the error is errno.
Error Code: The erofs file exists in the read-only file system.
The pathname pointer of the efault parameter exceeds the accessible memory space.
The pathname parameter of enametoolong is too long.
Insufficient enomem core memory
The pathname of the eloop parameter has too many symbolic connections.
Eio I/O Access Error
# Include
# Include
Int main (void)
{
File * fp = fopen ("junk. Kaplan", "W ");
Int status;
Fprintf (FP, "junk ");
Status = access ("junk. hh", 0 );
If (status = 0)
Printf ("file exists/N ");
Else
Printf ("file doesn't exist/N ");
Fclose (FP );
Unlink ("junk. Kaplan ");
Status = access ("junk. hh", 0 );
If (status = 0)
Printf ("file exists/N ");
Else
Printf ("file doesn't exist/N ");
Return 0;
} In maemo-launcher, the process signal processing function group contains the following function, where unlink is used to clear temporary files. Static void
Clean_daemon (INT signal)
{
If (is_parent)
{
Unlink (pidfilename );
Unlink (invoker_sock );
Killpg (0, signal );
}
Exit (0 );
}

Sometimes the program needs
Storage
A large amount of data, or several processes
Exchange
Data, you may consider using temporary files. To use temporary files, consider the following:
1. Ensure that the names of temporary files do not conflict with each other.
2. Ensure that the contents of temporary files are not peeked, deleted, or modified by other users or hackers.
Therefore
Linux
Function for processing temporary files
Mkstemp Function
The mkstemp function creates and opens a file with a unique file name in the system. Only the current user has the permission to access this temporary file, the current user can open and read and write this temporary file. 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.
When the temporary file completes her mission, if it is not cleared, or the program has exited before it is accidentally cleared, the directory where the temporary file is located will be full of garbage. Temporary files created by the mkstemp function cannot be automatically deleted (refer to the tmpfile function below ). After executing the mkstemp function, you need to call the unlink function. The unlink function deletes the directory entry of the file. Therefore, temporary files can be accessed through the file descriptor until the last opened process closes the file operator, or the temporary files are automatically and permanently deleted after the program exits.
Routine:
Use advanced directly
Linux
Programming routines, just translate comments
# 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 for operations. In this example, it is only a file descriptor */
Typedef int temp_file_handle;
/* Writes length bytes from buffer into a temporary file.
Temporary File is immediately unlinked. Returns a handle to
Temporary File .*/
/* Write the Length byte data from the buffer to the temporary file. The temporary file is deleted after it is created. 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
Characters that make the filename unique .*/
/* Create a new file name and file. xxxxxx in the file name will be replaced by a random string 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 be removed when
File descriptor is closed .*/
/* The file is unlink immediately, so that the file will be automatically deleted as soon as the file descriptor is closed */
Unlink (temp_filename );
/* Write the number of bytes to the file first .*/
/* First write the length of the data to be written */
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 .*/
/* The function returns the file descriptor as the handle to the temporary file */
Return FD;
}
/* Reads the contents of a temporary file temp_file created
Write_temp_file. the return value is a newly allocated buffer
Those contents, which the caller must deallocate with free.
* Length is set to the size of the contents, in bytes.
Temporary File is removed .*/
/* Read data from the temporary file created by write_temp_file. The returned value is the newly applied memory block containing the file content. This memory should be released by calling read_temp_file again.
* Length is the length of the body of the temporary file. The temporary file is permanently deleted after the read_temp_file function is executed */
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 the file descriptor used to access temporary files */
Int FD = temp_file;
/* Rewind to the beginning of the file .*/
/* Point 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 the body length of the temporary file */
Read (FD, length, sizeof (* length ));
/* Allocate a buffer and read the data .*/
/* Allocate memory blocks to read data */
Buffer = (char *) malloc (* length );
Read (FD, buffer, * length );
/* Close the file descriptor, which will cause the temporary file
Go away .*/
/* Close the file descriptor and delete the temporary file */
Close (FD );
Return buffer;
}
Tmpfile Function
If you use the C library I/O function and no other program uses this temporary file (note: in my understanding, it is in the same process or a parent-child process group.) There is a more concise function called tmpfile. The tmpfile function creates and opens a temporary file, and automatically runs the unlink temporary file. The tmpfile function returns a file descriptor. If execution fails, null is returned. When the program executes fclose or exits, the resource is released.
The mktemp, tmpnam, and tempnam functions are also provided in Linux. However, due to the robustness and
Security
They are not recommended for 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.