dbuenzli / bos

Basic OS interaction for OCaml
http://erratique.ch/software/bos
ISC License
64 stars 16 forks source link

Bos.OS.Path.fold does not recurse from root (/ or C:\) #61

Closed hcarty closed 8 years ago

hcarty commented 8 years ago

This code:

Bos.OS.Path.fold ~elements:`Files ~traverse:`Any (fun _ _ -> raise Exit) () [Fpath.v "/"]

returns Ok () without walking the filesystem. If I use a valid path other than / (or c:\ under Windows) then fold will recurse through directories as expected.

hcarty commented 8 years ago

I think this is related (example from utop, but confirmed in compiled code):

# OS.Path.fold ~dotfiles:true ~elements:`Files ~traverse:`Any (fun _ _ -> ()) () [Fpath.v "/"];;
Exception: Invalid_argument "\"./\": invalid segment".

I think the issue is coming from https://github.com/dbuenzli/bos/blob/master/src/bos_os_path.ml#L446, specifically the call to Fpath.base.

# Fpath.base @@ Fpath.v "/"
- : Fpath.t = ./

If I'm reading the OS.Path.fold code correctly (and it's been a long weekend so I may not be...), the ./ result from Fpath.base causes fold to skip everything under / if dotfiles is false. If dotfiles is true the code ends up interpreting ./ as an invalid path segment as seen in the Invalid_argument result above.

dbuenzli commented 8 years ago

Thanks for the analysis. It seems the line you mention is broken beyond that, it should be using split_base. The current implementation trips over filepaths whose last segment is ... I'm looking into it.

hcarty commented 8 years ago

Thanks!

dbuenzli commented 8 years ago

42a11f6 should solve most of the problems. There's still one quirk though see #64.