-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathupload.c
94 lines (76 loc) · 2.19 KB
/
upload.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* SPDX-License-Identifier: BSD-3-Clause */
#include "ucentral.h"
static int upload_pending = 0;
static char *upload_uuid;
static char *upload_file;
static char *upload_uri;
static void
upload_run_cb(time_t uuid, uint32_t id)
{
char *file;
char *name;
if (asprintf(&file, "data=@%s", upload_file) == -1 ||
asprintf(&name, "name=%s", upload_uuid) == -1) {
ULOG_INFO("failed to start upload task\n");
exit(1);
}
ULOG_INFO("Calling /usr/bin/curl -F %s -F %s %s %s", name, file, upload_uri,
client.selfsigned ? "--insecure" : "");
execlp("/usr/bin/curl", "/usr/bin/curl",
"-F", name, "-F", file, upload_uri,
client.selfsigned ? "--insecure" : NULL,
NULL);
ULOG_ERR("curl was not executed");
exit(1);
}
static void
upload_complete_cb(struct task *t, time_t uuid, uint32_t id, int ret)
{
upload_pending = 0;
unlink(upload_file);
if (ret) {
ULOG_ERR("ucentral: curl returned (%d)", ret);
log_send("failed to upload file", LOG_ERR);
return;
}
log_send("upload complete", LOG_INFO);
}
struct task upload_task = {
.run = upload_run_cb,
.complete = upload_complete_cb,
};
void
upload_run(struct blob_attr *a)
{
enum {
UPLOAD_UUID,
UPLOAD_FILE,
UPLOAD_URI,
__UPLOAD_MAX,
};
static const struct blobmsg_policy error_policy[__UPLOAD_MAX] = {
[UPLOAD_UUID] = { .name = "uuid", .type = BLOBMSG_TYPE_STRING },
[UPLOAD_FILE] = { .name = "file", .type = BLOBMSG_TYPE_STRING },
[UPLOAD_URI] = { .name = "uri", .type = BLOBMSG_TYPE_STRING },
};
struct blob_attr *tb[__UPLOAD_MAX] = {};
uint32_t id = 0;
if (upload_pending) {
log_send("could not upload file due to another pending upload", LOG_WARNING);
return;
}
blobmsg_parse(error_policy, __UPLOAD_MAX, tb, blobmsg_data(a),
blobmsg_data_len(a));
if (!tb[UPLOAD_UUID] || !tb[UPLOAD_FILE] || !tb[UPLOAD_URI]) {
log_send("invalid upload request", LOG_ERR);
return;
}
safe_free(&upload_uuid);
safe_free(&upload_file);
safe_free(&upload_uri);
upload_uuid = strdup(blobmsg_get_string(tb[UPLOAD_UUID]));
upload_file = strdup(blobmsg_get_string(tb[UPLOAD_FILE]));
upload_uri = strdup(blobmsg_get_string(tb[UPLOAD_URI]));
upload_pending = 1;
task_run(&upload_task, uuid_latest, id, 0);
}