src/Pure/General/file.ML
author wenzelm
Thu Apr 03 18:42:36 2008 +0200 (2008-04-03)
changeset 26538 d65504ffb47d
parent 26503 4dec4460244f
child 26656 62fff5feb756
permissions -rw-r--r--
replaced ETX/EOT by ENQ/ACK, which are less likely to be interpreted by tty etc.;
wenzelm@6118
     1
(*  Title:      Pure/General/file.ML
wenzelm@5009
     2
    ID:         $Id$
wenzelm@5009
     3
    Author:     Markus Wenzel, TU Muenchen
wenzelm@5009
     4
wenzelm@5009
     5
File system operations.
wenzelm@5009
     6
*)
wenzelm@5009
     7
wenzelm@5009
     8
signature FILE =
wenzelm@5009
     9
sig
wenzelm@16261
    10
  val platform_path: Path.T -> string
wenzelm@16261
    11
  val shell_path: Path.T -> string
wenzelm@6224
    12
  val cd: Path.T -> unit
wenzelm@6224
    13
  val pwd: unit -> Path.T
wenzelm@6224
    14
  val full_path: Path.T -> Path.T
wenzelm@6182
    15
  val tmp_path: Path.T -> Path.T
wenzelm@16261
    16
  val isatool: string -> int
wenzelm@16261
    17
  val system_command: string -> unit
wenzelm@23861
    18
  eqtype ident
wenzelm@23874
    19
  val rep_ident: ident -> string
wenzelm@23861
    20
  val ident: Path.T -> ident option
wenzelm@17826
    21
  val exists: Path.T -> bool
wenzelm@21858
    22
  val check: Path.T -> unit
wenzelm@17826
    23
  val rm: Path.T -> unit
wenzelm@17826
    24
  val mkdir: Path.T -> unit
