aboutsummaryrefslogtreecommitdiff
path: root/src/zen/cmds/print_cmd.cpp
diff options
context:
space:
mode:
authorDan Engelbrecht <[email protected]>2023-09-22 08:22:06 -0400
committerGitHub <[email protected]>2023-09-22 14:22:06 +0200
commitc7d4dc6a4d13881028d566f5ce501335e47e48bf (patch)
tree493110da583a8e5d97fe05e14f23469ee6244d2b /src/zen/cmds/print_cmd.cpp
parentadd trace command to enable/disable tracing at runtime (#416) (diff)
downloadarchived-zen-c7d4dc6a4d13881028d566f5ce501335e47e48bf.tar.xz
archived-zen-c7d4dc6a4d13881028d566f5ce501335e47e48bf.zip
Collect all zen admin-related commands into admin.h/.cpp (#418)
* move commands in scrub.h/cpp to admin_cmd.h/cpp * move job command into admin_cmd.h/.cpp * admin -> admin_cmd * bench -> bench_cmd * cache -> cache_cmd * copy -> copy_cmd * dedup -> dedup_cmd * hash -> hash_cmd * print -> print_cmd * projectstore -> projectstore_cmd * rpcreplay -> rpcreplay_cmd * serve -> serve_cmd * status -> status_cmd * top -> top_cmd * trace -> trace_cmd * up -> up_cmd * version -> version_cmd
Diffstat (limited to 'src/zen/cmds/print_cmd.cpp')
-rw-r--r--src/zen/cmds/print_cmd.cpp193
1 files changed, 193 insertions, 0 deletions
diff --git a/src/zen/cmds/print_cmd.cpp b/src/zen/cmds/print_cmd.cpp
new file mode 100644
index 000000000..acffb2002
--- /dev/null
+++ b/src/zen/cmds/print_cmd.cpp
@@ -0,0 +1,193 @@
+// Copyright Epic Games, Inc. All Rights Reserved.
+
+#include "print_cmd.h"
+
+#include <zencore/compactbinarypackage.h>
+#include <zencore/compactbinaryvalidation.h>
+#include <zencore/filesystem.h>
+#include <zencore/fmtutils.h>
+#include <zencore/logging.h>
+#include <zencore/string.h>
+#include <zenhttp/httpshared.h>
+
+using namespace std::literals;
+
+namespace zen {
+
+static void
+PrintCbObject(CbObject Object)
+{
+ zen::ExtendableStringBuilder<1024> ObjStr;
+ zen::CompactBinaryToJson(Object, ObjStr);
+ ZEN_CONSOLE("{}", ObjStr);
+}
+
+static void
+PrintCbObject(IoBuffer Data)
+{
+ zen::CbObject Object{SharedBuffer(Data)};
+
+ PrintCbObject(Object);
+}
+
+PrintCommand::PrintCommand()
+{
+ m_Options.add_options()("h,help", "Print help");
+ m_Options.add_option("", "s", "source", "Object payload file (use '-' to read from STDIN)", cxxopts::value(m_Filename), "<file name>");
+ m_Options.parse_positional({"source"});
+}
+
+PrintCommand::~PrintCommand() = default;
+
+int
+PrintCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
+{
+ ZEN_UNUSED(GlobalOptions);
+
+ if (!ParseOptions(argc, argv))
+ {
+ return 0;
+ }
+
+ // Validate arguments
+
+ if (m_Filename.empty())
+ throw std::runtime_error("No file specified");
+
+ zen::FileContents Fc;
+
+ if (m_Filename == "-")
+ {
+ Fc = zen::ReadStdIn();
+ }
+ else
+ {
+ Fc = zen::ReadFile(m_Filename);
+ }
+
+ if (Fc.ErrorCode)
+ {
+ ZEN_ERROR("Failed to read file '{}': {}", m_Filename, Fc.ErrorCode.message());
+
+ return 1;
+ }
+
+ IoBuffer Data = Fc.Flatten();
+
+ IoHash RawHash;
+ uint64_t RawSize;
+ if (CompressedBuffer::ValidateCompressedHeader(Data, RawHash, RawSize))
+ {
+ ZEN_CONSOLE("Compressed binary: size {}, raw size {}, hash: {}", Data.GetSize(), RawSize, RawHash);
+ }
+ else if (IsPackageMessage(Data))
+ {
+ CbPackage Package = ParsePackageMessage(Data);
+
+ CbObject Object = Package.GetObject();
+ std::span<const CbAttachment> Attachments = Package.GetAttachments();
+
+ ZEN_CONSOLE("Package - {} attachments, object hash {}", Package.GetAttachments().size(), Package.GetObjectHash());
+ ZEN_CONSOLE("");
+
+ int AttachmentIndex = 1;
+
+ for (const CbAttachment& Attachment : Attachments)
+ {
+ std::string AttachmentSize = "n/a";
+ const char* AttachmentType = "unknown";
+
+ if (Attachment.IsCompressedBinary())
+ {
+ AttachmentType = "Compressed";
+ AttachmentSize = fmt::format("{} ({} uncompressed)",
+ Attachment.AsCompressedBinary().GetCompressedSize(),
+ Attachment.AsCompressedBinary().DecodeRawSize());
+ }
+ else if (Attachment.IsBinary())
+ {
+ AttachmentType = "Binary";
+ AttachmentSize = fmt::format("{}", Attachment.AsBinary().GetSize());
+ }
+ else if (Attachment.IsObject())
+ {
+ AttachmentType = "Object";
+ AttachmentSize = fmt::format("{}", Attachment.AsObject().GetSize());
+ }
+ else if (Attachment.IsNull())
+ {
+ AttachmentType = "null";
+ }
+
+ ZEN_CONSOLE("Attachment #{} : {}, {}, size {}", AttachmentIndex, Attachment.GetHash(), AttachmentType, AttachmentSize);
+
+ ++AttachmentIndex;
+ }
+
+ ZEN_CONSOLE("---8<---");
+
+ PrintCbObject(Object);
+ }
+ else if (CbValidateError Result = ValidateCompactBinary(Data, CbValidateMode::All); Result == CbValidateError::None)
+ {
+ PrintCbObject(Data);
+ }
+ else
+ {
+ ZEN_ERROR("Data in file '{}' does not appear to be compact binary (validation error {:#x})", m_Filename, uint32_t(Result));
+
+ return 1;
+ }
+
+ return 0;
+}
+
+//////////////////////////////////////////////////////////////////////////
+
+PrintPackageCommand::PrintPackageCommand()
+{
+ m_Options.add_options()("h,help", "Print help");
+ m_Options.add_option("", "s", "source", "Package payload file", cxxopts::value(m_Filename), "<file name>");
+ m_Options.parse_positional({"source"});
+}
+
+PrintPackageCommand::~PrintPackageCommand()
+{
+}
+
+int
+PrintPackageCommand::Run(const ZenCliOptions& GlobalOptions, int argc, char** argv)
+{
+ ZEN_UNUSED(GlobalOptions);
+
+ if (!ParseOptions(argc, argv))
+ {
+ return 0;
+ }
+
+ // Validate arguments
+
+ if (m_Filename.empty())
+ throw std::runtime_error("No file specified");
+
+ zen::FileContents Fc = zen::ReadFile(m_Filename);
+ IoBuffer Data = Fc.Flatten();
+ zen::CbPackage Package;
+
+ bool Ok = Package.TryLoad(Data) || zen::legacy::TryLoadCbPackage(Package, Data, &UniqueBuffer::Alloc);
+
+ if (Ok)
+ {
+ zen::ExtendableStringBuilder<1024> ObjStr;
+ zen::CompactBinaryToJson(Package.GetObject(), ObjStr);
+ ZEN_CONSOLE("{}", ObjStr);
+ }
+ else
+ {
+ ZEN_ERROR("error: malformed package?");
+ }
+
+ return 0;
+}
+
+} // namespace zen