Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Refactor code duplication between do_grant() and do_revoke()

  • Loading branch information...
commit 70e5a716dcc6abc7b7a96acf7f7a9fb7e12fcefa 1 parent c1e0e77
Adam Spiers aspiers authored

Showing 1 changed file with 26 additions and 97 deletions. Show diff stats Hide diff stats

  1. +26 97 src/main.c
123 src/main.c
@@ -558,7 +558,7 @@ static int do_list(void)
558 558 return rv;
559 559 }
560 560
561   -static int do_grant(void)
  561 +static int do_command(cmd_request_t cmd)
562 562 {
563 563 char *buf;
564 564 struct boothc_header *h, reply;
@@ -576,7 +576,7 @@ static int do_grant(void)
576 576 h = (struct boothc_header *)buf;
577 577 if (cl.force)
578 578 force = BOOTHC_OPT_FORCE;
579   - init_header(h, BOOTHC_CMD_GRANT, force, 0,
  579 + init_header(h, cmd, force, 0,
580 580 sizeof(cl.site) + sizeof(cl.ticket));
581 581 strcpy(buf + sizeof(struct boothc_header), cl.site);
582 582 strcpy(buf + sizeof(struct boothc_header) + sizeof(cl.site), cl.ticket);
@@ -628,15 +628,28 @@ static int do_grant(void)
628 628 }
629 629
630 630 if (reply.result == BOOTHC_RLT_ASYNC) {
631   - log_info("grant command sent, result will be returned "
632   - "asynchronously, you can get the result from "
633   - "the booth log");
  631 + if (cmd == BOOTHC_CMD_GRANT)
  632 + log_info("grant command sent, result will be returned "
  633 + "asynchronously, you can get the result from "
  634 + "the log files");
  635 + else if (cmd == BOOTHC_CMD_REVOKE)
  636 + log_info("revoke command sent, result will be returned "
  637 + "asynchronously, you can get the result from "
  638 + "the log files after the ticket expiry time.");
  639 + else
  640 + log_error("internal error reading reply result!");
634 641 rv = 0;
635 642 } else if (reply.result == BOOTHC_RLT_SYNC_SUCC) {
636   - log_info("grant succeeded!");
  643 + if (cmd == BOOTHC_CMD_GRANT)
  644 + log_info("grant succeeded!");
  645 + else if (cmd == BOOTHC_CMD_REVOKE)
  646 + log_info("revoke succeeded!");
637 647 rv = 0;
638 648 } else if (reply.result == BOOTHC_RLT_SYNC_FAIL) {
639   - log_info("grant failed!");
  649 + if (cmd == BOOTHC_CMD_GRANT)
  650 + log_info("grant failed!");
  651 + else if (cmd == BOOTHC_CMD_REVOKE)
  652 + log_info("revoke failed!");
640 653 rv = 0;
641 654 } else {
642 655 log_error("internal error!");
@@ -651,98 +664,14 @@ static int do_grant(void)
651 664 return rv;
652 665 }
653 666
654   -static int do_revoke(void)
  667 +static int do_grant(void)
655 668 {
656   - char *buf;
657   - struct boothc_header *h, reply;
658   - int buflen;
659   - uint32_t force = 0;
660   - int fd, rv;
661   -
662   - buflen = sizeof(struct boothc_header) +
663   - sizeof(cl.site) + sizeof(cl.ticket);
664   - buf = malloc(buflen);
665   - if (!buf) {
666   - rv = -ENOMEM;
667   - goto out;
668   - }
669   - h = (struct boothc_header *)buf;
670   - if (cl.force)
671   - force = BOOTHC_OPT_FORCE;
672   - init_header(h, BOOTHC_CMD_REVOKE, force, 0,
673   - sizeof(cl.site) + sizeof(cl.ticket));
674   - strcpy(buf + sizeof(struct boothc_header), cl.site);
675   - strcpy(buf + sizeof(struct boothc_header) + sizeof(cl.site), cl.ticket);
676   -
677   -
678   - fd = do_connect(BOOTHC_SOCK_PATH);
679   - if (fd < 0) {
680   - rv = fd;
681   - goto out_free;
682   - }
683   -
684   - rv = do_write(fd, buf, buflen);
685   - if (rv < 0)
686   - goto out_close;
687   -
688   - rv = do_read(fd, &reply, sizeof(struct boothc_header));
689   - if (rv < 0)
690   - goto out_close;
691   -
692   - if (reply.result == BOOTHC_RLT_INVALID_ARG) {
693   - log_info("invalid argument!");
694   - rv = -1;
695   - goto out_close;
696   - }
697   -
698   - if (reply.result == BOOTHC_RLT_REMOTE_OP) {
699   - struct booth_node to;
700   - int s;
701   -
702   - memset(&to, 0, sizeof(struct booth_node));
703   - to.family = BOOTH_PROTO_FAMILY;
704   - strcpy(to.addr, cl.site);
705   -
706   - s = booth_transport[TCP].open(&to);
707   - if (s < 0)
708   - goto out_close;
709   -
710   - rv = booth_transport[TCP].send(s, buf, buflen);
711   - if (rv < 0) {
712   - booth_transport[TCP].close(s);
713   - goto out_close;
714   - }
715   - rv = booth_transport[TCP].recv(s, &reply,
716   - sizeof(struct boothc_header));
717   - if (rv < 0) {
718   - booth_transport[TCP].close(s);
719   - goto out_close;
720   - }
721   - booth_transport[TCP].close(s);
722   - }
723   -
724   - if (reply.result == BOOTHC_RLT_ASYNC) {
725   - log_info("revoke command sent, result will be returned "
726   - "asynchronously, you can get the result from "
727   - "booth log after the ticket expiry time.");
728   - rv = 0;
729   - } else if (reply.result == BOOTHC_RLT_SYNC_SUCC) {
730   - log_info("revoke succeeded!");
731   - rv = 0;
732   - } else if (reply.result == BOOTHC_RLT_SYNC_FAIL) {
733   - log_info("revoke failed!");
734   - rv = 0;
735   - } else {
736   - log_error("internal error!");
737   - rv = -1;
738   - }
  669 + return do_command(BOOTHC_CMD_GRANT);
  670 +}
739 671
740   -out_close:
741   - close(fd);
742   -out_free:
743   - free(buf);
744   -out:
745   - return rv;
  672 +static int do_revoke(void)
  673 +{
  674 + return do_command(BOOTHC_CMD_REVOKE);
746 675 }
747 676
748 677 static int lockfile(void)

0 comments on commit 70e5a71

Please sign in to comment.
Something went wrong with that request. Please try again.