Skip to content

Commit f2d0e83

Browse files
author
gitlab
committed
Merge branch 'ZSV-11771@@2' into 'feature-zsv-5.0.0-vm-support-vtpm-and-secuceboot'
<fix>[metadata]: extractOldPrefix use lastIndexOf See merge request zstackio/zstack!9560
2 parents 167977d + 4f8ecd1 commit f2d0e83

1 file changed

Lines changed: 16 additions & 7 deletions

File tree

header/src/main/java/org/zstack/header/vm/metadata/VmInstanceMetadataConstants.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,28 @@ private VmInstanceMetadataConstants() {
1212
"/rootVolumes/", "/dataVolumes/", "/volumeSnapshots/", "/memory/"
1313
};
1414

15+
/**
16+
* Extract the storage mount-point prefix from a volume install path.
17+
* <pre>
18+
* /mnt/ps/rootVolumes/acct-xxx/vol-xxx/xxx.qcow2 → /mnt/ps/
19+
* /local_ps/dataVolumes/acct-xxx/vol-xxx/xxx.qcow2 → /local_ps/
20+
* </pre>
21+
* Uses {@code lastIndexOf} so that mount paths which themselves contain a
22+
* marker string (e.g. {@code /rootVolumes/rootVolumes/…}) are handled
23+
* correctly — the last occurrence is always the real subdirectory boundary.
24+
*/
1525
public static String extractOldPrefix(String path) {
1626
if (path == null || !path.startsWith("/")) {
1727
return null;
1828
}
19-
int earliest = Integer.MAX_VALUE;
20-
String foundMarker = null;
29+
30+
int latest = -1;
2131
for (String marker : STORAGE_PATH_MARKERS) {
22-
int idx = path.indexOf(marker);
23-
if (idx >= 0 && idx < earliest) {
24-
earliest = idx;
25-
foundMarker = marker;
32+
int idx = path.lastIndexOf(marker);
33+
if (idx > latest) {
34+
latest = idx;
2635
}
2736
}
28-
return foundMarker == null ? null : path.substring(0, earliest + foundMarker.length());
37+
return latest < 0 ? null : path.substring(0, latest + 1);
2938
}
3039
}

0 commit comments

Comments
 (0)