summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2019-03-10 21:54:28 +0100
committerGitHub <noreply@github.com>2019-03-10 21:54:28 +0100
commit39e803732d90864351099a732f84bc4775c669c7 (patch)
treeeb3112468034aa231e66050105cec89146b6ad69
parentb04e96331e800c0d8e32125f02b55fd37e1510d1 (diff)
parentf43b9ab8ab3d336a53d1e62adcd20b48757ee22c (diff)
Merge pull request #8723 from vespa-engine/toregge/use-std-error-code
Use std::error_code instead of strerror_r.
-rw-r--r--fastos/src/vespa/fastos/file.cpp14
-rw-r--r--fastos/src/vespa/fastos/unix_file.cpp6
-rw-r--r--fastos/src/vespa/fastos/unix_process.cpp30
-rw-r--r--vespalib/src/apps/vespa-drop-file-from-cache/drop_file_from_cache.cpp10
-rw-r--r--vespalib/src/vespa/vespalib/io/fileutil.cpp5
-rw-r--r--vespalib/src/vespa/vespalib/util/error.cpp5
6 files changed, 27 insertions, 43 deletions
diff --git a/fastos/src/vespa/fastos/file.cpp b/fastos/src/vespa/fastos/file.cpp
index 5c4d7ce9445..aff4d1c54b5 100644
--- a/fastos/src/vespa/fastos/file.cpp
+++ b/fastos/src/vespa/fastos/file.cpp
@@ -328,18 +328,14 @@ FastOS_FileInterface::MakeDirIfNotPresentOrExit(const char *name)
}
if (statInfo._error != FastOS_StatInfo::FileNotFound) {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "Could not stat %s: %s\n", name, errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "Could not stat %s: %s\n", name, ec.message().c_str());
exit(1);
}
if (!FastOS_File::MakeDirectory(name)) {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "Could not mkdir(\"%s\", 0775): %s\n", name, errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "Could not mkdir(\"%s\", 0775): %s\n", name, ec.message().c_str());
exit(1);
}
}
@@ -498,4 +494,4 @@ FastOS_DirectoryScanInterface::FastOS_DirectoryScanInterface(const char *path)
FastOS_DirectoryScanInterface::~FastOS_DirectoryScanInterface()
{
free(_searchPath);
-} \ No newline at end of file
+}
diff --git a/fastos/src/vespa/fastos/unix_file.cpp b/fastos/src/vespa/fastos/unix_file.cpp
index 8b01a5255de..0b144c1f423 100644
--- a/fastos/src/vespa/fastos/unix_file.cpp
+++ b/fastos/src/vespa/fastos/unix_file.cpp
@@ -398,10 +398,8 @@ FastOS_UNIX_File::TranslateError (const int osError)
std::string
FastOS_UNIX_File::getErrorString(const int osError)
{
- char errorBuf[100];
- const char *errorString = strerror_r(osError, errorBuf, sizeof(errorBuf));
-
- return std::string(errorString);
+ std::error_code ec(osError, std::system_category());
+ return ec.message();
}
diff --git a/fastos/src/vespa/fastos/unix_process.cpp b/fastos/src/vespa/fastos/unix_process.cpp
index 80ad0605f78..c695c8d79a0 100644
--- a/fastos/src/vespa/fastos/unix_process.cpp
+++ b/fastos/src/vespa/fastos/unix_process.cpp
@@ -684,9 +684,8 @@ ForkAndExec(const char *command,
break;
case 127:
if (error != 0) {
- char errorBuf[100];
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "ERROR: Could not execve %s: %s\n", command, errorString);
+ std::error_code ec(error, std::system_category());
+ fprintf(stderr, "ERROR: Could not execve %s: %s\n", command, ec.message().c_str());
} else
fprintf(stderr, "ERROR: Could not execve %s\n", command);
break;
@@ -1398,16 +1397,12 @@ bool FastOS_UNIX_ProcessStarter::CreateSocketPairs ()
_mainSocketDescr = fileDescriptors[1];
rc = true;
} else {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "socketpair() failed: %s\n", errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "socketpair() failed: %s\n", ec.message().c_str());
}
} else {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "socketpair() failed: %s\n", errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "socketpair() failed: %s\n", ec.message().c_str());
}
return rc;
}
@@ -1457,17 +1452,12 @@ bool FastOS_UNIX_ProcessStarter::Start ()
rc = true;
}
} else {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "could not fork(): %s\n", errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "could not fork(): %s\n", ec.message().c_str());
}
} else {
- char errorBuf[100];
- int error = errno;
- const char *errorString = strerror_r(error, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "could not CreateSocketPairs: %s\n",
- errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "could not CreateSocketPairs: %s\n", ec.message().c_str());
}
return rc;
}
diff --git a/vespalib/src/apps/vespa-drop-file-from-cache/drop_file_from_cache.cpp b/vespalib/src/apps/vespa-drop-file-from-cache/drop_file_from_cache.cpp
index 042681a90fe..7e15885270c 100644
--- a/vespalib/src/apps/vespa-drop-file-from-cache/drop_file_from_cache.cpp
+++ b/vespalib/src/apps/vespa-drop-file-from-cache/drop_file_from_cache.cpp
@@ -5,9 +5,9 @@
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
+#include <system_error>
int main(int argc, char **argv) {
- char errorBuf[200];
if (argc != 2) {
fprintf(stderr, "%s <filename>\n", argv[0]);
return 1;
@@ -15,16 +15,16 @@ int main(int argc, char **argv) {
const char *fileName = argv[1];
int fh = open(fileName, O_RDONLY);
if (fh == -1) {
- const char *errorString = strerror_r(errno, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "Failed opening file %s: %s\n", fileName, errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "Failed opening file %s: %s\n", fileName, ec.message().c_str());
return 2;
}
int retval = 0;
int err = posix_fadvise(fh, 0, 0, POSIX_FADV_DONTNEED);
if (err != 0) {
- const char *errorString = strerror_r(errno, errorBuf, sizeof(errorBuf));
- fprintf(stderr, "posix_fadvise failed: %s\n", errorString);
+ std::error_code ec(errno, std::system_category());
+ fprintf(stderr, "posix_fadvise failed: %s\n", ec.message().c_str());
retval = 3;
}
close(fh);
diff --git a/vespalib/src/vespa/vespalib/io/fileutil.cpp b/vespalib/src/vespa/vespalib/io/fileutil.cpp
index 5acde54047a..5c3cd04cbb4 100644
--- a/vespalib/src/vespa/vespalib/io/fileutil.cpp
+++ b/vespalib/src/vespa/vespalib/io/fileutil.cpp
@@ -795,9 +795,8 @@ void addStat(asciistream &os, const string & name)
}
os << "[name=" << name;
if (statres != 0) {
- char errorBuf[128];
- const char *errorString = strerror_r(err, errorBuf, sizeof(errorBuf));
- os << " errno=" << err << "(\"" << errorString << "\")";
+ std::error_code ec(err, std::system_category());
+ os << " errno=" << err << "(\"" << ec.message() << "\")";
} else {
os << " mode=" << oct << filestat.st_mode << dec <<
" uid=" << filestat.st_uid << " gid=" << filestat.st_gid <<
diff --git a/vespalib/src/vespa/vespalib/util/error.cpp b/vespalib/src/vespa/vespalib/util/error.cpp
index 3c8bbd7d9e8..0bc1e896398 100644
--- a/vespalib/src/vespa/vespalib/util/error.cpp
+++ b/vespalib/src/vespa/vespalib/util/error.cpp
@@ -1,14 +1,15 @@
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/error.h>
+#include <system_error>
namespace vespalib {
vespalib::string
getErrorString(const int osError)
{
- char errorBuf[128];
- return strerror_r(osError, errorBuf, sizeof(errorBuf));
+ std::error_code ec(osError, std::system_category());
+ return ec.message();
}
vespalib::string