import-raw.c revision 49bb233bb734536b9617d838f09a7bf9b8336003
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <lzma.h>
#include "hashmap.h"
#include "utf8.h"
#include "curl-util.h"
#include "import-raw.h"
#include "strv.h"
#include "copy.h"
typedef struct RawImportFile RawImportFile;
struct RawImportFile {
char *url;
char *local;
struct curl_slist *request_header;
char *temp_path;
char *final_path;
char *etag;
char **old_etags;
void *payload;
bool force_local;
bool done;
int disk_fd;
bool compressed;
};
struct RawImport {
char *image_root;
void *userdata;
bool finished;
};
#define FILENAME_ESCAPE "/.#\"\'"
if (!f)
return NULL;
if (f->import)
safe_close(f->disk_fd);
free(f->final_path);
if (f->temp_path) {
}
free(f);
return NULL;
}
return;
if (import->on_finished)
else
}
static int raw_import_file_make_final_path(RawImportFile *f) {
assert(f);
if (f->final_path)
return 0;
if (!escaped_url)
return -ENOMEM;
if (f->etag) {
if (!escaped_etag)
return -ENOMEM;
f->final_path = strjoin(f->import->image_root, "/.raw-", escaped_url, ".", escaped_etag, ".raw", NULL);
} else
if (!f->final_path)
return -ENOMEM;
return 0;
}
static void raw_import_file_success(RawImportFile *f) {
int r;
assert(f);
f->done = true;
if (f->local) {
const char *p;
if (f->disk_fd >= 0) {
goto finish;
}
} else {
r = raw_import_file_make_final_path(f);
if (r < 0) {
log_oom();
goto finish;
}
if (f->disk_fd < 0) {
goto finish;
}
}
if (f->force_local)
(void) rm_rf_dangerous(p, false, true, false);
r = tempfn_random(p, &tp);
if (r < 0) {
log_oom();
goto finish;
}
if (dfd < 0) {
goto finish;
}
/* Turn off COW writing. This should greatly improve
* performance on COW file systems like btrfs, since it
* reduces fragmentation caused by not allowing in-place
* writes. */
if (r < 0)
if (r < 0) {
log_error_errno(r, "Failed to make writable copy of image: %m");
goto finish;
}
if (r < 0) {
goto finish;
}
log_info("Created new local image %s.", p);
}
r = 0;
raw_import_finish(f->import, r);
}
RawImportFile *f = NULL;
long status;
int r;
return;
if (!f || f->done)
return;
f->done = true;
r = -EIO;
goto fail;
}
r = -EIO;
goto fail;
} else if (status == 304) {
log_info("Image already downloaded. Skipping download.");
return;
} else if (status >= 300) {
r = -EIO;
goto fail;
} else if (status < 200) {
r = -EIO;
goto fail;
}
if (f->disk_fd < 0) {
log_error("No data received.");
r = -EIO;
goto fail;
}
f->content_length != f->written_compressed) {
log_error("Download truncated.");
r = -EIO;
goto fail;
}
if (f->etag)
if (f->url)
if (f->mtime != 0) {
}
goto fail;
}
/* Mark read-only */
assert(f->final_path);
if (r < 0) {
goto fail;
}
return;
fail:
raw_import_finish(f->import, r);
}
static int raw_import_file_open_disk_for_write(RawImportFile *f) {
int r;
assert(f);
if (f->disk_fd >= 0)
return 0;
r = raw_import_file_make_final_path(f);
if (r < 0)
return log_oom();
if (!f->temp_path) {
if (r < 0)
return log_oom();
}
if (f->disk_fd < 0)
return 0;
}
ssize_t n;
assert(f);
assert(p);
log_error("File too large, overflow");
return -EOVERFLOW;
}
log_error("File overly large, refusing");
return -EFBIG;
}
if (n < 0) {
return -errno;
}
log_error("Short write");
return -EIO;
}
f->written_uncompressed += sz;
return 0;
}
int r;
assert(f);
assert(p);
log_error("File too large, overflow");
return -EOVERFLOW;
}
log_error("Content length incorrect.");
return -EFBIG;
}
if (!f->compressed) {
r = raw_import_file_write_uncompressed(f, p, sz);
if (r < 0)
return r;
} else {
log_error("Decompression error.");
return -EIO;
}
if (r < 0)
return r;
}
}
f->written_compressed += sz;
return 0;
}
static int raw_import_file_detect_xz(RawImportFile *f) {
static const uint8_t xz_signature[] = {
'\xfd', '7', 'z', 'X', 'Z', '\x00'
};
int r;
assert(f);
if (f->payload_size < sizeof(xz_signature))
return 0;
if (f->compressed) {
log_error("Failed to initialize LZMA decoder.");
return -EIO;
}
}
if (r < 0)
return r;
if (r < 0)
return r;
f->payload_size = 0;
return 0;
}
static size_t raw_import_file_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
RawImportFile *f = userdata;
int r;
assert(f);
if (f->done) {
r = -ESTALE;
goto fail;
}
if (f->disk_fd < 0) {
uint8_t *p;
/* We haven't opened the file yet, let's first check what it actually is */
if (!p) {
r = log_oom();
goto fail;
}
f->payload_size = sz;
f->payload = p;
r = raw_import_file_detect_xz(f);
if (r < 0)
goto fail;
return sz;
}
if (r < 0)
goto fail;
return sz;
fail:
raw_import_finish(f->import, r);
return 0;
}
static size_t raw_import_file_header_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
RawImportFile *f = userdata;
char *etag;
int r;
assert(f);
if (f->done) {
r = -ESTALE;
goto fail;
}
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
log_info("Image already downloaded. Skipping download.");
return sz;
}
return sz;
}
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
char bytes[FORMAT_BYTES_MAX];
}
return sz;
}
if (r < 0) {
log_oom();
goto fail;
}
if (r > 0) {
return sz;
}
return sz;
fail:
raw_import_finish(f->import, r);
return 0;
}
static bool etag_is_valid(const char *etag) {
return false;
return false;
return true;
}
static int raw_import_file_find_old_etags(RawImportFile *f) {
int r;
if (!escaped_url)
return -ENOMEM;
if (!d) {
return 0;
return -errno;
}
const char *a, *b;
char *u;
continue;
if (!a)
continue;
a = startswith(a, escaped_url);
if (!a)
continue;
a = startswith(a, ".");
if (!a)
continue;
if (!b)
continue;
if (a >= b)
continue;
u = cunescape_length(a, b - a);
if (!u)
return -ENOMEM;
if (!etag_is_valid(u)) {
free(u);
continue;
}
r = strv_consume(&f->old_etags, u);
if (r < 0)
return r;
}
return 0;
}
static int raw_import_file_begin(RawImportFile *f) {
int r;
assert(f);
r = raw_import_file_find_old_etags(f);
if (r < 0)
return r;
if (r < 0)
return r;
if (!strv_isempty(f->old_etags)) {
if (!cc)
return -ENOMEM;
if (!hdr)
return -ENOMEM;
if (!f->request_header)
return -ENOMEM;
return -EIO;
}
return -EIO;
return -EIO;
return -EIO;
return -EIO;
if (r < 0)
return r;
return 0;
}
int raw_import_new(RawImport **import, sd_event *event, const char *image_root, raw_import_on_finished on_finished, void *userdata) {
int r;
if (!i)
return -ENOMEM;
i->on_finished = on_finished;
if (!i->image_root)
return -ENOMEM;
if (event)
else {
r = sd_event_default(&i->event);
if (r < 0)
return r;
}
if (r < 0)
return r;
*import = i;
i = NULL;
return 0;
}
RawImportFile *f;
if (!import)
return NULL;
return NULL;
}
RawImportFile *f;
if (!f)
return 0;
return 1;
}
int r;
return -EEXIST;
if (r < 0)
return r;
if (!f)
return -ENOMEM;
f->disk_fd = -1;
if (!f->url)
return -ENOMEM;
if (local) {
if (!f->local)
return -ENOMEM;
f->force_local = force_local;
}
if (r < 0)
return r;
r = raw_import_file_begin(f);
if (r < 0) {
f = NULL;
return r;
}
f = NULL;
return 0;
}
bool raw_url_is_valid(const char *url) {
return false;
return false;
return ascii_is_valid(url);
}