monochromatic-git-policy-cli 0.1.2

Repository-owned Git policy wrapper: forwards Git commands and checks commits against configured policies.
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
//! What:
//!  The real `index.lock` a landing holds,
//!  created exclusively,
//!  identified by device,
//!  inode and
//!       filesystem,
//!  announced through `index~pid.lock`,
//!  and released only while it is still the file
//!       that was created.
//! Why:
//!  The landing's index install depends on the exact lock file it created;
//!  a replaced lock must
//!      never be deleted,
//!  and a lock this process did not create must never be released
//!      (`src/policy-engine/commit-landing-index-lock.ts`).
//!
//! In TS you'd write (pseudocode):
//! ```ts
//! await using lock = await openRealIndexLock({ realIndexPath, timeoutMs });
//! ```

/// The lock identity record the transaction journal stores, and the numbered record's name.
use super::transaction_journal::{IndexLockRecord, LockIdentity, index_lock_record_filename};
/// The record's exact bytes.
use super::transaction_journal_encode::encode_index_lock;
/// Debug and warning text to the diagnostic log.
use super::diagnostic_log::{debug, warn};
/// The recovery install of a recorded post-index through the exact owned lock, and the release of that lock.
use super::recovery_files::{LockRelease, install_recovered_index, lock_release_name, release_owned_lock};
/// Recovery failures, which the install reports.
use super::recovery_error::RecoveryError;
#[cfg(unix)]
use super::transaction_journal::FileIdentity;
/// The waiting coordination and its failure.
use super::index_writer_coordination::{CoordinationFailure, wait_for_lock};
/// The filesystem identity of the worktree's volume.
#[cfg(unix)]
use super::filesystem_identity::filesystem_identity;
/// Announcing this process through the PID file, the journal record, and the lock's mode.
use super::private_storage::{protect_path, sync_directory, write_private_file};
/// `Path` and `PathBuf` are borrowed and owned filesystem paths.
use std::path::{Path, PathBuf};

/// Module tag for diagnostics.
const TAG: &str = "acquireRealIndexLock";

/// What:
///  A real `index.lock` created by this process.
/// Why:
///   Holding the identity lets the release prove the file is still the one created.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// type RealIndexLock = { lockPath: string; identity: LockIdentity; installIndex(source): Promise<void> };
/// ```
#[derive(Debug, Eq, PartialEq)]
pub struct RealIndexLock {
    /// The lock path,
    ///  `<real index>.lock`.
    lock_path: PathBuf,
    /// The real index the lock guards,
    ///  which an install replaces.
    real_index: PathBuf,
    /// The PID file path,
    ///  `<real index>~pid.lock`.
    pid_path: PathBuf,
    /// The identity recorded when the lock was created.
    identity: LockIdentity,
}

/// What:
///  The lock path of a real index.
/// Why:
///   Git names the lock beside the index.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// `${realIndexPath}.lock`
/// ```
pub fn real_lock_path(real_index: &Path) -> PathBuf {
    let mut name: std::ffi::OsString = real_index.as_os_str().to_owned();
    name.push(".lock");
    return PathBuf::from(name);
}

/// What:
///  The PID file path of a real index.
/// Why:
///   Git's `core.lockfilePid` file lives beside the lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// lockPidPath(realIndexPath) // `${realIndexPath}~pid.lock`
/// ```
pub fn real_pid_path(real_index: &Path) -> PathBuf {
    let mut name: std::ffi::OsString = real_index.as_os_str().to_owned();
    name.push("~pid.lock");
    return PathBuf::from(name);
}

impl RealIndexLock {
    /// What:
    ///  The identity recorded for the lock.
    /// Why:
    ///   The landing journal stores it.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// lock.identity
    /// ```
    pub fn identity(&self) -> &LockIdentity {
        return &self.identity;
    }

    /// What:
    ///  Whether the lock path still names the file this process created.
    /// Why:
    ///   A release or install must not act on a replaced lock.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// await assertLockIdentity({ lockPath, identity });
    /// ```
    pub fn assert_identity(&self) -> Result<(), String> {
        return same_file_identity(self.lock_path.as_path(), &self.identity);
    }

