tools: normalize exit codes
Change-Id: I6811d1fbfef817e74b5920ccb0026447c3238051
diff --git a/tools/ndngetfile.cpp b/tools/ndngetfile.cpp
index b552ec8..4844fd6 100644
--- a/tools/ndngetfile.cpp
+++ b/tools/ndngetfile.cpp
@@ -23,6 +23,7 @@
#include <fstream>
#include <iostream>
+#include <unistd.h>
#include <boost/lexical_cast.hpp>
@@ -260,20 +261,21 @@
}
}
-static int
-usage(const std::string& filename)
+static void
+usage(const char* programName)
{
- std::cerr << "Usage: \n "
- << filename << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n\n"
- << "-v: be verbose\n"
- << "-s: only get single data packet\n"
- << "-u: versioned: ndn-name contains version component\n"
- << " if -u is not specified, this command will return the rightmost child for the prefix\n"
- << "-l: InterestLifetime in milliseconds\n"
- << "-w: timeout in milliseconds for whole process (default unlimited)\n"
- << "-o: write to local file name instead of stdout\n"
- << "ndn-name: NDN Name prefix for Data to be read\n";
- return 1;
+ std::cerr << "Usage: "
+ << programName << " [-v] [-s] [-u] [-l lifetime] [-w timeout] [-o filename] ndn-name\n"
+ << "\n"
+ << " -v: be verbose\n"
+ << " -s: only get single data packet\n"
+ << " -u: versioned: ndn-name contains version component\n"
+ << " if -u is not specified, this command will return the rightmost child for the prefix\n"
+ << " -l: InterestLifetime in milliseconds\n"
+ << " -w: timeout in milliseconds for whole process (default unlimited)\n"
+ << " -o: write to local file name instead of stdout\n"
+ << " ndn-name: NDN Name prefix for Data to be read\n"
+ << std::endl;
}
static int
@@ -286,42 +288,46 @@
int timeout = 0; // in milliseconds
int opt;
- while ((opt = getopt(argc, argv, "vsul:w:o:")) != -1) {
+ while ((opt = getopt(argc, argv, "hvsul:w:o:")) != -1) {
switch (opt) {
- case 'v':
- verbose = true;
- break;
- case 's':
- single = true;
- break;
- case 'u':
- versioned = true;
- break;
- case 'l':
- try {
- interestLifetime = boost::lexical_cast<int>(optarg);
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "ERROR: -l option should be an integer" << std::endl;
- return 1;
- }
- interestLifetime = std::max(interestLifetime, 0);
- break;
- case 'w':
- try {
- timeout = boost::lexical_cast<int>(optarg);
- }
- catch (const boost::bad_lexical_cast&) {
- std::cerr << "ERROR: -w option should be an integer" << std::endl;
- return 1;
- }
- timeout = std::max(timeout, 0);
- break;
- case 'o':
- outputFile = optarg;
- break;
- default:
- return usage(argv[0]);
+ case 'h':
+ usage(argv[0]);
+ return 0;
+ case 'v':
+ verbose = true;
+ break;
+ case 's':
+ single = true;
+ break;
+ case 'u':
+ versioned = true;
+ break;
+ case 'l':
+ try {
+ interestLifetime = boost::lexical_cast<int>(optarg);
+ }
+ catch (const boost::bad_lexical_cast&) {
+ std::cerr << "ERROR: -l option should be an integer" << std::endl;
+ return 2;
+ }
+ interestLifetime = std::max(interestLifetime, 0);
+ break;
+ case 'w':
+ try {
+ timeout = boost::lexical_cast<int>(optarg);
+ }
+ catch (const boost::bad_lexical_cast&) {
+ std::cerr << "ERROR: -w option should be an integer" << std::endl;
+ return 2;
+ }
+ timeout = std::max(timeout, 0);
+ break;
+ case 'o':
+ outputFile = optarg;
+ break;
+ default:
+ usage(argv[0]);
+ return 2;
}
}
@@ -330,7 +336,8 @@
}
if (name.empty()) {
- return usage(argv[0]);
+ usage(argv[0]);
+ return 2;
}
std::streambuf* buf;
@@ -340,7 +347,7 @@
of.open(outputFile, std::ios::out | std::ios::binary | std::ios::trunc);
if (!of || !of.is_open()) {
std::cerr << "ERROR: cannot open " << outputFile << std::endl;
- return 1;
+ return 2;
}
buf = of.rdbuf();
}
@@ -356,6 +363,7 @@
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
+ return 1;
}
return 0;
diff --git a/tools/ndnputfile.cpp b/tools/ndnputfile.cpp
index 97dbd77..2564a60 100644
--- a/tools/ndnputfile.cpp
+++ b/tools/ndnputfile.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -28,13 +28,13 @@
#include <stdint.h>
#include <stdlib.h>
+#include <unistd.h>
#include <fstream>
#include <iostream>
#include <string>
#include <boost/asio.hpp>
-#include <boost/filesystem.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/iostreams/operations.hpp>
#include <boost/iostreams/read.hpp>
@@ -46,8 +46,6 @@
using std::shared_ptr;
using std::make_shared;
using std::bind;
-using std::placeholders::_1;
-using std::placeholders::_2;
static const uint64_t DEFAULT_BLOCK_SIZE = 1000;
static const uint64_t DEFAULT_INTEREST_LIFETIME = 4000;
@@ -61,11 +59,7 @@
class Error : public std::runtime_error
{
public:
- explicit
- Error(const std::string& what)
- : std::runtime_error(what)
- {
- }
+ using std::runtime_error::runtime_error;
};
NdnPutFile()
@@ -76,7 +70,7 @@
, interestLifetime(DEFAULT_INTEREST_LIFETIME)
, hasTimeout(false)
, timeout(0)
- , insertStream(0)
+ , insertStream(nullptr)
, isVerbose(false)
, m_scheduler(m_face.getIoService())
, m_timestampVersion(toUnixTimestamp(system_clock::now()).count())
@@ -188,17 +182,13 @@
for (size_t i = 0; i < nDataToPrepare && !m_isFinished; ++i) {
uint8_t buffer[DEFAULT_BLOCK_SIZE];
-
- std::streamsize readSize =
- boost::iostreams::read(*insertStream, reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE);
-
+ auto readSize = boost::iostreams::read(*insertStream,
+ reinterpret_cast<char*>(buffer), DEFAULT_BLOCK_SIZE);
if (readSize <= 0) {
BOOST_THROW_EXCEPTION(Error("Error reading from the input stream"));
}
- shared_ptr<ndn::Data> data =
- make_shared<ndn::Data>(Name(m_dataPrefix)
- .appendSegment(m_currentSegmentNo));
+ auto data = make_shared<ndn::Data>(Name(m_dataPrefix).appendSegment(m_currentSegmentNo));
if (insertStream->peek() == std::istream::traits_type::eof()) {
data->setFinalBlock(ndn::name::Component::fromSegment(m_currentSegmentNo));
@@ -232,9 +222,8 @@
bind(&NdnPutFile::onRegisterSuccess, this, _1),
bind(&NdnPutFile::onRegisterFailed, this, _1, _2));
-
if (hasTimeout)
- m_scheduler.scheduleEvent(timeout, bind(&NdnPutFile::stopProcess, this));
+ m_scheduler.scheduleEvent(timeout, [this] { stopProcess(); });
m_face.processEvents();
}
@@ -265,15 +254,14 @@
NdnPutFile::onInsertCommandResponse(const ndn::Interest& interest, const ndn::Data& data)
{
RepoCommandResponse response(data.getContent().blockFromValue());
- int statusCode = response.getCode();
+ auto statusCode = response.getCode();
if (statusCode >= 400) {
BOOST_THROW_EXCEPTION(Error("insert command failed with code " +
boost::lexical_cast<std::string>(statusCode)));
}
m_processId = response.getProcessId();
- m_scheduler.scheduleEvent(m_checkPeriod,
- bind(&NdnPutFile::startCheckCommand, this));
+ m_scheduler.scheduleEvent(m_checkPeriod, [this] { startCheckCommand(); });
}
void
@@ -347,7 +335,7 @@
BOOST_THROW_EXCEPTION(Error("Input data does not fit into one Data packet"));
}
- shared_ptr<ndn::Data> data = make_shared<ndn::Data>(m_dataPrefix);
+ auto data = make_shared<ndn::Data>(m_dataPrefix);
data->setContent(buffer, readSize);
data->setFreshnessPeriod(freshnessPeriod);
signData(*data);
@@ -397,7 +385,7 @@
NdnPutFile::onCheckCommandResponse(const ndn::Interest& interest, const ndn::Data& data)
{
RepoCommandResponse response(data.getContent().blockFromValue());
- int statusCode = response.getCode();
+ auto statusCode = response.getCode();
if (statusCode >= 400) {
BOOST_THROW_EXCEPTION(Error("Insert check command failed with code: " +
boost::lexical_cast<std::string>(statusCode)));
@@ -421,8 +409,7 @@
}
}
- m_scheduler.scheduleEvent(m_checkPeriod,
- bind(&NdnPutFile::startCheckCommand, this));
+ m_scheduler.scheduleEvent(m_checkPeriod, [this] { startCheckCommand(); });
}
void
@@ -452,36 +439,40 @@
}
static void
-usage()
+usage(const char* programName)
{
- fprintf(stderr,
- "ndnputfile [-u] [-s] [-D] [-d] [-i identity] [-I identity]"
- " [-x freshness] [-l lifetime] [-w timeout] repo-prefix ndn-name filename\n"
- "\n"
- " Write a file into a repo.\n"
- " -u: unversioned: do not add a version component\n"
- " -s: single: do not add version or segment component, implies -u\n"
- " -D: use DigestSha256 signing method instead of SignatureSha256WithRsa\n"
- " -i: specify identity used for signing Data\n"
- " -I: specify identity used for signing commands\n"
- " -x: FreshnessPeriod in milliseconds\n"
- " -l: InterestLifetime in milliseconds for each command\n"
- " -w: timeout in milliseconds for whole process (default unlimited)\n"
- " -v: be verbose\n"
- " repo-prefix: repo command prefix\n"
- " ndn-name: NDN Name prefix for written Data\n"
- " filename: local file name; \"-\" reads from stdin\n"
- );
- exit(1);
+ std::cerr << "Usage: "
+ << programName << " [-u] [-s] [-D] [-d] [-i identity] [-I identity] [-x freshness]"
+ " [-l lifetime] [-w timeout] repo-prefix ndn-name filename\n"
+ << "\n"
+ << "Write a file into a repo.\n"
+ << "\n"
+ << " -u: unversioned: do not add a version component\n"
+ << " -s: single: do not add version or segment component, implies -u\n"
+ << " -D: use DigestSha256 signing method instead of SignatureSha256WithRsa\n"
+ << " -i: specify identity used for signing Data\n"
+ << " -I: specify identity used for signing commands\n"
+ << " -x: FreshnessPeriod in milliseconds\n"
+ << " -l: InterestLifetime in milliseconds for each command\n"
+ << " -w: timeout in milliseconds for whole process (default unlimited)\n"
+ << " -v: be verbose\n"
+ << " repo-prefix: repo command prefix\n"
+ << " ndn-name: NDN Name prefix for written Data\n"
+ << " filename: local file name; \"-\" reads from stdin\n"
+ << std::endl;
}
-int
+static int
main(int argc, char** argv)
{
NdnPutFile ndnPutFile;
+
int opt;
- while ((opt = getopt(argc, argv, "usDi:I:x:l:w:vh")) != -1) {
+ while ((opt = getopt(argc, argv, "husDi:I:x:l:w:v")) != -1) {
switch (opt) {
+ case 'h':
+ usage(argv[0]);
+ return 0;
case 'u':
ndnPutFile.isUnversioned = true;
break;
@@ -503,7 +494,7 @@
}
catch (const boost::bad_lexical_cast&) {
std::cerr << "-x option should be an integer" << std::endl;;
- return 1;
+ return 2;
}
break;
case 'l':
@@ -512,7 +503,7 @@
}
catch (const boost::bad_lexical_cast&) {
std::cerr << "-l option should be an integer" << std::endl;;
- return 1;
+ return 2;
}
break;
case 'w':
@@ -522,26 +513,26 @@
}
catch (const boost::bad_lexical_cast&) {
std::cerr << "-w option should be an integer" << std::endl;;
- return 1;
+ return 2;
}
break;
case 'v':
ndnPutFile.isVerbose = true;
break;
- case 'h':
- usage();
- break;
default:
- break;
+ usage(argv[0]);
+ return 2;
}
}
+ if (argc != optind + 3) {
+ usage(argv[0]);
+ return 2;
+ }
+
argc -= optind;
argv += optind;
- if (argc != 3)
- usage();
-
ndnPutFile.repoPrefix = Name(argv[0]);
ndnPutFile.ndnName = Name(argv[1]);
if (strcmp(argv[2], "-") == 0) {
@@ -552,7 +543,7 @@
std::ifstream inputFileStream(argv[2], std::ios::in | std::ios::binary);
if (!inputFileStream.is_open()) {
std::cerr << "ERROR: cannot open " << argv[2] << std::endl;
- return 1;
+ return 2;
}
ndnPutFile.insertStream = &inputFileStream;
@@ -574,6 +565,6 @@
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
- return 2;
+ return 1;
}
}
diff --git a/tools/repo-ng-ls.cpp b/tools/repo-ng-ls.cpp
index 637b03f..e1a8281 100644
--- a/tools/repo-ng-ls.cpp
+++ b/tools/repo-ng-ls.cpp
@@ -1,6 +1,6 @@
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/*
- * Copyright (c) 2014-2018, Regents of the University of California.
+ * Copyright (c) 2014-2019, Regents of the University of California.
*
* This file is part of NDN repo-ng (Next generation of NDN repository).
* See AUTHORS.md for complete list of repo-ng authors and contributors.
@@ -18,10 +18,10 @@
*/
#include "../src/common.hpp"
-#include "config.hpp"
#include <iostream>
#include <string>
+#include <unistd.h>
#include <ndn-cxx/util/sqlite3-statement.hpp>
@@ -33,20 +33,19 @@
namespace repo {
static void
-printUsage(const char* programName)
+usage(const char* programName)
{
- std::cout
- << "Usage:\n"
- << " " << programName << " [-c <path/to/repo-ng.conf>] [-n] [-h]\n"
- << "\n"
- << "List names of Data packets in NDN repository. "
- << "By default, all names will include the implicit digest of Data packets\n"
- << "\n"
- << "Options:\n"
- << " -h: show help message\n"
- << " -c: set config file path\n"
- << " -n: do not show implicit digest\n"
- << std::endl;
+ std::cerr << "Usage: "
+ << programName << " [-c <path/to/repo-ng.conf>] [-n] [-h]\n"
+ << "\n"
+ << "List names of Data packets in NDN repository.\n"
+ << "By default, all names will include the implicit digest of Data packets.\n"
+ << "\n"
+ << "Options:\n"
+ << " -h: show help message\n"
+ << " -c: set config file path\n"
+ << " -n: do not show implicit digest\n"
+ << std::endl;
}
class RepoEnumerator
@@ -55,14 +54,10 @@
class Error : public std::runtime_error
{
public:
- explicit
- Error(const std::string& what)
- : std::runtime_error(what)
- {
- }
+ using std::runtime_error::runtime_error;
};
-public:
+ explicit
RepoEnumerator(const std::string& configFile);
uint64_t
@@ -146,16 +141,17 @@
return nEntries;
}
-int
+static int
main(int argc, char** argv)
{
std::string configPath = DEFAULT_CONFIG_FILE;
bool showImplicitDigest = true;
+
int opt;
while ((opt = getopt(argc, argv, "hc:n")) != -1) {
switch (opt) {
case 'h':
- printUsage(argv[0]);
+ usage(argv[0]);
return 0;
case 'c':
configPath = std::string(optarg);
@@ -164,7 +160,8 @@
showImplicitDigest = false;
break;
default:
- break;
+ usage(argv[0]);
+ return 2;
}
}
@@ -176,7 +173,6 @@
} // namespace repo
-
int
main(int argc, char** argv)
{
@@ -185,6 +181,6 @@
}
catch (const std::exception& e) {
std::cerr << "ERROR: " << e.what() << std::endl;
- return 2;
+ return 1;
}
}