1616 * document wrote the path.
1717 *
1818 * Unresolved:
19- * missing the path is not in the graph, but its directory is: the
20- * document names something that is not there (doc rot)
19+ * missing the path is not in the graph, but its directory is, or a
20+ * directory above it that the reference wrote is: the document
21+ * names something that is not there (doc rot; a moved or
22+ * renamed directory)
2123 * ambiguous a file name alone that is not next to the document but exists
2224 * elsewhere in the repository: the document does not say which
2325 * Not a reference to this repository at all (LOCAL: no edge, no row): a link
2426 * back into the same document; a path that is neither indexed nor of a type
2527 * the indexer reads (an image, an archive, an extensionless page); a path
26- * whose directory is not in the repository either (a file a tutorial asks
27- * the reader to create, a system path, a path above the root); and, in a
28- * code span, a rooted single segment without a file extension, which is a
28+ * none of whose directories is in the repository either (a file a tutorial
29+ * asks the reader to create, a system path, a path above the root); and, in
30+ * a code span, a rooted single segment without a file extension, which is a
2931 * URL route (`/docs`, `/health`), not the directory of that name.
3032 */
3133#include "pipeline/doc_links.h"
@@ -408,6 +410,25 @@ static const cbm_gbuf_node_t *mdr_folder(const mdr_index_t *x, const cbm_gbuf_t
408410 return path [0 ] ? (const cbm_gbuf_node_t * )cbm_ht_get (x -> folders , path ) : NULL ;
409411}
410412
413+ /* Whether a directory above `path` that the reference itself wrote, one
414+ * longer than the `base_len` bytes it was read from, is a Folder. */
415+ static bool mdr_written_ancestor (const mdr_index_t * x , const cbm_gbuf_t * graph , const char * path ,
416+ size_t base_len ) {
417+ char dir [MDR_PATH_CAP ];
418+ size_t n = strlen (path );
419+ if (n >= sizeof (dir )) {
420+ return false;
421+ }
422+ memcpy (dir , path , n + SKIP_ONE );
423+ for (char * s = strrchr (dir , '/' ); s && (size_t )(s - dir ) > base_len ; s = strrchr (dir , '/' )) {
424+ * s = '\0' ;
425+ if (mdr_folder (x , graph , dir )) {
426+ return true;
427+ }
428+ }
429+ return false;
430+ }
431+
411432static bool mdr_code_label (const char * label ) {
412433 for (size_t i = 0 ; label && MDR_CODE_LABELS [i ]; i ++ ) {
413434 if (strcmp (label , MDR_CODE_LABELS [i ]) == 0 ) {
@@ -1126,8 +1147,19 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11261147 char tries [MDR_TRIES ][MDR_PATH_CAP ];
11271148 int nt = 0 ;
11281149 int attempted = 0 ;
1150+ size_t base_len [MDR_TRIES ]; /* the directory each try reads from, before what p wrote */
11291151 if (!rooted ) {
11301152 attempted ++ ;
1153+ char lead [MDR_PATH_CAP ]; /* p's leading `./` and `../` segments */
1154+ char base [MDR_PATH_CAP ];
1155+ size_t ll = 0 ;
1156+ while (ll < sizeof (lead ) - PAIR_LEN - 1 && (strncmp (p + ll , "./" , PAIR_LEN ) == 0 ||
1157+ strncmp (p + ll , "../" , PAIR_LEN + 1 ) == 0 )) {
1158+ ll += p [ll + 1 ] == '/' ? PAIR_LEN : PAIR_LEN + 1 ;
1159+ }
1160+ memcpy (lead , p , ll );
1161+ lead [ll ] = '\0' ;
1162+ base_len [nt ] = mdr_join (doc_dir , lead , base , sizeof (base )) ? strlen (base ) : 0 ;
11311163 nt += mdr_join (doc_dir , p , tries [nt ], MDR_PATH_CAP );
11321164 }
11331165 if (!relative && !r -> name_only ) {
@@ -1137,7 +1169,7 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11371169 q ++ ;
11381170 }
11391171 if (mdr_join ("" , q , tries [nt ], MDR_PATH_CAP ) && (nt == 0 || strcmp (tries [0 ], tries [nt ]))) {
1140- nt ++ ;
1172+ base_len [ nt ++ ] = 0 ;
11411173 }
11421174 }
11431175 for (int i = 0 ; i < nt ; i ++ ) {
@@ -1174,7 +1206,8 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11741206 return ;
11751207 }
11761208 /* doc rot when the reference points into the repository: its directory is
1177- * there (a bare name in a link: the document's own directory) */
1209+ * there (a bare name in a link: the document's own directory), or a
1210+ * directory above it that the reference wrote is (one moved or renamed) */
11781211 for (int i = 0 ; i < nt ; i ++ ) {
11791212 char parent [MDR_PATH_CAP ];
11801213 const char * ps = strrchr (tries [i ], '/' );
@@ -1185,6 +1218,10 @@ static void mdr_resolve_path(const mdr_index_t *x, const cbm_gbuf_t *graph, cons
11851218 out -> reason = CBM_DOCLINK_REASON_MISSING ;
11861219 return ;
11871220 }
1221+ if (pl > 0 && strchr (p , '/' ) && mdr_written_ancestor (x , graph , tries [i ], base_len [i ])) {
1222+ out -> reason = CBM_DOCLINK_REASON_MISSING ;
1223+ return ;
1224+ }
11881225 if (pl == 0 && r -> link && !rooted ) {
11891226 out -> reason = CBM_DOCLINK_REASON_MISSING ;
11901227 return ;
0 commit comments