Skip to main content

cargo/util/
mod.rs

1use std::path::{Path, PathBuf};
2use std::time::Duration;
3
4pub use self::canonical_url::CanonicalUrl;
5pub use self::context::{ConfigValue, GlobalContext, homedir};
6pub(crate) use self::counter::MetricsCounter;
7pub use self::dependency_queue::DependencyQueue;
8pub use self::diagnostic_server::RustfixDiagnosticServer;
9pub use self::edit_distance::{closest, closest_msg, edit_distance};
10pub use self::errors::CliError;
11pub use self::errors::{CargoResult, CliResult, internal};
12pub use self::flock::{FileLock, Filesystem};
13pub use self::graph::Graph;
14pub use self::hasher::StableHasher;
15pub use self::hex::{hash_u64, short_hash, to_hex};
16pub use self::into_url::IntoUrl;
17pub use self::into_url_with_base::IntoUrlWithBase;
18pub(crate) use self::io::LimitErrorReader;
19pub use self::lockserver::{LockServer, LockServerClient, LockServerStarted};
20pub use self::logger::BuildLogger;
21pub use self::once::OnceExt;
22pub use self::progress::{Progress, ProgressStyle};
23pub use self::queue::Queue;
24pub use self::rustc::Rustc;
25pub use self::semver_ext::{OptVersionReq, VersionExt};
26pub use self::unhashed::Unhashed;
27pub use self::vcs::{FossilRepo, GitRepo, HgRepo, PijulRepo, existing_vcs_repo};
28pub use self::workspace::{
29    add_path_args, path_args, print_available_benches, print_available_binaries,
30    print_available_examples, print_available_packages, print_available_tests,
31};
32
33pub mod auth;
34pub mod cache_lock;
35mod canonical_url;
36pub mod command_prelude;
37pub mod context;
38mod counter;
39pub mod cpu;
40pub mod credential;
41mod dependency_queue;
42pub mod diagnostic_server;
43pub mod edit_distance;
44pub mod errors;
45pub mod flock;
46pub mod frontmatter;
47pub mod graph;
48mod hasher;
49pub mod hex;
50pub mod important_paths;
51pub mod interning;
52pub mod into_url;
53mod into_url_with_base;
54mod io;
55pub mod job;
56mod local_poll_adapter;
57pub use local_poll_adapter::LocalPollAdapter;
58mod lockserver;
59pub mod log_message;
60pub mod logger;
61pub mod machine_message;
62pub mod network;
63mod once;
64pub mod open;
65mod progress;
66mod queue;
67pub mod restricted_names;
68pub mod rustc;
69mod semver_eval_ext;
70mod semver_ext;
71pub mod sqlite;
72pub mod toml;
73pub mod toml_mut;
74mod unhashed;
75mod vcs;
76mod workspace;
77
78pub use cargo_util_terminal::style;
79pub(crate) use futures::executor::block_on;
80pub(crate) use futures::executor::block_on_stream;
81
82pub fn is_rustup() -> bool {
83    #[expect(clippy::disallowed_methods, reason = "consistency with rustup")]
84    std::env::var_os("RUSTUP_HOME").is_some()
85}
86
87pub fn elapsed(duration: Duration) -> String {
88    let secs = duration.as_secs();
89
90    if secs >= 60 {
91        format!("{}m {:02}s", secs / 60, secs % 60)
92    } else {
93        format!("{}.{:02}s", secs, duration.subsec_nanos() / 10_000_000)
94    }
95}
96
97/// Formats a number of bytes into a human readable SI-prefixed size.
98pub struct HumanBytes(pub u64);
99
100impl std::fmt::Display for HumanBytes {
101    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
102        const UNITS: [&str; 7] = ["B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];
103        let bytes = self.0 as f32;
104        let i = ((bytes.log2() / 10.0) as usize).min(UNITS.len() - 1);
105        let unit = UNITS[i];
106        let size = bytes / 1024_f32.powi(i as i32);
107
108        // Don't show a fractional number of bytes.
109        if i == 0 {
110            return write!(f, "{size}{unit}");
111        }
112
113        let Some(precision) = f.precision() else {
114            return write!(f, "{size}{unit}");
115        };
116        write!(f, "{size:.precision$}{unit}",)
117    }
118}
119
120pub fn indented_lines(text: &str) -> String {
121    text.lines()
122        .map(|line| {
123            if line.is_empty() {
124                String::from("\n")
125            } else {
126                format!("  {}\n", line)
127            }
128        })
129        .collect()
130}
131
132pub fn truncate_with_ellipsis(s: &str, max_width: usize) -> String {
133    // We should truncate at grapheme-boundary and compute character-widths,
134    // yet the dependencies on unicode-segmentation and unicode-width are
135    // not worth it.
136    let mut chars = s.chars();
137    let mut prefix = (&mut chars).take(max_width - 1).collect::<String>();
138    if chars.next().is_some() {
139        prefix.push('…');
140    }
141    prefix
142}
143
144#[cfg(not(windows))]
145#[inline]
146pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
147    std::fs::canonicalize(&path)
148}
149
150#[cfg(windows)]
151#[inline]
152pub fn try_canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
153    use std::io::Error;
154    use std::io::ErrorKind;
155
156    // On Windows `canonicalize` may fail, so we fall back to getting an absolute path.
157    std::fs::canonicalize(&path).or_else(|_| {
158        // Return an error if a file does not exist for better compatibility with `canonicalize`
159        if !path.as_ref().try_exists()? {
160            return Err(Error::new(ErrorKind::NotFound, "the path was not found"));
161        }
162        std::path::absolute(&path)
163    })
164}
165
166/// Get the current [`umask`] value.
167///
168/// [`umask`]: https://man7.org/linux/man-pages/man2/umask.2.html
169#[cfg(unix)]
170pub fn get_umask() -> u32 {
171    use std::sync::OnceLock;
172    static UMASK: OnceLock<libc::mode_t> = OnceLock::new();
173    // SAFETY: Syscalls are unsafe. Calling `umask` twice is even unsafer for
174    // multithreading program, since it doesn't provide a way to retrieve the
175    // value without modifications. We use a static `OnceLock` here to ensure
176    // it only gets call once during the entire program lifetime.
177    *UMASK.get_or_init(|| unsafe {
178        let umask = libc::umask(0o022);
179        libc::umask(umask);
180        umask
181    }) as u32 // it is u16 on macos
182}
183
184#[cfg(test)]
185mod test {
186    use super::*;
187
188    #[track_caller]
189    fn t(bytes: u64, expected: &str) {
190        assert_eq!(&HumanBytes(bytes).to_string(), expected);
191    }
192
193    #[test]
194    fn test_human_readable_bytes() {
195        t(0, "0B");
196        t(8, "8B");
197        t(1000, "1000B");
198        t(1024, "1KiB");
199        t(1024 * 420 + 512, "420.5KiB");
200        t(1024 * 1024, "1MiB");
201        t(1024 * 1024 + 1024 * 256, "1.25MiB");
202        t(1024 * 1024 * 1024, "1GiB");
203        t((1024. * 1024. * 1024. * 1.2345) as u64, "1.2345GiB");
204        t(1024 * 1024 * 1024 * 1024, "1TiB");
205        t(1024 * 1024 * 1024 * 1024 * 1024, "1PiB");
206        t(1024 * 1024 * 1024 * 1024 * 1024 * 1024, "1EiB");
207        t(u64::MAX, "16EiB");
208
209        assert_eq!(
210            &format!("{:.3}", HumanBytes((1024. * 1.23456) as u64)),
211            "1.234KiB"
212        );
213    }
214}