use Files.createTempDirectory() instead of new File() (#7713)

This commit is contained in:
dahn 2023-07-06 09:33:51 +02:00 committed by GitHub
parent acc6f4e725
commit e6ef8a5225
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 1 deletions

View File

@ -22,8 +22,11 @@ package com.cloud.storage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@ -186,8 +189,12 @@ public class JavaStorageLayer implements StorageLayer {
@Override
public File createUniqDir() throws IOException {
String dirName = System.getProperty("java.io.tmpdir");
String subDirNamePrefix = "";
FileAttribute<Set<PosixFilePermission>> perms = PosixFilePermissions
.asFileAttribute(PosixFilePermissions.fromString("rwxrwx---"));
if (dirName != null) {
File dir = new File(dirName);
Path p = Files.createTempDirectory(Path.of(dirName), subDirNamePrefix, perms);
File dir = p.toFile();
if (dir.exists()) {
if (isWorldReadable(dir)) {
if (STD_TMP_DIR_PATH.equals(dir.getAbsolutePath())) {

View File

@ -0,0 +1,46 @@
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
package com.cloud.storage;
import java.io.File;
import java.io.IOException;
import org.junit.Assert;
import org.junit.Test;
public class JavaStorageLayerTest {
JavaStorageLayer jsl = new JavaStorageLayer();
@Test
public void createUniqDir() {
try {
File one = jsl.createUniqDir();
Assert.assertTrue(one.isDirectory());
Assert.assertTrue(one.canRead());
Assert.assertTrue(one.canWrite());
Assert.assertTrue(one.canExecute());
} catch (IOException e) {
Assert.fail("creation of a unique dir should succeed.");
}
}
}