Skip to content

Commit e4e7db7

Browse files
committed
fix
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent eef3e49 commit e4e7db7

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

services/secondary-storage/server/src/main/java/org/apache/cloudstack/storage/template/UploadManagerImpl.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060

6161
public class UploadManagerImpl extends ManagerBase implements UploadManager {
6262

63-
protected static final String BASE_EXTRACT_DIR = "/var/www/html/userdata/";
63+
protected static final String EXTRACT_USERDATA_DIR = "userdata";
64+
protected static final String BASE_EXTRACT_PATH = String.format("/var/www/html/%s/", EXTRACT_USERDATA_DIR);
6465

6566
public class Completion implements UploadCompleteCallback {
6667
private final String jobId;
@@ -271,7 +272,7 @@ public CreateEntityDownloadURLAnswer handleCreateEntityURLCommand(CreateEntityDo
271272
return new CreateEntityDownloadURLAnswer(errorString, CreateEntityDownloadURLAnswer.RESULT_FAILURE);
272273
}
273274
// Create the directory structure so that its visible under apache server root
274-
String extractDir = BASE_EXTRACT_DIR;
275+
String extractDir = BASE_EXTRACT_PATH;
275276
extractDir = extractDir + cmd.getFilepathInExtractURL() + File.separator;
276277
Script command = new Script("/bin/su", logger);
277278
command.add("-s");
@@ -335,15 +336,20 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
335336
String extractUrl = cmd.getExtractUrl();
336337
String result;
337338
if (extractUrl != null) {
338-
String linkPath = extractUrl.substring(extractUrl.lastIndexOf(File.separator) + 1);
339-
command.add("unlink " + BASE_EXTRACT_DIR + linkPath);
339+
URI uri = URI.create(extractUrl);
340+
String uriPath = uri.getPath();
341+
String marker = String.format("/%s/", EXTRACT_USERDATA_DIR);
342+
String linkPath = uriPath.startsWith(marker)
343+
? uriPath.substring(marker.length())
344+
: uriPath.substring(uriPath.indexOf(marker) + marker.length());
345+
command.add("unlink " + BASE_EXTRACT_PATH + linkPath);
340346
result = command.execute();
341347
if (result != null) {
342348
// FIXME - Ideally should bail out if you can't delete symlink. Not doing it right now.
343349
// This is because the ssvm might already be destroyed and the symlinks do not exist.
344350
logger.warn("Error in deleting symlink :" + result);
345351
} else {
346-
deleteEntitySymlinkRootPathIfNeeded(cmd, linkPath);
352+
deleteEntitySymlinkRootDirectoryIfNeeded(cmd, linkPath);
347353
}
348354
}
349355

@@ -364,7 +370,7 @@ public Answer handleDeleteEntityDownloadURLCommand(DeleteEntityDownloadURLComman
364370
return new Answer(cmd, true, "");
365371
}
366372

367-
protected void deleteEntitySymlinkRootPathIfNeeded(DeleteEntityDownloadURLCommand cmd, String linkPath) {
373+
protected void deleteEntitySymlinkRootDirectoryIfNeeded(DeleteEntityDownloadURLCommand cmd, String linkPath) {
368374
if (StringUtils.isEmpty(linkPath)) {
369375
return;
370376
}
@@ -377,7 +383,7 @@ protected void deleteEntitySymlinkRootPathIfNeeded(DeleteEntityDownloadURLComman
377383
return;
378384
}
379385
logger.info("Deleting symlink root directory: {} for {}", rootDir, cmd.getExtractUrl());
380-
Path rootDirPath = Path.of(BASE_EXTRACT_DIR + rootDir);
386+
Path rootDirPath = Path.of(BASE_EXTRACT_PATH + rootDir);
381387
String failMsg = "Failed to delete symlink root directory: {} for {}";
382388
try {
383389
if (!FileUtil.deleteRecursively(rootDirPath)) {

services/secondary-storage/server/src/test/java/org/apache/cloudstack/storage/template/UploadManagerImplTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,21 @@ public void tearDown() {
5858
@Test
5959
public void doesNotDeleteWhenLinkPathIsEmpty() {
6060
String emptyLinkPath = "";
61-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), emptyLinkPath);
61+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), emptyLinkPath);
6262
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
6363
}
6464

6565
@Test
6666
public void doesNotDeleteWhenRootDirIsNotUuid() {
6767
String invalidLinkPath = "invalidRootDir/file";
68-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), invalidLinkPath);
68+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), invalidLinkPath);
6969
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), never());
7070
}
7171

7272
@Test
7373
public void deletesSymlinkRootDirectoryWhenValidUuid() {
7474
String validLinkPath = "123e4567-e89b-12d3-a456-426614174000/file";
75-
uploadManager.deleteEntitySymlinkRootPathIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
75+
uploadManager.deleteEntitySymlinkRootDirectoryIfNeeded(mock(DeleteEntityDownloadURLCommand.class), validLinkPath);
7676
fileUtilMock.verify(() -> FileUtil.deleteRecursively(any(Path.class)), times(1));
7777
}
7878
}

0 commit comments

Comments
 (0)