foch 0.0.1

EU4 mod analysis and merge toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
use super::super::super::error::MergeError;
use remove_dir_all::RemoveDir;
use std::ffi::OsString;
use std::fs::{self, File, OpenOptions};
use std::io;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

static OUTPUT_TRANSACTION_COUNTER: AtomicU64 = AtomicU64::new(0);

/// Commits a fully staged output tree without exposing partial contents.
///
/// This protects process-level atomicity and cooperating-writer serialization.
/// It does not fsync staged files or parent directories, so it is not a
/// power-loss durability transaction.
pub(crate) struct OutputTransaction {
	final_dir: PathBuf,
	initial_target: OutputTargetState,
	staging_identity: OutputDirectoryIdentity,
	staging_dir: Option<PathBuf>,
	_lock_file: File,
}

#[derive(Debug, Eq, PartialEq)]
enum OutputTargetState {
	Missing,
	Directory(OutputDirectoryIdentity),
}

#[derive(Debug, Eq, PartialEq)]
struct OutputDirectoryIdentity(same_file::Handle);

impl OutputTransaction {
	pub(crate) fn begin(final_dir: &Path) -> Result<Self, MergeError> {
		ensure_transaction_platform_supported()?;
		let parent = output_parent(final_dir);
		fs::create_dir_all(parent)?;
		let lock_file = acquire_output_lock(final_dir)?;
		let initial_target =
			normalize_empty_output_target(final_dir, inspect_output_target(final_dir)?)?;
		ensure_target_replacement_supported(&initial_target)?;
		let staging_dir = create_unique_sibling_dir(final_dir, "staging")?;
		let staging_identity = match inspect_output_target(&staging_dir) {
			Ok(OutputTargetState::Directory(identity)) => identity,
			Ok(OutputTargetState::Missing) => unreachable!("new staging directory disappeared"),
			Err(error) => {
				eprintln!(
					"[merge] warning: could not verify ownership of new staging directory {}; leaving it untouched: {error}",
					staging_dir.display()
				);
				return Err(error);
			}
		};
		Ok(Self {
			final_dir: final_dir.to_path_buf(),
			initial_target,
			staging_identity,
			staging_dir: Some(staging_dir),
			_lock_file: lock_file,
		})
	}

	pub(crate) fn staging_dir(&self) -> &Path {
		self.staging_dir
			.as_deref()
			.expect("committed output transaction has no staging directory")
	}

	pub(crate) fn prior_dir(&self) -> Option<&Path> {
		matches!(self.initial_target, OutputTargetState::Directory(_))
			.then_some(self.final_dir.as_path())
	}

	pub(crate) fn commit(mut self) -> Result<(), MergeError> {
		let staging_dir = self
			.staging_dir
			.as_ref()
			.cloned()
			.expect("output transaction can only be committed once");
		let current_target = inspect_output_target(&self.final_dir)?;
		if current_target != self.initial_target {
			return Err(output_target_changed_error(&self.final_dir));
		}

		let OutputTargetState::Directory(expected_identity) = current_target else {
			if let Err(error) = rename_output_noreplace(&staging_dir, &self.final_dir) {
				return Err(transaction_error(
					error,
					format!("failed to commit new output {}", self.final_dir.display()),
				));
			}
			self.staging_dir = None;
			return Ok(());
		};

		if let Err(error) = exchange_output_paths(&staging_dir, &self.final_dir) {
			return Err(transaction_error(
				error,
				format!(
					"failed to atomically commit output {}",
					self.final_dir.display()
				),
			));
		}

		match inspect_output_target(&staging_dir) {
			Ok(OutputTargetState::Directory(identity)) if identity == expected_identity => {
				self.staging_identity = expected_identity;
				self.cleanup_staging("committed output backup");
			}
			Ok(observed) => {
				self.staging_dir = None;
				return Err(MergeError::Io(io::Error::new(
					io::ErrorKind::InvalidInput,
					format!(
						"merge output {} changed during atomic commit; the displaced target ({observed:?}) was preserved at {} and the staged output is now committed",
						self.final_dir.display(),
						staging_dir.display()
					),
				)));
			}
			Err(error) => {
				self.staging_dir = None;
				let error_kind = match &error {
					MergeError::Io(error) => error.kind(),
					_ => io::ErrorKind::Other,
				};
				return Err(MergeError::Io(io::Error::new(
					error_kind,
					format!(
						"merge output {} was committed, but the displaced target at {} could not be verified and was left untouched: {error}",
						self.final_dir.display(),
						staging_dir.display()
					),
				)));
			}
		}
		Ok(())
	}

	fn cleanup_staging(&mut self, description: &str) {
		let Some(staging_dir) = self.staging_dir.as_deref() else {
			return;
		};
		match remove_owned_output_directory(staging_dir, &mut self.staging_identity) {
			Ok(OwnedCleanup::Removed | OwnedCleanup::Missing) => self.staging_dir = None,
			Ok(OwnedCleanup::NotOwned) => {
				eprintln!(
					"[merge] warning: transaction no longer owns {description} {}; leaving it untouched",
					staging_dir.display()
				);
				self.staging_dir = None;
			}
			Err(error) => eprintln!(
				"[merge] warning: failed to clean {description} {}: {error}",
				staging_dir.display()
			),
		}
	}
}

impl Drop for OutputTransaction {
	fn drop(&mut self) {
		self.cleanup_staging("transaction staging directory");
	}
}

#[derive(Clone, Copy, Debug, Eq, PartialEq)]
enum OwnedCleanup {
	Removed,
	Missing,
	NotOwned,
}

fn remove_owned_output_directory(
	path: &Path,
	expected_identity: &mut OutputDirectoryIdentity,
) -> io::Result<OwnedCleanup> {
	let metadata = match fs::symlink_metadata(path) {
		Ok(metadata) => metadata,
		Err(error) if error.kind() == io::ErrorKind::NotFound => {
			return Ok(OwnedCleanup::Missing);
		}
		Err(error) => return Err(error),
	};
	if !metadata.file_type().is_dir() {
		return Ok(OwnedCleanup::NotOwned);
	}
	let current_identity = OutputDirectoryIdentity(same_file::Handle::from_path(path)?);
	if &current_identity != expected_identity {
		return Ok(OwnedCleanup::NotOwned);
	}
	expected_identity
		.0
		.as_file_mut()
		.remove_dir_contents(Some(path))?;
	let current_identity = OutputDirectoryIdentity(same_file::Handle::from_path(path)?);
	if &current_identity != expected_identity {
		return Ok(OwnedCleanup::NotOwned);
	}
	fs::remove_dir(path)?;
	Ok(OwnedCleanup::Removed)
}

fn acquire_output_lock(final_dir: &Path) -> Result<File, MergeError> {
	let lock_path = sibling_control_path(final_dir, "lock")?;
	let lock_file = OpenOptions::new()
		.read(true)
		.write(true)
		.create(true)
		.truncate(false)
		.open(&lock_path)
		.map_err(|error| {
			transaction_error(
				error,
				format!(
					"failed to open output transaction lock {}",
					lock_path.display()
				),
			)
		})?;
	lock_file.lock().map_err(|error| {
		transaction_error(
			error,
			format!("failed to lock output transaction {}", lock_path.display()),
		)
	})?;
	Ok(lock_file)
}

fn create_unique_sibling_dir(final_dir: &Path, kind: &str) -> Result<PathBuf, MergeError> {
	for _ in 0..1_024 {
		let candidate = unique_sibling_path(final_dir, kind)?;
		match fs::create_dir(&candidate) {
			Ok(()) => return Ok(candidate),
			Err(error) if error.kind() == io::ErrorKind::AlreadyExists => {}
			Err(error) => return Err(MergeError::Io(error)),
		}
	}
	Err(MergeError::Io(io::Error::new(
		io::ErrorKind::AlreadyExists,
		format!(
			"failed to allocate sibling staging directory for {}",
			final_dir.display()
		),
	)))
}

fn unique_sibling_path(final_dir: &Path, kind: &str) -> Result<PathBuf, MergeError> {
	let file_name = final_dir.file_name().ok_or_else(|| {
		MergeError::Io(io::Error::new(
			io::ErrorKind::InvalidInput,
			format!(
				"merge output must name a directory with a sibling: {}",
				final_dir.display()
			),
		))
	})?;
	let nonce = OUTPUT_TRANSACTION_COUNTER.fetch_add(1, Ordering::Relaxed);
	let mut sibling_name = OsString::from(".");
	sibling_name.push(file_name);
	sibling_name.push(format!(".foch-{kind}-{}-{nonce}", std::process::id()));
	Ok(output_parent(final_dir).join(sibling_name))
}

fn sibling_control_path(final_dir: &Path, kind: &str) -> Result<PathBuf, MergeError> {
	let file_name = final_dir.file_name().ok_or_else(|| {
		MergeError::Io(io::Error::new(
			io::ErrorKind::InvalidInput,
			format!(
				"merge output must name a directory with a sibling: {}",
				final_dir.display()
			),
		))
	})?;
	let mut sibling_name = OsString::from(".");
	sibling_name.push(file_name);
	sibling_name.push(format!(".foch-{kind}"));
	Ok(output_parent(final_dir).join(sibling_name))
}

fn output_parent(path: &Path) -> &Path {
	path.parent()
		.filter(|parent| !parent.as_os_str().is_empty())
		.unwrap_or_else(|| Path::new("."))
}

fn inspect_output_target(path: &Path) -> Result<OutputTargetState, MergeError> {
	match fs::symlink_metadata(path) {
		Ok(metadata) if metadata.file_type().is_dir() => Ok(OutputTargetState::Directory(
			OutputDirectoryIdentity(same_file::Handle::from_path(path)?),
		)),
		Ok(_) => Err(MergeError::Io(io::Error::new(
			io::ErrorKind::InvalidInput,
			format!(
				"merge output must be a real directory or not exist: {}",
				path.display()
			),
		))),
		Err(error) if error.kind() == io::ErrorKind::NotFound => Ok(OutputTargetState::Missing),
		Err(error) => Err(MergeError::Io(error)),
	}
}

fn normalize_empty_output_target(
	path: &Path,
	target: OutputTargetState,
) -> Result<OutputTargetState, MergeError> {
	let OutputTargetState::Directory(_) = &target else {
		return Ok(target);
	};
	let mut entries = fs::read_dir(path)?;
	if let Some(entry) = entries.next() {
		entry?;
		return Ok(target);
	}
	drop(entries);
	if inspect_output_target(path)? != target {
		return Err(output_target_changed_error(path));
	}
	fs::remove_dir(path)?;
	Ok(OutputTargetState::Missing)
}

fn output_target_changed_error(path: &Path) -> MergeError {
	MergeError::Io(io::Error::new(
		io::ErrorKind::InvalidInput,
		format!(
			"merge output changed while the replacement was staged: {}",
			path.display()
		),
	))
}

#[cfg(target_vendor = "apple")]
fn rename_output_noreplace(source: &Path, target: &Path) -> io::Result<()> {
	rename_output_with_flags(source, target, rustix::fs::RenameFlags::NOREPLACE)
}

#[cfg(target_vendor = "apple")]
fn exchange_output_paths(source: &Path, target: &Path) -> io::Result<()> {
	rename_output_with_flags(source, target, rustix::fs::RenameFlags::EXCHANGE)
}

#[cfg(any(target_os = "linux", target_os = "android", target_os = "redox"))]
fn rename_output_noreplace(source: &Path, target: &Path) -> io::Result<()> {
	rename_output_with_flags(source, target, rustix::fs::RenameFlags::NOREPLACE)
}

