mirror of
				https://github.com/apache/cloudstack.git
				synced 2025-10-26 08:42:29 +01:00 
			
		
		
		
	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:
		
							parent
							
								
									a000361238
								
							
						
					
					
						commit
						1ed828b2a1
					
				| @ -21,15 +21,21 @@ package com.cloud.storage; | |||||||
| 
 | 
 | ||||||
| import java.io.File; | import java.io.File; | ||||||
| import java.io.IOException; | 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.ArrayList; | ||||||
| import java.util.List; | import java.util.List; | ||||||
| import java.util.Map; | import java.util.Map; | ||||||
|  | import java.util.Set; | ||||||
| import java.util.UUID; | import java.util.UUID; | ||||||
| 
 | 
 | ||||||
| import javax.naming.ConfigurationException; | import javax.naming.ConfigurationException; | ||||||
|  | import org.apache.log4j.Logger; | ||||||
| 
 | 
 | ||||||
| public class JavaStorageLayer implements StorageLayer { | 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; |     String _name; | ||||||
|     boolean _makeWorldWriteable = true; |     boolean _makeWorldWriteable = true; | ||||||
| 
 | 
 | ||||||
| @ -178,18 +184,25 @@ public class JavaStorageLayer implements StorageLayer { | |||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @Override | ||||||
|     public File createUniqDir() { |     public File createUniqDir() throws IOException { | ||||||
|         String dirName = System.getProperty("java.io.tmpdir"); |         String dirName = System.getProperty("java.io.tmpdir"); | ||||||
|         if (dirName != null) { |         if (dirName != null) { | ||||||
|             File dir = new File(dirName); |             File dir = new File(dirName); | ||||||
|             if (dir.exists()) { |             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(); |                 String uniqDirName = dir.getAbsolutePath() + File.separator + UUID.randomUUID().toString(); | ||||||
|                 if (mkdir(uniqDirName)) { |                 if (mkdir(uniqDirName)) { | ||||||
|                     return new File(uniqDirName); |                     return new File(uniqDirName); | ||||||
|                 } |                 } | ||||||
|             } |             } | ||||||
|         } |         } | ||||||
|         return null; |         throw new IOException("the tmp dir " + dirName + " does not exist"); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     @Override |     @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) { |     private List<String> listDirPaths(String path) { | ||||||
|         String[] dirNames = path.split("/"); |         String[] dirNames = path.split("/"); | ||||||
|         List<String> dirPaths = new ArrayList<String>(); |         List<String> dirPaths = new ArrayList<String>(); | ||||||
|  | |||||||
| @ -42,7 +42,7 @@ public interface StorageLayer extends Manager { | |||||||
|      */ |      */ | ||||||
|     long getSize(String path); |     long getSize(String path); | ||||||
| 
 | 
 | ||||||
|     File createUniqDir(); |     File createUniqDir() throws IOException; | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Is this path a directory? |      * Is this path a directory? | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user