Skip to content

Commit 4f8ecd1

Browse files
committed
<fix>[metadata]: extractOldPrefix use lastIndexOf
Resolves: ZSV-11771 Change-Id: I801a5f4d8ba84e47a642be4043043af88979dd4e
1 parent 912b02f commit 4f8ecd1

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)