@matheusbsbs / matheus-bagliano / commits / 75187c9deb
Fork 0

Refresh the remote lock if it is about to expire

Refresh the remote lock if it is about to expire Signed-off-by: Nick Hengeveld <nickh@reactrix.com> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nick Hengeveld committed Nov 4, 2005 at 14:22 UTC 75187c9debdc57d47e0842f97e30cb912d364ff2
1 file changed +79 -27
http-push.c
+79 -27
@@ -40,6 +40,9 @@ static const char http_push_usage[] =
40 #define PROPFIND_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:propfind xmlns:D=\"DAV:\">\n<D:prop xmlns:R=\"%s\">\n<D:supportedlock/>\n</D:prop>\n</D:propfind>"
41 #define LOCK_REQUEST "<?xml version=\"1.0\" encoding=\"utf-8\" ?>\n<D:lockinfo xmlns:D=\"DAV:\">\n<D:lockscope><D:exclusive/></D:lockscope>\n<D:locktype><D:write/></D:locktype>\n<D:owner>\n<D:href>mailto:%s</D:href>\n</D:owner>\n</D:lockinfo>"
42
43 +#define LOCK_TIME 600
44 +#define LOCK_REFRESH 30
45 +
46 static int active_requests = 0;
47 static int data_received;
48 static int pushing = 0;
@@ -143,10 +146,12 @@ struct active_lock
146 int ctx_timeout;
147 int ctx_locktoken;
148 int ctx_locktoken_href;
149 + char *url;
150 char *owner;
151 + char *token;
152 time_t start_time;
153 long timeout;
149 - char *token;
154 + int refreshing;
155 };
156
157 struct lockprop
@@ -580,11 +585,65 @@ static void start_move(struct transfer_request *request)
585 }
586 }
587
588 +int refresh_lock(struct active_lock *lock)
589 +{
590 + struct active_request_slot *slot;
591 + char *if_header;
592 + char timeout_header[25];
593 + struct curl_slist *dav_headers = NULL;
594 + int rc = 0;
595 +
596 + lock->refreshing = 1;
597 +
598 + if_header = xmalloc(strlen(lock->token) + 25);
599 + sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
600 + sprintf(timeout_header, "Timeout: Second-%ld", lock->timeout);
601 + dav_headers = curl_slist_append(dav_headers, if_header);
602 + dav_headers = curl_slist_append(dav_headers, timeout_header);
603 +
604 + slot = get_active_slot();
605 + curl_easy_setopt(slot->curl, CURLOPT_HTTPGET, 1);
606 + curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
607 + curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
608 + curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_LOCK);
609 + curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
610 +
611 + if (start_active_slot(slot)) {
612 + run_active_slot(slot);
613 + if (slot->curl_result != CURLE_OK) {
614 + fprintf(stderr, "Got HTTP error %ld\n", slot->http_code);
615 + } else {
616 + lock->start_time = time(NULL);
617 + rc = 1;
618 + }
619 + }
620 +
621 + lock->refreshing = 0;
622 + curl_slist_free_all(dav_headers);
623 + free(if_header);
624 +
625 + return rc;
626 +}
627 +
628 static void finish_request(struct transfer_request *request)
629 {
630 + time_t current_time = time(NULL);
631 + int time_remaining;
632 +
633 request->curl_result = request->slot->curl_result;
634 request->http_code = request->slot->http_code;
635 request->slot = NULL;
636 +
637 + /* Refresh the lock if it is close to timing out */
638 + time_remaining = request->lock->start_time + request->lock->timeout
639 + - current_time;
640 + if (time_remaining < LOCK_REFRESH && !request->lock->refreshing) {
641 + if (!refresh_lock(request->lock)) {
642 + fprintf(stderr, "Unable to refresh remote lock\n");
643 + aborted = 1;
644 + }
645 + }
646 +
647 if (request->headers != NULL)
648 curl_slist_free_all(request->headers);
649 if (request->state == RUN_HEAD) {
@@ -1114,7 +1173,7 @@ end_lockprop_element(void *userData, const char *name)
1173 }
1174 }
1175
1117 -struct active_lock *lock_remote(char *file, int timeout)
1176 +struct active_lock *lock_remote(char *file, long timeout)
1177 {
1178 struct active_request_slot *slot;
1179 struct buffer out_buffer;
@@ -1175,8 +1234,9 @@ struct active_lock *lock_remote(char *file, int timeout)
1234 new_lock->owner = NULL;
1235 new_lock->token = NULL;
1236 new_lock->timeout = -1;
1237 + new_lock->refreshing = 0;
1238
1179 - sprintf(timeout_header, "Timeout: Second-%d", timeout);
1239 + sprintf(timeout_header, "Timeout: Second-%ld", timeout);
1240 dav_headers = curl_slist_append(dav_headers, timeout_header);
1241 dav_headers = curl_slist_append(dav_headers, "Content-Type: text/xml");
1242
@@ -1211,7 +1271,6 @@ struct active_lock *lock_remote(char *file, int timeout)
1271 return NULL;
1272 }
1273
1214 - free(url);
1274 free(out_data);
1275
1276 XML_SetUserData(parser, new_lock);
@@ -1223,6 +1282,7 @@ struct active_lock *lock_remote(char *file, int timeout)
1282 if (result != XML_STATUS_OK) {
1283 fprintf(stderr, "%s", XML_ErrorString(
1284 XML_GetErrorCode(parser)));
1285 + free(url);
1286 free(new_lock);
1287 return NULL;
1288 }
@@ -1232,18 +1292,19 @@ struct active_lock *lock_remote(char *file, int timeout)
1292 free(new_lock->token);
1293 if (new_lock->owner != NULL)
1294 free(new_lock->owner);
1295 + free(url);
1296 free(new_lock);
1297 return NULL;
1298 }
1299
1300 + new_lock->url = url;
1301 new_lock->start_time = time(NULL);
1302 return new_lock;
1303 }
1304
1243 -int unlock_remote(char *file, struct active_lock *lock)
1305 +int unlock_remote(struct active_lock *lock)
1306 {
1307 struct active_request_slot *slot;
1246 - char *url;
1308 char *lock_token_header;
1309 struct curl_slist *dav_headers = NULL;
1310 int rc = 0;
@@ -1251,13 +1312,11 @@ int unlock_remote(char *file, struct active_lock *lock)
1312 lock_token_header = xmalloc(strlen(lock->token) + 31);
1313 sprintf(lock_token_header, "Lock-Token: <opaquelocktoken:%s>",
1314 lock->token);
1254 - url = xmalloc(strlen(remote->url) + strlen(file) + 1);
1255 - sprintf(url, "%s%s", remote->url, file);
1315 dav_headers = curl_slist_append(dav_headers, lock_token_header);
1316
1317 slot = get_active_slot();
1318 curl_easy_setopt(slot->curl, CURLOPT_WRITEFUNCTION, fwrite_null);
1260 - curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1319 + curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1320 curl_easy_setopt(slot->curl, CURLOPT_CUSTOMREQUEST, DAV_UNLOCK);
1321 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1322
@@ -1274,7 +1333,12 @@ int unlock_remote(char *file, struct active_lock *lock)
1333
1334 curl_slist_free_all(dav_headers);
1335 free(lock_token_header);
1277 - free(url);
1336 +
1337 + if (lock->owner != NULL)
1338 + free(lock->owner);
1339 + free(lock->url);
1340 + free(lock->token);
1341 + free(lock);
1342
1343 return rc;
1344 }
@@ -1423,20 +1487,15 @@ void get_delta(unsigned char *sha1, struct object *obj,
1487 }
1488 }
1489
1426 -int update_remote(char *remote_path, unsigned char *sha1,
1427 - struct active_lock *lock)
1490 +int update_remote(unsigned char *sha1, struct active_lock *lock)
1491 {
1492 struct active_request_slot *slot;
1430 - char *url;
1493 char *out_data;
1494 char *if_header;
1495 struct buffer out_buffer;
1496 struct curl_slist *dav_headers = NULL;
1497 int i;
1498
1437 - url = xmalloc(strlen(remote->url) + strlen(remote_path) + 1);
1438 - sprintf(url, "%s%s", remote->url, remote_path);
1439 -
1499 if_header = xmalloc(strlen(lock->token) + 25);
1500 sprintf(if_header, "If: (<opaquelocktoken:%s>)", lock->token);
1501 dav_headers = curl_slist_append(dav_headers, if_header);
@@ -1460,13 +1519,12 @@ int update_remote(char *remote_path, unsigned char *sha1,
1519 curl_easy_setopt(slot->curl, CURLOPT_HTTPHEADER, dav_headers);
1520 curl_easy_setopt(slot->curl, CURLOPT_UPLOAD, 1);
1521 curl_easy_setopt(slot->curl, CURLOPT_PUT, 1);
1463 - curl_easy_setopt(slot->curl, CURLOPT_URL, url);
1522 + curl_easy_setopt(slot->curl, CURLOPT_URL, lock->url);
1523
1524 if (start_active_slot(slot)) {
1525 run_active_slot(slot);
1526 free(out_data);
1527 free(if_header);
1469 - free(url);
1528 if (slot->curl_result != CURLE_OK) {
1529 fprintf(stderr,
1530 "PUT error: curl result=%d, HTTP code=%ld\n",
@@ -1477,7 +1535,6 @@ int update_remote(char *remote_path, unsigned char *sha1,
1535 } else {
1536 free(out_data);
1537 free(if_header);
1480 - free(url);
1538 fprintf(stderr, "Unable to start PUT request\n");
1539 return 0;
1540 }
@@ -1629,7 +1686,7 @@ int main(int argc, char **argv)
1686 free(remote_path);
1687 remote_path = xmalloc(strlen(remote_ref) + 12);
1688 sprintf(remote_path, "refs/heads/%s", remote_ref);
1632 - remote_lock = lock_remote(remote_path, 3600);
1689 + remote_lock = lock_remote(remote_path, LOCK_TIME);
1690 if (remote_lock == NULL) {
1691 fprintf(stderr, "Unable to lock remote branch %s\n",
1692 remote_ref);
@@ -1701,8 +1758,7 @@ int main(int argc, char **argv)
1758
1759 /* Update the remote branch if all went well */
1760 if (do_remote_update) {
1704 - if (!aborted && update_remote(remote_path,
1705 - local_sha1,
1761 + if (!aborted && update_remote(local_sha1,
1762 remote_lock)) {
1763 fprintf(stderr, "%s remote branch %s\n",
1764 new_branch ? "Created" : "Updated",
@@ -1718,12 +1774,8 @@ int main(int argc, char **argv)
1774 }
1775
1776 unlock:
1721 - unlock_remote(remote_path, remote_lock);
1777 + unlock_remote(remote_lock);
1778 free(remote_path);
1723 - if (remote_lock->owner != NULL)
1724 - free(remote_lock->owner);
1725 - free(remote_lock->token);
1726 - free(remote_lock);
1779 }
1780
1781 cleanup: