1616// under the License.
1717package org .apache .cloudstack .storage .template ;
1818
19+ import static com .cloud .utils .NumbersUtil .toHumanReadableSize ;
20+
1921import java .io .File ;
22+ import java .io .IOException ;
2023import java .net .URI ;
2124import java .net .URISyntaxException ;
2225import java .nio .file .Path ;
3033
3134import javax .naming .ConfigurationException ;
3235
33- import com .cloud .agent .api .Answer ;
34-
3536import org .apache .cloudstack .storage .resource .SecondaryStorageResource ;
37+ import org .apache .commons .lang3 .StringUtils ;
3638
39+ import com .cloud .agent .api .Answer ;
3740import com .cloud .agent .api .storage .CreateEntityDownloadURLAnswer ;
3841import com .cloud .agent .api .storage .CreateEntityDownloadURLCommand ;
3942import com .cloud .agent .api .storage .DeleteEntityDownloadURLCommand ;
4851import com .cloud .storage .template .TemplateUploader ;
4952import com .cloud .storage .template .TemplateUploader .Status ;
5053import com .cloud .storage .template .TemplateUploader .UploadCompleteCallback ;
54+ import com .cloud .utils .FileUtil ;
5155import com .cloud .utils .NumbersUtil ;
56+ import com .cloud .utils .UuidUtils ;
5257import com .cloud .utils .component .ManagerBase ;
5358import com .cloud .utils .exception .CloudRuntimeException ;
5459import com .cloud .utils .script .Script ;
5560
56- import static com .cloud .utils .NumbersUtil .toHumanReadableSize ;
57-
5861public class UploadManagerImpl extends ManagerBase implements UploadManager {
5962
63+ protected static final String BASE_EXTRACT_DIR = "/var/www/html/userdata/" ;
64+
6065 public class Completion implements UploadCompleteCallback {
6166 private final String jobId ;
6267
@@ -266,7 +271,7 @@ public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(CreateEntityDo
266271 return new CreateEntityDownloadURLAnswer (errorString , CreateEntityDownloadURLAnswer .RESULT_FAILURE );
267272 }
268273 // Create the directory structure so that its visible under apache server root
269- String extractDir = "/var/www/html/userdata/" ;
274+ String extractDir = BASE_EXTRACT_DIR ;
270275 extractDir = extractDir + cmd .getFilepathInExtractURL () + File .separator ;
271276 Script command = new Script ("/bin/su" , logger );
272277 command .add ("-s" );
@@ -330,12 +335,15 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
330335 String extractUrl = cmd .getExtractUrl ();
331336 String result ;
332337 if (extractUrl != null ) {
333- command .add ("unlink /var/www/html/userdata/" + extractUrl .substring (extractUrl .lastIndexOf (File .separator ) + 1 ));
338+ String linkPath = extractUrl .substring (extractUrl .lastIndexOf (File .separator ) + 1 );
339+ command .add ("unlink " + BASE_EXTRACT_DIR + linkPath );
334340 result = command .execute ();
335341 if (result != null ) {
336342 // FIXME - Ideally should bail out if you can't delete symlink. Not doing it right now.
337343 // This is because the ssvm might already be destroyed and the symlinks do not exist.
338344 logger .warn ("Error in deleting symlink :" + result );
345+ } else {
346+ deleteEntitySymlinkRootPathIfNeeded (cmd , linkPath );
339347 }
340348 }
341349
@@ -356,6 +364,30 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
356364 return new Answer (cmd , true , "" );
357365 }
358366
367+ protected void deleteEntitySymlinkRootPathIfNeeded (DeleteEntityDownloadURLCommand cmd , String linkPath ) {
368+ if (StringUtils .isEmpty (linkPath )) {
369+ return ;
370+ }
371+ String [] parts = linkPath .split ("/" );
372+ if (parts .length == 0 ) {
373+ return ;
374+ }
375+ String rootDir = parts [0 ];
376+ if (StringUtils .isEmpty (rootDir ) || !UuidUtils .isUuid (rootDir )) {
377+ return ;
378+ }
379+ logger .info ("Deleting symlink root directory: {} for {}" , rootDir , cmd .getExtractUrl ());
380+ Path rootDirPath = Path .of (BASE_EXTRACT_DIR + rootDir );
381+ String failMsg = "Failed to delete symlink root directory: {} for {}" ;
382+ try {
383+ if (!FileUtil .deleteRecursively (rootDirPath )) {
384+ logger .warn (failMsg , rootDir , cmd .getExtractUrl ());
385+ }
386+ } catch (IOException e ) {
387+ logger .warn (failMsg , rootDir , cmd .getExtractUrl (), e );
388+ }
389+ }
390+
359391 private String getInstallPath (String jobId ) {
360392 // TODO Auto-generated method stub
361393 return null ;
0 commit comments