CLOUDSTACK-10436:remind users to use correct permission for tmp dir and fixed an NPE (#5066)

* CLOUDSTACK-10436:remind users to use correct permission for tmp dir

* remove static

* fix potential NPE

* check /tmp

* simplified the code

* remove /tmp

* add /tmp

* add static tmp

* rename tmp

Co-authored-by: lujie <lujie@foxmail.com>
This commit is contained in:
lujiefsi 2021-09-03 19:41:24 +08:00 committed by GitHub
parent a000361238
commit 1ed828b2a1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 4 deletions

View File

@ -21,15 +21,21 @@ package com.cloud.storage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFilePermission;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
public class JavaStorageLayer implements StorageLayer {
private static final Logger s_logger = Logger.getLogger(JavaStorageLayer.class);
private static final String STD_TMP_DIR_PATH = "/tmp";
String _name;
boolean _makeWorldWriteable = true;
@ -178,18 +184,25 @@ public class JavaStorageLayer implements StorageLayer {
}
@Override
public File createUniqDir() {
public File createUniqDir() throws IOException {
String dirName = System.getProperty("java.io.tmpdir");
if (dirName != null) {
File dir = new File(dirName);
if (dir.exists()) {
if (isWorldReadable(dir)) {
if (STD_TMP_DIR_PATH.equals(dir.getAbsolutePath())) {
s_logger.warn(String.format("The temp dir is %s", STD_TMP_DIR_PATH));
} else {
s_logger.warn("The temp dir " + dir.getAbsolutePath() + " is World Readable");
}
}
String uniqDirName = dir.getAbsolutePath() + File.separator + UUID.randomUUID().toString();
if (mkdir(uniqDirName)) {
return new File(uniqDirName);
}
}
}
return null;
throw new IOException("the tmp dir " + dirName + " does not exist");
}
@Override
@ -217,6 +230,13 @@ public class JavaStorageLayer implements StorageLayer {
}
}
public boolean isWorldReadable(File file) throws IOException {
Set<PosixFilePermission> permissions;
permissions = Files.getPosixFilePermissions(
Paths.get(file.getAbsolutePath()));
return permissions.contains(PosixFilePermission.OTHERS_READ);
}
private List<String> listDirPaths(String path) {
String[] dirNames = path.split("/");
List<String> dirPaths = new ArrayList<String>();

View File

@ -42,7 +42,7 @@ public interface StorageLayer extends Manager {
*/
long getSize(String path);
File createUniqDir();
File createUniqDir() throws IOException;
/**
* Is this path a directory?