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
//! sync - synchronize cached writes to persistent storage
//!
//! POSIX.1-2017 compliant implementation.
//! Reference: https://pubs.opengroup.org/onlinepubs/9699919799/utilities/sync.html
/// sync - synchronize cached writes to persistent storage
///
/// # Synopsis
/// ```text
/// sync
/// ```
///
/// # Description
/// Force completion of pending disk writes (flush cache).
/// The sync utility writes all information in memory that should be on disk,
/// including modified superblocks, modified inodes, and delayed reads and writes.
///
/// # Exit Status
/// - 0: Success (always)
pub fn sync_cmd(_argc: i32, _argv: *const *const u8) -> i32 {
unsafe { libc::sync() };
0
}
#[cfg(test)]
mod tests {
extern crate std;
use std::process::Command;
use std::path::PathBuf;
fn get_armybox_path() -> PathBuf {
if let Ok(path) = std::env::var("ARMYBOX_PATH") {
return PathBuf::from(path);
}
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| std::env::current_dir().unwrap());
let release = manifest_dir.join("target/release/armybox");
if release.exists() { return release; }
manifest_dir.join("target/debug/armybox")
}
#[test]
fn test_sync_basic() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["sync"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
}
#[test]
fn test_sync_no_output() {
let armybox = get_armybox_path();
if !armybox.exists() { return; }
let output = Command::new(&armybox)
.args(["sync"])
.output()
.unwrap();
assert_eq!(output.status.code(), Some(0));
// sync should produce no output
assert!(output.stdout.is_empty());
}
}