Where container path `path` of `stage` came from: a repository path that is * what `want` asks for, or a go build of the stage; false: nothing known. */
| 826 | /* Where container path `path` of `stage` came from: a repository path that is |
| 827 | * what `want` asks for, or a go build of the stage; false: nothing known. */ |
| 828 | static bool dk_origin(dk_t *d, int stage, const char *path, int want, int depth, dk_found_t *out) { |
| 829 | if (depth > DK_MAX_DEPTH || stage < 0 || stage >= DK_MAX_STAGES || !path) { |
| 830 | return false; |
| 831 | } |
| 832 | for (const dk_build_t *b = d->st[stage].builds; b; b = b->next) { |
| 833 | const char *o = b->out ? dk_absolute(d, b->out, b->workdir) : NULL; |
| 834 | if (o && strcmp(o, path) == 0) { |
| 835 | out->build = b; |
| 836 | out->build_stage = stage; |
| 837 | return true; |
| 838 | } |
| 839 | } |
| 840 | for (const dk_copy_t *c = d->st[stage].copies; c; c = c->next) { |
| 841 | bool known = c->dst != NULL; |
| 842 | for (int s = 0; known && s < c->nsrc; s++) { |
| 843 | known = c->srcs[s] != NULL; |
| 844 | } |
| 845 | if (!known) { |
| 846 | continue; |
| 847 | } |
| 848 | const char *dst = dk_absolute(d, c->dst, c->workdir); |
| 849 | int src_stage = c->has_from ? dk_stage_of(d, c->from) : -1; |
| 850 | if (!dst || (c->has_from && src_stage < 0)) { |
| 851 | continue; /* out of an image, or an unknown --from */ |
| 852 | } |
| 853 | size_t dl = strlen(dst); |
| 854 | bool into_dir = c->nsrc > SKIP_ONE || dk_ends(c->dst, "/"); |
| 855 | const char *tail = NULL; |
| 856 | if (strcmp(dst, "/") == 0) { |
| 857 | tail = strcmp(path, "/") != 0 ? path + SKIP_ONE : NULL; |
| 858 | } else if (strncmp(path, dst, dl) == 0 && path[dl] == '/') { |
| 859 | tail = path + dl + SKIP_ONE; |
| 860 | } |
| 861 | for (int s = 0; s < c->nsrc; s++) { |
| 862 | /* the source itself, a path inside a directory source, or a file |
| 863 | * source copied into the directory dst */ |
| 864 | const char *rels[PAIR_LEN + SKIP_ONE]; |
| 865 | int nc = 0; |
| 866 | if (strcmp(path, dst) == 0 && !into_dir) { |
| 867 | rels[nc++] = ""; |
| 868 | } |
| 869 | if (tail) { |
| 870 | rels[nc++] = tail; |
| 871 | const char *sn = dk_norm(d, c->srcs[s]); |
| 872 | if (sn && strcmp(dk_base(sn), tail) == 0) { |
| 873 | rels[nc++] = ""; |
| 874 | } |
| 875 | } |
| 876 | for (int k = 0; k < nc; k++) { |
| 877 | if (src_stage >= 0) { |
| 878 | const char *from = rels[k][0] ? dk_join(d, c->srcs[s], rels[k]) : c->srcs[s]; |
| 879 | if (dk_origin(d, src_stage, dk_absolute(d, from, "/"), want, depth + SKIP_ONE, |
| 880 | out)) { |
| 881 | return true; |
| 882 | } |
| 883 | continue; |
| 884 | } |
| 885 | const char *hit = dk_repo(d, c->srcs[s], rels[k], want); |
no test coverage detected