#[cfg(any(target_os = "linux", target_os = "android"))]
fn exchange_output_paths(source: &Path, target: &Path) -> io::Result<()> {
	rename_output_with_flags(source, target, rustix::fs::RenameFlags::EXCHANGE)
}

#[cfg(any(
	target_vendor = "apple",
	target_os = "linux",
	target_os = "android",
	target_os = "redox"
))]
fn rename_output_with_flags(
	source: &Path,
	target: &Path,
	flags: rustix::fs::RenameFlags,
) -> io::Result<()> {
	rustix::fs::renameat_with(rustix::fs::CWD, source, rustix::fs::CWD, target, flags)
		.map_err(io::Error::from)
}

#[cfg(target_os = "windows")]
fn rename_output_noreplace(source: &Path, target: &Path) -> io::Result<()> {
	use std::os::windows::ffi::OsStrExt;
	use windows_sys::Win32::Storage::FileSystem::MoveFileExW;

	let source_wide: Vec<u16> = source.as_os_str().encode_wide().chain(Some(0)).collect();
	let target_wide: Vec<u16> = target.as_os_str().encode_wide().chain(Some(0)).collect();
	// Omitting MOVEFILE_REPLACE_EXISTING makes this a no-replace move.
	let moved = unsafe { MoveFileExW(source_wide.as_ptr(), target_wide.as_ptr(), 0) };
	if moved == 0 {
		Err(io::Error::last_os_error())
	} else {
		Ok(())
	}
}

#[cfg(any(target_os = "windows", target_os = "redox"))]
fn exchange_output_paths(source: &Path, target: &Path) -> io::Result<()> {
	let _ = (source, target);
	Err(io::Error::new(
		io::ErrorKind::Unsupported,
		"atomic replacement of an existing output directory is unsupported on this platform",
	))
}

#[cfg(not(any(
	target_vendor = "apple",
	target_os = "linux",
	target_os = "android",
	target_os = "redox",
	target_os = "windows"
)))]
fn rename_output_noreplace(_source: &Path, _target: &Path) -> io::Result<()> {
	Err(io::Error::new(
		io::ErrorKind::Unsupported,
		"atomic no-replace directory commit is unsupported on this platform",
	))
}

#[cfg(not(any(
	target_vendor = "apple",
	target_os = "linux",
	target_os = "android",
	target_os = "redox",
	target_os = "windows"
)))]
fn exchange_output_paths(_source: &Path, _target: &Path) -> io::Result<()> {
	Err(io::Error::new(
		io::ErrorKind::Unsupported,
		"transactional directory replacement is unsupported on this platform",
	))
}

#[cfg(any(
	target_vendor = "apple",
	target_os = "linux",
	target_os = "android",
	target_os = "redox",
	target_os = "windows"
))]
fn ensure_transaction_platform_supported() -> Result<(), MergeError> {
	Ok(())
}

#[cfg(not(any(
	target_vendor = "apple",
	target_os = "linux",
	target_os = "android",
	target_os = "redox",
	target_os = "windows"
)))]
fn ensure_transaction_platform_supported() -> Result<(), MergeError> {
	Err(MergeError::Io(io::Error::new(
		io::ErrorKind::Unsupported,
		"transactional directory commit is unsupported on this platform",
	)))
}

#[cfg(any(target_os = "windows", target_os = "redox"))]
fn ensure_target_replacement_supported(target: &OutputTargetState) -> Result<(), MergeError> {
	if matches!(target, OutputTargetState::Directory(_)) {
		return Err(MergeError::Io(io::Error::new(
			io::ErrorKind::Unsupported,
			"atomic replacement of an existing output directory is unsupported on this platform",
		)));
	}
	Ok(())
}

#[cfg(not(any(target_os = "windows", target_os = "redox")))]
fn ensure_target_replacement_supported(_target: &OutputTargetState) -> Result<(), MergeError> {
	Ok(())
}

fn transaction_error(error: io::Error, context: String) -> MergeError {
	MergeError::Io(io::Error::new(error.kind(), format!("{context}: {error}")))
}