|
Packit |
9ff65e |
(******************************************************************************)
|
|
Packit |
9ff65e |
(* ocaml-fileutils: files and filenames common operations *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* Copyright (C) 2003-2014, Sylvain Le Gall *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* This library is free software; you can redistribute it and/or modify it *)
|
|
Packit |
9ff65e |
(* under the terms of the GNU Lesser General Public License as published by *)
|
|
Packit |
9ff65e |
(* the Free Software Foundation; either version 2.1 of the License, or (at *)
|
|
Packit |
9ff65e |
(* your option) any later version, with the OCaml static compilation *)
|
|
Packit |
9ff65e |
(* exception. *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* This library is distributed in the hope that it will be useful, but *)
|
|
Packit |
9ff65e |
(* WITHOUT ANY WARRANTY; without even the implied warranty of *)
|
|
Packit |
9ff65e |
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file *)
|
|
Packit |
9ff65e |
(* COPYING for more details. *)
|
|
Packit |
9ff65e |
(* *)
|
|
Packit |
9ff65e |
(* You should have received a copy of the GNU Lesser General Public License *)
|
|
Packit |
9ff65e |
(* along with this library; if not, write to the Free Software Foundation, *)
|
|
Packit |
9ff65e |
(* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *)
|
|
Packit |
9ff65e |
(******************************************************************************)
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
open FileUtilTypes
|
|
Packit |
9ff65e |
open FilePath
|
|
Packit |
9ff65e |
open FileUtilMisc
|
|
Packit |
9ff65e |
open FileUtilTEST
|
|
Packit |
9ff65e |
open FileUtilLS
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
exception RmError of string
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
type rm_error =
|
|
Packit |
9ff65e |
[ `DirNotEmpty of filename
|
|
Packit |
9ff65e |
| `Exc of exn
|
|
Packit |
9ff65e |
| `NoRecurse of filename ]
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
|
|
Packit |
9ff65e |
let rm
|
|
Packit |
9ff65e |
?(error=fun str _ -> raise (RmError str))
|
|
Packit |
9ff65e |
?(force=Force)
|
|
Packit |
9ff65e |
?(recurse=false)
|
|
Packit |
9ff65e |
fln_lst =
|
|
Packit |
9ff65e |
let handle_error, handle_exception =
|
|
Packit |
9ff65e |
handle_error_gen "rm" error
|
|
Packit |
9ff65e |
(function
|
|
Packit |
9ff65e |
| `DirNotEmpty fn ->
|
|
Packit |
9ff65e |
Printf.sprintf "Directory %s not empty." fn
|
|
Packit |
9ff65e |
| `NoRecurse fn ->
|
|
Packit |
9ff65e |
Printf.sprintf
|
|
Packit |
9ff65e |
"Cannot delete directory %s when recurse is not set."
|
|
Packit |
9ff65e |
fn
|
|
Packit |
9ff65e |
| #exc -> "")
|
|
Packit |
9ff65e |
in
|
|
Packit |
9ff65e |
let test_dir = test (And(Is_dir, Not(Is_link))) in
|
|
Packit |
9ff65e |
let rmdir fn =
|
|
Packit |
9ff65e |
try
|
|
Packit |
9ff65e |
Unix.rmdir fn
|
|
Packit |
9ff65e |
with
|
|
Packit |
9ff65e |
| Unix.Unix_error(Unix.ENOTEMPTY, _, _) ->
|
|
Packit |
9ff65e |
handle_error ~fatal:true (`DirNotEmpty fn)
|
|
Packit |
9ff65e |
| e ->
|
|
Packit |
9ff65e |
handle_exception ~fatal:true e
|
|
Packit |
9ff65e |
in
|
|
Packit |
9ff65e |
let rec rm_aux lst =
|
|
Packit |
9ff65e |
List.iter
|
|
Packit |
9ff65e |
(fun fn ->
|
|
Packit |
9ff65e |
let exists =
|
|
Packit |
9ff65e |
try
|
|
Packit |
9ff65e |
let _st: Unix.LargeFile.stats = Unix.LargeFile.lstat fn in
|
|
Packit |
9ff65e |
true
|
|
Packit |
9ff65e |
with Unix.Unix_error(Unix.ENOENT, _, _) ->
|
|
Packit |
9ff65e |
false
|
|
Packit |
9ff65e |
in
|
|
Packit |
9ff65e |
if exists && (doit force fn) then begin
|
|
Packit |
9ff65e |
if test_dir fn then begin
|
|
Packit |
9ff65e |
if recurse then begin
|
|
Packit |
9ff65e |
rm_aux (ls fn);
|
|
Packit |
9ff65e |
rmdir fn
|
|
Packit |
9ff65e |
end else
|
|
Packit |
9ff65e |
handle_error ~fatal:true (`NoRecurse fn)
|
|
Packit |
9ff65e |
end else
|
|
Packit |
9ff65e |
Unix.unlink fn
|
|
Packit |
9ff65e |
end)
|
|
Packit |
9ff65e |
lst
|
|
Packit |
9ff65e |
in
|
|
Packit |
9ff65e |
rm_aux fln_lst
|