73
73
@ ElementTypesAreNonnullByDefault
74
74
public final class Files {
75
75
76
- /** Maximum loop count when creating temp directories. */
77
- private static final int TEMP_DIR_ATTEMPTS = 10000 ;
78
-
79
76
private Files () {}
80
77
81
78
/**
@@ -399,17 +396,19 @@ public static boolean equal(File file1, File file2) throws IOException {
399
396
* Atomically creates a new directory somewhere beneath the system's temporary directory (as
400
397
* defined by the {@code java.io.tmpdir} system property), and returns its name.
401
398
*
399
+ * <p>The temporary directory is created with permissions restricted to the current user or, in
400
+ * the case of Android, the current app. If that is not possible (as is the case under the very
401
+ * old Android Ice Cream Sandwich release), then this method throws an exception instead of
402
+ * creating a directory that would be more accessible. (This behavior is new in Guava 32.0.0.
403
+ * Previous versions would create a directory that is more accessible, as discussed in <a
404
+ * href="https://github.com/google/guava/issues/4011">CVE-2020-8908</a>.)
405
+ *
402
406
* <p>Use this method instead of {@link File#createTempFile(String, String)} when you wish to
403
407
* create a directory, not a regular file. A common pitfall is to call {@code createTempFile},
404
408
* delete the file and create a directory in its place, but this leads a race condition which can
405
409
* be exploited to create security vulnerabilities, especially when executable files are to be
406
410
* written into the directory.
407
411
*
408
- * <p>Depending on the environment that this code is run in, the system temporary directory (and
409
- * thus the directory this method creates) may be more visible that a program would like - files
410
- * written to this directory may be read or overwritten by hostile programs running on the same
411
- * machine.
412
- *
413
412
* <p>This method assumes that the temporary volume is writable, has free inodes and free blocks,
414
413
* and that it will not be called thousands of times per second.
415
414
*
@@ -418,36 +417,26 @@ public static boolean equal(File file1, File file2) throws IOException {
418
417
*
419
418
* @return the newly-created directory
420
419
* @throws IllegalStateException if the directory could not be created
420
+ * @throws UnsupportedOperationException if the system does not support creating temporary
421
+ * directories securely
421
422
* @deprecated For Android users, see the <a
422
423
* href="https://developer.android.com/training/data-storage" target="_blank">Data and File
423
424
* Storage overview</a> to select an appropriate temporary directory (perhaps {@code
424
- * context.getCacheDir()}). For developers on Java 7 or later, use {@link
425
- * java.nio.file.Files#createTempDirectory}, transforming it to a {@link File} using {@link
426
- * java.nio.file.Path#toFile() toFile()} if needed.
425
+ * context.getCacheDir()}), and create your own directory under that. (For example, you might
426
+ * use {@code new File(context.getCacheDir(), "directoryname").mkdir()}, or, if you need an
427
+ * arbitrary number of temporary directories, you might have to generate multiple directory
428
+ * names in a loop until {@code mkdir()} returns {@code true}.) For developers on Java 7 or
429
+ * later, use {@link java.nio.file.Files#createTempDirectory}, transforming it to a {@link
430
+ * File} using {@link java.nio.file.Path#toFile() toFile()} if needed. To restrict permissions
431
+ * as this method does, pass {@code
432
+ * PosixFilePermissions.asFileAttribute(PosixFilePermissions.fromString("rwx------"))} to your
433
+ * call to {@code createTempDirectory}.
427
434
*/
428
435
@ Beta
429
436
@ Deprecated
430
437
@ J2ObjCIncompatible
431
438
public static File createTempDir () {
432
- File baseDir = new File (System .getProperty ("java.io.tmpdir" ));
433
- @ SuppressWarnings ("GoodTime" ) // reading system time without TimeSource
434
- String baseName = System .currentTimeMillis () + "-" ;
435
-
436
- for (int counter = 0 ; counter < TEMP_DIR_ATTEMPTS ; counter ++) {
437
- File tempDir = new File (baseDir , baseName + counter );
438
- if (tempDir .mkdir ()) {
439
- return tempDir ;
440
- }
441
- }
442
- throw new IllegalStateException (
443
- "Failed to create directory within "
444
- + TEMP_DIR_ATTEMPTS
445
- + " attempts (tried "
446
- + baseName
447
- + "0 to "
448
- + baseName
449
- + (TEMP_DIR_ATTEMPTS - 1 )
450
- + ')' );
439
+ return TempFileCreator .INSTANCE .createTempDir ();
451
440
}
452
441
453
442
/**
0 commit comments