]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9223 Properly handle case when SONAR_USER_HOME point to a symlink
authorJulien HENRY <julien.henry@sonarsource.com>
Mon, 11 Sep 2017 15:13:16 +0000 (17:13 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Fri, 15 Sep 2017 06:51:08 +0000 (08:51 +0200)
sonar-scanner-engine/src/main/java/org/sonar/scanner/bootstrap/GlobalTempFolderProvider.java
sonar-scanner-engine/src/test/java/org/sonar/scanner/bootstrap/GlobalTempFolderProviderTest.java

index 5b6de50c622284949ffa667ddb7f9d780bb45e32..8be618a3e4b09d97ba02f98ff1c216df12a02be9 100644 (file)
@@ -44,7 +44,7 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone
   private static final long CLEAN_MAX_AGE = TimeUnit.DAYS.toMillis(21);
   static final String TMP_NAME_PREFIX = ".sonartmp_";
   private boolean started = false;
-  
+
   private System2 system;
   private DefaultTempFolder tempFolder;
 
@@ -66,7 +66,6 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone
         Path home = findSonarHome(bootstrapProps);
         workingPath = home.resolve(workingPath).normalize();
       }
-
       try {
         cleanTempFolders(workingPath);
       } catch (IOException e) {
@@ -80,7 +79,11 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone
 
   private static Path createTempFolder(Path workingPath) {
     try {
-      Files.createDirectories(workingPath);
+      Path realPath = workingPath;
+      if (Files.isSymbolicLink(realPath)) {
+        realPath = realPath.toRealPath();
+      }
+      Files.createDirectories(realPath);
     } catch (IOException e) {
       throw new IllegalStateException("Failed to create working path: " + workingPath, e);
     }
@@ -160,7 +163,7 @@ public class GlobalTempFolderProvider extends ProviderAdapter implements Compone
 
   @Override
   public void dispose(PicoContainer container) {
-  //nothing to do
+    // nothing to do
   }
 
   @Override
index 5585545241b545b18bb5f77ca6eacd5de1f7e270..4e99cdb8b4a03272c6b218287764f607f93534d2 100644 (file)
@@ -48,6 +48,7 @@ public class GlobalTempFolderProviderTest {
   @Test
   public void createTempFolderProps() throws Exception {
     File workingDir = temp.newFolder();
+    workingDir.delete();
 
     TempFolder tempFolder = tempFolderProvider.provide(new GlobalProperties(ImmutableMap.of(CoreProperties.GLOBAL_WORKING_DIRECTORY, workingDir.getAbsolutePath())));
     tempFolder.newDir();
@@ -127,6 +128,20 @@ public class GlobalTempFolderProviderTest {
     assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
   }
 
+  @Test
+  public void homeIsSymbolicLink() throws IOException {
+    File realSonarHome = temp.newFolder();
+    File symlink = temp.newFolder();
+    symlink.delete();
+    Files.createSymbolicLink(symlink.toPath(), realSonarHome.toPath());
+    GlobalProperties globalProperties = new GlobalProperties(ImmutableMap.of("sonar.userHome", symlink.getAbsolutePath()));
+
+    TempFolder tempFolder = tempFolderProvider.provide(globalProperties);
+    File newFile = tempFolder.newFile();
+    assertThat(newFile.getParentFile().getParentFile().getAbsolutePath()).isEqualTo(symlink.getAbsolutePath());
+    assertThat(newFile.getParentFile().getName()).startsWith(".sonartmp_");
+  }
+
   private File getCreatedTempDir(File workingDir) {
     assertThat(workingDir).isDirectory();
     assertThat(workingDir.listFiles()).hasSize(1);