import-job.h revision ff2670ad11a1a29d483b7a246c95b159ddc1137c
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#pragma once
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog/***
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog This file is part of systemd.
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog Copyright 2015 Lennart Poettering
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog systemd is free software; you can redistribute it and/or modify it
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog under the terms of the GNU Lesser General Public License as published by
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog the Free Software Foundation; either version 2.1 of the License, or
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog (at your option) any later version.
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog systemd is distributed in the hope that it will be useful, but
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog WITHOUT ANY WARRANTY; without even the implied warranty of
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog Lesser General Public License for more details.
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog You should have received a copy of the GNU Lesser General Public License
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog along with systemd; If not, see <http://www.gnu.org/licenses/>.
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog***/
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include <lzma.h>
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include <zlib.h>
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include <bzlib.h>
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include <gcrypt.h>
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include "macro.h"
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#include "curl-util.h"
a64edefacbad4f3d538b45a6a65d8a5a03797d78Thomas Hindoe Paaboel Andersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogtypedef struct ImportJob ImportJob;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersentypedef void (*ImportJobFinished)(ImportJob *job);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogtypedef int (*ImportJobOpenDisk)(ImportJob *job);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogtypedef int (*ImportJobHeader)(ImportJob*job, const char *header, size_t sz);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogtypedef enum ImportJobState {
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_INIT,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_ANALYZING, /* Still reading into ->payload, to figure out what we have */
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_RUNNING, /* Writing to destination */
89ca10c6a61309d84d54c5dc5a295387ce39e610Lennart Poettering IMPORT_JOB_DONE,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_FAILED,
89ca10c6a61309d84d54c5dc5a295387ce39e610Lennart Poettering _IMPORT_JOB_STATE_MAX,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog _IMPORT_JOB_STATE_INVALID = -1,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog} ImportJobState;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog#define IMPORT_JOB_STATE_IS_COMPLETE(j) (IN_SET((j)->state, IMPORT_JOB_DONE, IMPORT_JOB_FAILED))
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogtypedef enum ImportJobCompression {
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_UNCOMPRESSED,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_XZ,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_GZIP,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog IMPORT_JOB_BZIP2,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog _IMPORT_JOB_COMPRESSION_MAX,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog _IMPORT_JOB_COMPRESSION_INVALID = -1,
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog} ImportJobCompression;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersenstruct ImportJob {
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen ImportJobState state;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog int error;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog char *url;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog void *userdata;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog ImportJobFinished on_finished;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog ImportJobOpenDisk on_open_disk;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog ImportJobHeader on_header;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog CurlGlue *glue;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen CURL *curl;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen struct curl_slist *request_header;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog char *etag;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen char **old_etags;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen bool etag_exists;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint64_t content_length;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog uint64_t written_compressed;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint64_t written_uncompressed;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog uint64_t uncompressed_max;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint64_t compressed_max;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen uint8_t *payload;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog size_t payload_size;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen size_t payload_allocated;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen int disk_fd;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog usec_t mtime;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog ImportJobCompression compressed;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog lzma_stream xz;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen z_stream gzip;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen bz_stream bzip2;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog unsigned progress_percent;
996d16975b4d802335188a3be2bbc3635c1287f3Tom Gundersen usec_t start_usec;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog usec_t last_status_usec;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog bool allow_sparse;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog bool calc_checksum;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog gcry_md_hd_t checksum_context;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog char *checksum;
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog};
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogint import_job_new(ImportJob **job, const char *url, CurlGlue *glue, void *userdata);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar LindskogImportJob* import_job_unref(ImportJob *job);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogint import_job_begin(ImportJob *j);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskogvoid import_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar LindskogDEFINE_TRIVIAL_CLEANUP_FUNC(ImportJob*, import_job_unref);
d9bf4f8c6c47b8620ffa1a056208eb15118b78d5Umut Tezduyar Lindskog