Blame src/FileUtilCHMOD.ml

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 FileUtilMisc
Packit 9ff65e
open FileUtilPermission
Packit 9ff65e
open FileUtilSTAT
Packit 9ff65e
open FileUtilLS
Packit 9ff65e
open FileUtilUMASK
Packit 9ff65e
Packit 9ff65e
exception ChmodError of string
Packit 9ff65e
Packit 9ff65e
type chmod_error = [`Exc of exn]
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let chmod
Packit 9ff65e
      ?(error=fun str _ -> raise (ChmodError str))
Packit 9ff65e
      ?(recurse=false)
Packit 9ff65e
      mode lst =
Packit 9ff65e
  let _, handle_exception =
Packit 9ff65e
    handle_error_gen "chmod" error (function #exc -> "")
Packit 9ff65e
  in
Packit 9ff65e
  let rec chmod_one fn =
Packit 9ff65e
    let st = stat fn in
Packit 9ff65e
      if st.kind = Dir && recurse then begin
Packit 9ff65e
        List.iter chmod_one (ls fn)
Packit 9ff65e
      end;
Packit 9ff65e
      if not st.is_link then begin
Packit 9ff65e
        let int_perm =
Packit 9ff65e
          match mode with
Packit 9ff65e
            | `Octal i -> i
Packit 9ff65e
            | `Symbolic t ->
Packit 9ff65e
                FileUtilMode.apply
Packit 9ff65e
                  ~is_dir:(st.kind = Dir)
Packit 9ff65e
                  ~umask:(umask (`Octal (fun i -> i)))
Packit 9ff65e
                  (int_of_permission st.permission) t
Packit 9ff65e
        in
Packit 9ff65e
          if int_perm <> int_of_permission st.permission then
Packit 9ff65e
            try
Packit 9ff65e
              Unix.chmod fn int_perm
Packit 9ff65e
            with e ->
Packit 9ff65e
              handle_exception ~fatal:true e
Packit 9ff65e
      end
Packit 9ff65e
  in
Packit 9ff65e
    List.iter chmod_one lst