1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-23 21:35:11 +03:00

Merge pull request #20500 from poettering/import-tweaks

a few minor tweaks/bugfixlets to importd backends
This commit is contained in:
Yu Watanabe 2021-08-21 12:50:22 +09:00 committed by GitHub
commit 218262c0b1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 82 additions and 44 deletions

View File

@ -256,6 +256,9 @@ int curl_glue_make(CURL **ret, const char *url, void *userdata) {
if (curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, 30L) != CURLE_OK) if (curl_easy_setopt(c, CURLOPT_LOW_SPEED_LIMIT, 30L) != CURLE_OK)
return -EIO; return -EIO;
if (curl_easy_setopt(c, CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS|CURLPROTO_FILE) != CURLE_OK)
return -EIO;
*ret = TAKE_PTR(c); *ret = TAKE_PTR(c);
return 0; return 0;
} }

View File

@ -185,10 +185,11 @@ static int import_fs(int argc, char *argv[], void *userdata) {
if (r < 0) if (r < 0)
return log_oom(); return log_oom();
(void) mkdir_parents_label(temp_path, 0700);
dest = temp_path; dest = temp_path;
} }
(void) mkdir_parents_label(dest, 0700);
progress.limit = (RateLimit) { 200*USEC_PER_MSEC, 1 }; progress.limit = (RateLimit) { 200*USEC_PER_MSEC, 1 };
{ {

View File

@ -9,6 +9,7 @@
#include "btrfs-util.h" #include "btrfs-util.h"
#include "copy.h" #include "copy.h"
#include "fd-util.h" #include "fd-util.h"
#include "format-util.h"
#include "fs-util.h" #include "fs-util.h"
#include "hostname-util.h" #include "hostname-util.h"
#include "import-common.h" #include "import-common.h"
@ -248,6 +249,8 @@ static int raw_import_finish(RawImport *i) {
i->temp_path = mfree(i->temp_path); i->temp_path = mfree(i->temp_path);
log_info("Wrote %s.", FORMAT_BYTES(i->written_uncompressed));
return 0; return 0;
} }
@ -319,6 +322,9 @@ static int raw_import_try_reflink(RawImport *i) {
if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED) if (i->compress.type != IMPORT_COMPRESS_UNCOMPRESSED)
return 0; return 0;
if (i->offset != UINT64_MAX || i->size_max != UINT64_MAX)
return 0;
if (!S_ISREG(i->input_stat.st_mode) || !S_ISREG(i->output_stat.st_mode)) if (!S_ISREG(i->input_stat.st_mode) || !S_ISREG(i->output_stat.st_mode))
return 0; return 0;
@ -365,7 +371,7 @@ static int raw_import_write(const void *p, size_t sz, void *userdata) {
} }
/* Generate sparse file if we created/truncated the file */ /* Generate sparse file if we created/truncated the file */
if (S_ISREG(i->output_stat.st_mode)) { if (S_ISREG(i->output_stat.st_mode) && i->offset == UINT64_MAX) {
ssize_t n; ssize_t n;
n = sparse_write(i->output_fd, p, sz, 64); n = sparse_write(i->output_fd, p, sz, 64);

View File

@ -928,7 +928,7 @@ static int method_pull_tar_or_raw(sd_bus_message *msg, void *userdata, sd_bus_er
if (r < 0) if (r < 0)
return r; 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, return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS,
"URL %s is invalid", remote); "URL %s is invalid", remote);

View File

@ -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) { void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
PullJob *j = NULL; PullJob *j = NULL;
CURLcode code; CURLcode code;
long status; long protocol;
int r; int r;
if (curl_easy_getinfo(curl, CURLINFO_PRIVATE, (char **)&j) != CURLE_OK) 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; goto finish;
} }
code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status); code = curl_easy_getinfo(curl, CURLINFO_PROTOCOL, &protocol);
if (code != CURLE_OK) { if (code != CURLE_OK) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code)); r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; 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) { if (IN_SET(protocol, CURLPROTO_HTTP, CURLPROTO_HTTPS)) {
_cleanup_free_ char *new_url = NULL; long status;
/* This resource wasn't found, but the implementor wants to maybe let us know a new URL, query for it. */ code = curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &status);
r = j->on_not_found(j, &new_url); if (code != CURLE_OK) {
if (r < 0) r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; goto finish;
}
if (r > 0) { /* A new url to use */ if (status == 304) {
assert(new_url); log_info("Image already downloaded. Skipping download.");
j->etag_exists = true;
r = 0;
goto finish;
} else if (status >= 300) {
r = pull_job_restart(j, new_url); if (status == 404 && j->on_not_found) {
_cleanup_free_ char *new_url = NULL;
/* 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) if (r < 0)
goto finish; goto finish;
code = curl_easy_getinfo(j->curl, CURLINFO_RESPONSE_CODE, &status); if (r > 0) { /* A new url to use */
if (code != CURLE_OK) { assert(new_url);
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "Failed to retrieve response code: %s", curl_easy_strerror(code));
goto finish; 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( r = log_error_errno(
status == 404 ? SYNTHETIC_ERRNO(ENOMEDIUM) : SYNTHETIC_ERRNO(EIO), /* Make the most common error recognizable */ 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); "HTTP request to %s failed with code %li.", j->url, status);
goto finish; goto finish;
} else if (status < 200) { } else if (status < 200) {
r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status); r = log_error_errno(SYNTHETIC_ERRNO(EIO), "HTTP request to %s finished with unexpected code %li.", j->url, status);
goto finish; goto finish;
}
} }
if (j->state != PULL_JOB_RUNNING) { if (j->state != PULL_JOB_RUNNING) {
@ -260,6 +272,8 @@ void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result) {
} }
} }
log_info("Acquired %s.", FORMAT_BYTES(j->written_uncompressed));
r = 0; r = 0;
finish: finish:
@ -292,7 +306,7 @@ static int pull_job_write_uncompressed(const void *p, size_t sz, void *userdata)
if (j->disk_fd >= 0) { if (j->disk_fd >= 0) {
if (S_ISREG(j->disk_stat.st_mode)) { if (S_ISREG(j->disk_stat.st_mode) && j->offset == UINT64_MAX) {
ssize_t n; ssize_t n;
n = sparse_write(j->disk_fd, p, sz, 64); n = sparse_write(j->disk_fd, p, sz, 64);

View File

@ -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)) || !(flags & PULL_DIRECT));
assert(!(flags & (PULL_SETTINGS|PULL_ROOTHASH|PULL_ROOTHASH_SIGNATURE|PULL_VERITY)) || !checksum); 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; return -EINVAL;
if (local && !pull_validate_local(local, flags)) if (local && !pull_validate_local(local, flags))

View File

@ -597,7 +597,7 @@ int tar_pull_start(
assert(!(flags & PULL_SETTINGS) || !(flags & PULL_DIRECT)); assert(!(flags & PULL_SETTINGS) || !(flags & PULL_DIRECT));
assert(!(flags & PULL_SETTINGS) || !checksum); assert(!(flags & PULL_SETTINGS) || !checksum);
if (!http_url_is_valid(url)) if (!http_url_is_valid(url) && !file_url_is_valid(url))
return -EINVAL; return -EINVAL;
if (local && !pull_validate_local(local, flags)) if (local && !pull_validate_local(local, flags))

View File

@ -110,7 +110,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
int r; int r;
url = argv[1]; 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); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3) if (argc >= 3)
@ -183,7 +183,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
int r; int r;
url = argv[1]; 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); return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "URL '%s' is not valid.", url);
if (argc >= 3) if (argc >= 3)