    /// What:
    ///  Release the lock and this process's PID file,
    ///  refusing a lock that was replaced.
    /// Why:
    ///   A replaced lock belongs to another process and stays in place.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// await lock[Symbol.asyncDispose]();
    /// ```
    pub fn release(self) -> Result<(), String> {
        self.assert_identity()?;
        if let Err(error) = std::fs::remove_file(&self.lock_path) {
            return Err(error.to_string());
        }
        return remove_own_pid_file(self.pid_path.as_path());
    }

    /// What:
    ///  Install a computed index through the held lock,
    ///  consuming the lock,
    ///  then remove this process's PID file.
    /// Why:
    ///   `installIndex`:
    ///  the bytes are written into the lock this process created,
    ///  the lock is linked under a private name beside the post-index,
    ///  and that name is renamed over the real index,
    ///  so the new index keeps the lock's owner.
    ///  The lock is not released here on failure:
    ///  once the landing record is durable,
    ///  recovery completes the install through the same lock.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// await lock.installIndex(postIndexPath);
    /// ```
    pub fn install(self, post_index: &Path) -> Result<(), RecoveryError> {
        install_recovered_index(
            self.lock_path.as_path(),
            self.real_index.as_path(),
            post_index,
            &self.identity,
        )?;
        debug(TAG, format!("installed {} through {}", self.real_index.display(), self.lock_path.display()).as_str());
        return remove_own_pid_file(self.pid_path.as_path()).map_err(RecoveryError);
    }

    /// What:
    ///  Release a lock a landing leaves without installing:
    ///  the lock is removed only while it is still the file this process created,
    ///  and the PID file goes either way.
    /// Why:
    ///   The incumbent's disposal warns when the lock is gone or another owner's now
    ///  and still removes its own PID file,
    ///  and throws only for an operating-system error.
    ///
    /// In TS you'd write (pseudocode):
    /// ```ts
    /// await lock[Symbol.asyncDispose]();
    /// ```
    pub fn release_after_landing(self) -> Result<(), String> {
        match release_owned_lock(&self.identity, self.lock_path.as_path()) {
            Ok(LockRelease::Released) => {}
            Ok(other) => warn(
                TAG,
                format!("index lock was not this transaction's at release ({}): {}", lock_release_name(other), self.lock_path.display()).as_str(),
            ),
            Err(error) => return Err(error.0),
        }
        return remove_own_pid_file(self.pid_path.as_path());
    }
}

/// What:
///  Whether a path is a regular file with the recorded device and inode,
///  without following links.
/// Why:
///   Following a link would compare another file's identity.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const metadata = await lstat(path, { bigint: true });
/// ```
#[cfg(unix)]
fn same_file_identity(path: &Path, identity: &LockIdentity) -> Result<(), String> {
    use std::os::unix::fs::MetadataExt;
    let metadata: std::fs::Metadata = match std::fs::symlink_metadata(path) {
        Ok(found) => found,
        Err(error) => return Err(error.to_string()),
    };
    if !metadata.is_file()
        || metadata.dev().to_string() != identity.file.device
        || metadata.ino().to_string() != identity.file.inode
    {
        return Err(format!("the index lock identity changed: {}", path.display()));
    }
    return Ok(());
}

/// What:
///  On platforms without an inode,
///  the identity cannot be checked,
///  so the lock is never trusted.
/// Why:
///   The incumbent refuses a landing without a filesystem identity too.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// throw new TypeError('filesystem identity unavailable');
/// ```
#[cfg(not(unix))]
fn same_file_identity(path: &Path, _identity: &LockIdentity) -> Result<(), String> {
    return Err(format!("the index lock identity cannot be checked here: {}", path.display()));
}

/// What:
///  Create the lock file exclusively with private permissions.
/// Why:
///   `O_EXCL` is the mutual exclusion;
///  an existing file means another owner holds the lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// open(lockPath, O_CREAT | O_EXCL | O_RDWR, 0o600);
/// ```
#[cfg(unix)]
fn create_exclusive(path: &Path) -> Result<bool, String> {
    use std::os::unix::fs::OpenOptionsExt;
    let mut options: std::fs::OpenOptions = std::fs::OpenOptions::new();
    options.write(true).create_new(true).mode(0o600);
    match options.open(path) {
        Ok(_) => return Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
        Err(error) => return Err(error.to_string()),
    }
}

