summaryrefslogtreecommitdiffstats
path: root/src/edit.c
diff options
context:
space:
mode:
authordev <dev@example>2026-11-14 01:05:08 +0000
committerdev <dev@example>2026-11-14 01:05:08 +0000
commit312ddd4936f09ae27a180ca495cacf2c06faf33e (patch)
tree2fa46ab568ad55f7e8fb844c3447b1d296b2e340 /src/edit.c
parente737f3d560de2b8172e6dd83901bc1359283303c (diff)
downloadaccess-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.tar.gz
access-control-312ddd4936f09ae27a180ca495cacf2c06faf33e.zip
Comments: cut to what the code cannot say
The comments had grown into essays. A file header explaining why the file exists, then a paragraph above each function restating what the next eight lines do, then a paragraph above that one explaining why that matters. The reader gets no further than the first paragraph, and the second is the one that goes stale: it describes code that has since changed, and nothing fails when it does. Cut by about half. What is left is the three things a comment can do that code cannot: say why the constraint exists (no regex because a hook must not depend on what might be missing at 3am), say what is not obvious from the shape (a bare ref name is a branch, never a tag; the seventh field of /etc/passwd is the shell), and say where it hurts (the NULL in an execvp array, the mode and owner that have to survive an atomic rewrite, the child that must not keep the hook's stdin). Block comments became // lines, which is also what the other C trees here use, and the file headers went to two or three lines. Section banners stayed: they are navigation, not prose. Behaviour is unchanged and the suites prove it: 171 direct checks, 162 behavioural, clean under -Werror and under ASan/UBSan.
Diffstat (limited to 'src/edit.c')
-rw-r--r--src/edit.c100
1 files changed, 44 insertions, 56 deletions
diff --git a/src/edit.c b/src/edit.c
index 9b3c233..5579b50 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -1,21 +1,12 @@
-/* edit.c - change the policy file, without losing the file.
- *
- * The deployed policy is a read-only mount, usually a ConfigMap, and that is
- * not a bug to work around: it is what makes the policy reviewable and the
- * rollout visible. So these commands are for the source copy - a checkout, a
- * CI job, a notebook - and they say so plainly when the file they were given
- * cannot be written, rather than pretending to have granted something.
- *
- * What they preserve is the thing a generator cannot: the comments. A policy
- * file is also the record of why an access exists, so lines are replaced in
- * place and everything else is left exactly as it was. When a line has to be
- * written, it is written whole; the file is never truncated and rebuilt in
- * place, because a policy that disappears half way through a write is a policy
- * that denies everything (or, if the order were different, nothing).
- *
- * A file that does not parse is refused: editing a broken policy would hide the
- * breakage.
- */
+// The deployed policy is a read-only mount, on purpose: that is what makes it
+// reviewable and the rollout visible. These are for the source copy, and say so
+// when the file cannot be written rather than pretending to have granted
+// something.
+//
+// Comments survive, because the file is also the record of why an access exists.
+// The rewrite is a whole file to a temporary and a rename: a policy truncated
+// half way through a write is a policy that denies everything.
+
#include "authz.h"
#include <ctype.h>
@@ -43,7 +34,7 @@ static void lines_init(lines_t *l)
l->cap = 0;
}
-static void lines_push(lines_t *l, char *s) /* takes ownership */
+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;
@@ -62,7 +53,7 @@ static void lines_free(lines_t *l)
lines_init(l);
}
-/* Every line, newline included, exactly as it was. */
+// Every line, newline included, exactly as it was.
static int read_lines(const char *path, lines_t *l)
{
FILE *f;
@@ -83,8 +74,7 @@ static int read_lines(const char *path, lines_t *l)
lines_push(l, xstrdup(line));
continue;
}
- { /* a last line without its newline:
- * keep the file well formed */
+ { // a last line without its newline
char *own = xmalloc(n + 2);
memcpy(own, line, n);
@@ -98,9 +88,10 @@ static int read_lines(const char *path, lines_t *l)
return 0;
}
-/* Write to a temporary file beside the target, then rename. The reader either
- * sees the old file or the new one; and the mode and owner survive, because a
- * policy that becomes 0600 root-owned stops working for the git account. */
+// 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.
+
static int write_atomic(const lines_t *l, const char *path)
{
char *dirtext = xstrdup(path);
@@ -112,8 +103,8 @@ static int write_atomic(const lines_t *l, const char *path)
size_t i;
int had = stat(path, &st) == 0;
- /* dirname may return a pointer into its argument or a fresh one, and may
- * fail; the copy is ours either way and dirtext is freed at the end. */
+ // dirname may return a pointer into its argument or a fresh one.
+
dir = dirname(dirtext);
if (dir == NULL)
dir = (char *)".";
@@ -175,13 +166,11 @@ static int write_atomic(const lines_t *l, const char *path)
return 0;
}
-/* ------------------------------------------------------------------ matching */
+// ------------------------------------------------------------------ matching
+
+// Copied before splitting: splitting writes over the text, and these lines are
+// the file.
-/* Does this line grant to exactly this user and repository?
- *
- * The line is copied first: splitting writes over the text, and these lines are
- * the file - a matcher that flattened the comments out of the ones it did not
- * change would be a vandal. */
static int is_rule_for(const char *line, const char *user, const char *repo)
{
char *copy = xstrdup(line);
@@ -223,12 +212,11 @@ static char *rule_text(const char *user, const char *repo, const char *level,
return b.s;
}
-/* Fail before touching anything if the file cannot be parsed. */
-/* Load a policy for editing. A policy that does not parse is refused: editing
- * a file we cannot read would rewrite the parts we can read and keep the
- * damage. A policy that is not there yet is a bootstrap, not an error - the
- * source copy has to start somewhere - so the file begins with the grammar
- * written down, which is also the least surprising thing to find in it. */
+// A file that does not parse is refused: rewriting the parts that parse would
+// keep the damage. A file that is not there yet is bootstrapped with the
+// grammar
+// written at the top.
+
static int refuse_broken(const char *path, int *created)
{
policy_t probe;
@@ -248,10 +236,11 @@ static int refuse_broken(const char *path, int *created)
return 0;
}
-/* Pull the shared options out of the argument list. They are allowed anywhere
- * rather than only at the front, because "grant bob team write --out
- * /tmp/new.conf" is what people type - and treating "--out" as a ref pattern
- * would grant something nobody asked for. */
+// --out and --stdout are allowed anywhere, because "grant bob team write --out
+// /tmp/new.conf" is what people type; parsing them only at the front made "--
+// out"
+// a ref pattern.
+
int authz_parse_out(int argc, char **argv, edit_opts *o, int *consumed)
{
int i;
@@ -294,8 +283,8 @@ int authz_reject_options(int argc, char **argv)
return 0;
}
-/* Write the result and release the lines: every command ends here, so this is
- * where the file it read stops being held. */
+// Every command ends here, so this is where the lines are released.
+
static int finish(lines_t *l, const char *path, edit_opts o, const char *what)
{
int rc;
@@ -317,7 +306,7 @@ static int finish(lines_t *l, const char *path, edit_opts o, const char *what)
return rc;
}
-/* ------------------------------------------------------------------ commands */
+// ------------------------------------------------------------------ commands
int cmd_grant(const char *path, edit_opts o, int argc, char **argv)
{
@@ -361,9 +350,9 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv)
if (refuse_broken(path, &created) != 0)
return 3;
if (created) {
- /* Bootstrapped lines are xstrdup'd for the same reason as the rest:
- * lines_free frees every entry it holds, and a string literal is not
- * something free() may be handed. */
+ // xstrdup'd: lines_free frees every entry it holds, and a string
+ // literal is not something free() may be handed.
+
lines_init(&lines);
lines_push(&lines, xstrdup("# git-authz policy\n"));
lines_push(&lines, xstrdup("# <user> <repo> <write|force|admin> "
@@ -374,25 +363,24 @@ int cmd_grant(const char *path, edit_opts o, int argc, char **argv)
} else if (read_lines(path, &lines) != 0)
return 3;
- /* More than one rule for the same pair is legal but pointless - the
- * strongest wins - so the pair is collapsed into one line, in the place of
- * the first. Everything else, comments included, is byte for byte what it
- * was. */
+ // Several rules for one pair are legal and pointless - the strongest wins -
+ // so they collapse into one line, in the place of the first.
+
text = rule_text(argv[0], argv[1], argv[2], argc - 3, argv + 3);
for (i = 0; i < (int)lines.n; i++) {
if (!is_rule_for(lines.v[i], argv[0], argv[1]))
continue;
if (first < 0) {
- first = i; /* the line the new rule takes over */
+ first = i; // the line the new rule takes over
continue;
}
- free(lines.v[i]); /* a later rule for the same pair */
+ free(lines.v[i]); // a later rule for the same pair
lines.v[i] = NULL;
dropped++;
}
if (first < 0) {
- lines_push(&lines, text); /* the buffer belongs to the file now */
+ lines_push(&lines, text); // the buffer belongs to the file now
replaced = (int)lines.n;
} else {
size_t w;