1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-06-29 06:21:14 +02:00

Revert the enum struct change

Not a regular git revert as there have been many merges and things.
This commit is contained in:
John Ericson 2020-06-18 22:09:22 +00:00
parent bbbf3602a3
commit 15abb2aa2b
68 changed files with 418 additions and 457 deletions

View file

@ -10,7 +10,7 @@ namespace nix {
MakeError(UsageError, Error);
enum struct HashType : char;
enum HashType : char;
class Args
{

View file

@ -314,7 +314,7 @@ struct XzCompressionSink : CompressionSink
ret = lzma_stream_encoder_mt(&strm, &mt_options);
done = true;
#else
printMsg(Verbosity::Error, "warning: parallel XZ compression requested but not supported, falling back to single-threaded compression");
printMsg(lvlError, "warning: parallel XZ compression requested but not supported, falling back to single-threaded compression");
#endif
}

View file

@ -110,50 +110,50 @@ std::ostream& operator<<(std::ostream &out, const ErrorInfo &einfo)
string levelString;
switch (einfo.level) {
case Verbosity::Error: {
case Verbosity::lvlError: {
levelString = ANSI_RED;
levelString += "error:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Warn: {
case Verbosity::lvlWarn: {
levelString = ANSI_YELLOW;
levelString += "warning:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Info: {
case Verbosity::lvlInfo: {
levelString = ANSI_GREEN;
levelString += "info:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Talkative: {
case Verbosity::lvlTalkative: {
levelString = ANSI_GREEN;
levelString += "talk:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Chatty: {
case Verbosity::lvlChatty: {
levelString = ANSI_GREEN;
levelString += "chat:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Vomit: {
case Verbosity::lvlVomit: {
levelString = ANSI_GREEN;
levelString += "vomit:";
levelString += ANSI_NORMAL;
break;
}
case Verbosity::Debug: {
case Verbosity::lvlDebug: {
levelString = ANSI_YELLOW;
levelString += "debug:";
levelString += ANSI_NORMAL;
break;
}
default: {
levelString = fmt("invalid error level: %d", (uint8_t)einfo.level);
levelString = fmt("invalid error level: %1%", einfo.level);
break;
}
}

View file

@ -40,15 +40,15 @@ See the error-demo.cc program for usage examples.
*/
enum struct Verbosity {
Error = 0,
Warn,
Info,
Talkative,
Chatty,
Debug,
Vomit,
};
typedef enum {
lvlError = 0,
lvlWarn,
lvlInfo,
lvlTalkative,
lvlChatty,
lvlDebug,
lvlVomit
} Verbosity;
// ErrPos indicates the location of an error in a nix file.
struct ErrPos {
@ -113,7 +113,7 @@ public:
template<typename... Args>
BaseError(unsigned int status, const Args & ... args)
: err { .level = Verbosity::Error,
: err { .level = lvlError,
.hint = hintfmt(args...)
}
, status(status)
@ -121,13 +121,13 @@ public:
template<typename... Args>
BaseError(const std::string & fs, const Args & ... args)
: err { .level = Verbosity::Error,
: err { .level = lvlError,
.hint = hintfmt(fs, args...)
}
{ }
BaseError(hintformat hint)
: err { .level = Verbosity::Error,
: err { .level = lvlError,
.hint = hint
}
{ }

View file

@ -21,10 +21,10 @@ void Hash::init()
{
if (!type) abort();
switch (*type) {
case HashType::MD5: hashSize = md5HashSize; break;
case HashType::SHA1: hashSize = sha1HashSize; break;
case HashType::SHA256: hashSize = sha256HashSize; break;
case HashType::SHA512: hashSize = sha512HashSize; break;
case htMD5: hashSize = md5HashSize; break;
case htSHA1: hashSize = sha1HashSize; break;
case htSHA256: hashSize = sha256HashSize; break;
case htSHA512: hashSize = sha512HashSize; break;
}
assert(hashSize <= maxHashSize);
memset(hash, 0, maxHashSize);
@ -101,7 +101,7 @@ static string printHash32(const Hash & hash)
string printHash16or32(const Hash & hash)
{
return hash.to_string(hash.type == HashType::MD5 ? Base::Base16 : Base::Base32, false);
return hash.to_string(hash.type == htMD5 ? Base16 : Base32, false);
}
@ -115,19 +115,19 @@ HashType assertInitHashType(const Hash & h) {
std::string Hash::to_string(Base base, bool includeType) const
{
std::string s;
if (base == Base::SRI || includeType) {
if (base == SRI || includeType) {
s += printHashType(assertInitHashType(*this));
s += base == Base::SRI ? '-' : ':';
s += base == SRI ? '-' : ':';
}
switch (base) {
case Base::Base16:
case Base16:
s += printHash16(*this);
break;
case Base::Base32:
case Base32:
s += printHash32(*this);
break;
case Base::Base64:
case Base::SRI:
case Base64:
case SRI:
s += base64Encode(std::string((const char *) hash, hashSize));
break;
}
@ -241,29 +241,29 @@ union Ctx
static void start(HashType ht, Ctx & ctx)
{
if (ht == HashType::MD5) MD5_Init(&ctx.md5);
else if (ht == HashType::SHA1) SHA1_Init(&ctx.sha1);
else if (ht == HashType::SHA256) SHA256_Init(&ctx.sha256);
else if (ht == HashType::SHA512) SHA512_Init(&ctx.sha512);
if (ht == htMD5) MD5_Init(&ctx.md5);
else if (ht == htSHA1) SHA1_Init(&ctx.sha1);
else if (ht == htSHA256) SHA256_Init(&ctx.sha256);
else if (ht == htSHA512) SHA512_Init(&ctx.sha512);
}
static void update(HashType ht, Ctx & ctx,
const unsigned char * bytes, size_t len)
{
if (ht == HashType::MD5) MD5_Update(&ctx.md5, bytes, len);
else if (ht == HashType::SHA1) SHA1_Update(&ctx.sha1, bytes, len);
else if (ht == HashType::SHA256) SHA256_Update(&ctx.sha256, bytes, len);
else if (ht == HashType::SHA512) SHA512_Update(&ctx.sha512, bytes, len);
if (ht == htMD5) MD5_Update(&ctx.md5, bytes, len);
else if (ht == htSHA1) SHA1_Update(&ctx.sha1, bytes, len);
else if (ht == htSHA256) SHA256_Update(&ctx.sha256, bytes, len);
else if (ht == htSHA512) SHA512_Update(&ctx.sha512, bytes, len);
}
static void finish(HashType ht, Ctx & ctx, unsigned char * hash)
{
if (ht == HashType::MD5) MD5_Final(hash, &ctx.md5);
else if (ht == HashType::SHA1) SHA1_Final(hash, &ctx.sha1);
else if (ht == HashType::SHA256) SHA256_Final(hash, &ctx.sha256);
else if (ht == HashType::SHA512) SHA512_Final(hash, &ctx.sha512);
if (ht == htMD5) MD5_Final(hash, &ctx.md5);
else if (ht == htSHA1) SHA1_Final(hash, &ctx.sha1);
else if (ht == htSHA256) SHA256_Final(hash, &ctx.sha256);
else if (ht == htSHA512) SHA512_Final(hash, &ctx.sha512);
}
@ -344,10 +344,10 @@ Hash compressHash(const Hash & hash, unsigned int newSize)
std::optional<HashType> parseHashTypeOpt(const string & s)
{
if (s == "md5") return HashType::MD5;
else if (s == "sha1") return HashType::SHA1;
else if (s == "sha256") return HashType::SHA256;
else if (s == "sha512") return HashType::SHA512;
if (s == "md5") return htMD5;
else if (s == "sha1") return htSHA1;
else if (s == "sha256") return htSHA256;
else if (s == "sha512") return htSHA512;
else return std::optional<HashType> {};
}
@ -362,14 +362,15 @@ HashType parseHashType(const string & s)
string printHashType(HashType ht)
{
string ret;
switch (ht) {
case HashType::MD5: ret = "md5"; break;
case HashType::SHA1: ret = "sha1"; break;
case HashType::SHA256: ret = "sha256"; break;
case HashType::SHA512: ret = "sha512"; break;
case htMD5: return "md5"; break;
case htSHA1: return "sha1"; break;
case htSHA256: return "sha256"; break;
case htSHA512: return "sha512"; break;
}
return ret;
// illegal hash type enum value internally, as opposed to external input
// which should be validated with nice error message.
abort();
}
}

View file

@ -10,12 +10,7 @@ namespace nix {
MakeError(BadHash, Error);
enum struct HashType : char {
MD5,
SHA1,
SHA256,
SHA512,
};
enum HashType : char { htMD5, htSHA1, htSHA256, htSHA512 };
const int md5HashSize = 16;
@ -25,12 +20,7 @@ const int sha512HashSize = 64;
extern const string base32Chars;
enum struct Base {
Base64,
Base32,
Base16,
SRI,
};
enum Base : int { Base64, Base32, Base16, SRI };
struct Hash
@ -97,14 +87,14 @@ struct Hash
std::string gitRev() const
{
assert(type == HashType::SHA1);
return to_string(Base::Base16, false);
assert(type == htSHA1);
return to_string(Base16, false);
}
std::string gitShortRev() const
{
assert(type == HashType::SHA1);
return std::string(to_string(Base::Base16, false), 0, 7);
assert(type == htSHA1);
return std::string(to_string(Base16, false), 0, 7);
}
};

View file

@ -22,7 +22,7 @@ Logger * logger = makeSimpleLogger(true);
void Logger::warn(const std::string & msg)
{
log(Verbosity::Warn, ANSI_YELLOW "warning:" ANSI_NORMAL " " + msg);
log(lvlWarn, ANSI_YELLOW "warning:" ANSI_NORMAL " " + msg);
}
void Logger::writeToStdout(std::string_view s)
@ -57,10 +57,10 @@ public:
if (systemd) {
char c;
switch (lvl) {
case Verbosity::Error: c = '3'; break;
case Verbosity::Warn: c = '4'; break;
case Verbosity::Info: c = '5'; break;
case Verbosity::Talkative: case Verbosity::Chatty: c = '6'; break;
case lvlError: c = '3'; break;
case lvlWarn: c = '4'; break;
case lvlInfo: c = '5'; break;
case lvlTalkative: case lvlChatty: c = '6'; break;
default: c = '7';
}
prefix = std::string("<") + c + ">";
@ -87,18 +87,18 @@ public:
void result(ActivityId act, ResultType type, const Fields & fields) override
{
if (type == ResultType::BuildLogLine && printBuildLogs) {
if (type == resBuildLogLine && printBuildLogs) {
auto lastLine = fields[0].s;
printError(lastLine);
}
else if (type == ResultType::PostBuildLogLine && printBuildLogs) {
else if (type == resPostBuildLogLine && printBuildLogs) {
auto lastLine = fields[0].s;
printError("post-build-hook: " + lastLine);
}
}
};
Verbosity verbosity = Verbosity::Info;
Verbosity verbosity = lvlInfo;
void warnOnce(bool & haveWarned, const FormatOrString & fs)
{
@ -158,7 +158,7 @@ struct JSONLogger : Logger {
void write(const nlohmann::json & json)
{
prevLogger.log(Verbosity::Error, "@nix " + json.dump());
prevLogger.log(lvlError, "@nix " + json.dump());
}
void log(Verbosity lvl, const FormatOrString & fs) override
@ -246,7 +246,7 @@ bool handleJSONLogMessage(const std::string & msg,
if (action == "start") {
auto type = (ActivityType) json["type"];
if (trusted || type == ActivityType::Download)
if (trusted || type == actFileTransfer)
activities.emplace(std::piecewise_construct,
std::forward_as_tuple(json["id"]),
std::forward_as_tuple(*logger, (Verbosity) json["level"], type,
@ -264,7 +264,7 @@ bool handleJSONLogMessage(const std::string & msg,
else if (action == "setPhase") {
std::string phase = json["phase"];
act.result(ResultType::SetPhase, phase);
act.result(resSetPhase, phase);
}
else if (action == "msg") {

View file

@ -5,32 +5,32 @@
namespace nix {
enum struct ActivityType {
Unknown = 0,
CopyPath = 100,
Download = 101,
Realise = 102,
CopyPaths = 103,
Builds = 104,
Build = 105,
OptimiseStore = 106,
VerifyPaths = 107,
Substitute = 108,
QueryPathInfo = 109,
PostBuildHook = 110,
BuildWaiting = 111,
};
typedef enum {
actUnknown = 0,
actCopyPath = 100,
actFileTransfer = 101,
actRealise = 102,
actCopyPaths = 103,
actBuilds = 104,
actBuild = 105,
actOptimiseStore = 106,
actVerifyPaths = 107,
actSubstitute = 108,
actQueryPathInfo = 109,
actPostBuildHook = 110,
actBuildWaiting = 111,
} ActivityType;
enum struct ResultType {
FileLinked = 100,
BuildLogLine = 101,
UntrustedPath = 102,
CorruptedPath = 103,
SetPhase = 104,
Progress = 105,
SetExpected = 106,
PostBuildLogLine = 107,
};
typedef enum {
resFileLinked = 100,
resBuildLogLine = 101,
resUntrustedPath = 102,
resCorruptedPath = 103,
resSetPhase = 104,
resProgress = 105,
resSetExpected = 106,
resPostBuildLogLine = 107,
} ResultType;
typedef uint64_t ActivityId;
@ -64,7 +64,7 @@ public:
void log(const FormatOrString & fs)
{
log(Verbosity::Info, fs);
log(lvlInfo, fs);
}
virtual void logEI(const ErrorInfo &ei) = 0;
@ -109,17 +109,17 @@ struct Activity
Activity(Logger & logger, ActivityType type,
const Logger::Fields & fields = {}, ActivityId parent = getCurActivity())
: Activity(logger, Verbosity::Error, type, "", fields, parent) { };
: Activity(logger, lvlError, type, "", fields, parent) { };
Activity(const Activity & act) = delete;
~Activity();
void progress(uint64_t done = 0, uint64_t expected = 0, uint64_t running = 0, uint64_t failed = 0) const
{ result(ResultType::Progress, done, expected, running, failed); }
{ result(resProgress, done, expected, running, failed); }
void setExpected(ActivityType type2, uint64_t expected) const
{ result(ResultType::SetExpected, (uint64_t)type2, expected); }
{ result(resSetExpected, type2, expected); }
template<typename... Args>
void result(ResultType type, const Args & ... args) const
@ -167,8 +167,8 @@ extern Verbosity verbosity; /* suppress msgs > this */
} \
} while (0)
#define logError(errorInfo...) logErrorInfo(Verbosity::Error, errorInfo)
#define logWarning(errorInfo...) logErrorInfo(Verbosity::Warn, errorInfo)
#define logError(errorInfo...) logErrorInfo(lvlError, errorInfo)
#define logWarning(errorInfo...) logErrorInfo(lvlWarn, errorInfo)
/* Print a string message if the current log level is at least the specified
level. Note that this has to be implemented as a macro to ensure that the
@ -180,13 +180,13 @@ extern Verbosity verbosity; /* suppress msgs > this */
} \
} while (0)
#define printError(args...) printMsg(Verbosity::Error, args)
#define printInfo(args...) printMsg(Verbosity::Info, args)
#define printTalkative(args...) printMsg(Verbosity::Talkative, args)
#define debug(args...) printMsg(Verbosity::Debug, args)
#define vomit(args...) printMsg(Verbosity::Vomit, args)
#define printError(args...) printMsg(lvlError, args)
#define printInfo(args...) printMsg(lvlInfo, args)
#define printTalkative(args...) printMsg(lvlTalkative, args)
#define debug(args...) printMsg(lvlDebug, args)
#define vomit(args...) printMsg(lvlVomit, args)
/* if verbosity >= Verbosity::Warn, print a message with a yellow 'warning:' prefix. */
/* if verbosity >= lvlWarn, print a message with a yellow 'warning:' prefix. */
template<typename... Args>
inline void warn(const std::string & fs, const Args & ... args)
{

View file

@ -10,28 +10,28 @@ namespace nix {
TEST(hashString, testKnownMD5Hashes1) {
// values taken from: https://tools.ietf.org/html/rfc1321
auto s1 = "";
auto hash = hashString(HashType::MD5, s1);
auto hash = hashString(HashType::htMD5, s1);
ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:d41d8cd98f00b204e9800998ecf8427e");
}
TEST(hashString, testKnownMD5Hashes2) {
// values taken from: https://tools.ietf.org/html/rfc1321
auto s2 = "abc";
auto hash = hashString(HashType::MD5, s2);
auto hash = hashString(HashType::htMD5, s2);
ASSERT_EQ(hash.to_string(Base::Base16, true), "md5:900150983cd24fb0d6963f7d28e17f72");
}
TEST(hashString, testKnownSHA1Hashes1) {
// values taken from: https://tools.ietf.org/html/rfc3174
auto s = "abc";
auto hash = hashString(HashType::SHA1, s);
auto hash = hashString(HashType::htSHA1, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:a9993e364706816aba3e25717850c26c9cd0d89d");
}
TEST(hashString, testKnownSHA1Hashes2) {
// values taken from: https://tools.ietf.org/html/rfc3174
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashType::SHA1, s);
auto hash = hashString(HashType::htSHA1, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),"sha1:84983e441c3bd26ebaae4aa1f95129e5e54670f1");
}
@ -39,7 +39,7 @@ namespace nix {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abc";
auto hash = hashString(HashType::SHA256, s);
auto hash = hashString(HashType::htSHA256, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad");
}
@ -47,7 +47,7 @@ namespace nix {
TEST(hashString, testKnownSHA256Hashes2) {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq";
auto hash = hashString(HashType::SHA256, s);
auto hash = hashString(HashType::htSHA256, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha256:248d6a61d20638b8e5c026930c3e6039a33ce45964ff2167f6ecedd419db06c1");
}
@ -55,7 +55,7 @@ namespace nix {
TEST(hashString, testKnownSHA512Hashes1) {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abc";
auto hash = hashString(HashType::SHA512, s);
auto hash = hashString(HashType::htSHA512, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha512:ddaf35a193617abacc417349ae20413112e6fa4e89a9"
"7ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd"
@ -66,7 +66,7 @@ namespace nix {
// values taken from: https://tools.ietf.org/html/rfc4634
auto s = "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu";
auto hash = hashString(HashType::SHA512, s);
auto hash = hashString(HashType::htSHA512, s);
ASSERT_EQ(hash.to_string(Base::Base16, true),
"sha512:8e959b75dae313da8cf4f72814fc143f8f7779c6eb9f7fa1"
"7299aeadb6889018501d289e4900f7e4331b99dec4b5433a"

View file

@ -68,7 +68,7 @@ namespace nix {
TEST(logEI, loggingErrorOnInfoLevel) {
testing::internal::CaptureStderr();
logger->logEI({ .level = Verbosity::Info,
logger->logEI({ .level = lvlInfo,
.name = "Info name",
.description = "Info description",
});
@ -78,11 +78,11 @@ namespace nix {
}
TEST(logEI, loggingErrorOnTalkativeLevel) {
verbosity = Verbosity::Talkative;
verbosity = lvlTalkative;
testing::internal::CaptureStderr();
logger->logEI({ .level = Verbosity::Talkative,
logger->logEI({ .level = lvlTalkative,
.name = "Talkative name",
.description = "Talkative description",
});
@ -92,11 +92,11 @@ namespace nix {
}
TEST(logEI, loggingErrorOnChattyLevel) {
verbosity = Verbosity::Chatty;
verbosity = lvlChatty;
testing::internal::CaptureStderr();
logger->logEI({ .level = Verbosity::Chatty,
logger->logEI({ .level = lvlChatty,
.name = "Chatty name",
.description = "Talkative description",
});
@ -106,11 +106,11 @@ namespace nix {
}
TEST(logEI, loggingErrorOnDebugLevel) {
verbosity = Verbosity::Debug;
verbosity = lvlDebug;
testing::internal::CaptureStderr();
logger->logEI({ .level = Verbosity::Debug,
logger->logEI({ .level = lvlDebug,
.name = "Debug name",
.description = "Debug description",
});
@ -120,11 +120,11 @@ namespace nix {
}
TEST(logEI, loggingErrorOnVomitLevel) {
verbosity = Verbosity::Vomit;
verbosity = lvlVomit;
testing::internal::CaptureStderr();
logger->logEI({ .level = Verbosity::Vomit,
logger->logEI({ .level = lvlVomit,
.name = "Vomit name",
.description = "Vomit description",
});

View file

@ -442,7 +442,7 @@ void deletePath(const Path & path)
void deletePath(const Path & path, unsigned long long & bytesFreed)
{
//Activity act(*logger, Verbosity::Debug, format("recursively deleting path '%1%'") % path);
//Activity act(*logger, lvlDebug, format("recursively deleting path '%1%'") % path);
bytesFreed = 0;
_deletePath(path, bytesFreed);
}
@ -1433,7 +1433,7 @@ string base64Decode(std::string_view s)
char digit = decode[(unsigned char) c];
if (digit == -1)
throw Error("invalid character in Base::Base64 string");
throw Error("invalid character in Base64 string");
bits += 6;
d = d << 6 | digit;