chiark / gitweb /
2c01d723dbb71487ed8304fdd3764d1617370dcc
[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 <bzlib.h>
27 #include <gcrypt.h>
28
29 #include "macro.h"
30 #include "curl-util.h"
31
32 typedef struct ImportJob ImportJob;
33
34 typedef void (*ImportJobFinished)(ImportJob *job);
35 typedef int (*ImportJobOpenDisk)(ImportJob *job);
36 typedef int (*ImportJobHeader)(ImportJob *job, const char *header, size_t sz);
37 typedef void (*ImportJobProgress)(ImportJob *job);
38
39 typedef enum ImportJobState {
40         IMPORT_JOB_INIT,
41         IMPORT_JOB_ANALYZING, /* Still reading into ->payload, to figure out what we have */
42         IMPORT_JOB_RUNNING,  /* Writing to destination */
43         IMPORT_JOB_DONE,
44         IMPORT_JOB_FAILED,
45         _IMPORT_JOB_STATE_MAX,
46         _IMPORT_JOB_STATE_INVALID = -1,
47 } ImportJobState;
48
49 #define IMPORT_JOB_STATE_IS_COMPLETE(j) (IN_SET((j)->state, IMPORT_JOB_DONE, IMPORT_JOB_FAILED))
50
51 typedef enum ImportJobCompression {
52         IMPORT_JOB_UNCOMPRESSED,
53         IMPORT_JOB_XZ,
54         IMPORT_JOB_GZIP,
55         IMPORT_JOB_BZIP2,
56         _IMPORT_JOB_COMPRESSION_MAX,
57         _IMPORT_JOB_COMPRESSION_INVALID = -1,
58 } ImportJobCompression;
59
60 struct ImportJob {
61         ImportJobState state;
62         int error;
63
64         char *url;
65
66         void *userdata;
67         ImportJobFinished on_finished;
68         ImportJobOpenDisk on_open_disk;
69         ImportJobHeader on_header;
70         ImportJobProgress on_progress;
71
72         CurlGlue *glue;
73         CURL *curl;
74         struct curl_slist *request_header;
75
76         char *etag;
77         char **old_etags;
78         bool etag_exists;
79
80         uint64_t content_length;
81         uint64_t written_compressed;
82         uint64_t written_uncompressed;
83
84         uint64_t uncompressed_max;
85         uint64_t compressed_max;
86
87         uint8_t *payload;
88         size_t payload_size;
89         size_t payload_allocated;
90
91         int disk_fd;
92
93         usec_t mtime;
94
95         ImportJobCompression compressed;
96         lzma_stream xz;
97         z_stream gzip;
98         bz_stream bzip2;
99
100         unsigned progress_percent;
101         usec_t start_usec;
102         usec_t last_status_usec;
103
104         bool allow_sparse;
105
106         bool calc_checksum;
107         gcry_md_hd_t checksum_context;
108
109         char *checksum;
110
111         bool grow_machine_directory;
112         uint64_t written_since_last_grow;
113 };
114
115 int import_job_new(ImportJob **job, const char *url, CurlGlue *glue, void *userdata);
116 ImportJob* import_job_unref(ImportJob *job);
117
118 int import_job_begin(ImportJob *j);
119
120 void import_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result);
121
122 DEFINE_TRIVIAL_CLEANUP_FUNC(ImportJob*, import_job_unref);