1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3 
4 #include <sys/stat.h>
5 
6 #include "curl-util.h"
7 #include "import-compress.h"
8 #include "macro.h"
9 #include "openssl-util.h"
10 #include "pull-common.h"
11 
12 typedef struct PullJob PullJob;
13 
14 typedef void (*PullJobFinished)(PullJob *job);
15 typedef int (*PullJobOpenDisk)(PullJob *job);
16 typedef int (*PullJobHeader)(PullJob *job, const char *header, size_t sz);
17 typedef void (*PullJobProgress)(PullJob *job);
18 typedef int (*PullJobNotFound)(PullJob *job, char **ret_new_url);
19 
20 typedef enum PullJobState {
21         PULL_JOB_INIT,
22         PULL_JOB_ANALYZING, /* Still reading into ->payload, to figure out what we have */
23         PULL_JOB_RUNNING,   /* Writing to destination */
24         PULL_JOB_DONE,
25         PULL_JOB_FAILED,
26         _PULL_JOB_STATE_MAX,
27         _PULL_JOB_STATE_INVALID = -EINVAL,
28 } PullJobState;
29 
30 #define PULL_JOB_IS_COMPLETE(j) (IN_SET((j)->state, PULL_JOB_DONE, PULL_JOB_FAILED))
31 
32 struct PullJob {
33         PullJobState state;
34         int error;
35 
36         char *url;
37 
38         void *userdata;
39         PullJobFinished on_finished;
40         PullJobOpenDisk on_open_disk;
41         PullJobHeader on_header;
42         PullJobProgress on_progress;
43         PullJobNotFound on_not_found;
44 
45         CurlGlue *glue;
46         CURL *curl;
47         struct curl_slist *request_header;
48 
49         char *etag;
50         char **old_etags;
51         bool etag_exists;
52 
53         uint64_t content_length;
54         uint64_t written_compressed;
55         uint64_t written_uncompressed;
56         uint64_t offset;
57 
58         uint64_t uncompressed_max;
59         uint64_t compressed_max;
60 
61         uint8_t *payload;
62         size_t payload_size;
63 
64         int disk_fd;
65         bool close_disk_fd;
66         struct stat disk_stat;
67 
68         usec_t mtime;
69 
70         ImportCompress compress;
71 
72         unsigned progress_percent;
73         usec_t start_usec;
74         usec_t last_status_usec;
75 
76         bool calc_checksum;
77         hash_context_t checksum_ctx;
78 
79         char *checksum;
80         bool sync;
81         bool force_memory;
82 };
83 
84 int pull_job_new(PullJob **job, const char *url, CurlGlue *glue, void *userdata);
85 PullJob* pull_job_unref(PullJob *job);
86 
87 int pull_job_begin(PullJob *j);
88 
89 void pull_job_curl_on_finished(CurlGlue *g, CURL *curl, CURLcode result);
90 
91 void pull_job_close_disk_fd(PullJob *j);
92 
93 DEFINE_TRIVIAL_CLEANUP_FUNC(PullJob*, pull_job_unref);
94