author | wenzelm |
Sat, 24 Jul 2021 15:38:41 +0200 | |
changeset 74056 | fb8d5c0133c9 |
parent 73945 | e61add9d5b5e |
child 75107 | 7c0217c8b8a5 |
permissions | -rw-r--r-- |
43600 | 1 |
/* Title: Pure/General/path.scala |
2 |
Author: Makarius |
|
3 |
||
43601 | 4 |
Algebra of file-system paths: basic POSIX notation, extended by named |
5 |
roots (e.g. //foo) and variables (e.g. $BAR). |
|
43600 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
73897 | 11 |
import java.util.{Map => JMap} |
48409 | 12 |
import java.io.{File => JFile} |
73945 | 13 |
import java.nio.file.{Path => JPath} |
48373 | 14 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
15 |
import scala.util.matching.Regex |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
16 |
|
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
17 |
|
43600 | 18 |
object Path |
19 |
{ |
|
20 |
/* path elements */ |
|
21 |
||
45244 | 22 |
sealed abstract class Elem |
60215 | 23 |
private case class Root(name: String) extends Elem |
24 |
private case class Basic(name: String) extends Elem |
|
25 |
private case class Variable(name: String) extends Elem |
|
43600 | 26 |
private case object Parent extends Elem |
27 |
||
28 |
private def err_elem(msg: String, s: String): Nothing = |
|
69547 | 29 |
error(msg + " path element " + quote(s)) |
30 |
||
69548
415dc92050a6
more strict check: avoid confusion of Path.basic with Path.current / Path.parent;
wenzelm
parents:
69547
diff
changeset
|
31 |
private val illegal_elem = Set("", "~", "~~", ".", "..") |
69550
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
32 |
private val illegal_char = "/\\$:\"'<>|?*" |
43600 | 33 |
|
34 |
private def check_elem(s: String): String = |
|
69547 | 35 |
if (illegal_elem.contains(s)) err_elem("Illegal", s) |
52106 | 36 |
else { |
69550
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
37 |
for (c <- s) { |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
38 |
if (c.toInt < 32) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
39 |
err_elem("Illegal control character " + c.toInt + " in", s) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
40 |
if (illegal_char.contains(c)) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
41 |
err_elem("Illegal character " + quote(c.toString) + " in", s) |
69547 | 42 |
} |
52106 | 43 |
s |
44 |
} |
|
43600 | 45 |
|
46 |
private def root_elem(s: String): Elem = Root(check_elem(s)) |
|
47 |
private def basic_elem(s: String): Elem = Basic(check_elem(s)) |
|
48 |
private def variable_elem(s: String): Elem = Variable(check_elem(s)) |
|
49 |
||
50 |
private def apply_elem(y: Elem, xs: List[Elem]): List[Elem] = |
|
51 |
(y, xs) match { |
|
52 |
case (Root(_), _) => List(y) |
|
53 |
case (Parent, Root(_) :: _) => xs |
|
54 |
case (Parent, Basic(_) :: rest) => rest |
|
55 |
case _ => y :: xs |
|
56 |
} |
|
57 |
||
58 |
private def norm_elems(elems: List[Elem]): List[Elem] = |
|
73361 | 59 |
elems.foldRight(List.empty[Elem])(apply_elem) |
43600 | 60 |
|
56844 | 61 |
private def implode_elem(elem: Elem, short: Boolean): String = |
43600 | 62 |
elem match { |
63 |
case Root("") => "" |
|
64 |
case Root(s) => "//" + s |
|
65 |
case Basic(s) => s |
|
56844 | 66 |
case Variable("USER_HOME") if short => "~" |
67 |
case Variable("ISABELLE_HOME") if short => "~~" |
|
43600 | 68 |
case Variable(s) => "$" + s |
69 |
case Parent => ".." |
|
70 |
} |
|
71 |
||
72962 | 72 |
private def squash_elem(elem: Elem): String = |
73 |
elem match { |
|
74 |
case Root("") => "ROOT" |
|
75 |
case Root(s) => "SERVER_" + s |
|
76 |
case Basic(s) => s |
|
77 |
case Variable(s) => s |
|
78 |
case Parent => "PARENT" |
|
79 |
} |
|
80 |
||
43600 | 81 |
|
82 |
/* path constructors */ |
|
83 |
||
45244 | 84 |
val current: Path = new Path(Nil) |
85 |
val root: Path = new Path(List(Root(""))) |
|
86 |
def named_root(s: String): Path = new Path(List(root_elem(s))) |
|
71601 | 87 |
def make(elems: List[String]): Path = new Path(elems.reverse.map(basic_elem)) |
45244 | 88 |
def basic(s: String): Path = new Path(List(basic_elem(s))) |
89 |
def variable(s: String): Path = new Path(List(variable_elem(s))) |
|
90 |
val parent: Path = new Path(List(Parent)) |
|
43600 | 91 |
|
73522 | 92 |
val USER_HOME: Path = variable("USER_HOME") |
93 |
val ISABELLE_HOME: Path = variable("ISABELLE_HOME") |
|
94 |
||
43600 | 95 |
|
96 |
/* explode */ |
|
97 |
||
98 |
def explode(str: String): Path = |
|
99 |
{ |
|
52106 | 100 |
def explode_elem(s: String): Elem = |
101 |
try { |
|
102 |
if (s == "..") Parent |
|
103 |
else if (s == "~") Variable("USER_HOME") |
|
104 |
else if (s == "~~") Variable("ISABELLE_HOME") |
|
105 |
else if (s.startsWith("$")) variable_elem(s.substring(1)) |
|
106 |
else basic_elem(s) |
|
107 |
} |
|
108 |
catch { case ERROR(msg) => cat_error(msg, "The error(s) above occurred in " + quote(str)) } |
|
109 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43669
diff
changeset
|
110 |
val ss = space_explode('/', str) |
43600 | 111 |
val r = ss.takeWhile(_.isEmpty).length |
112 |
val es = ss.dropWhile(_.isEmpty) |
|
113 |
val (roots, raw_elems) = |
|
114 |
if (r == 0) (Nil, es) |
|
115 |
else if (r == 1) (List(Root("")), es) |
|
116 |
else if (es.isEmpty) (List(Root("")), Nil) |
|
117 |
else (List(root_elem(es.head)), es.tail) |
|
52106 | 118 |
val elems = raw_elems.filterNot(s => s.isEmpty || s == ".").map(explode_elem) |
119 |
||
63866 | 120 |
new Path(norm_elems(elems reverse_::: roots)) |
43600 | 121 |
} |
43669 | 122 |
|
55879
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
123 |
def is_wellformed(str: String): Boolean = |
48484 | 124 |
try { explode(str); true } catch { case ERROR(_) => false } |
125 |
||
55879
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
126 |
def is_valid(str: String): Boolean = |
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
127 |
try { explode(str).expand; true } catch { case ERROR(_) => false } |
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
128 |
|
43669 | 129 |
def split(str: String): List[Path] = |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43669
diff
changeset
|
130 |
space_explode(':', str).filterNot(_.isEmpty).map(explode) |
48457 | 131 |
|
132 |
||
133 |
/* encode */ |
|
134 |
||
135 |
val encode: XML.Encode.T[Path] = (path => XML.Encode.string(path.implode)) |
|
69551
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
136 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
137 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
138 |
/* reserved names */ |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
139 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
140 |
private val reserved_windows: Set[String] = |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
141 |
Set("CON", "PRN", "AUX", "NUL", |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
142 |
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
143 |
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9") |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
144 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
145 |
def is_reserved(name: String): Boolean = |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
146 |
Long_Name.explode(name).exists(a => reserved_windows.contains(Word.uppercase(a))) |
69904 | 147 |
|
148 |
||
149 |
/* case-insensitive names */ |
|
150 |
||
73340 | 151 |
def check_case_insensitive(paths: List[Path]): Unit = |
69904 | 152 |
{ |
153 |
val table = |
|
73359 | 154 |
paths.foldLeft(Multi_Map.empty[String, String]) { case (tab, path) => |
69904 | 155 |
val name = path.expand.implode |
156 |
tab.insert(Word.lowercase(name), name) |
|
73359 | 157 |
} |
69904 | 158 |
val collisions = |
159 |
(for { (_, coll) <- table.iterator_list if coll.length > 1 } yield coll).toList.flatten |
|
160 |
if (collisions.nonEmpty) { |
|
161 |
error(("Collision of file names due case-insensitivity:" :: collisions).mkString("\n ")) |
|
162 |
} |
|
163 |
} |
|
43600 | 164 |
} |
165 |
||
43669 | 166 |
|
72746 | 167 |
final class Path private(protected val elems: List[Path.Elem]) // reversed elements |
43600 | 168 |
{ |
72746 | 169 |
override def hashCode: Int = elems.hashCode |
170 |
override def equals(that: Any): Boolean = |
|
171 |
that match { |
|
172 |
case other: Path => elems == other.elems |
|
173 |
case _ => false |
|
174 |
} |
|
175 |
||
43600 | 176 |
def is_current: Boolean = elems.isEmpty |
59319 | 177 |
def is_absolute: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Root] |
65559 | 178 |
def is_root: Boolean = elems match { case List(Path.Root(_)) => true case _ => false } |
43600 | 179 |
def is_basic: Boolean = elems match { case List(Path.Basic(_)) => true case _ => false } |
72572 | 180 |
def starts_basic: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Basic] |
43600 | 181 |
|
73360 | 182 |
def +(other: Path): Path = new Path(other.elems.foldRight(elems)(Path.apply_elem)) |
43600 | 183 |
|
184 |
||
43604 | 185 |
/* implode */ |
43600 | 186 |
|
56844 | 187 |
private def gen_implode(short: Boolean): String = |
43600 | 188 |
elems match { |
189 |
case Nil => "." |
|
190 |
case List(Path.Root("")) => "/" |
|
56844 | 191 |
case _ => elems.map(Path.implode_elem(_, short)).reverse.mkString("/") |
43600 | 192 |
} |
56844 | 193 |
def implode: String = gen_implode(false) |
194 |
def implode_short: String = gen_implode(true) |
|
43600 | 195 |
|
43652 | 196 |
override def toString: String = quote(implode) |
43600 | 197 |
|
198 |
||
199 |
/* base element */ |
|
200 |
||
201 |
private def split_path: (Path, String) = |
|
202 |
elems match { |
|
45244 | 203 |
case Path.Basic(s) :: xs => (new Path(xs), s) |
43604 | 204 |
case _ => error("Cannot split path into dir/base: " + toString) |
43600 | 205 |
} |
206 |
||
207 |
def dir: Path = split_path._1 |
|
45244 | 208 |
def base: Path = new Path(List(Path.Basic(split_path._2))) |
43600 | 209 |
|
74056 | 210 |
def ends_with(a: String): Boolean = |
211 |
elems match { |
|
212 |
case Path.Basic(b) :: _ => b.endsWith(a) |
|
213 |
case _ => false |
|
214 |
} |
|
215 |
def is_java: Boolean = ends_with(".java") |
|
216 |
def is_scala: Boolean = ends_with(".scala") |
|
217 |
||
43600 | 218 |
def ext(e: String): Path = |
219 |
if (e == "") this |
|
220 |
else { |
|
221 |
val (prfx, s) = split_path |
|
43604 | 222 |
prfx + Path.basic(s + "." + e) |
43600 | 223 |
} |
43604 | 224 |
|
72575 | 225 |
def xz: Path = ext("xz") |
73660 | 226 |
def xml: Path = ext("xml") |
72962 | 227 |
def html: Path = ext("html") |
72574
d892f6d66402
build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents:
72572
diff
changeset
|
228 |
def tex: Path = ext("tex") |
d892f6d66402
build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents:
72572
diff
changeset
|
229 |
def pdf: Path = ext("pdf") |
72776 | 230 |
def thy: Path = ext("thy") |
73628 | 231 |
def tar: Path = ext("tar") |
232 |
def gz: Path = ext("gz") |
|
73691
2f9877db82a1
reimplemented Mirabelle as Isabelle/ML presentation hook + Isabelle/Scala tool, but sledgehammer is still inactive;
wenzelm
parents:
73660
diff
changeset
|
233 |
def log: Path = ext("log") |
72574
d892f6d66402
build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents:
72572
diff
changeset
|
234 |
|
53336 | 235 |
def backup: Path = |
236 |
{ |
|
237 |
val (prfx, s) = split_path |
|
238 |
prfx + Path.basic(s + "~") |
|
239 |
} |
|
240 |
||
58610 | 241 |
def backup2: Path = |
242 |
{ |
|
243 |
val (prfx, s) = split_path |
|
244 |
prfx + Path.basic(s + "~~") |
|
245 |
} |
|
246 |
||
72464 | 247 |
def platform_exe: Path = |
248 |
if (Platform.is_windows) ext("exe") else this |
|
249 |
||
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
250 |
private val Ext = new Regex("(.*)\\.([^.]*)") |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
251 |
|
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
252 |
def split_ext: (Path, String) = |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
253 |
{ |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
254 |
val (prefix, base) = split_path |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
255 |
base match { |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
256 |
case Ext(b, e) => (prefix + Path.basic(b), e) |
56556 | 257 |
case _ => (prefix + Path.basic(base), "") |
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
258 |
} |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
259 |
} |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
260 |
|
69367 | 261 |
def drop_ext: Path = split_ext._1 |
262 |
def get_ext: String = split_ext._2 |
|
263 |
||
72962 | 264 |
def squash: Path = new Path(elems.map(elem => Path.Basic(Path.squash_elem(elem)))) |
265 |
||
43604 | 266 |
|
267 |
/* expand */ |
|
268 |
||
73897 | 269 |
def expand_env(env: JMap[String, String]): Path = |
43604 | 270 |
{ |
271 |
def eval(elem: Path.Elem): List[Path.Elem] = |
|
272 |
elem match { |
|
43664 | 273 |
case Path.Variable(s) => |
64228
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
274 |
val path = Path.explode(Isabelle_System.getenv_strict(s, env)) |
48658 | 275 |
if (path.elems.exists(_.isInstanceOf[Path.Variable])) |
73715
bf51c23f3f99
clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents:
73712
diff
changeset
|
276 |
error("Illegal path variable nesting: " + Properties.Eq(s, path.toString)) |
48658 | 277 |
else path.elems |
43604 | 278 |
case x => List(x) |
279 |
} |
|
280 |
||
71383 | 281 |
new Path(Path.norm_elems(elems.flatMap(eval))) |
43604 | 282 |
} |
48373 | 283 |
|
64228
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
284 |
def expand: Path = expand_env(Isabelle_System.settings()) |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
285 |
|
69366 | 286 |
def file_name: String = expand.base.implode |
287 |
||
48373 | 288 |
|
72784 | 289 |
/* implode wrt. given directories */ |
48548 | 290 |
|
72784 | 291 |
def implode_symbolic: String = |
292 |
{ |
|
293 |
val directories = |
|
294 |
Library.space_explode(':', Isabelle_System.getenv("ISABELLE_DIRECTORIES")).reverse |
|
295 |
val full_name = expand.implode |
|
296 |
directories.view.flatMap(a => |
|
297 |
try { |
|
298 |
val b = Path.explode(a).expand.implode |
|
299 |
if (full_name == b) Some(a) |
|
300 |
else { |
|
301 |
Library.try_unprefix(b + "/", full_name) match { |
|
302 |
case Some(name) => Some(a + "/" + name) |
|
303 |
case None => None |
|
304 |
} |
|
305 |
} |
|
306 |
} catch { case ERROR(_) => None }).headOption.getOrElse(implode) |
|
307 |
} |
|
308 |
||
309 |
def position: Position.T = Position.File(implode_symbolic) |
|
48548 | 310 |
|
311 |
||
66232 | 312 |
/* platform files */ |
48373 | 313 |
|
60988 | 314 |
def file: JFile = File.platform_file(this) |
48548 | 315 |
def is_file: Boolean = file.isFile |
316 |
def is_dir: Boolean = file.isDirectory |
|
65833 | 317 |
|
73945 | 318 |
def java_path: JPath = file.toPath |
319 |
||
66232 | 320 |
def absolute_file: JFile = File.absolute(file) |
321 |
def canonical_file: JFile = File.canonical(file) |
|
67181 | 322 |
|
323 |
def absolute: Path = File.path(absolute_file) |
|
324 |
def canonical: Path = File.path(canonical_file) |
|
43600 | 325 |
} |