Skip to content

Commit affd010

Browse files
authored
templates: Fixed incorrect error message on invalid template type download (#4138)
When a template is downloaded, the first 1MB of the template is validated to determine if the template is of correct file type. On failure, the download is aborted and the input stream is set to null. This leads to a second error when the try-with-resources block tries to auto-close the stream and throws an ioexception. The "stream closed" error message is then written to the db. This PR checks if an error has been stored before setting a new error message. Fixes: #4127
1 parent 139aa13 commit affd010

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

core/src/main/java/com/cloud/storage/template/HttpTemplateDownloader.java

100644100755
Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import java.net.URISyntaxException;
2828
import java.util.Date;
2929

30+
import com.cloud.utils.exception.CloudRuntimeException;
3031
import org.apache.cloudstack.utils.imagestore.ImageStoreUtil;
3132
import org.apache.commons.httpclient.Credentials;
3233
import org.apache.commons.httpclient.Header;
@@ -63,7 +64,7 @@ public class HttpTemplateDownloader extends ManagedContextRunnable implements Te
6364
private String downloadUrl;
6465
private String toFile;
6566
public TemplateDownloader.Status status;
66-
public String errorString = " ";
67+
private String errorString = null;
6768
private long remoteSize = 0;
6869
public long downloadTime = 0;
6970
public long totalBytes;
@@ -218,7 +219,10 @@ public long download(boolean resume, DownloadCompleteCallback callback) {
218219
errorString = hte.getMessage();
219220
} catch (IOException ioe) {
220221
status = TemplateDownloader.Status.UNRECOVERABLE_ERROR; //probably a file write error?
221-
errorString = ioe.getMessage();
222+
// Let's not overwrite the original error message.
223+
if (errorString == null) {
224+
errorString = ioe.getMessage();
225+
}
222226
} finally {
223227
if (status == Status.UNRECOVERABLE_ERROR && file.exists() && !file.isDirectory()) {
224228
file.delete();
@@ -243,7 +247,6 @@ private boolean copyBytes(File file, InputStream in, RandomAccessFile out) throw
243247
offset = writeBlock(bytes, out, block, offset);
244248
if (!verifyFormat.isVerifiedFormat() && (offset >= 1048576 || offset >= remoteSize)) { //let's check format after we get 1MB or full file
245249
verifyFormat.invoke();
246-
if (verifyFormat.isInvalid()) return true;
247250
}
248251
} else {
249252
done = true;
@@ -443,7 +446,7 @@ public boolean isResume() {
443446

444447
@Override
445448
public String getDownloadError() {
446-
return errorString;
449+
return errorString == null ? " " : errorString;
447450
}
448451

449452
@Override
@@ -495,7 +498,6 @@ public ResourceType getResourceType() {
495498
}
496499

497500
private class VerifyFormat {
498-
private boolean invalidFormat;
499501
private File file;
500502
private boolean verifiedFormat;
501503

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

507-
boolean isInvalid() {
508-
return invalidFormat;
509-
}
510-
511509
public boolean isVerifiedFormat() {
512510
return verifiedFormat;
513511
}
@@ -529,11 +527,10 @@ public VerifyFormat invoke() {
529527
}
530528
status = Status.UNRECOVERABLE_ERROR;
531529
errorString = "Template content is unsupported, or mismatch between selected format and template content. Found : " + unsupportedFormat;
532-
invalidFormat = true;
530+
throw new CloudRuntimeException(errorString);
533531
} else {
534532
s_logger.debug("Verified format of downloading file " + file.getAbsolutePath() + " is supported");
535533
verifiedFormat = true;
536-
invalidFormat = false;
537534
}
538535
return this;
539536
}

0 commit comments

Comments
 (0)