virFileOpenTty;
virFilePrintf;
virFileReadAll;
+virFileReadHeaderFD;
virFileReadLimFD;
virFileResolveAllLinks;
virFileResolveLink;
return NULL;
}
+
+/* A wrapper around saferead_lim that merely stops reading at the
+ * specified maximum size. */
+int
+virFileReadHeaderFD(int fd, int maxlen, char **buf)
+{
+ size_t len;
+ char *s;
+
+ if (maxlen <= 0) {
+ errno = EINVAL;
+ return -1;
+ }
+ s = saferead_lim(fd, maxlen, &len);
+ if (s == NULL)
+ return -1;
+ *buf = s;
+ return len;
+}
+
+
/* A wrapper around saferead_lim that maps a failure due to
exceeding the maximum size limitation to EOVERFLOW. */
int
int virFileDeleteTree(const char *dir);
-int virFileReadLimFD(int fd, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
-
-int virFileReadAll(const char *path, int maxlen, char **buf) ATTRIBUTE_RETURN_CHECK;
+int virFileReadHeaderFD(int fd, int maxlen, char **buf)
+ ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
+int virFileReadLimFD(int fd, int maxlen, char **buf)
+ ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
+int virFileReadAll(const char *path, int maxlen, char **buf)
+ ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
int virFileWriteStr(const char *path, const char *str, mode_t mode)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
goto cleanup;
}
- if (VIR_ALLOC_N(buf, len) < 0)
- goto cleanup;
-
- if ((len = read(fd, buf, len)) < 0) {
+ if ((len = virFileReadHeaderFD(fd, len, &buf)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"), path);
goto cleanup;
}
return VIR_STORAGE_FILE_DIR;
}
- if (VIR_ALLOC_N(head, len) < 0)
- return -1;
-
if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
virReportSystemError(errno, _("cannot set to start of '%s'"), path);
goto cleanup;
}
- if ((len = read(fd, head, len)) < 0) {
+ if ((len = virFileReadHeaderFD(fd, len, &head)) < 0) {
virReportSystemError(errno, _("cannot read header '%s'"), path);
goto cleanup;
}