提交 c4d18e8b 编写于 作者: D Daniel P. Berrangé

util: replace strerror/strerror_r with g_strerror

g_strerror is offers the safety/correctness benefits of strerror_r, with
the API design convenience of strerror.

Use of virStrerror should be eliminated through the codebase in favour
of g_strerror.

commandhelper.c is a special case as its a tiny single threaded test
program, not linked to glib, so it just uses traditional strerror().
Reviewed-by: NJán Tomko <jtomko@redhat.com>
Signed-off-by: NDaniel P. Berrangé <berrange@redhat.com>
上级 71efb59a
......@@ -79,8 +79,6 @@ snprintf
socket
stat-time
strchrnul
strerror
strerror_r-posix
strptime
strsep
strtok_r
......
......@@ -1051,6 +1051,10 @@ BAD:
<dd>The GLib macros g_autoptr and G_DEFINE_AUTOPTR_CLEANUP_FUNC
should be used to manage autoclean of virObject classes.
This matches usage with GObject classes.</dd>
<dt>virStrerror</dt>
<dd>The GLib g_strerror() function should be used instead,
which has a simpler calling convention as an added benefit.</dd>
</dl>
<h2><a id="file_handling">File handling</a></h2>
......
......@@ -1322,8 +1322,11 @@ const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen)
{
int save_errno = errno;
const char *ret;
const char *str = g_strerror(theerrno);
size_t len = strlen(str);
strerror_r(theerrno, errBuf, errBufLen);
memcpy(errBuf, str, MIN(len, errBufLen));
errBuf[errBufLen-1] = '\0';
ret = errBuf;
errno = save_errno;
return ret;
......@@ -1349,11 +1352,9 @@ void virReportSystemErrorFull(int domcode,
const char *fmt, ...)
{
int save_errno = errno;
char strerror_buf[VIR_ERROR_MAX_LENGTH];
char msgDetailBuf[VIR_ERROR_MAX_LENGTH];
const char *errnoDetail = virStrerror(theerrno, strerror_buf,
sizeof(strerror_buf));
const char *errnoDetail = g_strerror(theerrno);
const char *msg = virErrorMsg(VIR_ERR_SYSTEM_ERROR, fmt);
const char *msgDetail = NULL;
......
......@@ -193,6 +193,7 @@ void virReportOOMErrorFull(int domcode,
int virSetError(virErrorPtr newerr);
virErrorPtr virErrorCopyNew(virErrorPtr err);
void virDispatchError(virConnectPtr conn);
/* DEPRECATED: use g_strerror() directly */
const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen);
typedef int (*virErrorLogPriorityFunc)(virErrorPtr, int);
......
......@@ -636,12 +636,12 @@ static int test16(const void *unused ATTRIBUTE_UNUSED)
}
if ((fd = open(abs_builddir "/commandhelper.log",
O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
printf("Cannot open log file: %s\n", strerror(errno));
printf("Cannot open log file: %s\n", g_strerror(errno));
goto cleanup;
}
virCommandWriteArgLog(cmd, fd);
if (VIR_CLOSE(fd) < 0) {
printf("Cannot close log file: %s\n", strerror(errno));
printf("Cannot close log file: %s\n", g_strerror(errno));
goto cleanup;
}
......@@ -1116,12 +1116,12 @@ static int test26(const void *unused ATTRIBUTE_UNUSED)
}
if ((fd = open(abs_builddir "/commandhelper.log",
O_CREAT | O_TRUNC | O_WRONLY, 0600)) < 0) {
printf("Cannot open log file: %s\n", strerror(errno));
printf("Cannot open log file: %s\n", g_strerror(errno));
goto cleanup;
}
virCommandWriteArgLog(cmd, fd);
if (VIR_CLOSE(fd) < 0) {
printf("Cannot close log file: %s\n", strerror(errno));
printf("Cannot close log file: %s\n", g_strerror(errno));
goto cleanup;
}
......@@ -1186,7 +1186,7 @@ static int test27(const void *unused ATTRIBUTE_UNUSED)
}
if (pipe(pipe1) < 0 || pipe(pipe2) < 0) {
printf("Could not create pipe: %s\n", strerror(errno));
printf("Could not create pipe: %s\n", g_strerror(errno));
goto cleanup;
}
......
......@@ -417,7 +417,7 @@ qemuMonitorTestFree(qemuMonitorTestPtr test)
VIR_FREE(test->items);
if (test->tmpdir && rmdir(test->tmpdir) < 0)
VIR_WARN("Failed to remove tempdir: %s", strerror(errno));
VIR_WARN("Failed to remove tempdir: %s", g_strerror(errno));
VIR_FREE(test->tmpdir);
......
......@@ -20,14 +20,14 @@ mymain(void)
model = virSecurityManagerGetModel(mgr);
if (!model) {
fprintf(stderr, "Failed to copy secModel model: %s",
strerror(errno));
g_strerror(errno));
return EXIT_FAILURE;
}
doi = virSecurityManagerGetDOI(mgr);
if (!doi) {
fprintf(stderr, "Failed to copy secModel DOI: %s",
strerror(errno));
g_strerror(errno));
return EXIT_FAILURE;
}
......
......@@ -172,12 +172,12 @@ virTestLoadFile(const char *file, char **buf)
int len, tmplen, buflen;
if (!fp) {
fprintf(stderr, "%s: failed to open: %s\n", file, strerror(errno));
fprintf(stderr, "%s: failed to open: %s\n", file, g_strerror(errno));
return -1;
}
if (fstat(fileno(fp), &st) < 0) {
fprintf(stderr, "%s: failed to fstat: %s\n", file, strerror(errno));
fprintf(stderr, "%s: failed to fstat: %s\n", file, g_strerror(errno));
VIR_FORCE_FCLOSE(fp);
return -1;
}
......@@ -208,7 +208,7 @@ virTestLoadFile(const char *file, char **buf)
tmplen -= len;
}
if (ferror(fp)) {
fprintf(stderr, "%s: read failed: %s\n", file, strerror(errno));
fprintf(stderr, "%s: read failed: %s\n", file, g_strerror(errno));
VIR_FORCE_FCLOSE(fp);
VIR_FREE(*buf);
return -1;
......
......@@ -37,7 +37,7 @@ linuxTestCompareFiles(const char *cpuinfofile,
cpuinfo = fopen(cpuinfofile, "r");
if (!cpuinfo) {
fprintf(stderr, "unable to open: %s : %s\n",
cpuinfofile, strerror(errno));
cpuinfofile, g_strerror(errno));
goto fail;
}
......@@ -86,7 +86,7 @@ linuxCPUStatsToBuf(virBufferPtr buf,
if ((sc_clk_tck = sysconf(_SC_CLK_TCK)) < 0) {
fprintf(stderr, "sysconf(_SC_CLK_TCK) fails : %s\n",
strerror(errno));
g_strerror(errno));
return -1;
}
tick_to_nsec = (1000ull * 1000ull * 1000ull) / sc_clk_tck;
......
......@@ -76,12 +76,12 @@ printFile(const char *file,
}
if (!(fp = real_fopen(output, "a"))) {
fprintf(stderr, "Unable to open %s: %s\n", output, strerror(errno));
fprintf(stderr, "Unable to open %s: %s\n", output, g_strerror(errno));
abort();
}
if (flock(fileno(fp), LOCK_EX) < 0) {
fprintf(stderr, "Unable to lock %s: %s\n", output, strerror(errno));
fprintf(stderr, "Unable to lock %s: %s\n", output, g_strerror(errno));
fclose(fp);
abort();
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册