src/Pure/General/file.ML
author berghofe
Mon, 23 Aug 2004 17:06:18 +0200
changeset 15155 6cdd6a8da9b9
parent 14981 e73f8140af78
child 15531 08c8dad8e399
permissions -rw-r--r--
Added function for "normalizing" absolute paths (i.e. dereferencing of symbolic links; the functions in path.ML cannot do this). This is used by function full_path.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6118
caa439435666 fixed titles;
wenzelm
parents: 5009
diff changeset
     1
(*  Title:      Pure/General/file.ML
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     2
    ID:         $Id$
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     3
    Author:     Markus Wenzel, TU Muenchen
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     4
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     5
File system operations.
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     6
*)
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     7
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     8
signature FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
     9
sig
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    10
  val sys_pack_fn: (Path.T -> string) ref
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    11
  val sys_unpack_fn: (string -> Path.T) ref
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    12
  val sysify_path: Path.T -> string
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
    13
  val quote_sysify_path: Path.T -> string
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    14
  val rm: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    15
  val cd: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    16
  val pwd: unit -> Path.T
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    17
  val full_path: Path.T -> Path.T
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    18
  val tmp_path: Path.T -> Path.T
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    19
  val read: Path.T -> string
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    20
  val write: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    21
  val append: Path.T -> string -> unit
9046
17c5edf1706b Exported system_command.
berghofe
parents: 8806
diff changeset
    22
  val system_command: string -> unit
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    23
  val copy: Path.T -> Path.T -> unit
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    24
  eqtype info
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    25
  val info: Path.T -> info option
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    26
  val exists: Path.T -> bool
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
    27
  val mkdir: Path.T -> unit
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
    28
  val use: Path.T -> unit
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    29
end;
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    30
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    31
structure File: FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    32
struct
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    33
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    34
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    35
(* system path representations (default: Unix) *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    36
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    37
val sys_pack_fn = ref Path.pack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    38
val sys_unpack_fn = ref Path.unpack;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    39
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    40
fun sysify_path path = ! sys_pack_fn (Path.expand path);
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
    41
val quote_sysify_path = enclose "'" "'" o sysify_path;
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    42
fun unsysify_path s = ! sys_unpack_fn s;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    43
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    44
val rm = OS.FileSys.remove o sysify_path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    45
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    46
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    47
(* current path *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    48
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    49
val cd = Library.cd o sysify_path;
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    50
val pwd = unsysify_path o Library.pwd;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    51
15155
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    52
fun norm_absolute p =
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    53
  let
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    54
    val p' = pwd ();
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    55
    fun norm p = if can cd p then pwd ()
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    56
      else Path.append (norm (Path.dir p)) (Path.base p);
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    57
    val p'' = norm p
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    58
  in (cd p'; p'') end
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    59
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    60
fun full_path path = norm_absolute
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    61
  (if Path.is_absolute path then path
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    62
   else Path.append (pwd ()) path);
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    63
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    64
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    65
(* tmp_path *)
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    66
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    67
fun tmp_path path =
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    68
  Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    69
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    70
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    71
(* read / write files *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    72
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    73
fun fail_safe f g x =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    74
  let val y = f x handle exn => (g x; raise exn)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    75
  in g x; y end;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    76
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    77
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    78
fun output txt stream = TextIO.output (stream, txt);
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    79
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    80
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    81
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
7716
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
    82
fun append path txt =
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
    83
  fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
6218
3e9d6edc99a8 added join_info;
wenzelm
parents: 6182
diff changeset
    84
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    85
fun copy inpath outpath = write outpath (read inpath);
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    86
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    87
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    88
(* file info *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    89
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    90
datatype info = Info of string;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    91
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    92
fun info path =
7129
7e0ec1b293c3 export sysify_path;
wenzelm
parents: 6640
diff changeset
    93
  let val name = sysify_path path in
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    94
    (case file_info name of
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    95
      "" => None
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    96
    | s => Some (Info s))
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    97
  end;
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    98
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    99
val exists = is_some o info;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   100
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   101
7716
b0cb304517d4 added copy_all;
wenzelm
parents: 7129
diff changeset
   102
(* directories *)
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
   103
7850
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
   104
fun system_command cmd =
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
   105
  if system cmd <> 0 then error ("System command failed: " ^ cmd)
3689adcf9b8b mkdir, copy_all: system_command;
wenzelm
parents: 7716
diff changeset
   106
  else ();
12894
61f485eb0dc2 moved copy_all to Thy/present.ML;
wenzelm
parents: 12116
diff changeset
   107
9784
d8f8dc335c8c export quote_sysify_path;
wenzelm
parents: 9046
diff changeset
   108
fun mkdir path = system_command ("mkdir -p " ^ quote_sysify_path path);
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   109
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   110
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
   111
(* use ML files *)
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   112
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
   113
val use = use o sysify_path;
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   114
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   115
end;