6118
|
1 |
(* Title: Pure/General/file.ML
|
5009
|
2 |
ID: $Id$
|
|
3 |
Author: Markus Wenzel, TU Muenchen
|
8806
|
4 |
License: GPL (GNU GENERAL PUBLIC LICENSE)
|
5009
|
5 |
|
|
6 |
File system operations.
|
|
7 |
*)
|
|
8 |
|
|
9 |
signature FILE =
|
|
10 |
sig
|
6224
|
11 |
val sys_pack_fn: (Path.T -> string) ref
|
|
12 |
val sys_unpack_fn: (string -> Path.T) ref
|
7129
|
13 |
val sysify_path: Path.T -> string
|
6224
|
14 |
val rm: Path.T -> unit
|
|
15 |
val cd: Path.T -> unit
|
|
16 |
val pwd: unit -> Path.T
|
|
17 |
val full_path: Path.T -> Path.T
|
6182
|
18 |
val tmp_path: Path.T -> Path.T
|
|
19 |
val read: Path.T -> string
|
|
20 |
val write: Path.T -> string -> unit
|
|
21 |
val append: Path.T -> string -> unit
|
9046
|
22 |
val system_command: string -> unit
|
6182
|
23 |
val copy: Path.T -> Path.T -> unit
|
6224
|
24 |
eqtype info
|
|
25 |
val info: Path.T -> info option
|
|
26 |
val exists: Path.T -> bool
|
6318
|
27 |
val mkdir: Path.T -> unit
|
7716
|
28 |
val copy_all: Path.T -> Path.T -> unit
|
6640
|
29 |
val plain_use: Path.T -> unit
|
|
30 |
val symbol_use: Path.T -> unit
|
5009
|
31 |
end;
|
|
32 |
|
|
33 |
structure File: FILE =
|
|
34 |
struct
|
|
35 |
|
|
36 |
|
6224
|
37 |
(* system path representations (default: Unix) *)
|
|
38 |
|
|
39 |
val sys_pack_fn = ref Path.pack;
|
|
40 |
val sys_unpack_fn = ref Path.unpack;
|
|
41 |
|
7129
|
42 |
fun sysify_path path = ! sys_pack_fn (Path.expand path);
|
|
43 |
fun unsysify_path s = ! sys_unpack_fn s;
|
6224
|
44 |
|
7129
|
45 |
val rm = OS.FileSys.remove o sysify_path;
|
6224
|
46 |
|
|
47 |
|
|
48 |
(* current path *)
|
|
49 |
|
7129
|
50 |
val cd = Library.cd o sysify_path;
|
|
51 |
val pwd = unsysify_path o Library.pwd;
|
6224
|
52 |
|
|
53 |
fun full_path path =
|
|
54 |
if Path.is_absolute path then path
|
|
55 |
else Path.append (pwd ()) path;
|
5009
|
56 |
|
6182
|
57 |
|
|
58 |
(* tmp_path *)
|
|
59 |
|
|
60 |
fun tmp_path path =
|
|
61 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path);
|
5009
|
62 |
|
|
63 |
|
6224
|
64 |
(* read / write files *)
|
|
65 |
|
|
66 |
fun fail_safe f g x =
|
|
67 |
let val y = f x handle exn => (g x; raise exn)
|
|
68 |
in g x; y end;
|
|
69 |
|
|
70 |
|
|
71 |
fun output txt stream = TextIO.output (stream, txt);
|
6182
|
72 |
|
7129
|
73 |
fun read path = fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (sysify_path path));
|
|
74 |
fun write path txt = fail_safe (output txt) TextIO.closeOut (TextIO.openOut (sysify_path path));
|
7716
|
75 |
fun append path txt =
|
|
76 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (sysify_path path));
|
6218
|
77 |
|
6224
|
78 |
fun copy inpath outpath = write outpath (read inpath);
|
|
79 |
|
|
80 |
|
|
81 |
(* file info *)
|
|
82 |
|
|
83 |
datatype info = Info of string;
|
5009
|
84 |
|
6182
|
85 |
fun info path =
|
7129
|
86 |
let val name = sysify_path path in
|
6182
|
87 |
(case file_info name of
|
|
88 |
"" => None
|
6224
|
89 |
| s => Some (Info s))
|
6182
|
90 |
end;
|
|
91 |
|
|
92 |
val exists = is_some o info;
|
5009
|
93 |
|
|
94 |
|
7716
|
95 |
(* directories *)
|
6318
|
96 |
|
7716
|
97 |
val quote_sysify = enclose "'" "'" o sysify_path;
|
6318
|
98 |
|
7850
|
99 |
fun system_command cmd =
|
|
100 |
if system cmd <> 0 then error ("System command failed: " ^ cmd)
|
|
101 |
else ();
|
|
102 |
|
|
103 |
fun mkdir path = system_command ("mkdir -p " ^ quote_sysify path);
|
6640
|
104 |
|
8163
|
105 |
fun copy_all path1 path2 = (*dereferences symlinks!*)
|
|
106 |
system_command ("cp -r " ^ quote_sysify path1 ^ " " ^ quote_sysify path2);
|
6640
|
107 |
|
|
108 |
|
|
109 |
(* symbol_use *)
|
|
110 |
|
7129
|
111 |
val plain_use = use o sysify_path;
|
6640
|
112 |
|
|
113 |
(* version of 'use' with input filtering *)
|
|
114 |
|
|
115 |
val symbol_use =
|
|
116 |
if not needs_filtered_use then plain_use (*ML system (wrongly!) accepts non-ASCII*)
|
|
117 |
else
|
|
118 |
fn path =>
|
|
119 |
let
|
|
120 |
val txt = read path;
|
|
121 |
val txt_out = Symbol.input txt;
|
|
122 |
in
|
|
123 |
if txt = txt_out then plain_use path
|
|
124 |
else
|
|
125 |
let val tmp_path = tmp_path path in
|
|
126 |
write tmp_path txt_out;
|
|
127 |
plain_use tmp_path handle exn => (rm tmp_path; raise exn);
|
|
128 |
rm tmp_path
|
|
129 |
end
|
|
130 |
end;
|
|
131 |
|
5009
|
132 |
end;
|