Skip to content

Commit

Permalink
Merge pull request #18 from obsidiansystems/ipfs-binary-cache-version…
Browse files Browse the repository at this point in the history
…-check

Check IPFS version on startup
  • Loading branch information
Ericson2314 authored Jun 17, 2020
2 parents ee24a54 + 2ffe057 commit dc8557c
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 58 deletions.
4 changes: 4 additions & 0 deletions src/libstore/ipfs-binary-cache-store.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "binary-cache-store.hh"
#include "filetransfer.hh"
#include "nar-info-disk-cache.hh"
#include "versions.hh"

namespace nix {

Expand Down Expand Up @@ -63,6 +64,9 @@ class IPFSBinaryCacheStore : public BinaryCacheStore
if (versionInfo.find("Version") == versionInfo.end())
throw Error("daemon for IPFS is not running properly");

if (compareVersions(versionInfo["Version"], "0.4.0") < 0)
throw Error("daemon for IPFS is %s, when a minimum of 0.4.0 is required", versionInfo["Version"]);

// Resolve the IPNS name to an IPFS object
if (optIpnsPath) {
auto ipnsPath = *optIpnsPath;
Expand Down
56 changes: 1 addition & 55 deletions src/libstore/names.cc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "names.hh"
#include "util.hh"
#include "versions.hh"


namespace nix {
Expand Down Expand Up @@ -41,60 +41,6 @@ bool DrvName::matches(DrvName & n)
}


string nextComponent(string::const_iterator & p,
const string::const_iterator end)
{
/* Skip any dots and dashes (component separators). */
while (p != end && (*p == '.' || *p == '-')) ++p;

if (p == end) return "";

/* If the first character is a digit, consume the longest sequence
of digits. Otherwise, consume the longest sequence of
non-digit, non-separator characters. */
string s;
if (isdigit(*p))
while (p != end && isdigit(*p)) s += *p++;
else
while (p != end && (!isdigit(*p) && *p != '.' && *p != '-'))
s += *p++;

return s;
}


static bool componentsLT(const string & c1, const string & c2)
{
int n1, n2;
bool c1Num = string2Int(c1, n1), c2Num = string2Int(c2, n2);

if (c1Num && c2Num) return n1 < n2;
else if (c1 == "" && c2Num) return true;
else if (c1 == "pre" && c2 != "pre") return true;
else if (c2 == "pre") return false;
/* Assume that `2.3a' < `2.3.1'. */
else if (c2Num) return true;
else if (c1Num) return false;
else return c1 < c2;
}


int compareVersions(const string & v1, const string & v2)
{
string::const_iterator p1 = v1.begin();
string::const_iterator p2 = v2.begin();

while (p1 != v1.end() || p2 != v2.end()) {
string c1 = nextComponent(p1, v1.end());
string c2 = nextComponent(p2, v2.end());
if (componentsLT(c1, c2)) return -1;
else if (componentsLT(c2, c1)) return 1;
}

return 0;
}


DrvNames drvNamesFromArgs(const Strings & opArgs)
{
DrvNames result;
Expand Down
4 changes: 1 addition & 3 deletions src/libstore/names.hh
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <memory>

#include "types.hh"
#include "versions.hh"
#include <regex>

namespace nix {
Expand All @@ -24,9 +25,6 @@ private:

typedef list<DrvName> DrvNames;

string nextComponent(string::const_iterator & p,
const string::const_iterator end);
int compareVersions(const string & v1, const string & v2);
DrvNames drvNamesFromArgs(const Strings & opArgs);

}
61 changes: 61 additions & 0 deletions src/libutil/versions.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "util.hh"
#include "versions.hh"

namespace nix {


string nextComponent(string::const_iterator & p,
const string::const_iterator end)
{
/* Skip any dots and dashes (component separators). */
while (p != end && (*p == '.' || *p == '-')) ++p;

if (p == end) return "";

/* If the first character is a digit, consume the longest sequence
of digits. Otherwise, consume the longest sequence of
non-digit, non-separator characters. */
string s;
if (isdigit(*p))
while (p != end && isdigit(*p)) s += *p++;
else
while (p != end && (!isdigit(*p) && *p != '.' && *p != '-'))
s += *p++;

return s;
}


static bool componentsLT(const string & c1, const string & c2)
{
int n1, n2;
bool c1Num = string2Int(c1, n1), c2Num = string2Int(c2, n2);

if (c1Num && c2Num) return n1 < n2;
else if (c1 == "" && c2Num) return true;
else if (c1 == "pre" && c2 != "pre") return true;
else if (c2 == "pre") return false;
/* Assume that `2.3a' < `2.3.1'. */
else if (c2Num) return true;
else if (c1Num) return false;
else return c1 < c2;
}


int compareVersions(const string & v1, const string & v2)
{
string::const_iterator p1 = v1.begin();
string::const_iterator p2 = v2.begin();

while (p1 != v1.end() || p2 != v2.end()) {
string c1 = nextComponent(p1, v1.end());
string c2 = nextComponent(p2, v2.end());
if (componentsLT(c1, c2)) return -1;
else if (componentsLT(c2, c1)) return 1;
}

return 0;
}


}
14 changes: 14 additions & 0 deletions src/libutil/versions.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once

#include <memory>

#include "types.hh"
#include <regex>

namespace nix {

string nextComponent(string::const_iterator & p,
const string::const_iterator end);
int compareVersions(const string & v1, const string & v2);

}

0 comments on commit dc8557c

Please sign in to comment.