wenzelm@26503
    25
  val open_input: (TextIO.instream -> 'a) -> Path.T -> 'a
wenzelm@26503
    26
  val open_output: (TextIO.outstream -> 'a) -> Path.T -> 'a
wenzelm@26503
    27
  val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a
wenzelm@6182
    28
  val read: Path.T -> string
wenzelm@6182
    29
  val write: Path.T -> string -> unit
wenzelm@6182
    30
  val append: Path.T -> string -> unit
wenzelm@16713
    31
  val write_list: Path.T -> string list -> unit
wenzelm@16713
    32
  val append_list: Path.T -> string list -> unit
wenzelm@16603
    33
  val eq: Path.T * Path.T -> bool
wenzelm@6182
    34
  val copy: Path.T -> Path.T -> unit
wenzelm@16261
    35
  val copy_dir: Path.T -> Path.T -> unit
wenzelm@5009
    36
end;
wenzelm@5009
    37
wenzelm@5009
    38
structure File: FILE =
wenzelm@5009
    39
struct
wenzelm@5009
    40
wenzelm@26503
    41
(* system path representations *)
wenzelm@6224
    42
wenzelm@26503
    43
val platform_path = Path.implode o Path.expand;
wenzelm@26503
    44
val shell_path = enclose "'" "'" o Path.implode o Path.expand;
wenzelm@6224
    45
wenzelm@6224
    46
wenzelm@26503
    47
(* current working directory *)
wenzelm@6224
    48
wenzelm@23826
    49
val cd = cd o platform_path;
wenzelm@26503
    50
val pwd = Path.explode o pwd;
wenzelm@6224
    51
wenzelm@26503
    52
(* FIXME not thread-safe! *)
wenzelm@26503
    53
fun norm_absolute p =
berghofe@15155
    54
  let
berghofe@15155
    55
    val p' = pwd ();
berghofe@15155
    56
    fun norm p = if can cd p then pwd ()
berghofe@15155
    57
      else Path.append (norm (Path.dir p)) (Path.base p);
wenzelm@16261
    58
    val p'' = norm p;
wenzelm@26503
    59
  in (cd p'; p'') end;
berghofe@15155
    60
wenzelm@16261
    61
fun full_path path =
berghofe@15155
    62
  (if Path.is_absolute path then path
wenzelm@16261
    63
   else Path.append (pwd ()) path)
wenzelm@16261
    64
  |> norm_absolute;
wenzelm@5009
    65
wenzelm@6182
    66
wenzelm@6182
    67
(* tmp_path *)
wenzelm@6182
    68
wenzelm@6182
    69
fun tmp_path path =
wenzelm@6182
    70
  Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
wenzelm@5009
    71
wenzelm@5009
    72
wenzelm@16261
    73
(* system commands *)
wenzelm@16261
    74
wenzelm@16261
    75
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd);
wenzelm@16261
    76
wenzelm@16261
    77
fun system_command cmd =
wenzelm@16261
    78
  if system cmd <> 0 then error ("System command failed: " ^ cmd)
wenzelm@16261
    79
  else ();
wenzelm@16261
    80
wenzelm@16261
    81
wenzelm@16261
    82
(* directory entries *)
wenzelm@16261
    83
wenzelm@23861
    84
datatype ident = Ident of string;
wenzelm@16261
    85
wenzelm@23874
    86
fun rep_ident (Ident s) = s;
wenzelm@23874
    87
wenzelm@23861
    88
fun ident path =
wenzelm@23861
    89
  (case try OS.FileSys.modTime (platform_path path) of
wenzelm@23861
    90
    NONE => NONE
wenzelm@23861
    91
  | SOME time => SOME (Ident
wenzelm@23861
    92
      (case getenv "ISABELLE_FILE_IDENT" of
wenzelm@23861
    93
        "" => Path.implode (full_path path) ^ ": " ^ Time.toString time
wenzelm@23861
    94
      | cmd =>
wenzelm@26220
    95
         let
wenzelm@26220
    96
           val (id, rc) = system_out ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_path path)
wenzelm@26220
    97
         in
wenzelm@26220
    98
           if id <> "" andalso rc = 0 then id
wenzelm@23861
    99
           else error ("Failed to identify file " ^ quote (Path.implode path) ^ " by " ^ cmd)
wenzelm@23861
   100
         end)));
wenzelm@16261
   101
wenzelm@23861
   102
val exists = can OS.FileSys.modTime o platform_path;
wenzelm@16261
   103
wenzelm@21858
   104
fun check path =
wenzelm@17826
   105
  if exists path then ()
wenzelm@21858
   106
  else error ("No such file or directory: " ^ quote (Path.implode path));
wenzelm@17826
   107
wenzelm@16261
   108
val rm = OS.FileSys.remove o platform_path;
wenzelm@16261
   109
wenzelm@16261
   110
fun mkdir path = system_command ("mkdir -p " ^ shell_path path);
wenzelm@16261
   111
wenzelm@16261
   112
fun is_dir path =
wenzelm@19473
   113
  the_default false (try OS.FileSys.isDir (platform_path path));
wenzelm@16261
   114
wenzelm@16261
   115
wenzelm@6224
   116
(* read / write files *)
wenzelm@6224
   117
wenzelm@16261
   118
local
wenzelm@16261
   119
wenzelm@26503
   120
fun with_file open_file close_file f path =
wenzelm@26503
   121
  let val file = open_file path
wenzelm@26503
   122
  in Exn.release (Exn.capture f file before close_file file) end;
wenzelm@6224
   123
wenzelm@26503
   124
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts;
wenzelm@6182
   125
wenzelm@16261
   126
in
wenzelm@6218
   127
wenzelm@26503
   128
fun open_input f = with_file TextIO.openIn TextIO.closeIn f o platform_path;
wenzelm@26503
   129
fun open_output f = with_file TextIO.openOut TextIO.closeOut f o platform_path;
wenzelm@26503
   130
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path;
wenzelm@6224
   131
wenzelm@26503
   132
val read = open_input TextIO.inputAll;
wenzelm@5009
   133
wenzelm@26503
   134
fun write_list path txts = open_output (output txts) path;
wenzelm@26503
   135
fun append_list path txts = open_append (output txts) path;
wenzelm@16713
   136
wenzelm@16713
   137
fun write path txt = write_list path [txt];
wenzelm@16713
   138
fun append path txt = append_list path [txt];
wenzelm@6182
   139
wenzelm@16261
   140
end;
wenzelm@5009
   141
wenzelm@16603
   142
fun eq paths =
wenzelm@16603
   143
  (case try (pairself (OS.FileSys.fileId o platform_path)) paths of
wenzelm@16603
   144
    SOME ids => OS.FileSys.compare ids = EQUAL
wenzelm@16603
   145
  | NONE => false);
wenzelm@16603
   146
wenzelm@21962
   147
fun copy src dst =
wenzelm@21962
   148
  if eq (src, dst) then ()
wenzelm@21962
   149
  else
wenzelm@21962
   150
    let val target = if is_dir dst then Path.append dst (Path.base src) else dst
wenzelm@21962
   151
    in write target (read src) end;
wenzelm@6318
   152
wenzelm@21962
   153
fun copy_dir src dst =
wenzelm@21962
   154
  if eq (src, dst) then ()
wenzelm@21962
   155
  else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ());
wenzelm@6640
   156
wenzelm@5009
   157
end;