Blame src/FileUtilUMASK.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 FileUtilMisc
Packit 9ff65e
Packit 9ff65e
exception UmaskError of string
Packit 9ff65e
Packit 9ff65e
type umask_error = [ `Exc of exn | `NoStickyBit of int ]
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let umask
Packit 9ff65e
      ?(error=(fun str _ -> raise (UmaskError str)))
Packit 9ff65e
      ?mode out =
Packit 9ff65e
  let handle_error, handle_exception =
Packit 9ff65e
    handle_error_gen "umask" error
Packit 9ff65e
      (function
Packit 9ff65e
       | `NoStickyBit i ->
Packit 9ff65e
           Printf.sprintf "Cannot set sticky bit in umask 0o%04o" i
Packit 9ff65e
       | #exc -> "")
Packit 9ff65e
  in
Packit 9ff65e
  let complement i = 0o0777 land (lnot i) in
Packit 9ff65e
  let try_umask i =
Packit 9ff65e
    try
Packit 9ff65e
      Unix.umask i
Packit 9ff65e
    with e ->
Packit 9ff65e
      handle_exception ~fatal:true e;
Packit 9ff65e
      raise e
Packit 9ff65e
  in
Packit 9ff65e
  let get () =
Packit 9ff65e
    let cmask = try_umask 0o777 in
Packit 9ff65e
    let _mask: int = try_umask cmask in
Packit 9ff65e
    cmask
Packit 9ff65e
  in
Packit 9ff65e
  let set i =
Packit 9ff65e
    let eff_i = i land 0o777 in
Packit 9ff65e
    let _i: int =
Packit 9ff65e
      if i <> eff_i then
Packit 9ff65e
        handle_error ~fatal:true (`NoStickyBit i);
Packit 9ff65e
      try_umask eff_i
Packit 9ff65e
    in
Packit 9ff65e
    eff_i
Packit 9ff65e
  in
Packit 9ff65e
  let v =
Packit 9ff65e
    match mode with
Packit 9ff65e
    | Some (`Symbolic s) ->
Packit 9ff65e
        let v = get () in
Packit 9ff65e
          set
Packit 9ff65e
            (complement
Packit 9ff65e
               (FileUtilMode.apply ~is_dir:false ~umask:0 (complement v) s))
Packit 9ff65e
    | Some (`Octal i) -> set i
Packit 9ff65e
    | None -> get ()
Packit 9ff65e
  in
Packit 9ff65e
  match out with
Packit 9ff65e
    | `Symbolic f -> f (FileUtilMode.of_int (0o0777 land (lnot v)))
Packit 9ff65e
    | `Octal f -> f v
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let umask_apply m = m land (lnot (umask (`Octal (fun i -> i))))