aboutsummaryrefslogtreecommitdiff
path: root/src/libutil
diff options
context:
space:
mode:
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/args.cc70
-rw-r--r--src/libutil/args.hh51
-rw-r--r--src/libutil/hash.cc4
-rw-r--r--src/libutil/hash.hh12
-rw-r--r--src/libutil/util.cc59
-rw-r--r--src/libutil/util.hh40
6 files changed, 201 insertions, 35 deletions
diff --git a/src/libutil/args.cc b/src/libutil/args.cc
index 7af2a1bf7..ba15ea571 100644
--- a/src/libutil/args.cc
+++ b/src/libutil/args.cc
@@ -200,4 +200,74 @@ void printTable(std::ostream & out, const Table2 & table)
}
}
+void Command::printHelp(const string & programName, std::ostream & out)
+{
+ Args::printHelp(programName, out);
+
+ auto exs = examples();
+ if (!exs.empty()) {
+ out << "\n";
+ out << "Examples:\n";
+ for (auto & ex : exs)
+ out << "\n"
+ << " " << ex.description << "\n" // FIXME: wrap
+ << " $ " << ex.command << "\n";
+ }
+}
+
+MultiCommand::MultiCommand(const Commands & commands)
+ : commands(commands)
+{
+ expectedArgs.push_back(ExpectedArg{"command", 1, true, [=](std::vector<std::string> ss) {
+ assert(!command);
+ auto i = commands.find(ss[0]);
+ if (i == commands.end())
+ throw UsageError("'%s' is not a recognised command", ss[0]);
+ command = i->second();
+ command->_name = ss[0];
+ }});
+}
+
+void MultiCommand::printHelp(const string & programName, std::ostream & out)
+{
+ if (command) {
+ command->printHelp(programName + " " + command->name(), out);
+ return;
+ }
+
+ out << "Usage: " << programName << " <COMMAND> <FLAGS>... <ARGS>...\n";
+
+ out << "\n";
+ out << "Common flags:\n";
+ printFlags(out);
+
+ out << "\n";
+ out << "Available commands:\n";
+
+ Table2 table;
+ for (auto & i : commands) {
+ auto command = i.second();
+ command->_name = i.first;
+ auto descr = command->description();
+ if (!descr.empty())
+ table.push_back(std::make_pair(command->name(), descr));
+ }
+ printTable(out, table);
+}
+
+bool MultiCommand::processFlag(Strings::iterator & pos, Strings::iterator end)
+{
+ if (Args::processFlag(pos, end)) return true;
+ if (command && command->processFlag(pos, end)) return true;
+ return false;
+}
+
+bool MultiCommand::processArgs(const Strings & args, bool finish)
+{
+ if (command)
+ return command->processArgs(args, finish);
+ else
+ return Args::processArgs(args, finish);
+}
+
}
diff --git a/src/libutil/args.hh b/src/libutil/args.hh
index ad5fcca39..b960a55a8 100644
--- a/src/libutil/args.hh
+++ b/src/libutil/args.hh
@@ -188,6 +188,57 @@ public:
friend class MultiCommand;
};
+/* A command is an argument parser that can be executed by calling its
+ run() method. */
+struct Command : virtual Args
+{
+private:
+ std::string _name;
+
+ friend class MultiCommand;
+
+public:
+
+ virtual ~Command() { }
+
+ std::string name() { return _name; }
+
+ virtual void prepare() { };
+ virtual void run() = 0;
+
+ struct Example
+ {
+ std::string description;
+ std::string command;
+ };
+
+ typedef std::list<Example> Examples;
+
+ virtual Examples examples() { return Examples(); }
+
+ void printHelp(const string & programName, std::ostream & out) override;
+};
+
+typedef std::map<std::string, std::function<ref<Command>()>> Commands;
+
+/* An argument parser that supports multiple subcommands,
+ i.e. ‘<command> <subcommand>’. */
+class MultiCommand : virtual Args
+{
+public:
+ Commands commands;
+
+ std::shared_ptr<Command> command;
+
+ MultiCommand(const Commands & commands);
+
+ void printHelp(const string & programName, std::ostream & out) override;
+
+ bool processFlag(Strings::iterator & pos, Strings::iterator end) override;
+
+ bool processArgs(const Strings & args, bool finish) override;
+};
+
Strings argvToStrings(int argc, char * * argv);
/* Helper function for rendering argument labels. */
diff --git a/src/libutil/hash.cc b/src/libutil/hash.cc
index 1c14ebb18..362c537fe 100644
--- a/src/libutil/hash.cc
+++ b/src/libutil/hash.cc
@@ -13,6 +13,10 @@
#include <sys/stat.h>
#include <fcntl.h>
+#if OPENSSL_VERSION_NUMBER < 0x10101000L
+#error "Unsupported version of OpenSSL, you need at least 1.1.1"
+#endif
+
namespace nix {
diff --git a/src/libutil/hash.hh b/src/libutil/hash.hh
index 2dbc3b630..edede8ace 100644
--- a/src/libutil/hash.hh
+++ b/src/libutil/hash.hh
@@ -80,6 +80,18 @@ struct Hash
or base-64. By default, this is prefixed by the hash type
(e.g. "sha256:"). */
std::string to_string(Base base = Base32, bool includeType = true) const;
+
+ std::string gitRev() const
+ {
+ assert(type == htSHA1);
+ return to_string(Base16, false);
+ }
+
+ std::string gitShortRev() const
+ {
+ assert(type == htSHA1);
+ return std::string(to_string(Base16, false), 0, 7);
+ }
};
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index 1b7449991..98a7ea397 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -22,6 +22,7 @@
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/time.h>
#include <unistd.h>
#ifdef __APPLE__
@@ -357,7 +358,6 @@ void writeFile(const Path & path, Source & source, mode_t mode)
}
}
-
string readLine(int fd)
{
string s;
@@ -475,6 +475,17 @@ Path createTempDir(const Path & tmpRoot, const Path & prefix,
}
+std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix)
+{
+ Path tmpl(getEnv("TMPDIR", "/tmp") + "/" + prefix + ".XXXXXX");
+ // Strictly speaking, this is UB, but who cares...
+ AutoCloseFD fd(mkstemp((char *) tmpl.c_str()));
+ if (!fd)
+ throw SysError("creating temporary file '%s'", tmpl);
+ return {std::move(fd), tmpl};
+}
+
+
static Lazy<Path> getHome2([]() {
Path homeDir = getEnv("HOME");
if (homeDir.empty()) {
@@ -551,20 +562,31 @@ Paths createDirs(const Path & path)
}
-void createSymlink(const Path & target, const Path & link)
+void createSymlink(const Path & target, const Path & link,
+ std::optional<time_t> mtime)
{
if (symlink(target.c_str(), link.c_str()))
throw SysError(format("creating symlink from '%1%' to '%2%'") % link % target);
+ if (mtime) {
+ struct timeval times[2];
+ times[0].tv_sec = *mtime;
+ times[0].tv_usec = 0;
+ times[1].tv_sec = *mtime;
+ times[1].tv_usec = 0;
+ if (lutimes(link.c_str(), times))
+ throw SysError("setting time of symlink '%s'", link);
+ }
}
-void replaceSymlink(const Path & target, const Path & link)
+void replaceSymlink(const Path & target, const Path & link,
+ std::optional<time_t> mtime)
{
for (unsigned int n = 0; true; n++) {
Path tmp = canonPath(fmt("%s/.%d_%s", dirOf(link), n, baseNameOf(link)));
try {
- createSymlink(target, tmp);
+ createSymlink(target, tmp, mtime);
} catch (SysError & e) {
if (e.errNo == EEXIST) continue;
throw;
@@ -976,12 +998,14 @@ std::vector<char *> stringsToCharPtrs(const Strings & ss)
return res;
}
-
+// Output = "standard out" output stream
string runProgram(Path program, bool searchPath, const Strings & args,
const std::optional<std::string> & input)
{
RunOptions opts(program, args);
opts.searchPath = searchPath;
+ // This allows you to refer to a program with a pathname relative to the
+ // PATH variable.
opts.input = input;
auto res = runProgram(opts);
@@ -992,6 +1016,7 @@ string runProgram(Path program, bool searchPath, const Strings & args,
return res.second;
}
+// Output = error code + "standard out" output stream
std::pair<int, std::string> runProgram(const RunOptions & options_)
{
RunOptions options(options_);
@@ -1064,6 +1089,8 @@ void runProgram2(const RunOptions & options)
if (options.searchPath)
execvp(options.program.c_str(), stringsToCharPtrs(args_).data());
+ // This allows you to refer to a program with a pathname relative
+ // to the PATH variable.
else
execv(options.program.c_str(), stringsToCharPtrs(args_).data());
@@ -1198,28 +1225,6 @@ template StringSet tokenizeString(const string & s, const string & separators);
template vector<string> tokenizeString(const string & s, const string & separators);
-string concatStringsSep(const string & sep, const Strings & ss)
-{
- string s;
- for (auto & i : ss) {
- if (s.size() != 0) s += sep;
- s += i;
- }
- return s;
-}
-
-
-string concatStringsSep(const string & sep, const StringSet & ss)
-{
- string s;
- for (auto & i : ss) {
- if (s.size() != 0) s += sep;
- s += i;
- }
- return s;
-}
-
-
string chomp(const string & s)
{
size_t i = s.find_last_not_of(" \n\r\t");
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 07c3d28ff..2bf9725af 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -122,10 +122,6 @@ void deletePath(const Path & path);
void deletePath(const Path & path, unsigned long long & bytesFreed);
-/* Create a temporary directory. */
-Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
- bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
-
/* Return $HOME or the user's home directory from /etc/passwd. */
Path getHome();
@@ -146,10 +142,12 @@ Path getDataDir();
Paths createDirs(const Path & path);
/* Create a symlink. */
-void createSymlink(const Path & target, const Path & link);
+void createSymlink(const Path & target, const Path & link,
+ std::optional<time_t> mtime = {});
/* Atomically create or replace a symlink. */
-void replaceSymlink(const Path & target, const Path & link);
+void replaceSymlink(const Path & target, const Path & link,
+ std::optional<time_t> mtime = {});
/* Wrappers arount read()/write() that read/write exactly the
@@ -203,6 +201,14 @@ public:
};
+/* Create a temporary directory. */
+Path createTempDir(const Path & tmpRoot = "", const Path & prefix = "nix",
+ bool includePid = true, bool useGlobalCounter = true, mode_t mode = 0755);
+
+/* Create a temporary file, returning a file handle and its path. */
+std::pair<AutoCloseFD, Path> createTempFile(const Path & prefix = "nix");
+
+
class Pipe
{
public:
@@ -343,8 +349,26 @@ template<class C> C tokenizeString(const string & s, const string & separators =
/* Concatenate the given strings with a separator between the
elements. */
-string concatStringsSep(const string & sep, const Strings & ss);
-string concatStringsSep(const string & sep, const StringSet & ss);
+template<class C>
+string concatStringsSep(const string & sep, const C & ss)
+{
+ string s;
+ for (auto & i : ss) {
+ if (s.size() != 0) s += sep;
+ s += i;
+ }
+ return s;
+}
+
+
+/* Add quotes around a collection of strings. */
+template<class C> Strings quoteStrings(const C & c)
+{
+ Strings res;
+ for (auto & s : c)
+ res.push_back("'" + s + "'");
+ return res;
+}
/* Remove trailing whitespace from a string. */