Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 7bd7729

Browse files
committed
run_make_support: move fs helpers to own module
1 parent 1a92b11 commit 7bd7729

File tree

2 files changed

+75
-65
lines changed

2 files changed

+75
-65
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use std::io;
2+
use std::path::Path;
3+
4+
use crate::fs_wrapper;
5+
6+
// FIXME(jieyouxu): modify create_symlink to panic on windows.
7+
8+
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
9+
#[cfg(target_family = "windows")]
10+
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
11+
if link.as_ref().exists() {
12+
std::fs::remove_dir(link.as_ref()).unwrap();
13+
}
14+
use std::os::windows::fs;
15+
fs::symlink_file(original.as_ref(), link.as_ref()).expect(&format!(
16+
"failed to create symlink {:?} for {:?}",
17+
link.as_ref().display(),
18+
original.as_ref().display(),
19+
));
20+
}
21+
22+
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
23+
#[cfg(target_family = "unix")]
24+
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
25+
if link.as_ref().exists() {
26+
std::fs::remove_dir(link.as_ref()).unwrap();
27+
}
28+
use std::os::unix::fs;
29+
fs::symlink(original.as_ref(), link.as_ref()).expect(&format!(
30+
"failed to create symlink {:?} for {:?}",
31+
link.as_ref().display(),
32+
original.as_ref().display(),
33+
));
34+
}
35+
36+
/// Copy a directory into another.
37+
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
38+
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
39+
let dst = dst.as_ref();
40+
if !dst.is_dir() {
41+
std::fs::create_dir_all(&dst)?;
42+
}
43+
for entry in std::fs::read_dir(src)? {
44+
let entry = entry?;
45+
let ty = entry.file_type()?;
46+
if ty.is_dir() {
47+
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
48+
} else {
49+
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
50+
}
51+
}
52+
Ok(())
53+
}
54+
55+
if let Err(e) = copy_dir_all_inner(&src, &dst) {
56+
// Trying to give more context about what exactly caused the failure
57+
panic!(
58+
"failed to copy `{}` to `{}`: {:?}",
59+
src.as_ref().display(),
60+
dst.as_ref().display(),
61+
e
62+
);
63+
}
64+
}
65+
66+
/// Helper for reading entries in a given directory.
67+
pub fn read_dir<P: AsRef<Path>, F: FnMut(&Path)>(dir: P, mut callback: F) {
68+
for entry in fs_wrapper::read_dir(dir) {
69+
callback(&entry.unwrap().path());
70+
}
71+
}

src/tools/run-make-support/src/lib.rs

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ pub mod artifact_names;
1111
pub mod diff;
1212
pub mod env_checked;
1313
pub mod external_deps;
14+
pub mod fs_helpers;
1415
pub mod fs_wrapper;
1516
pub mod path_helpers;
1617
pub mod run;
1718
pub mod targets;
1819

1920
use std::fs;
20-
use std::io;
2121
use std::panic;
2222
use std::path::{Path, PathBuf};
2323

@@ -71,35 +71,10 @@ pub use artifact_names::{
7171
/// Path-related helpers.
7272
pub use path_helpers::{cwd, cygpath_windows, path, source_root};
7373

74-
use command::{Command, CompletedProcess};
74+
/// Helpers for common fs operations.
75+
pub use fs_helpers::{copy_dir_all, create_symlink, read_dir};
7576

76-
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
77-
#[cfg(target_family = "windows")]
78-
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
79-
if link.as_ref().exists() {
80-
std::fs::remove_dir(link.as_ref()).unwrap();
81-
}
82-
use std::os::windows::fs;
83-
fs::symlink_file(original.as_ref(), link.as_ref()).expect(&format!(
84-
"failed to create symlink {:?} for {:?}",
85-
link.as_ref().display(),
86-
original.as_ref().display(),
87-
));
88-
}
89-
90-
/// Creates a new symlink to a path on the filesystem, adjusting for Windows or Unix.
91-
#[cfg(target_family = "unix")]
92-
pub fn create_symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) {
93-
if link.as_ref().exists() {
94-
std::fs::remove_dir(link.as_ref()).unwrap();
95-
}
96-
use std::os::unix::fs;
97-
fs::symlink(original.as_ref(), link.as_ref()).expect(&format!(
98-
"failed to create symlink {:?} for {:?}",
99-
link.as_ref().display(),
100-
original.as_ref().display(),
101-
));
102-
}
77+
use command::{Command, CompletedProcess};
10378

10479
// FIXME(Oneirical): This will no longer be required after compiletest receives the ability
10580
// to manipulate read-only files. See https://github.com/rust-lang/rust/issues/126334
@@ -255,36 +230,6 @@ pub fn invalid_utf8_not_contains<P: AsRef<Path>, S: AsRef<str>>(path: P, expecte
255230
}
256231
}
257232

258-
/// Copy a directory into another.
259-
pub fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) {
260-
fn copy_dir_all_inner(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
261-
let dst = dst.as_ref();
262-
if !dst.is_dir() {
263-
std::fs::create_dir_all(&dst)?;
264-
}
265-
for entry in std::fs::read_dir(src)? {
266-
let entry = entry?;
267-
let ty = entry.file_type()?;
268-
if ty.is_dir() {
269-
copy_dir_all_inner(entry.path(), dst.join(entry.file_name()))?;
270-
} else {
271-
std::fs::copy(entry.path(), dst.join(entry.file_name()))?;
272-
}
273-
}
274-
Ok(())
275-
}
276-
277-
if let Err(e) = copy_dir_all_inner(&src, &dst) {
278-
// Trying to give more context about what exactly caused the failure
279-
panic!(
280-
"failed to copy `{}` to `{}`: {:?}",
281-
src.as_ref().display(),
282-
dst.as_ref().display(),
283-
e
284-
);
285-
}
286-
}
287-
288233
/// Check that all files in `dir1` exist and have the same content in `dir2`. Panic otherwise.
289234
pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
290235
let dir2 = dir2.as_ref();
@@ -310,12 +255,6 @@ pub fn recursive_diff(dir1: impl AsRef<Path>, dir2: impl AsRef<Path>) {
310255
});
311256
}
312257

313-
pub fn read_dir<F: FnMut(&Path)>(dir: impl AsRef<Path>, mut callback: F) {
314-
for entry in fs_wrapper::read_dir(dir) {
315-
callback(&entry.unwrap().path());
316-
}
317-
}
318-
319258
/// Check that `actual` is equal to `expected`. Panic otherwise.
320259
#[track_caller]
321260
pub fn assert_equals<S1: AsRef<str>, S2: AsRef<str>>(actual: S1, expected: S2) {

0 commit comments

Comments
 (0)