diff --git a/core/src/main/java/com/cloud/storage/JavaStorageLayer.java b/core/src/main/java/com/cloud/storage/JavaStorageLayer.java index b65a76b6c25..d4c2639d478 100644 --- a/core/src/main/java/com/cloud/storage/JavaStorageLayer.java +++ b/core/src/main/java/com/cloud/storage/JavaStorageLayer.java @@ -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> 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())) { diff --git a/core/src/test/java/com/cloud/storage/JavaStorageLayerTest.java b/core/src/test/java/com/cloud/storage/JavaStorageLayerTest.java new file mode 100644 index 00000000000..241afcd5a92 --- /dev/null +++ b/core/src/test/java/com/cloud/storage/JavaStorageLayerTest.java @@ -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."); + } + } +} +