diff options
Diffstat (limited to 'src/edit.c')
| -rw-r--r-- | src/edit.c | 74 |
1 files changed, 5 insertions, 69 deletions
@@ -21,73 +21,6 @@ #define HIDDEN_PREFIX "/.authz" #define TEMPLATE_SUFFIX ".XXXXXX" -typedef struct { - char **v; - size_t n; - size_t cap; -} lines_t; - -static void lines_init(lines_t *l) -{ - l->v = NULL; - l->n = 0; - l->cap = 0; -} - -static void lines_push(lines_t *l, char *s) // takes ownership -{ - if (l->n == l->cap) { - l->cap = l->cap != 0 ? l->cap * 2 : 64; - l->v = xrealloc(l->v, l->cap * sizeof(*l->v)); - } - l->v[l->n++] = s; -} - -static void lines_free(lines_t *l) -{ - size_t i; - - for (i = 0; i < l->n; i++) - free(l->v[i]); - free(l->v); - lines_init(l); -} - -// Every line, newline included, exactly as it was. -static int read_lines(const char *path, lines_t *l) -{ - FILE *f; - char *line = NULL; - size_t cap = 0; - ssize_t len; - - lines_init(l); - f = fopen(path, "r"); - if (f == NULL) { - fprintf(stderr, "%s: cannot read %s: %s\n", progname, path, strerror(errno)); - return -1; - } - while ((len = getline(&line, &cap, f)) != -1) { - size_t n = strlen(line); - - if (n == 0 || line[n - 1] == '\n') { - lines_push(l, xstrdup(line)); - continue; - } - { // a last line without its newline - char *own = xmalloc(n + 2); - - memcpy(own, line, n); - own[n] = '\n'; - own[n + 1] = '\0'; - lines_push(l, own); - } - } - free(line); - fclose(f); - return 0; -} - // A temporary beside the target, then rename: a reader sees the old file or the // new one. Mode and owner are carried over, because a policy that becomes 0600 // root-owned stops working for the git account. @@ -385,8 +318,11 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv) "admin > force > write\n")); lines_push(&lines, xstrdup("# Name the refs a rule covers: with no " "patterns it covers the whole repository.\n")); - } else if (read_lines(path, &lines) != 0) + } else if (authz_read_lines(path, &lines) != 0) { + fprintf(stderr, "%s: cannot read %s: %s\n", progname, path, + strerror(errno)); return 3; + } // Several rules for one pair are legal and pointless - the strongest wins - // so they collapse into one line, in the place of the first. @@ -455,7 +391,7 @@ int cmd_revoke(const char *path, edit_opts o, int argc, char **argv) return 64; if (refuse_broken(path, NULL) != 0) return 3; - if (read_lines(path, &lines) != 0) { + if (authz_read_lines(path, &lines) != 0) { fprintf(stderr, "%s: no rule for %s on %s to remove\n", progname, argv[0], argv[1]); return 1; |
