abort checkout operations cleanly when Ctrl-C is hit
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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564
diff --git a/got/got.c b/got/got.c
index 0c2e16f..b0622d7 100644
--- a/got/got.c
+++ b/got/got.c
@@ -23,6 +23,7 @@
#include <err.h>
#include <errno.h>
#include <locale.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -43,6 +44,22 @@
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
+static volatile sig_atomic_t sigint_received;
+static volatile sig_atomic_t sigpipe_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
+static void
+catch_sigpipe(int signo)
+{
+ sigpipe_received = 1;
+}
+
+
struct cmd {
const char *cmd_name;
const struct got_error *(*cmd_main)(int, char *[]);
@@ -111,6 +128,9 @@ main(int argc, char *argv[])
if (argc <= 0)
usage();
+ signal(SIGINT, catch_sigint);
+ signal(SIGPIPE, catch_sigpipe);
+
for (i = 0; i < nitems(got_commands); i++) {
const struct got_error *error;
@@ -169,6 +189,14 @@ checkout_progress(void *arg, const char *path)
}
static const struct got_error *
+checkout_cancel(void *arg)
+{
+ if (sigint_received || sigpipe_received)
+ return got_error(GOT_ERR_CANCELLED);
+ return NULL;
+}
+
+static const struct got_error *
cmd_checkout(int argc, char *argv[])
{
const struct got_error *error = NULL;
@@ -256,7 +284,7 @@ cmd_checkout(int argc, char *argv[])
goto done;
error = got_worktree_checkout_files(worktree, head_ref, repo,
- checkout_progress, worktree_path);
+ checkout_progress, worktree_path, checkout_cancel, NULL);
if (error != NULL)
goto done;
diff --git a/include/got_error.h b/include/got_error.h
index 4376f1b..1a82e73 100644
--- a/include/got_error.h
+++ b/include/got_error.h
@@ -62,6 +62,7 @@
#define GOT_ERR_ITER_COMPLETED 46
#define GOT_ERR_RANGE 47
#define GOT_ERR_EXPECTED 48 /* for use in regress tests only */
+#define GOT_ERR_CANCELLED 49
static const struct got_error {
int code;
@@ -112,6 +113,7 @@ static const struct got_error {
{ GOT_ERR_ITER_COMPLETED,"iteration completed" },
{ GOT_ERR_RANGE, "value out of range" },
{ GOT_ERR_EXPECTED, "expected an error but have no error" },
+ { GOT_ERR_CANCELLED, "operation in progress has been cancelled" },
};
/*
diff --git a/include/got_worktree.h b/include/got_worktree.h
index f4b03a4..806d598 100644
--- a/include/got_worktree.h
+++ b/include/got_worktree.h
@@ -51,6 +51,10 @@ char *got_worktree_get_head_ref_name(struct got_worktree *);
/* A callback function which is invoked when a path is checked out. */
typedef void (*got_worktree_checkout_cb)(void *, const char *);
+/* A callback function which is invoked at cancellation points.
+ * May return GOT_ERR_CANCELLED to abort the runing operation. */
+typedef const struct got_error *(*got_worktree_cancel_cb)(void *);
+
/*
* Attempt to check out files into a work tree from its associated repository
* and path prefix, and update the work tree's file index accordingly.
@@ -61,4 +65,5 @@ typedef void (*got_worktree_checkout_cb)(void *, const char *);
*/
const struct got_error *got_worktree_checkout_files(struct got_worktree *,
struct got_reference *, struct got_repository *,
- got_worktree_checkout_cb progress, void *);
+ got_worktree_checkout_cb progress, void *,
+ got_worktree_cancel_cb, void *);
diff --git a/lib/worktree.c b/lib/worktree.c
index d93818f..20f110b 100644
--- a/lib/worktree.c
+++ b/lib/worktree.c
@@ -470,13 +470,15 @@ done:
static const struct got_error *
tree_checkout(struct got_worktree *, struct got_fileindex *,
struct got_tree_object *, const char *, struct got_repository *,
- got_worktree_checkout_cb progress_cb, void *progress_arg);
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg);
static const struct got_error *
tree_checkout_entry(struct got_worktree *worktree,
struct got_fileindex *fileindex, struct got_tree_entry *te,
const char *parent, struct got_repository *repo,
- got_worktree_checkout_cb progress_cb, void *progress_arg)
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
struct got_object *obj = NULL;
@@ -525,8 +527,9 @@ tree_checkout_entry(struct got_worktree *worktree,
if (err)
break;
}
+ /* XXX infinite recursion possible */
err = tree_checkout(worktree, fileindex, tree, path, repo,
- progress_cb, progress_arg);
+ progress_cb, progress_arg, cancel_cb, cancel_arg);
break;
default:
break;
@@ -547,7 +550,8 @@ static const struct got_error *
tree_checkout(struct got_worktree *worktree,
struct got_fileindex *fileindex, struct got_tree_object *tree,
const char *path, struct got_repository *repo,
- got_worktree_checkout_cb progress_cb, void *progress_arg)
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL;
const struct got_tree_entries *entries;
@@ -561,8 +565,13 @@ tree_checkout(struct got_worktree *worktree,
entries = got_object_tree_get_entries(tree);
SIMPLEQ_FOREACH(te, &entries->head, entry) {
+ if (cancel_cb) {
+ err = (*cancel_cb)(cancel_arg);
+ if (err)
+ break;
+ }
err = tree_checkout_entry(worktree, fileindex, te, path, repo,
- progress_cb, progress_arg);
+ progress_cb, progress_arg, cancel_cb, cancel_arg);
if (err)
break;
}
@@ -573,7 +582,8 @@ tree_checkout(struct got_worktree *worktree,
const struct got_error *
got_worktree_checkout_files(struct got_worktree *worktree,
struct got_reference *head_ref, struct got_repository *repo,
- got_worktree_checkout_cb progress_cb, void *progress_arg)
+ got_worktree_checkout_cb progress_cb, void *progress_arg,
+ got_worktree_cancel_cb cancel_cb, void *cancel_arg)
{
const struct got_error *err = NULL, *unlockerr;
struct got_object_id *commit_id = NULL;
@@ -637,7 +647,7 @@ got_worktree_checkout_files(struct got_worktree *worktree,
goto done;
err = tree_checkout(worktree, fileindex, tree, "/", repo,
- progress_cb, progress_arg);
+ progress_cb, progress_arg, cancel_cb, cancel_arg);
if (err)
goto done;
diff --git a/libexec/got-read-blob/got-read-blob.c b/libexec/got-read-blob/got-read-blob.c
index 0713310..d33d70d 100644
--- a/libexec/got-read-blob/got-read-blob.c
+++ b/libexec/got-read-blob/got-read-blob.c
@@ -23,6 +23,7 @@
#include <stdint.h>
#include <imsg.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,6 +39,14 @@
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
int
main(int argc, char *argv[])
{
@@ -45,6 +54,8 @@ main(int argc, char *argv[])
struct imsgbuf ibuf;
size_t datalen;
+ signal(SIGINT, catch_sigint);
+
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
@@ -66,6 +77,11 @@ main(int argc, char *argv[])
memset(&imsg_outfd, 0, sizeof(imsg_outfd));
imsg_outfd.fd = -1;
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
+
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
@@ -136,18 +152,16 @@ done:
close(imsg_outfd.fd);
imsg_free(&imsg);
imsg_free(&imsg_outfd);
- if (err) {
- if (err->code == GOT_ERR_PRIVSEP_PIPE)
- err = NULL;
- else
- got_privsep_send_error(&ibuf, err);
+ if (err)
break;
- }
}
imsg_clear(&ibuf);
- if (err)
+ if (err) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
+ got_privsep_send_error(&ibuf, err);
+ }
close(GOT_IMSG_FD_CHILD);
return err ? 1 : 0;
}
diff --git a/libexec/got-read-commit/got-read-commit.c b/libexec/got-read-commit/got-read-commit.c
index f6b5435..56aac6a 100644
--- a/libexec/got-read-commit/got-read-commit.c
+++ b/libexec/got-read-commit/got-read-commit.c
@@ -23,6 +23,7 @@
#include <stdint.h>
#include <imsg.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,6 +39,14 @@
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
static const struct got_error *
read_commit_object(struct got_commit_object **commit, struct got_object *obj,
FILE *f)
@@ -74,6 +83,8 @@ main(int argc, char *argv[])
struct imsgbuf ibuf;
size_t datalen;
+ signal(SIGINT, catch_sigint);
+
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
@@ -90,6 +101,11 @@ main(int argc, char *argv[])
struct got_imsg_object iobj;
FILE *f = NULL;
struct got_object *obj = NULL;
+
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
@@ -152,18 +168,16 @@ done:
imsg_free(&imsg);
if (obj)
got_object_close(obj);
- if (err) {
- if (err->code == GOT_ERR_PRIVSEP_PIPE)
- err = NULL;
- else
- got_privsep_send_error(&ibuf, err);
+ if (err)
break;
- }
}
imsg_clear(&ibuf);
- if (err)
+ if (err) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
+ got_privsep_send_error(&ibuf, err);
+ }
close(GOT_IMSG_FD_CHILD);
return err ? 1 : 0;
}
diff --git a/libexec/got-read-object/got-read-object.c b/libexec/got-read-object/got-read-object.c
index baca773..8f4ccd3 100644
--- a/libexec/got-read-object/got-read-object.c
+++ b/libexec/got-read-object/got-read-object.c
@@ -23,6 +23,7 @@
#include <stdint.h>
#include <imsg.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -45,6 +46,14 @@
#define GOT_OBJ_TAG_TREE "tree"
#define GOT_OBJ_TAG_BLOB "blob"
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
static const struct got_error *
parse_object_header(struct got_object **obj, char *buf, size_t len)
{
@@ -154,6 +163,8 @@ main(int argc, char *argv[])
struct imsgbuf ibuf;
size_t datalen;
+ signal(SIGINT, catch_sigint);
+
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
@@ -166,6 +177,11 @@ main(int argc, char *argv[])
#endif
while (1) {
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
+
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
@@ -197,18 +213,16 @@ done:
imsg_free(&imsg);
if (obj)
got_object_close(obj);
- if (err) {
- if (err->code == GOT_ERR_PRIVSEP_PIPE)
- err = NULL;
- else
- got_privsep_send_error(&ibuf, err);
+ if (err)
break;
- }
}
imsg_clear(&ibuf);
- if (err)
+ if (err) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ if(!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
+ got_privsep_send_error(&ibuf, err);
+ }
close(GOT_IMSG_FD_CHILD);
return err ? 1 : 0;
}
diff --git a/libexec/got-read-pack/got-read-pack.c b/libexec/got-read-pack/got-read-pack.c
index 010b14b..debf114 100644
--- a/libexec/got-read-pack/got-read-pack.c
+++ b/libexec/got-read-pack/got-read-pack.c
@@ -23,6 +23,7 @@
#include <sys/mman.h>
#include <limits.h>
+#include <signal.h>
#include <stdint.h>
#include <imsg.h>
#include <stdio.h>
@@ -42,6 +43,14 @@
#include "got_lib_privsep.h"
#include "got_lib_pack.h"
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
+
static const struct got_error *
object_request(struct imsg *imsg, struct imsgbuf *ibuf, struct got_pack *pack,
struct got_packidx *packidx, struct got_object_cache *objcache)
@@ -412,6 +421,8 @@ main(int argc, char *argv[])
//static int attached;
//while (!attached) sleep(1);
+ signal(SIGINT, catch_sigint);
+
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
err = got_object_cache_init(&objcache, GOT_OBJECT_CACHE_TYPE_OBJ);
@@ -445,6 +456,11 @@ main(int argc, char *argv[])
while (1) {
imsg.fd = -1;
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
+
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
if (err->code == GOT_ERR_PRIVSEP_PIPE)
@@ -480,13 +496,8 @@ main(int argc, char *argv[])
if (imsg.fd != -1)
close(imsg.fd);
imsg_free(&imsg);
- if (err) {
- if (err->code == GOT_ERR_PRIVSEP_PIPE)
- err = NULL;
- else
- got_privsep_send_error(&ibuf, err);
+ if (err)
break;
- }
}
if (packidx)
@@ -495,8 +506,11 @@ main(int argc, char *argv[])
got_pack_close(pack);
got_object_cache_close(&objcache);
imsg_clear(&ibuf);
- if (err)
+ if (err) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
+ got_privsep_send_error(&ibuf, err);
+ }
close(GOT_IMSG_FD_CHILD);
return err ? 1 : 0;
}
diff --git a/libexec/got-read-tree/got-read-tree.c b/libexec/got-read-tree/got-read-tree.c
index 5abd564..176d429 100644
--- a/libexec/got-read-tree/got-read-tree.c
+++ b/libexec/got-read-tree/got-read-tree.c
@@ -23,6 +23,7 @@
#include <stdint.h>
#include <imsg.h>
+#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -38,6 +39,13 @@
#include "got_lib_object_parse.h"
#include "got_lib_privsep.h"
+static volatile sig_atomic_t sigint_received;
+
+static void
+catch_sigint(int signo)
+{
+ sigint_received = 1;
+}
static const struct got_error *
read_tree_object(struct got_tree_object **tree, struct got_object *obj, FILE *f)
{
@@ -73,6 +81,8 @@ main(int argc, char *argv[])
struct imsgbuf ibuf;
size_t datalen;
+ signal(SIGINT, catch_sigint);
+
imsg_init(&ibuf, GOT_IMSG_FD_CHILD);
#ifndef PROFILE
@@ -89,6 +99,11 @@ main(int argc, char *argv[])
struct got_imsg_object iobj;
FILE *f = NULL;
struct got_object *obj = NULL;
+
+ if (sigint_received) {
+ err = got_error(GOT_ERR_CANCELLED);
+ break;
+ }
err = got_privsep_recv_imsg(&imsg, &ibuf, 0);
if (err) {
@@ -151,18 +166,16 @@ done:
imsg_free(&imsg);
if (obj)
got_object_close(obj);
- if (err) {
- if (err->code == GOT_ERR_PRIVSEP_PIPE)
- err = NULL;
- else
- got_privsep_send_error(&ibuf, err);
+ if (err)
break;
- }
}
imsg_clear(&ibuf);
- if (err)
+ if (err) {
fprintf(stderr, "%s: %s\n", getprogname(), err->msg);
+ if (!sigint_received && err->code != GOT_ERR_PRIVSEP_PIPE)
+ got_privsep_send_error(&ibuf, err);
+ }
close(GOT_IMSG_FD_CHILD);
return err ? 1 : 0;
}
diff --git a/regress/worktree/worktree_test.c b/regress/worktree/worktree_test.c
index 1b97298..f5e074f 100644
--- a/regress/worktree/worktree_test.c
+++ b/regress/worktree/worktree_test.c
@@ -335,7 +335,7 @@ worktree_checkout(const char *repo_path)
goto done;
err = got_worktree_checkout_files(worktree, head_ref, repo,
- process_cb, NULL);
+ process_cb, NULL, NULL, NULL);
if (err != NULL)
goto done;