|
38 | 38 | import java.nio.file.Path; |
39 | 39 | import java.nio.file.SimpleFileVisitor; |
40 | 40 | import java.nio.file.attribute.BasicFileAttributes; |
| 41 | +import java.nio.file.attribute.FileAttribute; |
| 42 | +import java.nio.file.attribute.PosixFilePermission; |
| 43 | +import java.nio.file.attribute.PosixFilePermissions; |
41 | 44 | import java.util.ArrayList; |
42 | 45 | import java.util.Arrays; |
43 | 46 | import java.util.List; |
44 | 47 | import java.util.Map; |
| 48 | +import java.util.Set; |
45 | 49 | import java.util.logging.Level; |
46 | 50 | import java.util.logging.Logger; |
47 | 51 |
|
| 52 | +import org.apache.commons.lang3.SystemUtils; |
48 | 53 | import org.jetbrains.annotations.NotNull; |
49 | 54 | import org.opengrok.indexer.logger.LoggerFactory; |
50 | 55 |
|
@@ -303,4 +308,31 @@ public static String getFileContent(File file) { |
303 | 308 | } |
304 | 309 | return ""; |
305 | 310 | } |
| 311 | + |
| 312 | + /** |
| 313 | + * Create temporary directory with permissions restricted to the owner. |
| 314 | + * @param prefix prefix for the temporary directory name |
| 315 | + * @return File object |
| 316 | + * @throws IOException on I/O error or failure to set the permissions |
| 317 | + */ |
| 318 | + public static File createTemporaryDirectory(String prefix) throws IOException { |
| 319 | + File tmp; |
| 320 | + if (SystemUtils.IS_OS_UNIX) { |
| 321 | + FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions. |
| 322 | + asFileAttribute(PosixFilePermissions.fromString("rwx------")); |
| 323 | + tmp = Files.createTempDirectory(prefix, attr).toFile(); |
| 324 | + } else { |
| 325 | + tmp = Files.createTempDirectory(prefix).toFile(); |
| 326 | + if (!tmp.setReadable(true, true)) { |
| 327 | + throw new IOException("unable to set read permissions for '" + tmp.getAbsolutePath() + "'"); |
| 328 | + } |
| 329 | + if (!tmp.setWritable(true, true)) { |
| 330 | + throw new IOException("unable to set write permissions for '" + tmp.getAbsolutePath() + "'"); |
| 331 | + } |
| 332 | + if (!tmp.setExecutable(true, true)) { |
| 333 | + throw new IOException("unable to set executable permissions for '" + tmp.getAbsolutePath() + "'"); |
| 334 | + } |
| 335 | + } |
| 336 | + return tmp; |
| 337 | + } |
306 | 338 | } |
0 commit comments