summaryrefslogtreecommitdiff
path: root/src/import
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-08-19 18:15:37 +0200
committerLennart Poettering <lennart@poettering.net>2021-08-20 21:56:39 +0200
commitc456862f87237831ce2bbaeb53a37d1b3d669285 (patch)
tree763c4d08764dae35c15b6c8f27b89cd1ff63efd4 /src/import
parent55b90ee00b78a449c8f187a5e8141f8ccb100bf4 (diff)
downloadsystemd-c456862f87237831ce2bbaeb53a37d1b3d669285.tar.gz
import: allow file:// in addition to HTTP(S)
Previously we only allows http/https urls, let's open this up a bit. Why? Because it makes testing *so* *much* *easier* as we don't need to run a HTTP server all the time. CURL mostly abstracts the differences of http/https away from us, hence we can get away with very little extra work.
Diffstat (limited to 'src/import')
-rw-r--r--src/import/importd.c2
-rw-r--r--src/import/pull-job.c76
-rw-r--r--src/import/pull-raw.c2
-rw-r--r--src/import/pull-tar.c2
-rw-r--r--src/import/pull.c4
5 files changed, 49 insertions, 37 deletions
diff --git a/src/import/importd.c b/src/import/importd.c
index 0a87056819..86181628d9 100644
--- a/src/import/importd.c
+++ b/src/import/importd.c
@@ -928,7 +928,7 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
if (r < 0)
return r;
- if (!http_url_is_valid(remote))
+ if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"URL %s is invalid", remote);
diff --git a/src/import/pull-job.c b/src/import/pull-job.c
index 8764207960..826da95378 100644
--- a/src/import/pull-job.c
+++ b/src/import/pull-job.c
@@ -117,7 +117,7 @@ static int pull_job_restart(PullJob *j, const char *new_url) {
void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
PullJob *j = NULL;
CURLcode code;
- long status;
+ long protocol;
int r;
if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK)
@@ -131,50 +131,62 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
goto finish;
}
- code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
+ code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if (code != CURLE_OK) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish;
- } else if (status == 304) {
- log_info("Image already downloaded. Skipping download.");
- j->etag_exists = true;
- r = 0;
- goto finish;
- } else if (status >= 300) {
+ }
- if (status == 404 && j->on_not_found) {
- _cleanup_free_ char *new_url = NULL;
+ if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) {
+ long status;
- /* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */
- r = j->on_not_found(j, &new_url);
- if (r < 0)
- goto finish;
+ code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
+ if (code != CURLE_OK) {
+ r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
+ goto finish;
+ }
+
+ if (status == 304) {
+ log_info("Image already downloaded. Skipping download.");
+ j->etag_exists = true;
+ r = 0;
+ goto finish;
+ } else if (status >= 300) {
- if (r > 0) { /* A new url to use */
- assert(new_url);
+ if (status == 404 && j->on_not_found) {
+ _cleanup_free_ char *new_url = NULL;
- r = pull_job_restart(j, new_url);
+ /* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */
+ r = j->on_not_found(j, &new_url);
if (r < 0)
goto finish;
- code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status);
- if (code != CURLE_OK) {
- r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
- goto finish;
- }
+ if (r > 0) { /* A new url to use */
+ assert(new_url);
+
+ r = pull_job_restart(j, new_url);
+ if (r < 0)
+ goto finish;
+
+ code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status);
+ if (code != CURLE_OK) {
+ r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
+ goto finish;
+ }
- if (status == 0)
- return;
+ if (status == 0)
+ return;
+ }
}
- }
- r = log_error_errno(
- status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */
- "HTTP request to %s failed with code %li.", j->url, status);
- goto finish;
- } else if (status < 200) {
- r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
- goto finish;
+ r = log_error_errno(
+ status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */
+ "HTTP request to %s failed with code %li.", j->url, status);
+ goto finish;
+ } else if (status < 200) {
+ r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
+ goto finish;
+ }
}
if (j->state != PULL_JOB_RUNNING) {
diff --git a/src/import/pull-raw.c b/src/import/pull-raw.c
index c071e134f2..6a0c2c8b17 100644
--- a/src/import/pull-raw.c
+++ b/src/import/pull-raw.c
@@ -835,7 +835,7 @@ int raw_pull_start(
assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !(flags & PULL_DIRECT));
assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !checksum);
- if (!http_url_is_valid(url))
+ if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL;
if (local && !pull_validate_local(local, flags))
diff --git a/src/import/pull-tar.c b/src/import/pull-tar.c
index 6aca3c9979..06d336bca9 100644
--- a/src/import/pull-tar.c
+++ b/src/import/pull-tar.c
@@ -597,7 +597,7 @@ int tar_pull_start(
assert(!(flags & PULL_SETTINGS) || !(flags & PULL_DIRECT));
assert(!(flags & PULL_SETTINGS) || !checksum);
- if (!http_url_is_valid(url))
+ if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL;
if (local && !pull_validate_local(local, flags))
diff --git a/src/import/pull.c b/src/import/pull.c
index a903c35aa7..2cf0cca14f 100644
--- a/src/import/pull.c
+++ b/src/import/pull.c
@@ -110,7 +110,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
int r;
url = argv[1];
- if (!http_url_is_valid(url))
+ if (!http_url_is_valid(url) && !file_url_is_valid(url))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3)
@@ -183,7 +183,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
int r;
url = argv[1];
- if (!http_url_is_valid(url))
+ if (!http_url_is_valid(url) && !file_url_is_valid(url))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3)