let rec add root path entries =
match path, entries with
| [], _ -> entries
| xpath :: xspath, (Dir(dpath, dname, dst, dattr, dentries) as d) :: entries ->
if xpath = dname then
Dir(dpath, dname, dst, dattr, lazy (add (root/xpath) xspath !*dentries)) :: entries
else d :: add root path entries
| [xpath], [] ->
[File(root, xpath, lazy (My_unix.stat (root/xpath)), ())]
| xpath :: xspath, [] ->
[Dir(root/(join xspath), xpath,
lazy (My_unix.stat (root/(join path))), (),
lazy (add (root/xpath) xspath []))]
| _, Nothing :: entries -> add root path entries
| _, Error _ :: _ -> entries
| [xpath], (File(_, fname, _, _) as f) :: entries' ->
if xpath = fname then entries
else f :: add root path entries'
| xpath :: xspath, (File(fpath, fname, fst, fattr) as f) :: entries' ->
if xpath = fname then
Dir(fpath, fname, fst, fattr, lazy (add (root/xpath) xspath [])) :: entries'
else f :: add root path entries'