forked from open-io/oio-sds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_del.c
140 lines (115 loc) · 3.68 KB
/
http_del.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
OpenIO SDS core library
Copyright (C) 2017-2018 OpenIO SAS, as part of OpenIO SDS
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3.0 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library.
*/
#include <errno.h>
#include <curl/curl.h>
#include <curl/curlver.h>
#include <metautils/lib/metautils.h>
#include "http_del.h"
#include "http_internals.h"
static GError *
_chunks_removal_step(CURLM *mhandle, gboolean *next)
{
fd_set fdread, fdwrite, fdexcep;
int maxfd = -1;
*next = FALSE;
/* Prepare the select() call */
FD_ZERO(&fdread);
FD_ZERO(&fdwrite);
FD_ZERO(&fdexcep);
curl_multi_fdset(mhandle, &fdread, &fdwrite, &fdexcep, &maxfd);
GRID_WARN("maxfd %d", maxfd);
if (maxfd < 0) {
GRID_TRACE("No pending I/O");
} else {
long timeout = 0;
curl_multi_timeout (mhandle, &timeout);
struct timeval tv = {};
tv.tv_sec = timeout / 1000;
tv.tv_usec = (timeout % 1000) * 1000;
/* Wait for events to happen */
int rc = select(maxfd+1, &fdread, &fdwrite, &fdexcep, &tv);
if (rc < 0 && errno != EINTR && errno != EAGAIN)
return SYSERR("select() error: (%d) %s", errno, strerror(errno));
}
int nb = 0;
curl_multi_perform(mhandle, &nb);
/* run those pool of operations */
for (;;) {
int msgs_left = 0;
CURLMsg *msg = curl_multi_info_read(mhandle, &msgs_left);
if (!msg && !msgs_left)
break;
if (msg->msg != CURLMSG_DONE) {
GRID_TRACE("Unexpected CURL event");
} else {
long http_ret = 0;
gchar *url = NULL;
CURLcode curl_ret = msg->data.result;
CURL *handle = msg->easy_handle;
g_assert_nonnull(handle);
curl_easy_getinfo(handle, CURLINFO_PRIVATE, &url);
g_assert_nonnull(url);
curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &http_ret);
if (curl_ret != CURLE_OK) {
GRID_WARN("curl error code=%u strerror=%s",
curl_ret, curl_easy_strerror(curl_ret));
}
if (http_ret / 100 == 2) {
GRID_DEBUG("Deleted [%s] code=%ld strerror=%s",
url, http_ret, curl_easy_strerror(curl_ret));
} else {
GRID_WARN("Delete error [%s] code=%ld strerror=%s",
url, http_ret, curl_easy_strerror(curl_ret));
}
const CURLMcode mrc =
curl_multi_remove_handle(mhandle, handle);
g_assert_cmpint(mrc, ==, CURLM_OK);
}
}
*next = (nb != 0);
return NULL;
}
GError *
http_poly_delete (gchar **urlv)
{
CURLM *mhandle = curl_multi_init();
if (!mhandle)
return SYSERR("CURL multi allocation error");
GSList *handles = NULL;
/* Prepare the multiplexed curl operations */
for (gchar **purl=urlv; urlv && *purl ;++purl) {
CURL *handle = _curl_get_handle_blob();
curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(handle, CURLOPT_URL, *purl);
curl_easy_setopt(handle, CURLOPT_PRIVATE, *purl);
const CURLMcode rc = curl_multi_add_handle(mhandle, handle);
g_assert_cmpint(rc, ==, CURLM_OK);
handles = g_slist_prepend(handles, handle);
}
/* Loop until there is no pending call */
for (gboolean next=TRUE; next; ) {
GError *err = _chunks_removal_step(mhandle, &next);
if (err) {
GRID_WARN("CURL error while removing chunks: (%d) %s",
err->code, err->message);
break;
}
}
curl_multi_cleanup(mhandle);
for (GSList *l=handles; l ;l=l->next)
curl_easy_cleanup(l->data);
g_slist_free(handles);
return NULL;
}