mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-08-28 11:31:16 +00:00
util: factor out reading file into preallocated buffer
Signed-off-by: Nikolay Shirokovskiy <nshirokovskiy@virtuozzo.com>
This commit is contained in:
parent
600977e293
commit
3506ad7f0a
@ -1512,6 +1512,7 @@ virFileOpenTty;
|
|||||||
virFilePrintf;
|
virFilePrintf;
|
||||||
virFileReadAll;
|
virFileReadAll;
|
||||||
virFileReadAllQuiet;
|
virFileReadAllQuiet;
|
||||||
|
virFileReadBufQuiet;
|
||||||
virFileReadHeaderFD;
|
virFileReadHeaderFD;
|
||||||
virFileReadLimFD;
|
virFileReadLimFD;
|
||||||
virFileRelLinkPointsTo;
|
virFileRelLinkPointsTo;
|
||||||
|
@ -1401,6 +1401,30 @@ virFileReadAllQuiet(const char *path, int maxlen, char **buf)
|
|||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Read @file into preallocated buffer @buf of size @len.
|
||||||
|
* Return value is -errno in case of errors and size
|
||||||
|
* of data read (no trailing zero) in case of success.
|
||||||
|
* If there is more data then @len - 1 then data will be
|
||||||
|
* truncated. */
|
||||||
|
int
|
||||||
|
virFileReadBufQuiet(const char *file, char *buf, int len)
|
||||||
|
{
|
||||||
|
int fd;
|
||||||
|
ssize_t sz;
|
||||||
|
|
||||||
|
fd = open(file, O_RDONLY);
|
||||||
|
if (fd < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
sz = saferead(fd, buf, len - 1);
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
if (sz < 0)
|
||||||
|
return -errno;
|
||||||
|
|
||||||
|
buf[sz] = '\0';
|
||||||
|
return sz;
|
||||||
|
}
|
||||||
|
|
||||||
/* Truncate @path and write @str to it. If @mode is 0, ensure that
|
/* Truncate @path and write @str to it. If @mode is 0, ensure that
|
||||||
@path exists; otherwise, use @mode if @path must be created.
|
@path exists; otherwise, use @mode if @path must be created.
|
||||||
Return 0 for success, nonzero for failure.
|
Return 0 for success, nonzero for failure.
|
||||||
|
@ -131,6 +131,8 @@ int virFileReadAll(const char *path, int maxlen, char **buf)
|
|||||||
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
|
||||||
int virFileReadAllQuiet(const char *path, int maxlen, char **buf)
|
int virFileReadAllQuiet(const char *path, int maxlen, char **buf)
|
||||||
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
|
||||||
|
int virFileReadBufQuiet(const char *file, char *buf, int len)
|
||||||
|
ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2);
|
||||||
|
|
||||||
int virFileWriteStr(const char *path, const char *str, mode_t mode)
|
int virFileWriteStr(const char *path, const char *str, mode_t mode)
|
||||||
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_RETURN_CHECK;
|
||||||
|
@ -231,15 +231,8 @@ getDMISystemUUID(char *uuid, int len)
|
|||||||
};
|
};
|
||||||
|
|
||||||
while (paths[i]) {
|
while (paths[i]) {
|
||||||
int fd = open(paths[i], O_RDONLY);
|
if (virFileReadBufQuiet(paths[i], uuid, len) == len - 1)
|
||||||
if (fd >= 0) {
|
return 0;
|
||||||
if (saferead(fd, uuid, len - 1) == len - 1) {
|
|
||||||
uuid[len - 1] = '\0';
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
VIR_FORCE_CLOSE(fd);
|
|
||||||
}
|
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user