1+ import java .io .File ;
2+ import java .io .IOException ;
13import java .nio .file .Files ;
24import java .nio .file .attribute .PosixFilePermission ;
35import java .nio .file .attribute .PosixFilePermissions ;
@@ -9,11 +11,90 @@ void exampleSafe() throws IOException {
911
1012 Path temp2 = Files .createTempDirectory ("random-directory" ); // GOOD: File has permissions `drwx------`
1113
12- File tempDirChildFile = new File (System .getProperty ("java.io.tmpdir" ), "/child-create-file.txt" );
14+ // Creating a temporary file with a non-randomly generated name
15+ File tempChildFile = new File (System .getProperty ("java.io.tmpdir" ), "/child-create-file.txt" );
16+ // Warning: This will fail on windows as it doesn't support PosixFilePermissions.
17+ // See `exampleSafeWithWindowsSupportFile` if your code needs to support windows and unix-like systems.
1318 Files .createFile (
14- tempDirChildFile .toPath (),
15- tempDirChild .toPath (),
19+ tempChildFile .toPath (),
1620 PosixFilePermissions .asFileAttribute (EnumSet .of (PosixFilePermission .OWNER_READ , PosixFilePermission .OWNER_WRITE ))
1721 ); // GOOD: Good has permissions `-rw-------`
1822 }
23+
24+ /*
25+ * An example of a safe use of createFile or createDirectory if your code must support windows and unix-like systems.
26+ */
27+ void exampleSafeWithWindowsSupportFile () {
28+ // Creating a temporary file with a non-randomly generated name
29+ File tempChildFile = new File (System .getProperty ("java.io.tmpdir" ), "/child-create-file.txt" );
30+ createTempFile (tempChildFile .toPath ()); // GOOD: Good has permissions `-rw-------`
31+ }
32+
33+ static void createTempFile (Path tempDir ) {
34+ try {
35+ if (tempDirChild .getFileSystem ().supportedFileAttributeViews ().contains ("posix" )) {
36+ // Explicit permissions setting is only required on unix-like systems because
37+ // the temporary directory is shared between all users.
38+ // This is not necessary on Windows, each user has their own temp directory
39+ final EnumSet <PosixFilePermission > posixFilePermissions =
40+ EnumSet .of (
41+ PosixFilePermission .OWNER_READ ,
42+ PosixFilePermission .OWNER_WRITE
43+ );
44+ if (!Files .exists (tempDirChild )) {
45+ Files .createFile (
46+ tempDirChild ,
47+ PosixFilePermissions .asFileAttribute (posixFilePermissions )
48+ ); // GOOD: Directory has permissions `-rw-------`
49+ } else {
50+ Files .setPosixFilePermissions (
51+ tempDirChild ,
52+ posixFilePermissions
53+ ); // GOOD: Good has permissions `-rw-------`, or will throw an exception if this fails
54+ }
55+ } else if (!Files .exists (tempDirChild )) {
56+ // On Windows, we still need to create the directory, when it doesn't already exist.
57+ Files .createDirectory (tempDirChild ); // GOOD: Windows doesn't share the temp directory between users
58+ }
59+ } catch (IOException exception ) {
60+ throw new UncheckedIOException ("Failed to create temp file" , exception );
61+ }
62+ }
63+
64+ void exampleSafeWithWindowsSupportDirectory () {
65+ File tempDirChildDir = new File (System .getProperty ("java.io.tmpdir" ), "/child-dir" );
66+ createTempDirectories (tempDirChildDir .toPath ()); // GOOD: Directory has permissions `drwx------`
67+ }
68+
69+ static void createTempDirectories (Path tempDirChild ) {
70+ try {
71+ if (tempDirChild .getFileSystem ().supportedFileAttributeViews ().contains ("posix" )) {
72+ // Explicit permissions setting is only required on unix-like systems because
73+ // the temporary directory is shared between all users.
74+ // This is not necessary on Windows, each user has their own temp directory
75+ final EnumSet <PosixFilePermission > posixFilePermissions =
76+ EnumSet .of (
77+ PosixFilePermission .OWNER_READ ,
78+ PosixFilePermission .OWNER_WRITE ,
79+ PosixFilePermission .OWNER_EXECUTE
80+ );
81+ if (!Files .exists (tempDirChild )) {
82+ Files .createDirectories (
83+ tempDirChild ,
84+ PosixFilePermissions .asFileAttribute (posixFilePermissions )
85+ ); // GOOD: Directory has permissions `drwx------`
86+ } else {
87+ Files .setPosixFilePermissions (
88+ tempDirChild ,
89+ posixFilePermissions
90+ ); // GOOD: Good has permissions `drwx------`, or will throw an exception if this fails
91+ }
92+ } else if (!Files .exists (tempDirChild )) {
93+ // On Windows, we still need to create the directory, when it doesn't already exist.
94+ Files .createDirectories (tempDirChild ); // GOOD: Windows doesn't share the temp directory between users
95+ }
96+ } catch (IOException exception ) {
97+ throw new UncheckedIOException ("Failed to create temp dir" , exception );
98+ }
99+ }
19100}
0 commit comments