Skip to content

Commit e6ef8a5

Browse files
authored
use Files.createTempDirectory() instead of new File() (#7713)
1 parent acc6f4e commit e6ef8a5

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

core/src/main/java/com/cloud/storage/JavaStorageLayer.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@
2222
import java.io.File;
2323
import java.io.IOException;
2424
import java.nio.file.Files;
25+
import java.nio.file.Path;
2526
import java.nio.file.Paths;
27+
import java.nio.file.attribute.FileAttribute;
2628
import java.nio.file.attribute.PosixFilePermission;
29+
import java.nio.file.attribute.PosixFilePermissions;
2730
import java.util.ArrayList;
2831
import java.util.List;
2932
import java.util.Map;
@@ -186,8 +189,12 @@ public long getSize(String path) {
186189
@Override
187190
public File createUniqDir() throws IOException {
188191
String dirName = System.getProperty("java.io.tmpdir");
192+
String subDirNamePrefix = "";
193+
FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions
194+
.asFileAttribute(PosixFilePermissions.fromString("rwxrwx---"));
189195
if (dirName != null) {
190-
File dir = new File(dirName);
196+
Path p = Files.createTempDirectory(Path.of(dirName), subDirNamePrefix, perms);
197+
File dir = p.toFile();
191198
if (dir.exists()) {
192199
if (isWorldReadable(dir)) {
193200
if (STD_TMP_DIR_PATH.equals(dir.getAbsolutePath())) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// Licensed to the Apache Software Foundation (ASF) under one
3+
// or more contributor license agreements. See the NOTICE file
4+
// distributed with this work for additional information
5+
// regarding copyright ownership. The ASF licenses this file
6+
// to you under the Apache License, Version 2.0 (the
7+
// "License"); you may not use this file except in compliance
8+
// with the License. You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing,
13+
// software distributed under the License is distributed on an
14+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
// KIND, either express or implied. See the License for the
16+
// specific language governing permissions and limitations
17+
// under the License.
18+
//
19+
20+
package com.cloud.storage;
21+
22+
import java.io.File;
23+
import java.io.IOException;
24+
25+
import org.junit.Assert;
26+
import org.junit.Test;
27+
28+
public class JavaStorageLayerTest {
29+
30+
JavaStorageLayer jsl = new JavaStorageLayer();
31+
32+
@Test
33+
public void createUniqDir() {
34+
35+
try {
36+
File one = jsl.createUniqDir();
37+
Assert.assertTrue(one.isDirectory());
38+
Assert.assertTrue(one.canRead());
39+
Assert.assertTrue(one.canWrite());
40+
Assert.assertTrue(one.canExecute());
41+
} catch (IOException e) {
42+
Assert.fail("creation of a unique dir should succeed.");
43+
}
44+
}
45+
}
46+

0 commit comments

Comments
 (0)