Do not use Java 7 files interface parameters on Linux Standardopenoption.delete_on_close

Source: Internet
Author: User

Recently, referring to security code specification recommendations for how to delete temporary files created, it is recommended to use the functions of files in Jdk7, with parameter standardopenoption.delete_on_close to control

code example

BufferedWriter writer = Files.newbufferedwriter (tempfile, Charset.forname ("UTF8"), Standardopenoption.delete_on_ CLOSE)

As soon as the file is close, the file is automatically deleted by the system, so you can avoid forgetting to delete the resulting temporary file in some scenarios.

Standardopenoption.delete_on_close parameters

Let's take a look at what the code standardopenoption.delete_on_close parameters do under Linux, and we see in Java code the parameters of the control valve

if (flags.deleteonclose) {            try {                if (DFD >= 0) {                    unlinkat (DFD, Path.asbytearray (), 0);                } else {                    UN Link (path);                }            } catch (Unixexception ignore) {                //Best-effort            }        }

Java's unlink function

static void unlink (Unixpath path) throws unixexception {        Nativebuffer buffer = copytonativebuffer (path);        try {            unlink0 (buffer.address ());        } finally {            buffer.release ()}    }


JNI unlink0 function

Jniexport void Jnicalljava_sun_nio_fs_unixnativedispatcher_unlink0 (jnienv* env, Jclass this,    Jlong pathAddress) {    const CHAR* Path = (const char*) jlong_to_ptr (pathaddress);    /* Eintr not listed as a possible error */    if (unlink (path) = =-1) {        throwunixexception (env, errno);}    }
Finally, the system unlink function is called.
The difference between unlink RM Unlinkat remove

We usually use shell RM to delete the file, let's look at what RM did, see the system call through Strace strace RM filepath See the last function called Unlinkat

The difference between unlink and Unlinkat

Unlinkat can delete empty directories by parameter At_removedir, others are the same as unlink

The difference between remove and unlink

Remove is a collection of unlink and rmdir, and the difference is also on the delete directory

You can see that the parameter Standardopenoption.delete_on_close on Linux is called unlink deleted file name, then it means that you do not read and write to this file is invalid.

The difference between Linux and Windows

Parameter Standardopenoption.delete_on_close in Linux and Windows is not the same implementation, Windows only call the Writer.close () This file will be deleted, That is, the open function on Windows itself has the file attribute File_flag_delete_on_close, and when you explicitly call CLOSE, the file is deleted naturally, and if the process exits it will also delete the file.


Summarize:

Parameter standardopenoption.delete_on_close is an invalid operation for Linux, although it guarantees the deletion of the file, but it also violates the protocol of its own API (call close to delete the file).

And it doesn't really make sense, because the file has been deleted, which means that it can't write anything to the file, so the meaning of creating the temporary file doesn't exist. This function is implemented on Windows, and there are no similar kernel functions in Linux to implement such a function.

Method of the File function Deleteonexit

File.deleteonexit ()


This method adds a hook when the JVM exits and deletes the file when it exits. But what if the JVM does not exit normally? For example, we often use the signal to exit, there is no way to delete the file normally.

Standardopenoption.delete_on_close This is required to implement this attribute in kernel functions, rather than in the JVM application layer, and the JVM directly calls unlink on Linux to avoid complications.

Do not use Java 7 files interface parameters on Linux Standardopenoption.delete_on_close

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.