chiark / gitweb /
import: make verification code generic, in preparation for using it pull-tar
[elogind.git] / src / import / import-job.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2015 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <lzma.h>
25 #include <zlib.h>
26 #include <gcrypt.h>
27
28 #include "macro.h"
29 #include "curl-util.h"
30
31 typedef struct ImportJob ImportJob;
32
33 typedef void (*ImportJobFinished)(ImportJob *job);
34 typedef int (*ImportJobOpenDisk)(ImportJob *job);
35
36 typedef enum ImportJobState {
37         IMPORT_JOB_INIT,
38         IMPORT_JOB_ANALYZING, /* Still reading into ->payload, to figure out what we have */
39         IMPORT_JOB_RUNNING,  /* Writing to destination */
40         IMPORT_JOB_DONE,
41         IMPORT_JOB_FAILED,
42         _IMPORT_JOB_STATE_MAX,
43         _IMPORT_JOB_STATE_INVALID = -1,
44 } ImportJobState;
45
46 #define IMPORT_JOB_STATE_IS_COMPLETE(j) (IN_SET((j)->state, IMPORT_JOB_DONE, IMPORT_JOB_FAILED))
47
48 typedef enum ImportJobCompression {
49         IMPORT_JOB_UNCOMPRESSED,
50         IMPORT_JOB_XZ,
51         IMPORT_JOB_GZIP,
52         _IMPORT_JOB_COMPRESSION_MAX,
53         _IMPORT_JOB_COMPRESSION_INVALID = -1,
54 } ImportJobCompression;
55
56 struct ImportJob {
57         ImportJobState state;
58         int error;
59
60         char *url;
61
62         void *userdata;
63         ImportJobFinished on_finished;
64         ImportJobOpenDisk on_open_disk;
65
66         CurlGlue *glue;
67         CURL *curl;
68         struct curl_slist *request_header;
69
70         char *etag;
71         char **old_etags;
72         bool etag_exists;
73
74         uint64_t content_length;
75         uint64_t written_compressed;
76         uint64_t written_uncompressed;
77
78         uint64_t uncompressed_max;
79         uint64_t compressed_max;
80
81         uint8_t *payload;
82         size_t payload_size;
83         size_t payload_allocated;
84
85         int disk_fd;
86
87         usec_t mtime;
88
89         ImportJobCompression compressed;
90         lzma_stream xz;
91         z_stream gzip;
92
93         unsigned progress_percent;
94         usec_t start_usec;
95         usec_t last_status_usec;
96
97         bool allow_sparse;
98
99         bool calc_checksum;
100         gcry_md_hd_t checksum_context;
101
102         char *checksum;
103 };
104
105 int import_job_new(ImportJob **job, const char *url, CurlGlue *glue, void *userdata);
106 ImportJob* import_job_unref(ImportJob *job);
107
108 int import_job_begin(ImportJob *j);
109
110 void import_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result);
111
112 DEFINE_TRIVIAL_CLEANUP_FUNC(ImportJob*, import_job_unref);