Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nix-channel: implement in c++ #1026

Merged
merged 1 commit into from
Aug 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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