src/Pure/General/file.ML
author wenzelm
Thu, 27 Apr 2006 15:06:40 +0200
changeset 19484 89484a62184a
parent 19473 d87a8838afa4
child 19960 a0e3f2df9b0e
permissions -rw-r--r--
added map_filter;
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
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    10
  val unpack_platform_path_fn: (string -> Path.T) ref
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    11
  val unpack_platform_path: string -> Path.T
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    12
  val platform_path_fn: (Path.T -> string) ref
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    13
  val platform_path: Path.T -> string
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    14
  val shell_path_fn: (Path.T -> string) ref
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    15
  val shell_path: Path.T -> string
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    16
  val cd: Path.T -> unit
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    17
  val pwd: unit -> Path.T
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    18
  val full_path: Path.T -> Path.T
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    19
  val tmp_path: Path.T -> Path.T
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    20
  val isatool: string -> int
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    21
  val system_command: string -> unit
17826
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    22
  eqtype info
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    23
  val info: Path.T -> info option
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    24
  val exists: Path.T -> bool
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    25
  val assert: Path.T -> unit
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    26
  val rm: Path.T -> unit
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    27
  val mkdir: Path.T -> unit
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    28
  val read: Path.T -> string
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    29
  val write: Path.T -> string -> unit
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    30
  val append: Path.T -> string -> unit
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
    31
  val write_list: Path.T -> string list -> unit
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
    32
  val append_list: Path.T -> string list -> unit
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
    33
  val eq: Path.T * Path.T -> bool
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    34
  val copy: Path.T -> Path.T -> unit
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    35
  val copy_dir: Path.T -> Path.T -> unit
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
    36
  val use: Path.T -> unit
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    37
end;
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    38
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    39
structure File: FILE =
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    40
struct
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    41
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    42
(* platform specific path representations *)
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    43
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    44
val unpack_platform_path_fn = ref Path.unpack;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    45
fun unpack_platform_path s = ! unpack_platform_path_fn s;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    46
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    47
val platform_path_fn = ref (Path.pack o Path.expand);
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    48
fun platform_path path = ! platform_path_fn path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    49
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    50
val shell_path_fn = ref (enclose "'" "'" o Path.pack o Path.expand);
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    51
fun shell_path path = ! shell_path_fn path;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    52
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    53
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    54
(* current path *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    55
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    56
val cd = Library.cd o platform_path;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    57
val pwd = unpack_platform_path o Library.pwd;
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
    58
15155
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    59
fun norm_absolute p =
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    60
  let
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    61
    val p' = pwd ();
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    62
    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
    63
      else Path.append (norm (Path.dir p)) (Path.base p);
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    64
    val p'' = norm p;
15155
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    65
  in (cd p'; p'') end
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    66
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    67
fun full_path path =
15155
6cdd6a8da9b9 Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents: 14981
diff changeset
    68
  (if Path.is_absolute path then path
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    69
   else Path.append (pwd ()) path)
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    70
  |> norm_absolute;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    71
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    72
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    73
(* tmp_path *)
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    74
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    75
fun tmp_path path =
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
    76
  Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    77
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
    78
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    79
(* system commands *)
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    80
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    81
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd);
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    82
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    83
fun system_command cmd =
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    84
  if system cmd <> 0 then error ("System command failed: " ^ cmd)
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    85
  else ();
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    86
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    87
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    88
(* directory entries *)
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    89
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    90
datatype info = Info of string;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    91
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    92
fun info path =
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    93
  Option.map (Info o Time.toString) (try OS.FileSys.modTime (platform_path path));
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    94
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    95
val exists = is_some o info;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
    96
17826
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    97
fun assert path =
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    98
  if exists path then ()
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
    99
  else error ("No such file or directory: " ^ quote (Path.pack path));
afa2696eacce added assert;
wenzelm
parents: 16713
diff changeset
   100
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   101
val rm = OS.FileSys.remove o platform_path;
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   102
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   103
fun mkdir path = system_command ("mkdir -p " ^ shell_path path);
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   104
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   105
fun is_dir path =
19473
wenzelm
parents: 18715
diff changeset
   106
  the_default false (try OS.FileSys.isDir (platform_path path));
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   107
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   108
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   109
(* read / write files *)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   110
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   111
local
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   112
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   113
fun fail_safe f g x =
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   114
  let val y = f x handle exn => (g x; raise exn)
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   115
  in g x; y end;
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   116
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   117
fun output txts stream = List.app (fn txt => TextIO.output (stream, txt)) txts;
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
   118
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   119
in
6218
3e9d6edc99a8 added join_info;
wenzelm
parents: 6182
diff changeset
   120
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   121
fun read path =
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   122
  fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path));
6224
0c08846be6f3 fail_safe close;
wenzelm
parents: 6218
diff changeset
   123
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   124
fun write_list path txts =
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   125
  fail_safe (output txts) TextIO.closeOut (TextIO.openOut (platform_path path));
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   126
16713
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   127
fun append_list path txts =
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   128
  fail_safe (output txts) TextIO.closeOut (TextIO.openAppend (platform_path path));
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   129
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   130
fun write path txt = write_list path [txt];
be5763901788 added write_list, append_list;
wenzelm
parents: 16603
diff changeset
   131
fun append path txt = append_list path [txt];
6182
4a07dfe3583f use Path.T;
wenzelm
parents: 6118
diff changeset
   132
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   133
end;
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   134
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   135
fun eq paths =
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   136
  (case try (pairself (OS.FileSys.fileId o platform_path)) paths of
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   137
    SOME ids => OS.FileSys.compare ids = EQUAL
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   138
  | NONE => false);
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   139
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   140
fun copy from to = conditional (not (eq (from, to))) (fn () =>
16261
28803c418b59 removed sysify_path, quote_sysity_path etc.;
wenzelm
parents: 16002
diff changeset
   141
  let val target = if is_dir to then Path.append to (Path.base from) else to
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   142
  in write target (read from) end);
6318
4a423e8a0b54 added mkdir;
wenzelm
parents: 6224
diff changeset
   143
16603
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   144
fun copy_dir from to = conditional (not (eq (from, to))) (fn () =>
1776d276f848 added eq;
wenzelm
parents: 16304
diff changeset
   145
  (system_command ("cp -r " ^ shell_path from ^ "/. " ^ shell_path to); ()))
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   146
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   147
12116
4027b15377a5 got rid of obsolete input filtering;
wenzelm
parents: 9784
diff changeset
   148
(* use ML files *)
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   149
18715
f809deffdd8f use: Output.ML_errors;
wenzelm
parents: 18678
diff changeset
   150
val use = Output.ML_errors use o platform_path;
6640
d2e8342bf5c3 rearranged order of modules;
wenzelm
parents: 6318
diff changeset
   151
5009
05b152a41922 moved Thy/file.ML to General/file.ML;
wenzelm
parents:
diff changeset
   152
end;