提交 2485f921 编写于 作者: E Eric Blake

storage: refactor metadata lookup

Prior to this patch, we had the callchains:
external users
  \-> virStorageFileGetMetadataFromFD
      \-> virStorageFileGetMetadataFromBuf
virStorageFileGetMetadataRecurse
  \-> virStorageFileGetMetadataFromFD
      \-> virStorageFileGetMetadataFromBuf

However, a future patch wants to add an additional parameter to
the bottom of the chain, for use by virStorageFileGetMetadataRecurse,
without affecting existing external callers.  Since there is only a
single caller of the internal function, we can repurpose it to fit
our needs, with this patch giving us:

external users
  \-> virStorageFileGetMetadataFromFD
      \-> virStorageFileGetMetadataInternal
virStorageFileGetMetadataRecurse /
  \-> virStorageFileGetMetadataInternal

* src/util/virstoragefile.c (virStorageFileGetMetadataFromFD):
Move most of the guts...
(virStorageFileGetMetadataFromBuf): ...here, and rename...
(virStorageFileGetMetadataInternal): ...to this.
(virStorageFileGetMetadataRecurse): Use internal helper.
上级 b7df4f92
...@@ -658,22 +658,63 @@ cleanup: ...@@ -658,22 +658,63 @@ cleanup:
static virStorageFileMetadataPtr static virStorageFileMetadataPtr
virStorageFileGetMetadataFromBuf(int format, virStorageFileGetMetadataInternal(const char *path,
const char *path, int fd,
unsigned char *buf, int format)
size_t len,
virStorageFileMetadata *meta)
{ {
virStorageFileMetadata *meta = NULL;
unsigned char *buf = NULL;
ssize_t len = STORAGE_MAX_HEAD;
virStorageFileMetadata *ret = NULL; virStorageFileMetadata *ret = NULL;
struct stat sb;
VIR_DEBUG("path=%s, fd=%d, format=%d", path, fd, format);
VIR_DEBUG("path=%s format=%d", path, format); if (VIR_ALLOC(meta) < 0) {
virReportOOMError();
return NULL;
}
if (fstat(fd, &sb) < 0) {
virReportSystemError(errno,
_("cannot stat file '%s'"),
path);
goto cleanup;
}
/* No header to probe for directories, but also no backing file */
if (S_ISDIR(sb.st_mode))
return meta;
if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
virReportSystemError(errno, _("cannot seek to start of '%s'"), path);
goto cleanup;
}
if (VIR_ALLOC_N(buf, len) < 0) {
virReportOOMError();
goto cleanup;
}
if ((len = read(fd, buf, len)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"), path);
goto cleanup;
}
if (format == VIR_STORAGE_FILE_AUTO)
format = virStorageFileProbeFormatFromBuf(path, buf, len);
if (format <= VIR_STORAGE_FILE_NONE ||
format >= VIR_STORAGE_FILE_LAST) {
virReportSystemError(EINVAL, _("unknown storage file format %d"),
format);
goto cleanup;
}
/* XXX we should consider moving virStorageBackendUpdateVolInfo /* XXX we should consider moving virStorageBackendUpdateVolInfo
* code into this method, for non-magic files * code into this method, for non-magic files
*/ */
if (format <= VIR_STORAGE_FILE_NONE || if (!fileTypeInfo[format].magic)
format >= VIR_STORAGE_FILE_LAST ||
!fileTypeInfo[format].magic)
goto done; goto done;
/* Optionally extract capacity from file */ /* Optionally extract capacity from file */
...@@ -747,7 +788,11 @@ virStorageFileGetMetadataFromBuf(int format, ...@@ -747,7 +788,11 @@ virStorageFileGetMetadataFromBuf(int format,
done: done:
ret = meta; ret = meta;
meta = NULL;
cleanup: cleanup:
virStorageFileFreeMetadata(meta);
VIR_FREE(buf);
return ret; return ret;
} }
...@@ -861,62 +906,7 @@ virStorageFileGetMetadataFromFD(const char *path, ...@@ -861,62 +906,7 @@ virStorageFileGetMetadataFromFD(const char *path,
int fd, int fd,
int format) int format)
{ {
virStorageFileMetadata *meta = NULL; return virStorageFileGetMetadataInternal(path, fd, format);
unsigned char *buf = NULL;
ssize_t len = STORAGE_MAX_HEAD;
virStorageFileMetadata *ret = NULL;
struct stat sb;
if (VIR_ALLOC(meta) < 0) {
virReportOOMError();
return NULL;
}
if (fstat(fd, &sb) < 0) {
virReportSystemError(errno,
_("cannot stat file '%s'"),
path);
goto cleanup;
}
/* No header to probe for directories, but also no backing file */
if (S_ISDIR(sb.st_mode))
return meta;
if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
virReportSystemError(errno, _("cannot seek to start of '%s'"), path);
goto cleanup;
}
if (VIR_ALLOC_N(buf, len) < 0) {
virReportOOMError();
goto cleanup;
}
if ((len = read(fd, buf, len)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"), path);
goto cleanup;
}
if (format == VIR_STORAGE_FILE_AUTO)
format = virStorageFileProbeFormatFromBuf(path, buf, len);
if (format <= VIR_STORAGE_FILE_NONE ||
format >= VIR_STORAGE_FILE_LAST) {
virReportSystemError(EINVAL, _("unknown storage file format %d"),
format);
goto cleanup;
}
if (!virStorageFileGetMetadataFromBuf(format, path, buf, len, meta))
goto cleanup;
ret = meta;
meta = NULL;
cleanup:
virStorageFileFreeMetadata(meta);
VIR_FREE(buf);
return ret;
} }
/* Recursive workhorse for virStorageFileGetMetadata. */ /* Recursive workhorse for virStorageFileGetMetadata. */
...@@ -945,7 +935,7 @@ virStorageFileGetMetadataRecurse(const char *path, int format, ...@@ -945,7 +935,7 @@ virStorageFileGetMetadataRecurse(const char *path, int format,
return NULL; return NULL;
} }
ret = virStorageFileGetMetadataFromFD(path, fd, format); ret = virStorageFileGetMetadataInternal(path, fd, format);
if (VIR_CLOSE(fd) < 0) if (VIR_CLOSE(fd) < 0)
VIR_WARN("could not close file %s", path); VIR_WARN("could not close file %s", path);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册