Fix size issue for template created from snapshot.

This commit is contained in:
Min Chen 2013-05-13 17:45:22 -07:00
parent 0ce01e56db
commit 3424ac86ad
2 changed files with 38 additions and 33 deletions

View File

@ -35,17 +35,17 @@ import com.cloud.utils.NumbersUtil;
public class TemplateLocation {
private static final Logger s_logger = Logger.getLogger(TemplateLocation.class);
public final static String Filename = "template.properties";
StorageLayer _storage;
String _templatePath;
boolean _isCorrupted;
ResourceType _resourceType = ResourceType.TEMPLATE;
File _file;
Properties _props;
ArrayList<FormatInfo> _formats;
public TemplateLocation(StorageLayer storage, String templatePath) {
_storage = storage;
_templatePath = templatePath;
@ -63,16 +63,16 @@ public class TemplateLocation {
}
_isCorrupted = false;
}
public boolean create(long id, boolean isPublic, String uniqueName) throws IOException {
boolean result = load();
_props.setProperty("id", Long.toString(id));
_props.setProperty("public", Boolean.toString(isPublic));
_props.setProperty("uniquename", uniqueName);
return result;
}
public boolean purge() {
boolean purged = true;
String[] files = _storage.listFiles(_templatePath);
@ -85,10 +85,10 @@ public class TemplateLocation {
s_logger.debug((r ? "R" : "Unable to r") + "emove " + file);
}
}
return purged;
}
public boolean load() throws IOException {
FileInputStream strm = null;
try {
@ -102,7 +102,7 @@ public class TemplateLocation {
}
}
}
for (ImageFormat format : ImageFormat.values()) {
String ext = _props.getProperty(format.getFileExtension());
if (ext != null) {
@ -115,22 +115,22 @@ public class TemplateLocation {
info.size = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".size"), -1);
_props.setProperty("physicalSize", Long.toString(info.size));
info.virtualSize = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".virtualsize"), -1);
_formats.add(info);
_formats.add(info);
if (!checkFormatValidity(info)) {
_isCorrupted = true;
s_logger.warn("Cleaning up inconsistent information for " + format);
}
}
}
}
if (_props.getProperty("uniquename") == null || _props.getProperty("virtualsize") == null) {
return false;
}
return (_formats.size() > 0);
}
public boolean save() {
for (FormatInfo info : _formats) {
_props.setProperty(info.format.getFileExtension(), "true");
@ -152,60 +152,64 @@ public class TemplateLocation {
} catch (IOException e) {
}
}
}
}
return true;
}
public TemplateProp getTemplateInfo() {
TemplateProp tmplInfo = new TemplateProp();
TemplateProp tmplInfo = new TemplateProp();
tmplInfo.id = Long.parseLong(_props.getProperty("id"));
tmplInfo.installPath = _templatePath + File.separator + _props.getProperty("filename");
if (_resourceType == ResourceType.VOLUME){
tmplInfo.installPath = tmplInfo.installPath.substring(tmplInfo.installPath.indexOf("volumes"));
tmplInfo.installPath = tmplInfo.installPath.substring(tmplInfo.installPath.indexOf("volumes"));
}else {
tmplInfo.installPath = tmplInfo.installPath.substring(tmplInfo.installPath.indexOf("template"));
}
tmplInfo.isCorrupted = _isCorrupted;
tmplInfo.isPublic = Boolean.parseBoolean(_props.getProperty("public"));
tmplInfo.templateName = _props.getProperty("uniquename");
tmplInfo.size = Long.parseLong(_props.getProperty("virtualsize"));
tmplInfo.physicalSize = Long.parseLong(_props.getProperty("physicalSize"));
if (_props.getProperty("virtualsize") != null) {
tmplInfo.size = Long.parseLong(_props.getProperty("virtualsize"));
}
if (_props.getProperty("physicalSize") != null) {
tmplInfo.physicalSize = Long.parseLong(_props.getProperty("physicalSize"));
}
return tmplInfo;
}
public FormatInfo getFormat(ImageFormat format) {
for (FormatInfo info : _formats) {
if (info.format == format) {
return info;
}
}
return null;
}
public boolean addFormat(FormatInfo newInfo) {
deleteFormat(newInfo.format);
if (!checkFormatValidity(newInfo)) {
s_logger.warn("Format is invalid ");
return false;
}
_props.setProperty("virtualsize", Long.toString(newInfo.virtualSize));
_formats.add(newInfo);
return true;
}
public void updateVirtualSize(long virtualSize) {
_props.setProperty("virtualsize", Long.toString(virtualSize));
}
protected boolean checkFormatValidity(FormatInfo info) {
return (info.format != null && info.size > 0 && info.virtualSize > 0 && info.filename != null);
}
protected FormatInfo deleteFormat(ImageFormat format) {
Iterator<FormatInfo> it = _formats.iterator();
while (it.hasNext()) {
@ -219,7 +223,7 @@ public class TemplateLocation {
return info;
}
}
return null;
}
}

View File

@ -222,6 +222,7 @@ public class TemplateObject implements TemplateInfo {
templateStoreRef.setInstallPath(newTemplate.getPath());
templateStoreRef.setDownloadPercent(100);
templateStoreRef.setDownloadState(Status.DOWNLOADED);
templateStoreRef.setSize(newTemplate.getSize());
templateStoreDao.update(templateStoreRef.getId(), templateStoreRef);
if (this.getDataStore().getRole() == DataStoreRole.Image) {
VMTemplateVO templateVO = this.imageDao.findById(this.getId());