Skip to content

Commit bec48c6

Browse files
bwswyadvr
authored andcommitted
CLOUDSTACK-10140: Fix for when template is created from snapshot template.properties are corrupted (#2322)
Fix for when template is created from snapshot template.properties are corrupted
1 parent f506a99 commit bec48c6

2 files changed

Lines changed: 38 additions & 23 deletions

File tree

core/src/com/cloud/storage/template/TemplateLocation.java

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.ArrayList;
2727
import java.util.Iterator;
2828
import java.util.Properties;
29+
import java.util.Arrays;
2930

3031
import org.apache.log4j.Logger;
3132

@@ -81,12 +82,12 @@ public boolean purge() {
8182
boolean purged = true;
8283
String[] files = _storage.listFiles(_templatePath);
8384
for (String file : files) {
84-
boolean r = _storage.delete(file);
85-
if (!r) {
85+
boolean isRemoved = _storage.delete(file);
86+
if (!isRemoved) {
8687
purged = false;
8788
}
8889
if (s_logger.isDebugEnabled()) {
89-
s_logger.debug((r ? "R" : "Unable to r") + "emove " + file);
90+
s_logger.debug((isRemoved ? "Removed " : "Unable to remove") + file);
9091
}
9192
}
9293

@@ -97,43 +98,60 @@ public boolean load() throws IOException {
9798
try (FileInputStream strm = new FileInputStream(_file);) {
9899
_props.load(strm);
99100
} catch (IOException e) {
100-
s_logger.warn("Unable to load the template properties", e);
101+
s_logger.warn("Unable to load the template properties for '" + _file + "': ", e);
101102
}
102103

103104
for (ImageFormat format : ImageFormat.values()) {
104-
String ext = _props.getProperty(format.getFileExtension());
105+
String currentExtension = format.getFileExtension();
106+
String ext = _props.getProperty(currentExtension);
105107
if (ext != null) {
108+
if (s_logger.isDebugEnabled()) {
109+
s_logger.debug("File extension '" + currentExtension + "' was found in '" + _file + "'.");
110+
}
106111
FormatInfo info = new FormatInfo();
107112
info.format = format;
108-
info.filename = _props.getProperty(format.getFileExtension() + ".filename");
113+
info.filename = _props.getProperty(currentExtension + ".filename");
109114
if (info.filename == null) {
115+
if (s_logger.isDebugEnabled()) {
116+
s_logger.debug("Property '" + currentExtension + ".filename' was not found in '" + _file + "'. Current format is ignored.");
117+
}
110118
continue;
111119
}
112-
info.size = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".size"), -1);
120+
if (s_logger.isDebugEnabled()) {
121+
s_logger.debug("Property '" + currentExtension + ".filename' was found in '" + _file + "'. Current format will be parsed.");
122+
}
123+
info.size = NumbersUtil.parseLong(_props.getProperty(currentExtension + ".size"), -1);
113124
_props.setProperty("physicalSize", Long.toString(info.size));
114-
info.virtualSize = NumbersUtil.parseLong(_props.getProperty(format.getFileExtension() + ".virtualsize"), -1);
125+
info.virtualSize = NumbersUtil.parseLong(_props.getProperty(currentExtension + ".virtualsize"), -1);
115126
_formats.add(info);
116127

117128
if (!checkFormatValidity(info)) {
118129
_isCorrupted = true;
119130
s_logger.warn("Cleaning up inconsistent information for " + format);
120131
}
132+
} else {
133+
if (s_logger.isDebugEnabled()) {
134+
s_logger.debug("Format extension '" + currentExtension + "' wasn't found in '" + _file + "'.");
135+
}
121136
}
122137
}
123138

124139
if (_props.getProperty("uniquename") == null || _props.getProperty("virtualsize") == null) {
140+
if (s_logger.isDebugEnabled()) {
141+
s_logger.debug("Property 'uniquename' or 'virtualsize' weren't found in '" + _file + "'. Loading failed.");
142+
}
125143
return false;
126144
}
127-
128145
return (_formats.size() > 0);
129146
}
130147

131148
public boolean save() {
132149
for (FormatInfo info : _formats) {
133-
_props.setProperty(info.format.getFileExtension(), "true");
134-
_props.setProperty(info.format.getFileExtension() + ".filename", info.filename);
135-
_props.setProperty(info.format.getFileExtension() + ".size", Long.toString(info.size));
136-
_props.setProperty(info.format.getFileExtension() + ".virtualsize", Long.toString(info.virtualSize));
150+
String formatExtension = info.format.getFileExtension();
151+
_props.setProperty(formatExtension, "true");
152+
_props.setProperty(formatExtension + ".filename", info.filename);
153+
_props.setProperty(formatExtension + ".size", Long.toString(info.size));
154+
_props.setProperty(formatExtension + ".virtualsize", Long.toString(info.virtualSize));
137155
}
138156
try (FileOutputStream strm = new FileOutputStream(_file);) {
139157
_props.store(strm, "");
@@ -205,10 +223,11 @@ protected FormatInfo deleteFormat(ImageFormat format) {
205223
FormatInfo info = it.next();
206224
if (info.format == format) {
207225
it.remove();
208-
_props.remove(format.getFileExtension());
209-
_props.remove(format.getFileExtension() + ".filename");
210-
_props.remove(format.getFileExtension() + ".size");
211-
_props.remove(format.getFileExtension() + ".virtualsize");
226+
String formatExtension = format.getFileExtension();
227+
_props.remove(formatExtension);
228+
for(String propertySuffix : Arrays.asList("filename","size","virtualsize")) {
229+
_props.remove(formatExtension + "." + propertySuffix);
230+
}
212231
return info;
213232
}
214233
}

services/secondary-storage/server/src/org/apache/cloudstack/storage/resource/NfsSecondaryStorageResource.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -521,12 +521,8 @@ protected Answer copySnapshotToTemplateFromNfsToNfs(CopyCommand cmd, SnapshotObj
521521
bufferWriter.write("uniquename=" + destData.getName());
522522
bufferWriter.write("\n");
523523
bufferWriter.write("filename=" + fileName);
524-
bufferWriter.write("\n");
525-
long size = _storage.getSize(destFileFullPath);
526-
bufferWriter.write("size=" + size);
527-
bufferWriter.close();
528-
writer.close();
529-
524+
}
525+
try {
530526
/**
531527
* Snapshots might be in either QCOW2 or RAW image format
532528
*

0 commit comments

Comments
 (0)