Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.net.URISyntaxException;
import java.util.Date;

import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.cloudstack.utils.imagestore.ImageStoreUtil;
import org.apache.commons.httpclient.Credentials;
import org.apache.commons.httpclient.Header;
Expand Down Expand Up @@ -63,7 +64,7 @@ public class HttpTemplateDownloader extends ManagedContextRunnable implements Te
private String downloadUrl;
private String toFile;
public TemplateDownloader.Status status;
public String errorString = " ";
private String errorString = null;
private long remoteSize = 0;
public long downloadTime = 0;
public long totalBytes;
Expand Down Expand Up @@ -218,7 +219,10 @@ public long download(boolean resume, DownloadCompleteCallback callback) {
errorString = hte.getMessage();
Comment thread
Spaceman1984 marked this conversation as resolved.
} catch (IOException ioe) {
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; //probably a file write error?
errorString = ioe.getMessage();
// Let's not overwrite the original error message.
if (errorString == null) {
errorString = ioe.getMessage();
}
} finally {
if (status == Status.UNRECOVERABLE_ERROR && file.exists() && !file.isDirectory()) {
file.delete();
Expand All @@ -243,7 +247,6 @@ private boolean copyBytes(File file, InputStream in, RandomAccessFile out) throw
offset = writeBlock(bytes, out, block, offset);
if (!verifyFormat.isVerifiedFormat() && (offset >= 1048576 || offset >= remoteSize)) { //let's check format after we get 1MB or full file
verifyFormat.invoke();
if (verifyFormat.isInvalid()) return true;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Spaceman1984 cc @nvazquez why are we not validating format now?

Copy link
Copy Markdown
Contributor Author

@Spaceman1984 Spaceman1984 Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rhtyd If validation fails, an exception is thrown, so there is no setting of an invalid state that has to be checked. Validation happens inside verifyFormat.invoke();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In short, prior to these changes the invoke() method only set the boolean variable retrieved in case of valid/invalid. After these changes, if it is invalid invoke() throws an exception, so no need to check isInvalid() in this line

}
} else {
done = true;
Expand Down Expand Up @@ -443,7 +446,7 @@ public boolean isResume() {

@Override
public String getDownloadError() {
return errorString;
return errorString == null ? " " : errorString;
}

@Override
Expand Down Expand Up @@ -495,7 +498,6 @@ public ResourceType getResourceType() {
}

private class VerifyFormat {
private boolean invalidFormat;
private File file;
private boolean verifiedFormat;

Expand All @@ -504,10 +506,6 @@ public VerifyFormat(File file) {
this.verifiedFormat = false;
}

boolean isInvalid() {
return invalidFormat;
}

public boolean isVerifiedFormat() {
return verifiedFormat;
}
Expand All @@ -529,11 +527,10 @@ public VerifyFormat invoke() {
}
status = Status.UNRECOVERABLE_ERROR;
errorString = "Template content is unsupported, or mismatch between selected format and template content. Found : " + unsupportedFormat;
invalidFormat = true;
throw new CloudRuntimeException(errorString);
} else {
s_logger.debug("Verified format of downloading file " + file.getAbsolutePath() + " is supported");
verifiedFormat = true;
invalidFormat = false;
}
return this;
}
Expand Down