Fix rmrf to properly handle symlinks

This commit is contained in:
Bryan Bennett 2023-12-21 14:03:31 -05:00
parent bddef99967
commit b7413a17f1
No known key found for this signature in database
GPG key ID: EE149E4215408DE9

View file

@ -128,21 +128,20 @@ let get_watches = () => {
} }
}; };
let rec rmrf = (path) => switch (Sys_unix.is_directory(~follow_symlinks=false, path)) { // TODO: Maybe make this more terse?
| `Yes => { let rec rmrf = (path) => {
Sys_unix.readdir(path) |> Array.iter(~f=name => { print_endline(Filename.concat(path, name)); rmrf(Filename.concat(path, name))}); switch (Unix.lstat(path).st_kind) {
| S_REG => Unix.unlink(path)
| S_LNK => Unix.unlink(path)
| S_DIR => {
Sys_unix.readdir(path) |> Array.iter(~f=name => rmrf(Filename.concat(path, name)));
Unix.rmdir(path) Unix.rmdir(path)
} }
| `No => { | S_CHR => Printf.eprintf("Don't know how to handle Character Device file\n")
switch (Sys_unix.is_file(~follow_symlinks=false, path)) { | S_BLK => Printf.eprintf("Don't know how to handle Block Device file\n")
| `Yes => Sys_unix.remove(path) | S_FIFO => Printf.eprintf("Don't know how to handle FIFO file\n")
| _ => () | S_SOCK => Printf.eprintf("Don't know how to handle Socket file\n")
} }
}
| `Unknown => {
Printf.eprintf("Cannot determine what file type of path %s", path);
exit(1);
}
}; };
let clean_old_gcroots = (layout_dir) => { let clean_old_gcroots = (layout_dir) => {