refactor(3p/nix): Apply clang-tidy's readability-* fixes
This applies the readability fixes listed here: https://clang.llvm.org/extra/clang-tidy/checks/list.html
This commit is contained in:
parent
d331d3a0b5
commit
689ef502f5
78 changed files with 863 additions and 792 deletions
20
third_party/nix/src/libutil/archive.cc
vendored
20
third_party/nix/src/libutil/archive.cc
vendored
|
|
@ -40,7 +40,7 @@ const std::string narVersionMagic1 = "nix-archive-1";
|
|||
|
||||
static string caseHackSuffix = "~nix~case~hack~";
|
||||
|
||||
PathFilter defaultPathFilter = [](const Path&) { return true; };
|
||||
PathFilter defaultPathFilter = [](const Path& /*unused*/) { return true; };
|
||||
|
||||
static void dumpContents(const Path& path, size_t size, Sink& sink) {
|
||||
sink << "contents" << size;
|
||||
|
|
@ -67,7 +67,7 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) {
|
|||
checkInterrupt();
|
||||
|
||||
struct stat st;
|
||||
if (lstat(path.c_str(), &st)) {
|
||||
if (lstat(path.c_str(), &st) != 0) {
|
||||
throw SysError(format("getting attributes of path '%1%'") % path);
|
||||
}
|
||||
|
||||
|
|
@ -76,7 +76,7 @@ static void dump(const Path& path, Sink& sink, PathFilter& filter) {
|
|||
if (S_ISREG(st.st_mode)) {
|
||||
sink << "type"
|
||||
<< "regular";
|
||||
if (st.st_mode & S_IXUSR) {
|
||||
if ((st.st_mode & S_IXUSR) != 0u) {
|
||||
sink << "executable"
|
||||
<< "";
|
||||
}
|
||||
|
|
@ -167,7 +167,7 @@ static void parseContents(ParseSink& sink, Source& source, const Path& path) {
|
|||
unsigned long long left = size;
|
||||
std::vector<unsigned char> buf(65536);
|
||||
|
||||
while (left) {
|
||||
while (left != 0u) {
|
||||
checkInterrupt();
|
||||
auto n = buf.size();
|
||||
if ((unsigned long long)n > left) {
|
||||
|
|
@ -208,7 +208,7 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
|
|||
break;
|
||||
}
|
||||
|
||||
else if (s == "type") {
|
||||
if (s == "type") {
|
||||
if (type != tpUnknown) {
|
||||
throw badArchive("multiple type fields");
|
||||
}
|
||||
|
|
@ -240,14 +240,15 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
|
|||
|
||||
else if (s == "executable" && type == tpRegular) {
|
||||
auto s = readString(source);
|
||||
if (s != "") {
|
||||
if (!s.empty()) {
|
||||
throw badArchive("executable marker has non-empty value");
|
||||
}
|
||||
sink.isExecutable();
|
||||
}
|
||||
|
||||
else if (s == "entry" && type == tpDirectory) {
|
||||
string name, prevName;
|
||||
string name;
|
||||
string prevName;
|
||||
|
||||
s = readString(source);
|
||||
if (s != "(") {
|
||||
|
|
@ -261,7 +262,8 @@ static void parse(ParseSink& sink, Source& source, const Path& path) {
|
|||
|
||||
if (s == ")") {
|
||||
break;
|
||||
} else if (s == "name") {
|
||||
}
|
||||
if (s == "name") {
|
||||
name = readString(source);
|
||||
if (name.empty() || name == "." || name == ".." ||
|
||||
name.find('/') != string::npos ||
|
||||
|
|
@ -350,7 +352,7 @@ struct RestoreSink : ParseSink {
|
|||
|
||||
void preallocateContents(unsigned long long len) override {
|
||||
#if HAVE_POSIX_FALLOCATE
|
||||
if (len) {
|
||||
if (len != 0u) {
|
||||
errno = posix_fallocate(fd.get(), 0, len);
|
||||
/* Note that EINVAL may indicate that the underlying
|
||||
filesystem doesn't support preallocation (e.g. on
|
||||
|
|
|
|||
18
third_party/nix/src/libutil/args.cc
vendored
18
third_party/nix/src/libutil/args.cc
vendored
|
|
@ -7,9 +7,9 @@ namespace nix {
|
|||
Args::FlagMaker Args::mkFlag() { return FlagMaker(*this); }
|
||||
|
||||
Args::FlagMaker::~FlagMaker() {
|
||||
assert(flag->longName != "");
|
||||
assert(!flag->longName.empty());
|
||||
args.longFlags[flag->longName] = flag;
|
||||
if (flag->shortName) {
|
||||
if (flag->shortName != 0) {
|
||||
args.shortFlags[flag->shortName] = flag;
|
||||
}
|
||||
}
|
||||
|
|
@ -26,12 +26,12 @@ void Args::parseCmdline(const Strings& _cmdline) {
|
|||
/* Expand compound dash options (i.e., `-qlf' -> `-q -l -f',
|
||||
`-j3` -> `-j 3`). */
|
||||
if (!dashDash && arg.length() > 2 && arg[0] == '-' && arg[1] != '-' &&
|
||||
isalpha(arg[1])) {
|
||||
(isalpha(arg[1]) != 0)) {
|
||||
*pos = (string) "-" + arg[1];
|
||||
auto next = pos;
|
||||
++next;
|
||||
for (unsigned int j = 2; j < arg.length(); j++) {
|
||||
if (isalpha(arg[j])) {
|
||||
if (isalpha(arg[j]) != 0) {
|
||||
cmdline.insert(next, (string) "-" + arg[j]);
|
||||
} else {
|
||||
cmdline.insert(next, string(arg, j));
|
||||
|
|
@ -74,11 +74,11 @@ void Args::printHelp(const string& programName, std::ostream& out) {
|
|||
std::cout << "\n";
|
||||
|
||||
auto s = description();
|
||||
if (s != "") {
|
||||
if (!s.empty()) {
|
||||
std::cout << "\nSummary: " << s << ".\n";
|
||||
}
|
||||
|
||||
if (longFlags.size()) {
|
||||
if (!longFlags.empty() != 0u) {
|
||||
std::cout << "\n";
|
||||
std::cout << "Flags:\n";
|
||||
printFlags(out);
|
||||
|
|
@ -88,11 +88,11 @@ void Args::printHelp(const string& programName, std::ostream& out) {
|
|||
void Args::printFlags(std::ostream& out) {
|
||||
Table2 table;
|
||||
for (auto& flag : longFlags) {
|
||||
if (hiddenCategories.count(flag.second->category)) {
|
||||
if (hiddenCategories.count(flag.second->category) != 0u) {
|
||||
continue;
|
||||
}
|
||||
table.push_back(std::make_pair(
|
||||
(flag.second->shortName
|
||||
(flag.second->shortName != 0
|
||||
? std::string("-") + flag.second->shortName + ", "
|
||||
: " ") +
|
||||
"--" + flag.first + renderLabels(flag.second->labels),
|
||||
|
|
@ -188,7 +188,7 @@ Strings argvToStrings(int argc, char** argv) {
|
|||
Strings args;
|
||||
argc--;
|
||||
argv++;
|
||||
while (argc--) {
|
||||
while ((argc--) != 0) {
|
||||
args.push_back(*argv++);
|
||||
}
|
||||
return args;
|
||||
|
|
|
|||
48
third_party/nix/src/libutil/compression.cc
vendored
48
third_party/nix/src/libutil/compression.cc
vendored
|
|
@ -21,7 +21,7 @@ struct ChunkedCompressionSink : CompressionSink {
|
|||
|
||||
void write(const unsigned char* data, size_t len) override {
|
||||
const size_t CHUNK_SIZE = sizeof(outbuf) << 2;
|
||||
while (len) {
|
||||
while (len != 0u) {
|
||||
size_t n = std::min(CHUNK_SIZE, len);
|
||||
writeInternal(data, n);
|
||||
data += n;
|
||||
|
|
@ -68,10 +68,10 @@ struct XzDecompressionSink : CompressionSink {
|
|||
strm.next_in = data;
|
||||
strm.avail_in = len;
|
||||
|
||||
while (!finished && (!data || strm.avail_in)) {
|
||||
while (!finished && ((data == nullptr) || (strm.avail_in != 0u))) {
|
||||
checkInterrupt();
|
||||
|
||||
lzma_ret ret = lzma_code(&strm, data ? LZMA_RUN : LZMA_FINISH);
|
||||
lzma_ret ret = lzma_code(&strm, data != nullptr ? LZMA_RUN : LZMA_FINISH);
|
||||
if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
|
||||
throw CompressionError("error %d while decompressing xz file", ret);
|
||||
}
|
||||
|
|
@ -116,7 +116,7 @@ struct BzipDecompressionSink : ChunkedCompressionSink {
|
|||
strm.next_in = (char*)data;
|
||||
strm.avail_in = len;
|
||||
|
||||
while (strm.avail_in) {
|
||||
while (strm.avail_in != 0u) {
|
||||
checkInterrupt();
|
||||
|
||||
int ret = BZ2_bzDecompress(&strm);
|
||||
|
|
@ -142,7 +142,7 @@ struct BrotliDecompressionSink : ChunkedCompressionSink {
|
|||
|
||||
explicit BrotliDecompressionSink(Sink& nextSink) : nextSink(nextSink) {
|
||||
state = BrotliDecoderCreateInstance(nullptr, nullptr, nullptr);
|
||||
if (!state) {
|
||||
if (state == nullptr) {
|
||||
throw CompressionError("unable to initialize brotli decoder");
|
||||
}
|
||||
}
|
||||
|
|
@ -160,11 +160,11 @@ struct BrotliDecompressionSink : ChunkedCompressionSink {
|
|||
uint8_t* next_out = outbuf;
|
||||
size_t avail_out = sizeof(outbuf);
|
||||
|
||||
while (!finished && (!data || avail_in)) {
|
||||
while (!finished && ((data == nullptr) || (avail_in != 0u))) {
|
||||
checkInterrupt();
|
||||
|
||||
if (!BrotliDecoderDecompressStream(state, &avail_in, &next_in, &avail_out,
|
||||
&next_out, nullptr)) {
|
||||
if (BrotliDecoderDecompressStream(state, &avail_in, &next_in, &avail_out,
|
||||
&next_out, nullptr) == 0u) {
|
||||
throw CompressionError("error while decompressing brotli file");
|
||||
}
|
||||
|
||||
|
|
@ -174,7 +174,7 @@ struct BrotliDecompressionSink : ChunkedCompressionSink {
|
|||
avail_out = sizeof(outbuf);
|
||||
}
|
||||
|
||||
finished = BrotliDecoderIsFinished(state);
|
||||
finished = (BrotliDecoderIsFinished(state) != 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -189,9 +189,10 @@ ref<std::string> decompress(const std::string& method, const std::string& in) {
|
|||
|
||||
ref<CompressionSink> makeDecompressionSink(const std::string& method,
|
||||
Sink& nextSink) {
|
||||
if (method == "none" || method == "") {
|
||||
if (method == "none" || method.empty()) {
|
||||
return make_ref<NoneSink>(nextSink);
|
||||
} else if (method == "xz") {
|
||||
}
|
||||
if (method == "xz") {
|
||||
return make_ref<XzDecompressionSink>(nextSink);
|
||||
} else if (method == "bzip2") {
|
||||
return make_ref<BzipDecompressionSink>(nextSink);
|
||||
|
|
@ -260,10 +261,10 @@ struct XzCompressionSink : CompressionSink {
|
|||
strm.next_in = data;
|
||||
strm.avail_in = len;
|
||||
|
||||
while (!finished && (!data || strm.avail_in)) {
|
||||
while (!finished && ((data == nullptr) || (strm.avail_in != 0u))) {
|
||||
checkInterrupt();
|
||||
|
||||
lzma_ret ret = lzma_code(&strm, data ? LZMA_RUN : LZMA_FINISH);
|
||||
lzma_ret ret = lzma_code(&strm, data != nullptr ? LZMA_RUN : LZMA_FINISH);
|
||||
if (ret != LZMA_OK && ret != LZMA_STREAM_END) {
|
||||
throw CompressionError("error %d while compressing xz file", ret);
|
||||
}
|
||||
|
|
@ -308,10 +309,10 @@ struct BzipCompressionSink : ChunkedCompressionSink {
|
|||
strm.next_in = (char*)data;
|
||||
strm.avail_in = len;
|
||||
|
||||
while (!finished && (!data || strm.avail_in)) {
|
||||
while (!finished && ((data == nullptr) || (strm.avail_in != 0u))) {
|
||||
checkInterrupt();
|
||||
|
||||
int ret = BZ2_bzCompress(&strm, data ? BZ_RUN : BZ_FINISH);
|
||||
int ret = BZ2_bzCompress(&strm, data != nullptr ? BZ_RUN : BZ_FINISH);
|
||||
if (ret != BZ_RUN_OK && ret != BZ_FINISH_OK && ret != BZ_STREAM_END) {
|
||||
throw CompressionError("error %d while compressing bzip2 file", ret);
|
||||
}
|
||||
|
|
@ -335,7 +336,7 @@ struct BrotliCompressionSink : ChunkedCompressionSink {
|
|||
|
||||
explicit BrotliCompressionSink(Sink& nextSink) : nextSink(nextSink) {
|
||||
state = BrotliEncoderCreateInstance(nullptr, nullptr, nullptr);
|
||||
if (!state) {
|
||||
if (state == nullptr) {
|
||||
throw CompressionError("unable to initialise brotli encoder");
|
||||
}
|
||||
}
|
||||
|
|
@ -353,12 +354,14 @@ struct BrotliCompressionSink : ChunkedCompressionSink {
|
|||
uint8_t* next_out = outbuf;
|
||||
size_t avail_out = sizeof(outbuf);
|
||||
|
||||
while (!finished && (!data || avail_in)) {
|
||||
while (!finished && ((data == nullptr) || (avail_in != 0u))) {
|
||||
checkInterrupt();
|
||||
|
||||
if (!BrotliEncoderCompressStream(
|
||||
state, data ? BROTLI_OPERATION_PROCESS : BROTLI_OPERATION_FINISH,
|
||||
&avail_in, &next_in, &avail_out, &next_out, nullptr)) {
|
||||
if (BrotliEncoderCompressStream(state,
|
||||
data != nullptr ? BROTLI_OPERATION_PROCESS
|
||||
: BROTLI_OPERATION_FINISH,
|
||||
&avail_in, &next_in, &avail_out,
|
||||
&next_out, nullptr) == 0) {
|
||||
throw CompressionError("error while compressing brotli compression");
|
||||
}
|
||||
|
||||
|
|
@ -368,7 +371,7 @@ struct BrotliCompressionSink : ChunkedCompressionSink {
|
|||
avail_out = sizeof(outbuf);
|
||||
}
|
||||
|
||||
finished = BrotliEncoderIsFinished(state);
|
||||
finished = (BrotliEncoderIsFinished(state) != 0);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
@ -377,7 +380,8 @@ ref<CompressionSink> makeCompressionSink(const std::string& method,
|
|||
Sink& nextSink, const bool parallel) {
|
||||
if (method == "none") {
|
||||
return make_ref<NoneSink>(nextSink);
|
||||
} else if (method == "xz") {
|
||||
}
|
||||
if (method == "xz") {
|
||||
return make_ref<XzCompressionSink>(nextSink, parallel);
|
||||
} else if (method == "bzip2") {
|
||||
return make_ref<BzipCompressionSink>(nextSink);
|
||||
|
|
|
|||
4
third_party/nix/src/libutil/config.cc
vendored
4
third_party/nix/src/libutil/config.cc
vendored
|
|
@ -303,7 +303,7 @@ template class BaseSetting<Strings>;
|
|||
template class BaseSetting<StringSet>;
|
||||
|
||||
void PathSetting::set(const std::string& str) {
|
||||
if (str == "") {
|
||||
if (str.empty()) {
|
||||
if (allowEmpty) {
|
||||
value = "";
|
||||
} else {
|
||||
|
|
@ -356,7 +356,7 @@ GlobalConfig globalConfig;
|
|||
GlobalConfig::ConfigRegistrations* GlobalConfig::configRegistrations;
|
||||
|
||||
GlobalConfig::Register::Register(Config* config) {
|
||||
if (!configRegistrations) {
|
||||
if (configRegistrations == nullptr) {
|
||||
configRegistrations = new ConfigRegistrations;
|
||||
}
|
||||
configRegistrations->emplace_back(config);
|
||||
|
|
|
|||
10
third_party/nix/src/libutil/hash.cc
vendored
10
third_party/nix/src/libutil/hash.cc
vendored
|
|
@ -193,7 +193,7 @@ Hash::Hash(const std::string& s, HashType type) : type(type) {
|
|||
if (i < hashSize - 1) {
|
||||
hash[i + 1] |= digit >> (8 - j);
|
||||
} else {
|
||||
if (digit >> (8 - j)) {
|
||||
if ((digit >> (8 - j)) != 0) {
|
||||
throw BadHash("invalid base-32 hash '%s'", s);
|
||||
}
|
||||
}
|
||||
|
|
@ -280,7 +280,7 @@ Hash hashFile(HashType ht, const Path& path) {
|
|||
|
||||
std::vector<unsigned char> buf(8192);
|
||||
ssize_t n;
|
||||
while ((n = read(fd.get(), buf.data(), buf.size()))) {
|
||||
while ((n = read(fd.get(), buf.data(), buf.size())) != 0) {
|
||||
checkInterrupt();
|
||||
if (n == -1) {
|
||||
throw SysError(format("reading file '%1%'") % path);
|
||||
|
|
@ -341,7 +341,8 @@ Hash compressHash(const Hash& hash, unsigned int newSize) {
|
|||
HashType parseHashType(const string& s) {
|
||||
if (s == "md5") {
|
||||
return htMD5;
|
||||
} else if (s == "sha1") {
|
||||
}
|
||||
if (s == "sha1") {
|
||||
return htSHA1;
|
||||
} else if (s == "sha256") {
|
||||
return htSHA256;
|
||||
|
|
@ -355,7 +356,8 @@ HashType parseHashType(const string& s) {
|
|||
string printHashType(HashType ht) {
|
||||
if (ht == htMD5) {
|
||||
return "md5";
|
||||
} else if (ht == htSHA1) {
|
||||
}
|
||||
if (ht == htSHA1) {
|
||||
return "sha1";
|
||||
} else if (ht == htSHA256) {
|
||||
return "sha256";
|
||||
|
|
|
|||
6
third_party/nix/src/libutil/json.cc
vendored
6
third_party/nix/src/libutil/json.cc
vendored
|
|
@ -27,7 +27,7 @@ void toJSON(std::ostream& str, const char* start, const char* end) {
|
|||
}
|
||||
|
||||
void toJSON(std::ostream& str, const char* s) {
|
||||
if (!s) {
|
||||
if (s == nullptr) {
|
||||
str << "null";
|
||||
} else {
|
||||
toJSON(str, s, s + strlen(s));
|
||||
|
|
@ -91,7 +91,7 @@ JSONWriter::JSONWriter(std::ostream& str, bool indent)
|
|||
JSONWriter::JSONWriter(JSONState* state) : state(state) { state->stack++; }
|
||||
|
||||
JSONWriter::~JSONWriter() {
|
||||
if (state) {
|
||||
if (state != nullptr) {
|
||||
assertActive();
|
||||
state->stack--;
|
||||
if (state->stack == 0) {
|
||||
|
|
@ -150,7 +150,7 @@ void JSONObject::open() {
|
|||
}
|
||||
|
||||
JSONObject::~JSONObject() {
|
||||
if (state) {
|
||||
if (state != nullptr) {
|
||||
state->depth--;
|
||||
if (state->indent && !first) {
|
||||
indent();
|
||||
|
|
|
|||
14
third_party/nix/src/libutil/serialise.cc
vendored
14
third_party/nix/src/libutil/serialise.cc
vendored
|
|
@ -16,7 +16,7 @@ void BufferedSink::operator()(const unsigned char* data, size_t len) {
|
|||
buffer = decltype(buffer)(new unsigned char[bufSize]);
|
||||
}
|
||||
|
||||
while (len) {
|
||||
while (len != 0u) {
|
||||
/* Optimisation: bypass the buffer if the data exceeds the
|
||||
buffer size. */
|
||||
if (bufPos + len >= bufSize) {
|
||||
|
|
@ -81,7 +81,7 @@ void FdSink::write(const unsigned char* data, size_t len) {
|
|||
bool FdSink::good() { return _good; }
|
||||
|
||||
void Source::operator()(unsigned char* data, size_t len) {
|
||||
while (len) {
|
||||
while (len != 0u) {
|
||||
size_t n = read(data, len);
|
||||
data += n;
|
||||
len -= n;
|
||||
|
|
@ -108,7 +108,7 @@ size_t BufferedSource::read(unsigned char* data, size_t len) {
|
|||
buffer = decltype(buffer)(new unsigned char[bufSize]);
|
||||
}
|
||||
|
||||
if (!bufPosIn) {
|
||||
if (bufPosIn == 0u) {
|
||||
bufPosIn = readUnbuffered(buffer.get(), bufSize);
|
||||
}
|
||||
|
||||
|
|
@ -177,7 +177,7 @@ std::unique_ptr<Source> sinkToSource(std::function<void(Sink&)> fun,
|
|||
if (!coro) {
|
||||
coro = coro_t::pull_type([&](coro_t::push_type& yield) {
|
||||
LambdaSink sink([&](const unsigned char* data, size_t len) {
|
||||
if (len) {
|
||||
if (len != 0u) {
|
||||
yield(std::string((const char*)data, len));
|
||||
}
|
||||
});
|
||||
|
|
@ -210,7 +210,7 @@ std::unique_ptr<Source> sinkToSource(std::function<void(Sink&)> fun,
|
|||
}
|
||||
|
||||
void writePadding(size_t len, Sink& sink) {
|
||||
if (len % 8) {
|
||||
if ((len % 8) != 0u) {
|
||||
unsigned char zero[8];
|
||||
memset(zero, 0, sizeof(zero));
|
||||
sink(zero, 8 - (len % 8));
|
||||
|
|
@ -247,12 +247,12 @@ Sink& operator<<(Sink& sink, const StringSet& s) {
|
|||
}
|
||||
|
||||
void readPadding(size_t len, Source& source) {
|
||||
if (len % 8) {
|
||||
if ((len % 8) != 0u) {
|
||||
unsigned char zero[8];
|
||||
size_t n = 8 - (len % 8);
|
||||
source(zero, n);
|
||||
for (unsigned int i = 0; i < n; i++) {
|
||||
if (zero[i]) {
|
||||
if (zero[i] != 0u) {
|
||||
throw SerialisationError("non-zero padding");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
10
third_party/nix/src/libutil/thread-pool.cc
vendored
10
third_party/nix/src/libutil/thread-pool.cc
vendored
|
|
@ -8,9 +8,9 @@ namespace nix {
|
|||
ThreadPool::ThreadPool(size_t _maxThreads) : maxThreads(_maxThreads) {
|
||||
restoreAffinity(); // FIXME
|
||||
|
||||
if (!maxThreads) {
|
||||
if (maxThreads == 0u) {
|
||||
maxThreads = std::thread::hardware_concurrency();
|
||||
if (!maxThreads) {
|
||||
if (maxThreads == 0u) {
|
||||
maxThreads = 1;
|
||||
}
|
||||
}
|
||||
|
|
@ -111,8 +111,8 @@ void ThreadPool::doWork(bool mainThread) {
|
|||
try {
|
||||
std::rethrow_exception(exc);
|
||||
} catch (std::exception& e) {
|
||||
if (!dynamic_cast<Interrupted*>(&e) &&
|
||||
!dynamic_cast<ThreadPoolShutDown*>(&e)) {
|
||||
if ((dynamic_cast<Interrupted*>(&e) == nullptr) &&
|
||||
(dynamic_cast<ThreadPoolShutDown*>(&e) == nullptr)) {
|
||||
ignoreException();
|
||||
}
|
||||
} catch (...) {
|
||||
|
|
@ -135,7 +135,7 @@ void ThreadPool::doWork(bool mainThread) {
|
|||
/* If there are no active or pending items, and the
|
||||
main thread is running process(), then no new items
|
||||
can be added. So exit. */
|
||||
if (!state->active && state->draining) {
|
||||
if ((state->active == 0u) && state->draining) {
|
||||
quit = true;
|
||||
work.notify_all();
|
||||
return;
|
||||
|
|
|
|||
95
third_party/nix/src/libutil/util.cc
vendored
95
third_party/nix/src/libutil/util.cc
vendored
|
|
@ -35,7 +35,7 @@
|
|||
#include <sys/prctl.h>
|
||||
#endif
|
||||
|
||||
extern char** environ __attribute__((weak));
|
||||
__attribute__((weak));
|
||||
|
||||
namespace nix {
|
||||
|
||||
|
|
@ -53,15 +53,15 @@ std::string SysError::addErrno(const std::string& s) {
|
|||
|
||||
string getEnv(const string& key, const string& def) {
|
||||
char* value = getenv(key.c_str());
|
||||
return value ? string(value) : def;
|
||||
return value != nullptr ? string(value) : def;
|
||||
}
|
||||
|
||||
std::map<std::string, std::string> getEnv() {
|
||||
std::map<std::string, std::string> env;
|
||||
for (size_t i = 0; environ[i]; ++i) {
|
||||
for (size_t i = 0; environ[i] != nullptr; ++i) {
|
||||
auto s = environ[i];
|
||||
auto eq = strchr(s, '=');
|
||||
if (!eq) {
|
||||
if (eq == nullptr) {
|
||||
// invalid env, just keep going
|
||||
continue;
|
||||
}
|
||||
|
|
@ -85,7 +85,7 @@ void replaceEnv(std::map<std::string, std::string> newEnv) {
|
|||
|
||||
Path absPath(Path path, Path dir) {
|
||||
if (path[0] != '/') {
|
||||
if (dir == "") {
|
||||
if (dir.empty()) {
|
||||
#ifdef __GNU__
|
||||
/* GNU (aka. GNU/Hurd) doesn't have any limitation on path
|
||||
lengths and doesn't define `PATH_MAX'. */
|
||||
|
|
@ -93,7 +93,7 @@ Path absPath(Path path, Path dir) {
|
|||
if (buf == NULL)
|
||||
#else
|
||||
char buf[PATH_MAX];
|
||||
if (!getcwd(buf, sizeof(buf))) {
|
||||
if (getcwd(buf, sizeof(buf)) == nullptr) {
|
||||
#endif
|
||||
throw SysError("cannot get cwd");
|
||||
}
|
||||
|
|
@ -108,7 +108,7 @@ return canonPath(path);
|
|||
} // namespace nix
|
||||
|
||||
Path canonPath(const Path& path, bool resolveSymlinks) {
|
||||
assert(path != "");
|
||||
assert(!path.empty());
|
||||
|
||||
string s;
|
||||
|
||||
|
|
@ -116,12 +116,14 @@ Path canonPath(const Path& path, bool resolveSymlinks) {
|
|||
throw Error(format("not an absolute path: '%1%'") % path);
|
||||
}
|
||||
|
||||
string::const_iterator i = path.begin(), end = path.end();
|
||||
string::const_iterator i = path.begin();
|
||||
string::const_iterator end = path.end();
|
||||
string temp;
|
||||
|
||||
/* Count the number of times we follow a symlink and stop at some
|
||||
arbitrary (but high) limit to prevent infinite loops. */
|
||||
unsigned int followCount = 0, maxFollow = 1024;
|
||||
unsigned int followCount = 0;
|
||||
unsigned int maxFollow = 1024;
|
||||
|
||||
while (true) {
|
||||
/* Skip slashes. */
|
||||
|
|
@ -210,7 +212,7 @@ bool isDirOrInDir(const Path& path, const Path& dir) {
|
|||
|
||||
struct stat lstat(const Path& path) {
|
||||
struct stat st;
|
||||
if (lstat(path.c_str(), &st)) {
|
||||
if (lstat(path.c_str(), &st) != 0) {
|
||||
throw SysError(format("getting status of '%1%'") % path);
|
||||
}
|
||||
return st;
|
||||
|
|
@ -220,7 +222,7 @@ bool pathExists(const Path& path) {
|
|||
int res;
|
||||
struct stat st;
|
||||
res = lstat(path.c_str(), &st);
|
||||
if (!res) {
|
||||
if (res == 0) {
|
||||
return true;
|
||||
}
|
||||
if (errno != ENOENT && errno != ENOTDIR) {
|
||||
|
|
@ -238,9 +240,9 @@ Path readLink(const Path& path) {
|
|||
if (rlSize == -1) {
|
||||
if (errno == EINVAL) {
|
||||
throw Error("'%1%' is not a symlink", path);
|
||||
} else {
|
||||
throw SysError("reading symbolic link '%1%'", path);
|
||||
}
|
||||
throw SysError("reading symbolic link '%1%'", path);
|
||||
|
||||
} else if (rlSize < bufSize) {
|
||||
return string(buf.data(), rlSize);
|
||||
}
|
||||
|
|
@ -436,9 +438,8 @@ static Path tempName(Path tmpRoot, const Path& prefix, bool includePid,
|
|||
if (includePid) {
|
||||
return (format("%1%/%2%-%3%-%4%") % tmpRoot % prefix % getpid() % counter++)
|
||||
.str();
|
||||
} else {
|
||||
return (format("%1%/%2%-%3%") % tmpRoot % prefix % counter++).str();
|
||||
}
|
||||
return (format("%1%/%2%-%3%") % tmpRoot % prefix % counter++).str();
|
||||
}
|
||||
|
||||
Path createTempDir(const Path& tmpRoot, const Path& prefix, bool includePid,
|
||||
|
|
@ -473,7 +474,7 @@ Path createTempDir(const Path& tmpRoot, const Path& prefix, bool includePid,
|
|||
|
||||
std::string getUserName() {
|
||||
auto pw = getpwuid(geteuid());
|
||||
std::string name = pw ? pw->pw_name : getEnv("USER", "");
|
||||
std::string name = pw != nullptr ? pw->pw_name : getEnv("USER", "");
|
||||
if (name.empty()) {
|
||||
throw Error("cannot figure out user name");
|
||||
}
|
||||
|
|
@ -487,7 +488,7 @@ static Lazy<Path> getHome2([]() {
|
|||
struct passwd pwbuf;
|
||||
struct passwd* pw;
|
||||
if (getpwuid_r(geteuid(), &pwbuf, buf.data(), buf.size(), &pw) != 0 ||
|
||||
!pw || !pw->pw_dir || !pw->pw_dir[0]) {
|
||||
(pw == nullptr) || (pw->pw_dir == nullptr) || (pw->pw_dir[0] == 0)) {
|
||||
throw Error("cannot determine user's home directory");
|
||||
}
|
||||
homeDir = pw->pw_dir;
|
||||
|
|
@ -557,7 +558,7 @@ Paths createDirs(const Path& path) {
|
|||
}
|
||||
|
||||
void createSymlink(const Path& target, const Path& link) {
|
||||
if (symlink(target.c_str(), link.c_str())) {
|
||||
if (symlink(target.c_str(), link.c_str()) != 0) {
|
||||
throw SysError(format("creating symlink from '%1%' to '%2%'") % link %
|
||||
target);
|
||||
}
|
||||
|
|
@ -585,7 +586,7 @@ void replaceSymlink(const Path& target, const Path& link) {
|
|||
}
|
||||
|
||||
void readFull(int fd, unsigned char* buf, size_t count) {
|
||||
while (count) {
|
||||
while (count != 0u) {
|
||||
checkInterrupt();
|
||||
ssize_t res = read(fd, (char*)buf, count);
|
||||
if (res == -1) {
|
||||
|
|
@ -604,7 +605,7 @@ void readFull(int fd, unsigned char* buf, size_t count) {
|
|||
|
||||
void writeFull(int fd, const unsigned char* buf, size_t count,
|
||||
bool allowInterrupts) {
|
||||
while (count) {
|
||||
while (count != 0u) {
|
||||
if (allowInterrupts) {
|
||||
checkInterrupt();
|
||||
}
|
||||
|
|
@ -989,11 +990,12 @@ void runProgram2(const RunOptions& options) {
|
|||
}
|
||||
|
||||
/* Create a pipe. */
|
||||
Pipe out, in;
|
||||
if (options.standardOut) {
|
||||
Pipe out;
|
||||
Pipe in;
|
||||
if (options.standardOut != nullptr) {
|
||||
out.create();
|
||||
}
|
||||
if (source) {
|
||||
if (source != nullptr) {
|
||||
in.create();
|
||||
}
|
||||
|
||||
|
|
@ -1011,7 +1013,7 @@ void runProgram2(const RunOptions& options) {
|
|||
if (options.environment) {
|
||||
replaceEnv(*options.environment);
|
||||
}
|
||||
if (options.standardOut &&
|
||||
if ((options.standardOut != nullptr) &&
|
||||
dup2(out.writeSide.get(), STDOUT_FILENO) == -1) {
|
||||
throw SysError("dupping stdout");
|
||||
}
|
||||
|
|
@ -1020,7 +1022,8 @@ void runProgram2(const RunOptions& options) {
|
|||
throw SysError("cannot dup stdout into stderr");
|
||||
}
|
||||
}
|
||||
if (source && dup2(in.readSide.get(), STDIN_FILENO) == -1) {
|
||||
if ((source != nullptr) &&
|
||||
dup2(in.readSide.get(), STDIN_FILENO) == -1) {
|
||||
throw SysError("dupping stdin");
|
||||
}
|
||||
|
||||
|
|
@ -1065,7 +1068,7 @@ void runProgram2(const RunOptions& options) {
|
|||
}
|
||||
});
|
||||
|
||||
if (source) {
|
||||
if (source != nullptr) {
|
||||
in.readSide = -1;
|
||||
writerThread = std::thread([&]() {
|
||||
try {
|
||||
|
|
@ -1087,7 +1090,7 @@ void runProgram2(const RunOptions& options) {
|
|||
});
|
||||
}
|
||||
|
||||
if (options.standardOut) {
|
||||
if (options.standardOut != nullptr) {
|
||||
drainFD(out.readSide.get(), *options.standardOut);
|
||||
}
|
||||
|
||||
|
|
@ -1095,11 +1098,11 @@ void runProgram2(const RunOptions& options) {
|
|||
int status = pid.wait();
|
||||
|
||||
/* Wait for the writer thread to finish. */
|
||||
if (source) {
|
||||
if (source != nullptr) {
|
||||
promise.get_future().get();
|
||||
}
|
||||
|
||||
if (status) {
|
||||
if (status != 0) {
|
||||
throw ExecError(status, fmt("program '%1%' %2%", options.program,
|
||||
statusToString(status)));
|
||||
}
|
||||
|
|
@ -1110,7 +1113,7 @@ void closeMostFDs(const set<int>& exceptions) {
|
|||
try {
|
||||
for (auto& s : readDirectory("/proc/self/fd")) {
|
||||
auto fd = std::stoi(s.name);
|
||||
if (!exceptions.count(fd)) {
|
||||
if (exceptions.count(fd) == 0u) {
|
||||
DLOG(INFO) << "closing leaked FD " << fd;
|
||||
close(fd);
|
||||
}
|
||||
|
|
@ -1123,7 +1126,7 @@ void closeMostFDs(const set<int>& exceptions) {
|
|||
int maxFD = 0;
|
||||
maxFD = sysconf(_SC_OPEN_MAX);
|
||||
for (int fd = 0; fd < maxFD; ++fd) {
|
||||
if (!exceptions.count(fd)) {
|
||||
if (exceptions.count(fd) == 0u) {
|
||||
close(fd);
|
||||
} /* ignore result */
|
||||
}
|
||||
|
|
@ -1150,7 +1153,7 @@ void _interrupted() {
|
|||
/* Block user interrupts while an exception is being handled.
|
||||
Throwing an exception while another exception is being handled
|
||||
kills the program! */
|
||||
if (!interruptThrown && !std::uncaught_exceptions()) {
|
||||
if (!interruptThrown && (std::uncaught_exceptions() == 0)) {
|
||||
interruptThrown = true;
|
||||
throw Interrupted("interrupted by the user");
|
||||
}
|
||||
|
|
@ -1182,7 +1185,7 @@ template vector<string> tokenizeString(const string& s,
|
|||
string concatStringsSep(const string& sep, const Strings& ss) {
|
||||
string s;
|
||||
for (auto& i : ss) {
|
||||
if (s.size() != 0) {
|
||||
if (!s.empty()) {
|
||||
s += sep;
|
||||
}
|
||||
s += i;
|
||||
|
|
@ -1193,7 +1196,7 @@ string concatStringsSep(const string& sep, const Strings& ss) {
|
|||
string concatStringsSep(const string& sep, const StringSet& ss) {
|
||||
string s;
|
||||
for (auto& i : ss) {
|
||||
if (s.size() != 0) {
|
||||
if (!s.empty()) {
|
||||
s += sep;
|
||||
}
|
||||
s += i;
|
||||
|
|
@ -1233,7 +1236,8 @@ string statusToString(int status) {
|
|||
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
|
||||
if (WIFEXITED(status)) {
|
||||
return (format("failed with exit code %1%") % WEXITSTATUS(status)).str();
|
||||
} else if (WIFSIGNALED(status)) {
|
||||
}
|
||||
if (WIFSIGNALED(status)) {
|
||||
int sig = WTERMSIG(status);
|
||||
#if HAVE_STRSIGNAL
|
||||
const char* description = strsignal(sig);
|
||||
|
|
@ -1294,7 +1298,8 @@ void ignoreException() {
|
|||
|
||||
std::string filterANSIEscapes(const std::string& s, bool filterAll,
|
||||
unsigned int width) {
|
||||
std::string t, e;
|
||||
std::string t;
|
||||
std::string e;
|
||||
size_t w = 0;
|
||||
auto i = s.begin();
|
||||
|
||||
|
|
@ -1333,7 +1338,7 @@ std::string filterANSIEscapes(const std::string& s, bool filterAll,
|
|||
i++;
|
||||
t += ' ';
|
||||
w++;
|
||||
while (w < (size_t)width && w % 8) {
|
||||
while (w < (size_t)width && ((w % 8) != 0u)) {
|
||||
t += ' ';
|
||||
w++;
|
||||
}
|
||||
|
|
@ -1357,7 +1362,8 @@ static char base64Chars[] =
|
|||
|
||||
string base64Encode(const string& s) {
|
||||
string res;
|
||||
int data = 0, nbits = 0;
|
||||
int data = 0;
|
||||
int nbits = 0;
|
||||
|
||||
for (char c : s) {
|
||||
data = data << 8 | (unsigned char)c;
|
||||
|
|
@ -1368,10 +1374,10 @@ string base64Encode(const string& s) {
|
|||
}
|
||||
}
|
||||
|
||||
if (nbits) {
|
||||
if (nbits != 0) {
|
||||
res.push_back(base64Chars[data << (6 - nbits) & 0x3f]);
|
||||
}
|
||||
while (res.size() % 4) {
|
||||
while ((res.size() % 4) != 0u) {
|
||||
res.push_back('=');
|
||||
}
|
||||
|
||||
|
|
@ -1391,7 +1397,8 @@ string base64Decode(const string& s) {
|
|||
}
|
||||
|
||||
string res;
|
||||
unsigned int d = 0, bits = 0;
|
||||
unsigned int d = 0;
|
||||
unsigned int bits = 0;
|
||||
|
||||
for (char c : s) {
|
||||
if (c == '=') {
|
||||
|
|
@ -1478,7 +1485,7 @@ static sigset_t savedSignalMask;
|
|||
void startSignalHandlerThread() {
|
||||
updateWindowSize();
|
||||
|
||||
if (sigprocmask(SIG_BLOCK, nullptr, &savedSignalMask)) {
|
||||
if (sigprocmask(SIG_BLOCK, nullptr, &savedSignalMask) != 0) {
|
||||
throw SysError("quering signal mask");
|
||||
}
|
||||
|
||||
|
|
@ -1489,7 +1496,7 @@ void startSignalHandlerThread() {
|
|||
sigaddset(&set, SIGHUP);
|
||||
sigaddset(&set, SIGPIPE);
|
||||
sigaddset(&set, SIGWINCH);
|
||||
if (pthread_sigmask(SIG_BLOCK, &set, nullptr)) {
|
||||
if (pthread_sigmask(SIG_BLOCK, &set, nullptr) != 0) {
|
||||
throw SysError("blocking signals");
|
||||
}
|
||||
|
||||
|
|
@ -1497,7 +1504,7 @@ void startSignalHandlerThread() {
|
|||
}
|
||||
|
||||
void restoreSignals() {
|
||||
if (sigprocmask(SIG_SETMASK, &savedSignalMask, nullptr)) {
|
||||
if (sigprocmask(SIG_SETMASK, &savedSignalMask, nullptr) != 0) {
|
||||
throw SysError("restoring signals");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
4
third_party/nix/src/libutil/util.hh
vendored
4
third_party/nix/src/libutil/util.hh
vendored
|
|
@ -188,10 +188,10 @@ class AutoCloseFD {
|
|||
AutoCloseFD();
|
||||
AutoCloseFD(int fd);
|
||||
AutoCloseFD(const AutoCloseFD& fd) = delete;
|
||||
AutoCloseFD(AutoCloseFD&& fd);
|
||||
AutoCloseFD(AutoCloseFD&& that);
|
||||
~AutoCloseFD();
|
||||
AutoCloseFD& operator=(const AutoCloseFD& fd) = delete;
|
||||
AutoCloseFD& operator=(AutoCloseFD&& fd);
|
||||
AutoCloseFD& operator=(AutoCloseFD&& that);
|
||||
int get() const;
|
||||
explicit operator bool() const;
|
||||
int release();
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue