author | wenzelm |
Thu, 15 Sep 2022 21:37:17 +0200 | |
changeset 76166 | dbafa8d688fb |
parent 76136 | 1bb677cceea4 |
child 76170 | 5912209b4fb6 |
permissions | -rw-r--r-- |
75523 | 1 |
/* Title: Pure/General/rsync.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for rsync: see also https://rsync.samba.org |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Rsync { |
|
75527 | 11 |
sealed case class Context(progress: Progress, |
76136
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
12 |
ssh_port: Int = 0, |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
13 |
ssh_control_path: String = "", |
75527 | 14 |
archive: Boolean = true, |
75554 | 15 |
protect_args: Boolean = true // requires rsync 3.0.0, or later |
75527 | 16 |
) { |
76136
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
17 |
require(ssh_control_path.isEmpty || ssh_control_path == Bash.string(ssh_control_path), |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
18 |
"Malformed socket path: " + quote(ssh_control_path)) |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
19 |
|
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
20 |
def command: String = { |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
21 |
val ssh_command = |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
22 |
"ssh" + (if (ssh_port > 0) " -p " + ssh_port else "") + |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
23 |
(if (ssh_control_path.nonEmpty) " -o ControlPath=" + ssh_control_path else "") |
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
24 |
|
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
25 |
"rsync --rsh=" + Bash.string(ssh_command) + |
75527 | 26 |
(if (archive) " --archive" else "") + |
27 |
(if (protect_args) " --protect-args" else "") |
|
76136
1bb677cceea4
let rsync re-use ssh connection via control path;
wenzelm
parents:
76131
diff
changeset
|
28 |
} |
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
29 |
} |
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
30 |
|
75526 | 31 |
def exec( |
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
32 |
context: Context, |
75523 | 33 |
verbose: Boolean = false, |
34 |
thorough: Boolean = false, |
|
35 |
prune_empty_dirs: Boolean = false, |
|
36 |
dry_run: Boolean = false, |
|
37 |
clean: Boolean = false, |
|
38 |
list: Boolean = false, |
|
39 |
filter: List[String] = Nil, |
|
40 |
args: List[String] = Nil |
|
41 |
): Process_Result = { |
|
42 |
val script = |
|
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
43 |
context.command + |
75523 | 44 |
(if (verbose) " --verbose" else "") + |
45 |
(if (thorough) " --ignore-times" else " --omit-dir-times") + |
|
46 |
(if (prune_empty_dirs) " --prune-empty-dirs" else "") + |
|
47 |
(if (dry_run) " --dry-run" else "") + |
|
48 |
(if (clean) " --delete-excluded" else "") + |
|
49 |
(if (list) " --list-only --no-human-readable" else "") + |
|
50 |
filter.map(s => " --filter=" + Bash.string(s)).mkString + |
|
51 |
(if (args.nonEmpty) " " + Bash.strings(args) else "") |
|
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
52 |
context.progress.bash(script, echo = true) |
75523 | 53 |
} |
54 |
||
75526 | 55 |
def init(context: Context, target: String, |
75523 | 56 |
contents: List[File.Content] = Nil |
57 |
): Unit = |
|
58 |
Isabelle_System.with_tmp_dir("sync") { tmp_dir => |
|
59 |
val init_dir = Isabelle_System.make_directory(tmp_dir + Path.explode("init")) |
|
60 |
contents.foreach(_.write(init_dir)) |
|
75534
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
61 |
exec(context.copy(archive = false), |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
62 |
thorough = true, |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
63 |
args = |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
64 |
List(if (contents.nonEmpty) "--archive" else "--dirs", |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
65 |
File.bash_path(init_dir) + "/.", target)).check |
75523 | 66 |
} |
75524 | 67 |
|
68 |
def terminate(target: String): String = |
|
69 |
if (target.endsWith(":") || target.endsWith("/")) target + "." |
|
70 |
else if (target.endsWith(":.") || target.endsWith("/.")) target |
|
71 |
else target + "/." |
|
72 |
||
73 |
def append(target: String, rest: String): String = |
|
74 |
if (target.endsWith(":") || target.endsWith("/")) target + rest |
|
75 |
else target + "/" + rest |
|
75523 | 76 |
} |