Blame src/FileUtilTOUCH.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 FileUtilSTAT
Packit 9ff65e
open FileUtilTEST
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let touch ?atime ?mtime ?(create=true) ?(time=Touch_now) fln =
Packit 9ff65e
Packit 9ff65e
  let atime, mtime =
Packit 9ff65e
    match atime, mtime with
Packit 9ff65e
    | None, None -> true, true
Packit 9ff65e
    | Some b, None -> b, false
Packit 9ff65e
    | None, Some b -> false, b
Packit 9ff65e
    | Some b1, Some b2 -> b1, b2
Packit 9ff65e
  in
Packit 9ff65e
Packit 9ff65e
  let set_time () =
Packit 9ff65e
    let fatime, fmtime =
Packit 9ff65e
      match time with
Packit 9ff65e
      | Touch_now -> 0.0, 0.0
Packit 9ff65e
      | Touch_timestamp time_ref -> time_ref, time_ref
Packit 9ff65e
      | Touch_file_time fln_ref ->
Packit 9ff65e
        let st = stat fln_ref in
Packit 9ff65e
        st.access_time, st.modification_time
Packit 9ff65e
    in
Packit 9ff65e
    let fatime, fmtime =
Packit 9ff65e
      if not (atime && mtime) then begin
Packit 9ff65e
        let st = stat fln in
Packit 9ff65e
        (if atime then fatime else st.access_time),
Packit 9ff65e
        (if mtime then fmtime else st.modification_time)
Packit 9ff65e
      end else begin
Packit 9ff65e
        fatime, fmtime
Packit 9ff65e
      end
Packit 9ff65e
    in
Packit 9ff65e
    Unix.utimes fln fatime fmtime
Packit 9ff65e
  in
Packit 9ff65e
    (* Create file if required *)
Packit 9ff65e
    if test_exists fln then begin
Packit 9ff65e
      set_time ()
Packit 9ff65e
    end else if create then begin
Packit 9ff65e
      close_out (open_out fln);
Packit 9ff65e
      set_time ()
Packit 9ff65e
    end