MCPcopy Create free account
hub / github.com/DeusData/codebase-memory-mcp / dk_norm

Function dk_norm

src/pipeline/pass_docker.c:699–754  ·  view source on GitHub ↗

`p` with "." and empty segments dropped and ".." applied; an absolute path * stays absolute ("/" at most), a relative one climbing out keeps "..". */

Source from the content-addressed store, hash-verified

697/* `p` with "." and empty segments dropped and ".." applied; an absolute path
698 * stays absolute ("/" at most), a relative one climbing out keeps "..". */
699static const char *dk_norm(dk_t *d, const char *p) {
700 if (!p) {
701 return NULL;
702 }
703 size_t cap = strlen(p) + PAIR_LEN;
704 char *out = cbm_arena_alloc(d->a, cap);
705 if (!out) {
706 return NULL;
707 }
708 bool abs = p[0] == '/';
709 size_t len = 0;
710 if (abs) {
711 out[len++] = '/';
712 }
713 size_t base = len;
714 for (const char *s = p; *s;) {
715 while (*s == '/') {
716 s++;
717 }
718 const char *e = s;
719 while (*e && *e != '/') {
720 e++;
721 }
722 size_t sl = (size_t)(e - s);
723 if (sl == 0) {
724 break;
725 }
726 bool dot = sl == SKIP_ONE && s[0] == '.';
727 bool up = sl == PAIR_LEN && s[0] == '.' && s[SKIP_ONE] == '.';
728 if (up) {
729 size_t k = len;
730 while (k > base && out[k - SKIP_ONE] != '/') {
731 k--;
732 }
733 bool last_up = len - k == PAIR_LEN && out[k] == '.' && out[k + SKIP_ONE] == '.';
734 if (len > base && !last_up) {
735 len = k > base ? k - SKIP_ONE : base;
736 } else if (!abs) {
737 if (len > base) {
738 out[len++] = '/';
739 }
740 out[len++] = '.';
741 out[len++] = '.';
742 }
743 } else if (!dot) {
744 if (len > base) {
745 out[len++] = '/';
746 }
747 memcpy(out + len, s, sl);
748 len += sl;
749 }
750 s = e;
751 }
752 out[len] = '\0';
753 return out;
754}
755
756/* Container path `p` made absolute against `workdir`, normalised. */

Callers 4

dk_absoluteFunction · 0.85
dk_repoFunction · 0.85
dk_originFunction · 0.85
dk_in_ctxFunction · 0.85

Calls 1

cbm_arena_allocFunction · 0.50

Tested by

no test coverage detected