Skip to content

Commit

Permalink
nix-channel: implement in c++
Browse files Browse the repository at this point in the history
  • Loading branch information
shlevy committed Aug 11, 2016
1 parent a6eed13 commit 5912422
Show file tree
Hide file tree
Showing 8 changed files with 304 additions and 233 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Makefile.config
/scripts/nix-switch
/scripts/nix-collect-garbage
/scripts/nix-prefetch-url
/scripts/nix-channel
/scripts/nix-build
/scripts/nix-copy-closure
/scripts/NixConfig.pm
Expand Down Expand Up @@ -73,6 +72,9 @@ Makefile.config
# /src/nix-daemon/
/src/nix-daemon/nix-daemon

# /src/nix-channel/
/src/nix-channel/nix-channel

# /src/download-via-ssh/
/src/download-via-ssh/download-via-ssh

Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ makefiles = \
src/nix-daemon/local.mk \
src/nix-collect-garbage/local.mk \
src/nix-prefetch-url/local.mk \
src/nix-channel/local.mk \
perl/local.mk \
scripts/local.mk \
corepkgs/local.mk \
Expand Down
1 change: 0 additions & 1 deletion scripts/local.mk
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
nix_bin_scripts := \
$(d)/nix-build \
$(d)/nix-channel \
$(d)/nix-copy-closure \
$(d)/nix-push

Expand Down
228 changes: 0 additions & 228 deletions scripts/nix-channel.in

This file was deleted.

20 changes: 17 additions & 3 deletions src/libstore/download.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ struct CurlDownloader : public Downloader
{
CURL * curl;
ref<std::string> data;
string etag, status, expectedETag;
string etag, status, expectedETag, effectiveUrl;

struct curl_slist * requestHeaders;

Expand Down Expand Up @@ -199,6 +199,11 @@ struct CurlDownloader : public Downloader
% url % curl_easy_strerror(res) % res);
}

char *effectiveUrlCStr;
curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &effectiveUrlCStr);
if (effectiveUrlCStr)
effectiveUrl = effectiveUrlCStr;

if (httpStatus == 304) return false;

return true;
Expand All @@ -212,6 +217,7 @@ struct CurlDownloader : public Downloader
res.data = data;
} else
res.cached = true;
res.effectiveUrl = effectiveUrl;
res.etag = etag;
return res;
}
Expand All @@ -223,6 +229,12 @@ ref<Downloader> makeDownloader()
}

Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, const Hash & expectedHash)
{
string ignored;
return downloadCached(store, url_, unpack, ignored, expectedHash);
}

Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpack, string & effectiveUrl, const Hash & expectedHash)
{
auto url = resolveUri(url_);

Expand Down Expand Up @@ -259,9 +271,10 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
auto ss = tokenizeString<vector<string>>(readFile(dataFile), "\n");
if (ss.size() >= 3 && ss[0] == url) {
time_t lastChecked;
if (string2Int(ss[2], lastChecked) && lastChecked + ttl >= time(0))
if (string2Int(ss[2], lastChecked) && lastChecked + ttl >= time(0)) {
skip = true;
else if (!ss[1].empty()) {
effectiveUrl = url_;
} else if (!ss[1].empty()) {
printMsg(lvlDebug, format("verifying previous ETag ‘%1%’") % ss[1]);
expectedETag = ss[1];
}
Expand All @@ -276,6 +289,7 @@ Path Downloader::downloadCached(ref<Store> store, const string & url_, bool unpa
DownloadOptions options;
options.expectedETag = expectedETag;
auto res = download(url, options);
effectiveUrl = res.effectiveUrl;

if (!res.cached) {
ValidPathInfo info;
Expand Down
6 changes: 6 additions & 0 deletions src/libstore/download.hh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ struct DownloadResult
{
bool cached;
string etag;
string effectiveUrl;
std::shared_ptr<std::string> data;
};

Expand All @@ -31,6 +32,11 @@ struct Downloader
Path downloadCached(ref<Store> store, const string & url, bool unpack,
const Hash & expectedHash = Hash());

/* Need to overload because can't have an rvalue default value for non-const reference */

Path downloadCached(ref<Store> store, const string & url, bool unpack,
string & effectiveUrl, const Hash & expectedHash = Hash());

enum Error { NotFound, Forbidden, Misc };
};

Expand Down
7 changes: 7 additions & 0 deletions src/nix-channel/local.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
programs += nix-channel

nix-channel_DIR := $(d)

nix-channel_LIBS = libmain libutil libformat libstore

nix-channel_SOURCES := $(d)/nix-channel.cc
Loading

0 comments on commit 5912422

Please sign in to comment.