diff --git a/src/util/virstoragefile.c b/src/util/virstoragefile.c index bc12cea14ff255e8633f461d72284ca7baf95020..b7d95f99ad433997b6c353089f05a0f4e48352cf 100644 --- a/src/util/virstoragefile.c +++ b/src/util/virstoragefile.c @@ -1544,48 +1544,40 @@ virStorageFileChainLookup(virStorageFileMetadataPtr chain, const char **parent) { const char *start = chain->canonPath; - virStorageFileMetadataPtr owner; const char *tmp; + const char *parentDir = "."; + bool nameIsFile = virStorageIsFile(name); if (!parent) parent = &tmp; *parent = NULL; - if (name ? STREQ(start, name) || virFileLinkPointsTo(start, name) : - !chain->backingStore) { - if (meta) - *meta = chain; - return start; - } - - owner = chain; - *parent = start; - while (owner) { - if (!owner->backingStore) - goto error; + while (chain) { if (!name) { - if (!owner->backingMeta || - !owner->backingMeta->backingStore) + if (!chain->backingMeta) break; - } else if (STREQ_NULLABLE(name, owner->backingStoreRaw) || - STREQ(name, owner->backingStore)) { - break; - } else if (virStorageIsFile(owner->backingStore)) { - int result = virFileRelLinkPointsTo(owner->directory, name, - owner->backingStore); - if (result < 0) - goto error; - if (result > 0) + } else { + if (STREQ(name, chain->path)) break; + if (nameIsFile && (chain->type == VIR_STORAGE_TYPE_FILE || + chain->type == VIR_STORAGE_TYPE_BLOCK)) { + int result = virFileRelLinkPointsTo(parentDir, name, + chain->canonPath); + if (result < 0) + goto error; + if (result > 0) + break; + } } - *parent = owner->backingStore; - owner = owner->backingMeta; + *parent = chain->canonPath; + parentDir = chain->relDir; + chain = chain->backingMeta; } - if (!owner) + if (!chain) goto error; if (meta) - *meta = owner->backingMeta; - return owner->backingStore; + *meta = chain; + return chain->canonPath; error: if (name) diff --git a/tests/virstoragetest.c b/tests/virstoragetest.c index 5db3cde122b4fdc7b8214f2c92f5f695a5fe5680..4798517cb29b1d106b663d693f13d17042107266 100644 --- a/tests/virstoragetest.c +++ b/tests/virstoragetest.c @@ -935,8 +935,7 @@ mymain(void) TEST_LOOKUP(19, abswrap, chain->canonPath, chain, NULL); TEST_LOOKUP(20, "../qcow2", chain->backingStore, chain->backingMeta, chain->canonPath); - TEST_LOOKUP(21, "qcow2", chain->backingStore, chain->backingMeta, - chain->canonPath); + TEST_LOOKUP(21, "qcow2", NULL, NULL, NULL); TEST_LOOKUP(22, absqcow2, chain->backingStore, chain->backingMeta, chain->canonPath); TEST_LOOKUP(23, "raw", chain->backingMeta->backingStore,