chiark / gitweb /
fsck: use _cleanup_close_pair_ where appropriate
[elogind.git] / src / import / pull-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 <gcrypt.h>
25
26 #include "macro.h"
27 #include "curl-util.h"
28 #include "import-compress.h"
29
30 typedef struct PullJob PullJob;
31
32 typedef void (*PullJobFinished)(PullJob *job);
33 typedef int (*PullJobOpenDisk)(PullJob *job);
34 typedef int (*PullJobHeader)(PullJob *job, const char *header, size_t sz);
35 typedef void (*PullJobProgress)(PullJob *job);
36
37 typedef enum PullJobState {
38         PULL_JOB_INIT,
39         PULL_JOB_ANALYZING, /* Still reading into ->payload, to figure out what we have */
40         PULL_JOB_RUNNING,  /* Writing to destination */
41         PULL_JOB_DONE,
42         PULL_JOB_FAILED,
43         _PULL_JOB_STATE_MAX,
44         _PULL_JOB_STATE_INVALID = -1,
45 } PullJobState;
46
47 #define PULL_JOB_STATE_IS_COMPLETE(j) (IN_SET((j)->state, PULL_JOB_DONE, PULL_JOB_FAILED))
48
49 typedef enum PullJobCompression {
50         PULL_JOB_UNCOMPRESSED,
51         PULL_JOB_XZ,
52         PULL_JOB_GZIP,
53         PULL_JOB_BZIP2,
54         _PULL_JOB_COMPRESSION_MAX,
55         _PULL_JOB_COMPRESSION_INVALID = -1,
56 } PullJobCompression;
57
58 struct PullJob {
59         PullJobState state;
60         int error;
61
62         char *url;
63
64         void *userdata;
65         PullJobFinished on_finished;
66         PullJobOpenDisk on_open_disk;
67         PullJobHeader on_header;
68         PullJobProgress on_progress;
69
70         CurlGlue *glue;
71         CURL *curl;
72         struct curl_slist *request_header;
73
74         char *etag;
75         char **old_etags;
76         bool etag_exists;
77
78         uint64_t content_length;
79         uint64_t written_compressed;
80         uint64_t written_uncompressed;
81
82         uint64_t uncompressed_max;
83         uint64_t compressed_max;
84
85         uint8_t *payload;
86         size_t payload_size;
87         size_t payload_allocated;
88
89         int disk_fd;
90
91         usec_t mtime;
92
93         ImportCompress compress;
94
95         unsigned progress_percent;
96         usec_t start_usec;
97         usec_t last_status_usec;
98
99         bool allow_sparse;
100
101         bool calc_checksum;
102         gcry_md_hd_t checksum_context;
103
104         char *checksum;
105
106         bool grow_machine_directory;
107         uint64_t written_since_last_grow;
108 };
109
110 int pull_job_new(PullJob **job, const char *url, CurlGlue *glue, void *userdata);
111 PullJob* pull_job_unref(PullJob *job);
112
113 int pull_job_begin(PullJob *j);
114
115 void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result);
116
117 DEFINE_TRIVIAL_CLEANUP_FUNC(PullJob*, pull_job_unref);