/// What:
///  Create the lock file exclusively,
///  on platforms without Unix modes.
/// Why:
///   Private permissions are not applied there;
///  the identity check still refuses the lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// open(lockPath, O_CREAT | O_EXCL | O_RDWR);
/// ```
#[cfg(not(unix))]
fn create_exclusive(path: &Path) -> Result<bool, String> {
    let mut options: std::fs::OpenOptions = std::fs::OpenOptions::new();
    options.write(true).create_new(true);
    match options.open(path) {
        Ok(_) => return Ok(true),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => return Ok(false),
        Err(error) => return Err(error.to_string()),
    }
}

/// What:
///  The identity of a freshly created lock file.
/// Why:
///   The journal records it;
///  a platform without device and inode numbers records nothing.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// { file: { device: String(stat.dev), inode: String(stat.ino) }, fsId: await resolveFsId(dirname(path)) };
/// ```
#[cfg(unix)]
fn created_identity(lock_path: &Path, real_index: &Path) -> Result<LockIdentity, String> {
    use std::os::unix::fs::MetadataExt;
    let metadata: std::fs::Metadata = match std::fs::symlink_metadata(lock_path) {
        Ok(found) => found,
        Err(error) => return Err(error.to_string()),
    };
    let directory: &Path = real_index.parent().unwrap_or(Path::new("."));
    let fs_id: String = match filesystem_identity(directory) {
        Some(found) => found,
        None => return Err(String::from("the filesystem identity of the worktree is unavailable")),
    };
    return Ok(LockIdentity {
        file: FileIdentity {
            device: metadata.dev().to_string(),
            inode: metadata.ino().to_string(),
        },
        fs_id,
    });
}

/// What:
///  On platforms without device and inode numbers,
///  no identity is recorded and the landing stops.
/// Why:
///   The Windows volume serial is not exposed by the standard library,
///  as the design notes say.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// throw new TypeError('filesystem identity unavailable');
/// ```
#[cfg(not(unix))]
fn created_identity(lock_path: &Path, _real_index: &Path) -> Result<LockIdentity, String> {
    return Err(format!("the index lock identity cannot be recorded here: {}", lock_path.display()));
}

/// What:
///  Remove the PID file when it still names this process.
/// Why:
///   Another process may have announced itself meanwhile.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// if ((await readFile(pidPath, 'utf8')) === `pid ${process.pid}\n`) await rm(pidPath);
/// ```
fn remove_own_pid_file(pid_path: &Path) -> Result<(), String> {
    let own: String = format!("pid {}\n", std::process::id());
    match std::fs::read_to_string(pid_path) {
        Ok(text) => {
            if text == own && let Err(error) = std::fs::remove_file(pid_path) {
                return Err(error.to_string());
            }
            return Ok(());
        }
        Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(()),
        Err(error) => return Err(error.to_string()),
    }
}

/// What:
///  Create the lock exclusively,
///  waiting for a foreign holder under the classification rules,
///  and read the identity of the file created.
/// Why:
///   `openLockWaiting` plus the identity step of the landing;
///  a lock whose identity cannot be recorded is removed again,
///  because nothing proves it is ours.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const handle = await openLockWaiting({ realIndexPath, timeoutMs });
/// ```
fn create_identified_lock(
    real_index: &Path,
    timeout_ms: u64,
    consequence: &str,
) -> Result<(PathBuf, LockIdentity), CoordinationFailure> {
    let lock_path: PathBuf = real_lock_path(real_index);
    loop {
        match create_exclusive(lock_path.as_path()) {
            Ok(true) => break,
            Ok(false) => wait_for_lock(real_index, timeout_ms, consequence)?,
            Err(reason) => return Err(CoordinationFailure::Stop(reason)),
        }
    }
    match created_identity(lock_path.as_path(), real_index) {
        Ok(identity) => return Ok((lock_path, identity)),
        Err(reason) => {
            let _ = std::fs::remove_file(lock_path.as_path());
            return Err(CoordinationFailure::Stop(reason));
        }
    }
}

/// What:
///  Take the real index lock,
///  waiting for a foreign holder under the classification rules,
///  then
///       record its identity and announce this process through the PID file.
/// Why:
///   `openLockWaiting` plus the identity and PID steps of the landing.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// const handle = await openLockWaiting({ realIndexPath, timeoutMs });
/// ```
pub fn acquire_real_index_lock(
    real_index: &Path,
    timeout_ms: u64,
    consequence: &str,
) -> Result<RealIndexLock, CoordinationFailure> {
    let (lock_path, identity): (PathBuf, LockIdentity) = create_identified_lock(real_index, timeout_ms, consequence)?;
    let pid_path: PathBuf = real_pid_path(real_index);
    if let Err(error) = write_private_file(pid_path.as_path(), format!("pid {}\n", std::process::id()).as_bytes()) {
        let _ = std::fs::remove_file(lock_path.as_path());
        return Err(CoordinationFailure::Stop(error.to_string()));
    }
    return Ok(RealIndexLock {
        lock_path,
        real_index: real_index.to_path_buf(),
        pid_path,
        identity,
    });
}

