Blame src/FileUtilSTAT.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 FileUtilPermission
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
let stat ?(dereference=false) fln =
Packit 9ff65e
  let kind_of_stat ustat =
Packit 9ff65e
    match ustat.Unix.LargeFile.st_kind with
Packit 9ff65e
      | Unix.S_REG -> File
Packit 9ff65e
      | Unix.S_DIR -> Dir
Packit 9ff65e
      | Unix.S_CHR -> Dev_char
Packit 9ff65e
      | Unix.S_BLK -> Dev_block
Packit 9ff65e
      | Unix.S_FIFO -> Fifo
Packit 9ff65e
      | Unix.S_SOCK -> Socket
Packit 9ff65e
      | Unix.S_LNK -> Symlink
Packit 9ff65e
  in
Packit 9ff65e
  try
Packit 9ff65e
    let ustat = Unix.LargeFile.lstat fln in
Packit 9ff65e
    let is_link = (kind_of_stat ustat = Symlink) in
Packit 9ff65e
    let ustat =
Packit 9ff65e
      if is_link && dereference then
Packit 9ff65e
        Unix.LargeFile.stat fln
Packit 9ff65e
      else
Packit 9ff65e
        ustat
Packit 9ff65e
    in
Packit 9ff65e
    {
Packit 9ff65e
      kind              = kind_of_stat ustat;
Packit 9ff65e
      is_link           = is_link;
Packit 9ff65e
      permission        = permission_of_int ustat.Unix.LargeFile.st_perm;
Packit 9ff65e
      size              = B ustat.Unix.LargeFile.st_size;
Packit 9ff65e
      owner             = ustat.Unix.LargeFile.st_uid;
Packit 9ff65e
      group_owner       = ustat.Unix.LargeFile.st_gid;
Packit 9ff65e
      access_time       = ustat.Unix.LargeFile.st_atime;
Packit 9ff65e
      modification_time = ustat.Unix.LargeFile.st_mtime;
Packit 9ff65e
      creation_time     = ustat.Unix.LargeFile.st_ctime;
Packit 9ff65e
      device            = ustat.Unix.LargeFile.st_dev;
Packit 9ff65e
      inode             = ustat.Unix.LargeFile.st_ino;
Packit 9ff65e
    }
Packit 9ff65e
  with Unix.Unix_error(Unix.ENOENT, _, _) ->
Packit 9ff65e
    raise (FileDoesntExist fln)
Packit 9ff65e