-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathmod.rs
More file actions
581 lines (513 loc) · 17.4 KB
/
Copy pathmod.rs
File metadata and controls
581 lines (513 loc) · 17.4 KB
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
//! Utility functions for Rustup
use std::env;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::{self, BufReader, Write};
use std::ops::{BitAnd, BitAndAssign};
use std::path::{Path, PathBuf};
use std::process::ExitStatus;
use anyhow::{Context, Result, anyhow};
use retry::delay::{Fibonacci, jitter};
use retry::{OperationResult, retry};
use tracing::{debug, info, warn};
use url::Url;
use crate::errors::RustupError;
#[cfg(not(windows))]
pub(crate) use crate::utils::raw::find_cmd;
pub(crate) use crate::utils::raw::is_directory;
pub use crate::utils::raw::{is_file, path_exists};
pub(crate) mod notify;
pub mod raw;
pub(crate) mod units;
#[must_use]
#[derive(Debug, PartialEq, Eq)]
pub struct ExitCode(pub i32);
impl ExitCode {
/// Successful execution.
pub const SUCCESS: Self = Self(0);
/// Generic failure.
pub const FAILURE: Self = Self(1);
/// Updates are available.
pub const UPDATES_AVAILABLE: Self = Self(100);
}
impl BitAnd for ExitCode {
type Output = Self;
// If `self` is `0` (success), yield `rhs`.
fn bitand(self, rhs: Self) -> Self::Output {
match self.0 {
0 => rhs,
_ => self,
}
}
}
impl BitAndAssign for ExitCode {
// If `self` is `0` (success), set `self` to `rhs`.
fn bitand_assign(&mut self, rhs: Self) {
if self.0 == 0 {
*self = rhs
}
}
}
impl From<ExitStatus> for ExitCode {
fn from(status: ExitStatus) -> Self {
Self(match status.success() {
true => 0,
false => status.code().unwrap_or(1),
})
}
}
pub fn ensure_dir_exists(name: &'static str, path: &Path) -> Result<bool> {
raw::ensure_dir_exists(path, |_| {
debug!(name, path = %path.display(), "creating directory");
})
.with_context(|| RustupError::CreatingDirectory {
name,
path: PathBuf::from(path),
})
}
pub fn read_file(name: &'static str, path: &Path) -> Result<String> {
fs::read_to_string(path).with_context(|| RustupError::ReadingFile {
name,
path: PathBuf::from(path),
})
}
pub fn write_file(name: &'static str, path: &Path, contents: &str) -> Result<()> {
raw::write_file(path, contents).with_context(|| RustupError::WritingFile {
name,
path: PathBuf::from(path),
})
}
pub(crate) fn append_file(name: &'static str, path: &Path, line: &str) -> Result<()> {
raw::append_file(path, line).with_context(|| RustupError::WritingFile {
name,
path: PathBuf::from(path),
})
}
pub(crate) fn write_line(
name: &'static str,
mut file: impl Write,
path: &Path,
line: &str,
) -> Result<()> {
writeln!(file, "{line}").with_context(|| RustupError::WritingFile {
name,
path: path.to_path_buf(),
})
}
pub(crate) fn write_str(name: &'static str, file: &mut File, path: &Path, s: &str) -> Result<()> {
write!(file, "{s}").with_context(|| RustupError::WritingFile {
name,
path: path.to_path_buf(),
})
}
pub(crate) fn filter_file<F: FnMut(&str) -> bool>(
name: &'static str,
src: &Path,
dest: &Path,
filter: F,
) -> Result<usize> {
raw::filter_file(src, dest, filter).with_context(|| {
format!(
"could not copy {} file from '{}' to '{}'",
name,
src.display(),
dest.display()
)
})
}
pub(crate) fn canonicalize_path(path: &Path) -> PathBuf {
fs::canonicalize(path).unwrap_or_else(|_| {
warn!("could not canonicalize path {}", path.display());
PathBuf::from(path)
})
}
pub(crate) fn parse_url(url: &str) -> Result<Url> {
Url::parse(url).with_context(|| format!("failed to parse url: {url}"))
}
pub(crate) fn assert_is_file(path: &Path) -> Result<()> {
if !is_file(path) {
Err(anyhow!(format!("not a file: '{}'", path.display())))
} else {
Ok(())
}
}
pub(crate) fn assert_is_directory(path: &Path) -> Result<()> {
if !is_directory(path) {
Err(anyhow!(format!("not a directory: '{}'", path.display())))
} else {
Ok(())
}
}
pub(crate) fn symlink_dir(src: &Path, dest: &Path) -> Result<()> {
debug!(source = %src.display(), destination = %dest.display(), "linking directory");
raw::symlink_dir(src, dest).with_context(|| {
format!(
"could not create link from '{}' to '{}'",
src.display(),
dest.display()
)
})
}
/// Attempts to symlink a file, falling back to hard linking if that fails.
///
/// If `dest` already exists then it will be replaced.
pub(crate) fn symlink_or_hardlink_file(src: &Path, dest: &Path) -> Result<()> {
let _ = fs::remove_file(dest);
// Use a relative symlink path if the src and dest are in the same directory.
let symlink_target = if src.parent() == dest.parent() {
src.file_name().map(Path::new).unwrap_or(src)
} else {
src
};
// The error is only used by macos
let Err(_err) = symlink_file(symlink_target, dest) else {
return Ok(());
};
// Some mac filesystems can do hardlinks to symlinks, some can't.
// See rust-lang/rustup#3136 for why it's better never to use them.
#[cfg(target_os = "macos")]
if fs::symlink_metadata(src)
.map(|m| m.file_type().is_symlink())
.unwrap_or(false)
{
return Err(_err);
}
hardlink_file(src, dest)
}
pub fn hardlink_file(src: &Path, dest: &Path) -> Result<()> {
fs::hard_link(src, dest).with_context(|| RustupError::LinkingFile {
src: PathBuf::from(src),
dest: PathBuf::from(dest),
})
}
#[cfg(unix)]
fn symlink_file(src: &Path, dest: &Path) -> Result<()> {
std::os::unix::fs::symlink(src, dest).with_context(|| RustupError::LinkingFile {
src: PathBuf::from(src),
dest: PathBuf::from(dest),
})
}
#[cfg(windows)]
fn symlink_file(src: &Path, dest: &Path) -> Result<()> {
std::os::windows::fs::symlink_file(src, dest).with_context(|| RustupError::LinkingFile {
src: PathBuf::from(src),
dest: PathBuf::from(dest),
})
}
pub(crate) fn copy_dir(src: &Path, dest: &Path) -> Result<()> {
debug!(source = %src.display(), destination = %dest.display(), "copying directory");
raw::copy_dir(src, dest).with_context(|| {
format!(
"could not copy directory from '{}' to '{}'",
src.display(),
dest.display()
)
})
}
/// Copy a file from `src` to `dst`, preserving the symlink target if `src` is a symlink.
/// This is the default behavior for component installation.
pub(crate) fn copy_file(src: &Path, dest: &Path) -> Result<()> {
copy_file_impl(src, dest, true)
}
/// Copy a file from `src` to `dst`, or if `src` is a symlink, create a new symlink
/// at `dst` pointing to it.
/// Used for self-update where we want to preserve the symlink to the original location.
pub(crate) fn copy_file_symlink_to_source(src: &Path, dest: &Path) -> Result<()> {
copy_file_impl(src, dest, false)
}
fn copy_file_impl(src: &Path, dest: &Path, preserve_symlink: bool) -> Result<()> {
let metadata = fs::symlink_metadata(src).with_context(|| RustupError::ReadingFile {
name: "metadata for",
path: PathBuf::from(src),
})?;
if metadata.file_type().is_symlink() {
let target = if preserve_symlink {
&fs::read_link(src).with_context(|| RustupError::ReadingFile {
name: "symlink target for",
path: PathBuf::from(src),
})?
} else {
src
};
symlink_file(target, dest).map(|_| ())
} else {
fs::copy(src, dest)
.with_context(|| {
format!(
"could not copy file from '{}' to '{}'",
src.display(),
dest.display()
)
})
.map(|_| ())
}
}
pub(crate) fn remove_dir(name: &'static str, path: &Path) -> Result<()> {
debug!(name, path = %path.display(), "removing directory");
raw::remove_dir(path).with_context(|| RustupError::RemovingDirectory {
name,
path: PathBuf::from(path),
})
}
pub fn remove_file(name: &'static str, path: &Path) -> Result<()> {
// Most files we go to remove won't ever be in use. Some, like proxies, may
// be for indefinite periods, and this will mean we are slower to error and
// have the user fix the issue. Others, like the setup binary, are
// transiently in use, and this wait loop will fix the issue transparently
// for a rare performance hit.
retry(
Fibonacci::from_millis(1).map(jitter).take(10),
|| match fs::remove_file(path) {
Ok(()) => OperationResult::Ok(()),
Err(e) => match e.kind() {
io::ErrorKind::PermissionDenied => OperationResult::Retry(e),
_ => OperationResult::Err(e),
},
},
)
.with_context(|| RustupError::RemovingFile {
name,
path: PathBuf::from(path),
})
}
pub(crate) fn ensure_file_removed(name: &'static str, path: &Path) -> Result<()> {
let result = remove_file(name, path);
if let Err(err) = &result
&& let Some(retry::Error { error: e, .. }) = err.downcast_ref::<retry::Error<io::Error>>()
&& e.kind() == io::ErrorKind::NotFound
{
return Ok(());
}
result.with_context(|| RustupError::RemovingFile {
name,
path: PathBuf::from(path),
})
}
pub(crate) fn read_dir(name: &'static str, path: &Path) -> Result<fs::ReadDir> {
fs::read_dir(path).with_context(|| RustupError::ReadingDirectory {
name,
path: PathBuf::from(path),
})
}
pub(crate) fn open_browser(path: impl AsRef<OsStr>) -> Result<()> {
opener::open_browser(path).context("couldn't open browser")
}
#[cfg(not(windows))]
fn set_permissions(path: &Path, perms: fs::Permissions) -> Result<()> {
fs::set_permissions(path, perms).map_err(|e| {
RustupError::SettingPermissions {
p: PathBuf::from(path),
source: e,
}
.into()
})
}
pub fn file_size(path: &Path) -> Result<u64> {
Ok(fs::metadata(path)
.with_context(|| RustupError::ReadingFile {
name: "metadata for",
path: PathBuf::from(path),
})?
.len())
}
pub(crate) fn make_executable(path: &Path) -> Result<()> {
#[allow(clippy::unnecessary_wraps)]
#[cfg(windows)]
fn inner(_: &Path) -> Result<()> {
Ok(())
}
#[cfg(not(windows))]
fn inner(path: &Path) -> Result<()> {
use std::os::unix::fs::PermissionsExt;
let metadata = fs::metadata(path).map_err(|e| RustupError::SettingPermissions {
p: PathBuf::from(path),
source: e,
})?;
let mut perms = metadata.permissions();
let mode = perms.mode();
let new_mode = (mode & !0o777) | 0o755;
// Check if permissions are ok already - #1638
if mode == new_mode {
return Ok(());
}
perms.set_mode(new_mode);
set_permissions(path, perms)
}
inner(path)
}
pub fn current_exe() -> Result<PathBuf> {
env::current_exe().context(RustupError::LocatingWorkingDir)
}
pub(crate) fn format_path_for_display(path: &str) -> String {
let unc_present = path.find(r"\\?\");
match unc_present {
None => path.to_owned(),
Some(_) => path[4..].to_owned(),
}
}
#[cfg(target_os = "linux")]
fn copy_and_delete(name: &'static str, src: &Path, dest: &Path) -> Result<()> {
// https://github.com/rust-lang/rustup/issues/1239
// This uses std::fs::copy() instead of the faster std::fs::rename() to
// avoid cross-device link errors.
if src.is_dir() {
copy_dir(src, dest).and(remove_dir_all::remove_dir_all(src).with_context(|| {
RustupError::RemovingDirectory {
name,
path: PathBuf::from(src),
}
}))
} else {
copy_file(src, dest).and(remove_file(name, src))
}
}
pub fn rename(
name: &'static str,
src: &Path,
dest: &Path,
#[allow(unused_variables)] // Only used on Linux
permit_copy_rename: bool,
) -> Result<()> {
// https://github.com/rust-lang/rustup/issues/1870
// 21 fib steps from 1 sums to ~28 seconds, hopefully more than enough
// for our previous poor performance that avoided the race condition with
// McAfee and Norton.
#[cfg(target_os = "linux")]
use libc::EXDEV;
retry(
Fibonacci::from_millis(1).map(jitter).take(26),
|| match fs::rename(src, dest) {
Ok(()) => OperationResult::Ok(()),
Err(e) => match e.kind() {
io::ErrorKind::PermissionDenied => {
// Renaming encountered a file in use error and is retrying.
// The InUse aspect is a heuristic - the OS specifies
// Permission denied, but as we work in users home dirs and
// running programs like virus scanner are known to cause this
// the heuristic is quite good.
info!("retrying renaming {} to {}", src.display(), dest.display());
OperationResult::Retry(e)
}
#[cfg(target_os = "linux")]
_ if permit_copy_rename && Some(EXDEV) == e.raw_os_error() => {
match copy_and_delete(name, src, dest) {
Ok(()) => OperationResult::Ok(()),
Err(_) => OperationResult::Err(e),
}
}
_ => OperationResult::Err(e),
},
},
)
.map_err(|e| {
RustupError::RenamingFile {
name,
src: PathBuf::from(src),
dest: PathBuf::from(dest),
source: e.error,
}
.into()
})
}
pub(crate) fn delete_dir_contents_following_links(dir_path: &Path) {
use remove_dir_all::RemoveDir;
match raw::open_dir_following_links(dir_path).and_then(|mut p| p.remove_dir_contents(None)) {
Err(e) if e.kind() != io::ErrorKind::NotFound => {
warn!("unable to clean up {}: {e}", dir_path.display());
}
_ => {}
}
}
pub(crate) fn buffered(path: &Path) -> Result<BufReader<File>, anyhow::Error> {
match File::open(path) {
Ok(fh) => Ok(BufReader::with_capacity(8 * 1024 * 1024, fh)),
Err(_) => Err(anyhow!(RustupError::ReadingFile {
name: "downloaded",
path: path.to_path_buf(),
})),
}
}
// search user database to get home dir of euid user
#[cfg(unix)]
pub(crate) fn home_dir_from_passwd() -> Option<PathBuf> {
use std::ffi::{CStr, OsString};
use std::mem::MaybeUninit;
use std::os::unix::ffi::OsStringExt;
use std::ptr;
unsafe {
let init_size = match libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) {
-1 => 1024,
n => n as usize,
};
let mut buf = Vec::with_capacity(init_size);
let mut pwd: MaybeUninit<libc::passwd> = MaybeUninit::uninit();
let mut pwdp = ptr::null_mut();
match libc::getpwuid_r(
libc::geteuid(),
pwd.as_mut_ptr(),
buf.as_mut_ptr(),
buf.capacity(),
&mut pwdp,
) {
0 if !pwdp.is_null() => {
let pwd = pwd.assume_init();
let bytes = CStr::from_ptr(pwd.pw_dir).to_bytes().to_vec();
let pw_dir = OsString::from_vec(bytes);
Some(PathBuf::from(pw_dir))
}
_ => None,
}
}
}
#[cfg(unix)]
pub(crate) fn disk_free(path: impl AsRef<OsStr>) -> Result<u64> {
use libc::statvfs;
use std::mem::MaybeUninit;
use std::os::unix::ffi::OsStrExt;
let mut os_path = path.as_ref().as_bytes().to_vec();
os_path.push(0);
let mut stat = MaybeUninit::<statvfs>::uninit();
match unsafe { statvfs(os_path.as_ptr() as *const _, stat.as_mut_ptr()) } {
// bit width of f_bavail and f_bsize may differ on platforms and sometimes u32
#[allow(clippy::useless_conversion)]
0 => {
let stat = unsafe { stat.assume_init() };
let available_blocks: u64 = stat.f_bavail.into();
let block_size: u64 = stat.f_bsize.into();
Ok(available_blocks.saturating_mul(block_size))
}
_ => anyhow::bail!("failed to acquire block size"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_remove_file() {
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
let f_path = tempdir.path().join("f");
File::create(&f_path).unwrap();
assert!(f_path.exists());
assert!(remove_file("f", &f_path).is_ok());
assert!(!f_path.exists());
let result = remove_file("f", &f_path);
let err = result.unwrap_err();
match err.downcast_ref::<RustupError>() {
Some(RustupError::RemovingFile { name, path }) => {
assert_eq!(*name, "f");
assert_eq!(path.clone(), f_path);
}
_ => panic!("Expected an error removing file"),
}
}
#[test]
fn test_ensure_file_removed() {
let tempdir = tempfile::Builder::new().prefix("rustup").tempdir().unwrap();
let f_path = tempdir.path().join("f");
File::create(&f_path).unwrap();
assert!(f_path.exists());
assert!(ensure_file_removed("f", &f_path).is_ok());
assert!(!f_path.exists());
assert!(ensure_file_removed("f", &f_path).is_ok());
}
}