View File

@ -2125,7 +2125,7 @@ static int pull_tar(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
remote = argv[1]; remote = argv[1];
if (!http_url_is_valid(remote)) if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"URL '%s' is not valid.", remote); "URL '%s' is not valid.", remote);
@ -2181,7 +2181,7 @@ static int pull_raw(int argc, char *argv[], void *userdata) {
assert(bus); assert(bus);
remote = argv[1]; remote = argv[1];
if (!http_url_is_valid(remote)) if (!http_url_is_valid(remote) && !file_url_is_valid(remote))
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
"URL '%s' is not valid.", remote); "URL '%s' is not valid.", remote);

View File

@ -36,16 +36,29 @@ bool http_url_is_valid(const char *url) {
return ascii_is_valid(p); return ascii_is_valid(p);
} }
bool file_url_is_valid(const char *url) {
const char *p;
if (isempty(url))
return false;
p = startswith(url, "file:/");
if (isempty(p))
return false;
return ascii_is_valid(p);
}
bool documentation_url_is_valid(const char *url) { bool documentation_url_is_valid(const char *url) {
const char *p; const char *p;
if (isempty(url)) if (isempty(url))
return false; return false;
if (http_url_is_valid(url)) if (http_url_is_valid(url) || file_url_is_valid(url))
return true; return true;
p = STARTSWITH_SET(url, "file:/", "info:", "man:"); p = STARTSWITH_SET(url, "info:", "man:");
if (isempty(p)) if (isempty(p))
return false; return false;

View File

@ -6,6 +6,7 @@
#include "macro.h" #include "macro.h"
bool http_url_is_valid(const char *url) _pure_; bool http_url_is_valid(const char *url) _pure_;
bool file_url_is_valid(const char *url) _pure_;
bool documentation_url_is_valid(const char *url) _pure_; bool documentation_url_is_valid(const char *url) _pure_;