about summary refs log tree commit diff
path: root/src/libutil
diff options
context:
space:
mode:
authorEelco Dolstra <eelco.dolstra@logicblox.com>2015-04-09T09·42+0200
committerEelco Dolstra <eelco.dolstra@logicblox.com>2015-04-09T09·42+0200
commit60340ce3e2f793caf1704997a4d7a5a066e9ef24 (patch)
tree09b6e74798faa332bfcbb6142b28e7bcaea562ed /src/libutil
parent1711679ea5fd6a37db5a7a1b40eba1f58ad6c999 (diff)
Implement caching of fetchurl/fetchTarball results
ETags are used to prevent redownloading unchanged files.
Diffstat (limited to 'src/libutil')
-rw-r--r--src/libutil/util.cc20
-rw-r--r--src/libutil/util.hh7
2 files changed, 27 insertions, 0 deletions
diff --git a/src/libutil/util.cc b/src/libutil/util.cc
index be0a9bf317d1..ab0a3b303076 100644
--- a/src/libutil/util.cc
+++ b/src/libutil/util.cc
@@ -413,6 +413,17 @@ void createSymlink(const Path & target, const Path & link)
 }
 
 
+void replaceSymlink(const Path & target, const Path & link)
+{
+    Path tmp = canonPath(dirOf(link) + "/.new_" + baseNameOf(link));
+
+    createSymlink(target, tmp);
+
+    if (rename(tmp.c_str(), link.c_str()) != 0)
+        throw SysError(format("renaming ‘%1%’ to ‘%2%’") % tmp % link);
+}
+
+
 LogType logType = ltPretty;
 Verbosity verbosity = lvlInfo;
 
@@ -1076,6 +1087,15 @@ string chomp(const string & s)
 }
 
 
+string trim(const string & s, const string & whitespace)
+{
+    auto i = s.find_first_not_of(whitespace);
+    if (i == string::npos) return "";
+    auto j = s.find_last_not_of(whitespace);
+    return string(s, i, j == string::npos ? j : j - i + 1);
+}
+
+
 string statusToString(int status)
 {
     if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
diff --git a/src/libutil/util.hh b/src/libutil/util.hh
index 20330fb7699e..7d20351eec2e 100644
--- a/src/libutil/util.hh
+++ b/src/libutil/util.hh
@@ -110,6 +110,9 @@ Paths createDirs(const Path & path);
 /* Create a symlink. */
 void createSymlink(const Path & target, const Path & link);
 
+/* Atomically create or replace a symlink. */
+void replaceSymlink(const Path & target, const Path & link);
+
 
 template<class T, class A>
 T singleton(const A & a)
@@ -334,6 +337,10 @@ string concatStringsSep(const string & sep, const StringSet & ss);
 string chomp(const string & s);
 
 
+/* Remove whitespace from the start and end of a string. */
+string trim(const string & s, const string & whitespace = " \n\r\t");
+
+
 /* Convert the exit status of a child as returned by wait() into an
    error string. */
 string statusToString(int status);