From 06ab3faa3338f1b6a8009d2ced6ba2b3ae29341f Mon Sep 17 00:00:00 2001 From: Uwe Seimet Date: Tue, 14 Nov 2023 09:52:58 +0100 Subject: [PATCH] Add binary output file --- cpp/scsictl/scsictl_core.cpp | 28 ++++++++++++++++++++++++++-- cpp/scsictl/scsictl_core.h | 1 + doc/scsictl.1 | 10 +++++++--- doc/scsictl_man_page.txt | 21 +++++++++++++-------- 4 files changed, 47 insertions(+), 13 deletions(-) diff --git a/cpp/scsictl/scsictl_core.cpp b/cpp/scsictl/scsictl_core.cpp index ea1e4c04..63f6dcf5 100644 --- a/cpp/scsictl/scsictl_core.cpp +++ b/cpp/scsictl/scsictl_core.cpp @@ -81,13 +81,14 @@ int ScsiCtl::run(const vector& args) const string token; bool list = false; bool to_json = false; + bool to_binary = false; string locale = GetLocale(); opterr = 1; int opt; while ((opt = getopt(static_cast(args.size()), args.data(), - "e::lmos::vDINOSTVXa:b:c:d:f:h:i:j:n:p:r:t:x:z:C:E:F:L:P::R:")) != -1) { + "e::lmos::vDINOSTVXa:b:c:d:f:h:i:n:p:r:t:x:z:B:C:E:F:J:L:P::R:")) != -1) { switch (opt) { case 'i': if (const string error = SetIdAndLun(*device, optarg); !error.empty()) { @@ -152,11 +153,16 @@ int ScsiCtl::run(const vector& args) const hostname = optarg; break; - case 'j': + case 'J': filename = optarg; to_json = true; break; + case 'B': + filename = optarg; + to_binary = true; + break; + case 'I': command.set_operation(RESERVED_IDS_INFO); break; @@ -276,6 +282,9 @@ int ScsiCtl::run(const vector& args) const if (to_json) { return ExportAsJson(command, filename); } + if (to_binary) { + return ExportAsBinary(command, filename); + } ScsictlCommands scsictl_commands(command, hostname, port); @@ -321,3 +330,18 @@ int ScsiCtl::ExportAsJson(const PbCommand &command, const string &filename) cons return EXIT_SUCCESS; } + +int ScsiCtl::ExportAsBinary(const PbCommand &command, const string &filename) const +{ + const string binary = command.SerializeAsString(); + + ofstream out; + out.open(filename, ios::binary); + out << binary; + if (out.fail()) { + cerr << "Error: Can't create JSON file '" << filename << "'" << endl; + return EXIT_FAILURE; + } + + return EXIT_SUCCESS; +} diff --git a/cpp/scsictl/scsictl_core.h b/cpp/scsictl/scsictl_core.h index 05079398..4914fe79 100644 --- a/cpp/scsictl/scsictl_core.h +++ b/cpp/scsictl/scsictl_core.h @@ -30,4 +30,5 @@ private: void Banner(const vector&) const; int ExportAsJson(const PbCommand&, const string&) const; + int ExportAsBinary(const PbCommand&, const string&) const; }; diff --git a/doc/scsictl.1 b/doc/scsictl.1 index 34665861..04de71bf 100644 --- a/doc/scsictl.1 +++ b/doc/scsictl.1 @@ -18,7 +18,8 @@ scsictl \- Sends management commands to the piscsi process \fB\-V\fR | \fB\-X\fR | [\fB\-d\fR \fIFILENAME\fR] | -[\fB\-j\fR \fIFILENAME\fR] | +[\fB\-B\fR \fIFILENAME\fR] | +[\fB\-J\fR \fIFILENAME\fR] | [\fB\-C\fR \fIFILENAME:FILESIZE\fR] | [\fB\-E\fR \fIFILENAME\fR] | [\fB\-F\fR \fIIMAGE_FOLDER\fR] | @@ -119,8 +120,11 @@ Shut down the piscsi process. .BR \-d\fI " "\fIFILENAME Delete an image file in the default image folder. .TP -.BR \-j\fI " "\fIFILENAME -Do not send command to piscsi but write it to a JSON file. +.BR \-B\fI " "\fIFILENAME +Do not send command to piscsi but write it to a binary protobuf file. +.TP +.BR \-J\fI " "\fIFILENAME +Do not send command to piscsi but write it to a JSON protobuf file. .TP .BR \-x\fI " "\fICURRENT_NAME:NEW_NAME Copy an image file in the default image folder. diff --git a/doc/scsictl_man_page.txt b/doc/scsictl_man_page.txt index d494946c..a1919306 100644 --- a/doc/scsictl_man_page.txt +++ b/doc/scsictl_man_page.txt @@ -7,12 +7,12 @@ NAME SYNOPSIS scsictl -e | -l | -m | -o | -v | -D | -I | -L | -O | -P | -S | -T | -V - | -X | [-d FILENAME] | [-j FILENAME] | [-C FILENAME:FILESIZE] | [-E - FILENAME] | [-F IMAGE_FOLDER] | [-R CURRENT_NAME:NEW_NAME] | [-c CMD] | - [-f FILE|PARAM] | [-g LOG_LEVEL] | [-h HOST] | [-i ID[:LUN]] | [-n - NAME] | [-p PORT] | [-r RESERVED_IDS] | [-s [FOLDER_PATTERN:FILE_PAT‐ - TERN:OPERATIONS]] | [-t TYPE] | [-x CURRENT_NAME:NEW_NAME] | [-z LO‐ - CALE] + | -X | [-d FILENAME] | [-B FILENAME] | [-J FILENAME] | [-C FILE‐ + NAME:FILESIZE] | [-E FILENAME] | [-F IMAGE_FOLDER] | [-R CUR‐ + RENT_NAME:NEW_NAME] | [-c CMD] | [-f FILE|PARAM] | [-g LOG_LEVEL] | [-h + HOST] | [-i ID[:LUN]] | [-n NAME] | [-p PORT] | [-r RESERVED_IDS] | [-s + [FOLDER_PATTERN:FILE_PATTERN:OPERATIONS]] | [-t TYPE] | [-x CUR‐ + RENT_NAME:NEW_NAME] | [-z LOCALE] DESCRIPTION scsictl sends commands to the piscsi process to make configuration ad‐ @@ -94,8 +94,13 @@ OPTIONS -d FILENAME Delete an image file in the default image folder. - -j FILENAME - Do not send command to piscsi but write it to a JSON file. + -B FILENAME + Do not send command to piscsi but write it to a binary protobuf + file. + + -J FILENAME + Do not send command to piscsi but write it to a JSON protobuf + file. -x CURRENT_NAME:NEW_NAME Copy an image file in the default image folder.