/// What:
///  Write `index-lock-<n>.json` for a lock just created,
///  then make the directory entry durable.
/// Why:
///   Recovery can prove ownership of the lock before any landing record exists,
///  so the record must be durable before the PID file announces the lock.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await writeJournalRecord({ directory: transactionDirectory, filename: indexLockRecordFilename(attempt), record });
/// ```
fn write_lock_record(transaction_directory: &Path, attempt: i64, identity: &LockIdentity) -> Result<(), String> {
    let record: IndexLockRecord = IndexLockRecord {
        attempt,
        lock: identity.clone(),
    };
    let path: PathBuf = transaction_directory.join(index_lock_record_filename(attempt));
    if let Err(error) = write_private_file(path.as_path(), encode_index_lock(&record).as_bytes()) {
        return Err(format!("writing {} failed: {error}", path.display()));
    }
    if let Err(error) = sync_directory(transaction_directory) {
        return Err(format!("syncing {} failed: {error}", transaction_directory.display()));
    }
    return Ok(());
}

/// What:
///  Take the real index lock for a landing attempt:
///  create it,
///  journal its identity in `index-lock-<n>.json`,
///  fix its mode,
///  and only then announce this process through the PID file.
/// Why:
///   `acquireRealIndexLock`:
///  the journal comes before the PID file,
///  so a crash never leaves a PID file naming a lock that recovery cannot prove ours;
///  an existing PID file is left alone,
///  as the incumbent does.
///  A failure after the lock exists removes the lock again.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await using lock = await acquireRealIndexLock({ realIndexPath, transactionDirectory, attempt, timeoutMs });
/// ```
pub fn acquire_landing_index_lock(
    real_index: &Path,
    transaction_directory: &Path,
    attempt: i64,
    timeout_ms: u64,
    consequence: &str,
) -> Result<RealIndexLock, CoordinationFailure> {
    let (lock_path, identity): (PathBuf, LockIdentity) = create_identified_lock(real_index, timeout_ms, consequence)?;
    let announced: Result<(), String> = announce_landing_lock(real_index, transaction_directory, attempt, lock_path.as_path(), &identity);
    if let Err(reason) = announced {
        let _ = std::fs::remove_file(lock_path.as_path());
        return Err(CoordinationFailure::Stop(reason));
    }
    return Ok(RealIndexLock {
        lock_path,
        real_index: real_index.to_path_buf(),
        pid_path: real_pid_path(real_index),
        identity,
    });
}

/// What:
///  The steps between creating a landing's lock and holding it:
///  journal record,
///  mode,
///  PID file.
/// Why:
///   One failure path removes the lock,
///  whichever step failed.
///
/// In TS you'd write (pseudocode):
/// ```ts
/// await writeJournalRecord(...); await protectPath({ path: lockPath, directory: false }); await writeOwnPidFile(pidPath);
/// ```
fn announce_landing_lock(
    real_index: &Path,
    transaction_directory: &Path,
    attempt: i64,
    lock_path: &Path,
    identity: &LockIdentity,
) -> Result<(), String> {
    write_lock_record(transaction_directory, attempt, identity)?;
    if let Err(error) = protect_path(lock_path, 0o600) {
        return Err(format!("protecting {} failed: {error}", lock_path.display()));
    }
    let pid_path: PathBuf = real_pid_path(real_index);
    match write_private_file(pid_path.as_path(), format!("pid {}\n", std::process::id()).as_bytes()) {
        Ok(()) => return Ok(()),
        Err(error) if error.kind() == std::io::ErrorKind::AlreadyExists => {
            debug(TAG, format!("a PID file already exists beside the lock: {}", pid_path.display()).as_str());
            return Ok(());
        }
        Err(error) => return Err(format!("writing {} failed: {error}", pid_path.display())),
    }
}

/// Real-lock controls stay out of the release executable.
#[cfg(test)]
#[path = "real_index_lock_tests.rs"]
mod tests;