Skip to content
Myr

ignore: remove unused parameter

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrew Gallant · committed by Andrew Gallant · parent e14f919 · Browse files · 2 files+373 added−389 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+373 added−389 removed

  • other:+373 added−389 removed

2 files+373 added−389 removed

modified:ignore/src/dir.rs+158 added-167 removed
12 unmodified lines
13// with non-obvious failure modes. Alas, such things haven't been documented14// well.1516use std::collections::HashMap;17use std::ffi::{OsString, OsStr};17use std::ffi::{OsStr, OsString};18use std::path::{Path, PathBuf};19use std::sync::{Arc, RwLock};2021use gitignore::{self, Gitignore, GitignoreBuilder};22use pathutil::{is_hidden, strip_prefix};22use overrides::{self, Override};23use pathutil::{is_hidden, strip_prefix};24use types::{self, Types};25use walk::DirEntry;26use {Error, Match, PartialErrorBuilder};27
123 unmodified lines
151 /// Create a new `Ignore` matcher with the parent directories of `dir`.152 ///153 /// Note that this can only be called on an `Ignore` matcher with no154 /// parents (i.e., `is_root` returns `true`). This will panic otherwise.155 pub fn add_parents<P: AsRef<Path>>(156 &self,157 path: P,158 ) -> (Ignore, Option<Error>) {155 pub fn add_parents<P: AsRef<Path>>(&self, path: P) -> (Ignore, Option<Error>) {156 if !self.0.opts.parents157 && !self.0.opts.git_ignore158 && !self.0.opts.git_exclude159 && !self.0.opts.git_global
33 unmodified lines
193 let (mut igtmp, err) = ig.add_child_path(parent);194 errs.maybe_push(err);195 igtmp.is_absolute_parent = true;196 igtmp.absolute_base = Some(absolute_base.clone());200 igtmp.has_git =201 if self.0.opts.git_ignore {202 parent.join(".git").exists()203 } else {204 false205 };197 igtmp.has_git = if self.0.opts.git_ignore {198 parent.join(".git").exists()199 } else {200 false201 };202 ig = Ignore(Arc::new(igtmp));203 compiled.insert(parent.as_os_str().to_os_string(), ig.clone());204 }205 (ig, errs.into_error_option())
6 unmodified lines
212 /// a matcher is always returned (which may match nothing) and an error is213 /// returned if it exists.214 ///215 /// Note that all I/O errors are completely ignored.220 pub fn add_child<P: AsRef<Path>>(221 &self,222 dir: P,223 ) -> (Ignore, Option<Error>) {216 pub fn add_child<P: AsRef<Path>>(&self, dir: P) -> (Ignore, Option<Error>) {217 let (ig, err) = self.add_child_path(dir.as_ref());218 (Ignore(Arc::new(ig)), err)219 }220221 /// Like add_child, but takes a full path and returns an IgnoreInner.222 fn add_child_path(&self, dir: &Path) -> (IgnoreInner, Option<Error>) {223 let mut errs = PartialErrorBuilder::default();231 let custom_ig_matcher =232 if self.0.custom_ignore_filenames.is_empty() {233 Gitignore::empty()234 } else {235 let (m, err) =236 create_gitignore(237 &dir,238 &self.0.custom_ignore_filenames,239 self.0.opts.ignore_case_insensitive,240 );241 errs.maybe_push(err);242 m243 };244 let ig_matcher =245 if !self.0.opts.ignore {246 Gitignore::empty()247 } else {248 let (m, err) =249 create_gitignore(250 &dir,251 &[".ignore"],252 self.0.opts.ignore_case_insensitive,253 );254 errs.maybe_push(err);255 m256 };257 let gi_matcher =258 if !self.0.opts.git_ignore {259 Gitignore::empty()260 } else {261 let (m, err) =262 create_gitignore(263 &dir,264 &[".gitignore"],265 self.0.opts.ignore_case_insensitive,266 );267 errs.maybe_push(err);268 m269 };270 let gi_exclude_matcher =271 if !self.0.opts.git_exclude {272 Gitignore::empty()273 } else {274 let (m, err) =275 create_gitignore(276 &dir,277 &[".git/info/exclude"],278 self.0.opts.ignore_case_insensitive,279 );280 errs.maybe_push(err);281 m282 };283 let has_git =284 if self.0.opts.git_ignore {285 dir.join(".git").exists()286 } else {287 false288 };224 let custom_ig_matcher = if self.0.custom_ignore_filenames.is_empty() {225 Gitignore::empty()226 } else {227 let (m, err) = create_gitignore(228 &dir,229 &self.0.custom_ignore_filenames,230 self.0.opts.ignore_case_insensitive,231 );232 errs.maybe_push(err);233 m234 };235 let ig_matcher = if !self.0.opts.ignore {236 Gitignore::empty()237 } else {238 let (m, err) =239 create_gitignore(&dir, &[".ignore"], self.0.opts.ignore_case_insensitive);240 errs.maybe_push(err);241 m242 };243 let gi_matcher = if !self.0.opts.git_ignore {244 Gitignore::empty()245 } else {246 let (m, err) =247 create_gitignore(&dir, &[".gitignore"], self.0.opts.ignore_case_insensitive);248 errs.maybe_push(err);249 m250 };251 let gi_exclude_matcher = if !self.0.opts.git_exclude {252 Gitignore::empty()253 } else {254 let (m, err) = create_gitignore(255 &dir,256 &[".git/info/exclude"],257 self.0.opts.ignore_case_insensitive,258 );259 errs.maybe_push(err);260 m261 };262 let has_git = if self.0.opts.git_ignore {263 dir.join(".git").exists()264 } else {265 false266 };267 let ig = IgnoreInner {268 compiled: self.0.compiled.clone(),269 dir: dir.to_path_buf(),270 overrides: self.0.overrides.clone(),
19 unmodified lines
290 let opts = self.0.opts;291 let has_custom_ignore_files = !self.0.custom_ignore_filenames.is_empty();292 let has_explicit_ignores = !self.0.explicit_ignores.is_empty();293316 opts.ignore || opts.git_global || opts.git_ignore317 || opts.git_exclude || has_custom_ignore_files318 || has_explicit_ignores294 opts.ignore295 || opts.git_global296 || opts.git_ignore297 || opts.git_exclude298 || has_custom_ignore_files299 || has_explicit_ignores300 }301302 /// Like `matched`, but works with a directory entry instead.322 pub fn matched_dir_entry<'a>(323 &'a self,324 dent: &DirEntry,325 ) -> Match<IgnoreMatch<'a>> {303 pub fn matched_dir_entry<'a>(&'a self, dent: &DirEntry) -> Match<IgnoreMatch<'a>> {304 let m = self.matched(dent.path(), dent.is_dir());305 if m.is_none() && self.0.opts.hidden && is_hidden(dent) {306 return Match::Ignore(IgnoreMatch::hidden());307 }
3 unmodified lines
311 /// Returns a match indicating whether the given file path should be312 /// ignored or not.313 ///314 /// The match contains information about its origin.337 fn matched<'a, P: AsRef<Path>>(338 &'a self,339 path: P,340 is_dir: bool,341 ) -> Match<IgnoreMatch<'a>> {315 fn matched<'a, P: AsRef<Path>>(&'a self, path: P, is_dir: bool) -> Match<IgnoreMatch<'a>> {316 // We need to be careful with our path. If it has a leading ./, then317 // strip it because it causes nothing but trouble.318 let mut path = path.as_ref();319 if let Some(p) = strip_prefix("./", path) {
3 unmodified lines
323 // regardless of whether it's whitelist/ignore, then we quit and324 // return that result immediately. Overrides have the highest325 // precedence.326 if !self.0.overrides.is_empty() {353 let mat =354 self.0.overrides.matched(path, is_dir)355 .map(IgnoreMatch::overrides);327 let mat = self328 .0329 .overrides330 .matched(path, is_dir)331 .map(IgnoreMatch::overrides);332 if !mat.is_none() {333 return mat;334 }335 }
6 unmodified lines
342 whitelisted = mat;343 }344 }345 if !self.0.types.is_empty() {370 let mat =371 self.0.types.matched(path, is_dir).map(IgnoreMatch::types);346 let mat = self.0.types.matched(path, is_dir).map(IgnoreMatch::types);347 if mat.is_ignore() {348 return mat;349 } else if mat.is_whitelist() {350 whitelisted = mat;
3 unmodified lines
354 }355356 /// Performs matching only on the ignore files for this directory and357 /// all parent directories.383 fn matched_ignore<'a>(384 &'a self,385 path: &Path,386 is_dir: bool,387 ) -> Match<IgnoreMatch<'a>> {388 let (mut m_custom_ignore, mut m_ignore, mut m_gi, mut m_gi_exclude, mut m_explicit) =389 (Match::None, Match::None, Match::None, Match::None, Match::None);358 fn matched_ignore<'a>(&'a self, path: &Path, is_dir: bool) -> Match<IgnoreMatch<'a>> {359 let (mut m_custom_ignore, mut m_ignore, mut m_gi, mut m_gi_exclude, mut m_explicit) = (360 Match::None,361 Match::None,362 Match::None,363 Match::None,364 Match::None,365 );366 let any_git = self.parents().any(|ig| ig.0.has_git);367 let mut saw_git = false;368 for ig in self.parents().take_while(|ig| !ig.0.is_absolute_parent) {369 if m_custom_ignore.is_none() {370 m_custom_ignore =395 ig.0.custom_ignore_matcher.matched(path, is_dir)396 .map(IgnoreMatch::gitignore);371 ig.0.custom_ignore_matcher372 .matched(path, is_dir)373 .map(IgnoreMatch::gitignore);374 }375 if m_ignore.is_none() {376 m_ignore =400 ig.0.ignore_matcher.matched(path, is_dir)401 .map(IgnoreMatch::gitignore);377 ig.0.ignore_matcher378 .matched(path, is_dir)379 .map(IgnoreMatch::gitignore);380 }381 if any_git && !saw_git && m_gi.is_none() {382 m_gi =405 ig.0.git_ignore_matcher.matched(path, is_dir)406 .map(IgnoreMatch::gitignore);383 ig.0.git_ignore_matcher384 .matched(path, is_dir)385 .map(IgnoreMatch::gitignore);386 }387 if any_git && !saw_git && m_gi_exclude.is_none() {388 m_gi_exclude =410 ig.0.git_exclude_matcher.matched(path, is_dir)411 .map(IgnoreMatch::gitignore);389 ig.0.git_exclude_matcher390 .matched(path, is_dir)391 .map(IgnoreMatch::gitignore);392 }393 saw_git = saw_git || ig.0.has_git;394 }395 if self.0.opts.parents {396 if let Some(abs_parent_path) = self.absolute_base() {397 let path = abs_parent_path.join(path);418 for ig in self.parents().skip_while(|ig|!ig.0.is_absolute_parent) {398 for ig in self.parents().skip_while(|ig| !ig.0.is_absolute_parent) {399 if m_custom_ignore.is_none() {400 m_custom_ignore =421 ig.0.custom_ignore_matcher.matched(&path, is_dir)422 .map(IgnoreMatch::gitignore);401 ig.0.custom_ignore_matcher402 .matched(&path, is_dir)403 .map(IgnoreMatch::gitignore);404 }405 if m_ignore.is_none() {406 m_ignore =426 ig.0.ignore_matcher.matched(&path, is_dir)427 .map(IgnoreMatch::gitignore);407 ig.0.ignore_matcher408 .matched(&path, is_dir)409 .map(IgnoreMatch::gitignore);410 }411 if any_git && !saw_git && m_gi.is_none() {412 m_gi =431 ig.0.git_ignore_matcher.matched(&path, is_dir)432 .map(IgnoreMatch::gitignore);413 ig.0.git_ignore_matcher414 .matched(&path, is_dir)415 .map(IgnoreMatch::gitignore);416 }417 if any_git && !saw_git && m_gi_exclude.is_none() {418 m_gi_exclude =436 ig.0.git_exclude_matcher.matched(&path, is_dir)437 .map(IgnoreMatch::gitignore);419 ig.0.git_exclude_matcher420 .matched(&path, is_dir)421 .map(IgnoreMatch::gitignore);422 }423 saw_git = saw_git || ig.0.has_git;424 }425 }
3 unmodified lines
429 break;430 }431 m_explicit = gi.matched(&path, is_dir).map(IgnoreMatch::gitignore);432 }449 let m_global =450 if any_git {451 self.0.git_global_matcher452 .matched(&path, is_dir)453 .map(IgnoreMatch::gitignore)454 } else {455 Match::None456 };433 let m_global = if any_git {434 self.0435 .git_global_matcher436 .matched(&path, is_dir)437 .map(IgnoreMatch::gitignore)438 } else {439 Match::None440 };441458 m_custom_ignore.or(m_ignore).or(m_gi).or(m_gi_exclude).or(m_global).or(m_explicit)442 m_custom_ignore443 .or(m_ignore)444 .or(m_gi)445 .or(m_gi_exclude)446 .or(m_global)447 .or(m_explicit)448 }449450 /// Returns an iterator over parent ignore matchers, including this one.451 pub fn parents(&self) -> Parents {
71 unmodified lines
523 ///524 /// The matcher returned won't match anything until ignore rules from525 /// directories are added to it.526 pub fn build(&self) -> Ignore {538 let git_global_matcher =539 if !self.opts.git_global {540 Gitignore::empty()541 } else {542 let mut builder = GitignoreBuilder::new("");543 builder544 .case_insensitive(self.opts.ignore_case_insensitive)545 .unwrap();546 let (gi, err) = builder.build_global();547 if let Some(err) = err {548 debug!("{}", err);549 }550 gi551 };527 let git_global_matcher = if !self.opts.git_global {528 Gitignore::empty()529 } else {530 let mut builder = GitignoreBuilder::new("");531 builder532 .case_insensitive(self.opts.ignore_case_insensitive)533 .unwrap();534 let (gi, err) = builder.build_global();535 if let Some(err) = err {536 debug!("{}", err);537 }538 gi539 };540541 Ignore(Arc::new(IgnoreInner {542 compiled: Arc::new(RwLock::new(HashMap::new())),543 dir: self.dir.clone(),
47 unmodified lines
591 /// When specifying multiple names, earlier names have lower precedence than592 /// later names.593 pub fn add_custom_ignore_filename<S: AsRef<OsStr>>(594 &mut self,607 file_name: S595 file_name: S,596 ) -> &mut IgnoreBuilder {609 self.custom_ignore_filenames.push(file_name.as_ref().to_os_string());597 self.custom_ignore_filenames598 .push(file_name.as_ref().to_os_string());599 self600 }601602 /// Enables ignoring hidden files.
63 unmodified lines
666667 /// Process ignore files case insensitively668 ///669 /// This is disabled by default.681 pub fn ignore_case_insensitive(682 &mut self,683 yes: bool,684 ) -> &mut IgnoreBuilder {670 pub fn ignore_case_insensitive(&mut self, yes: bool) -> &mut IgnoreBuilder {671 self.opts.ignore_case_insensitive = yes;672 self673 }674}
52 unmodified lines
727 _ => panic!("expected partial error but got {:?}", err),728 }729 }730745 fn tmpdir(prefix: &str) -> TempDir {731 fn tmpdir() -> TempDir {732 TempDir::new().unwrap()733 }734735 #[test]736 fn explicit_ignore() {751 let td = tmpdir("ignore-test-");737 let td = tmpdir();738 wfile(td.path().join("not-an-ignore"), "foo\n!bar");739740 let (gi, err) = Gitignore::new(td.path().join("not-an-ignore"));741 assert!(err.is_none());742 let (ig, err) = IgnoreBuilder::new()757 .add_ignore(gi).build().add_child(td.path());743 .add_ignore(gi)744 .build()745 .add_child(td.path());746 assert!(err.is_none());747 assert!(ig.matched("foo", false).is_ignore());748 assert!(ig.matched("bar", false).is_whitelist());749 assert!(ig.matched("baz", false).is_none());750 }751752 #[test]753 fn git_exclude() {766 let td = tmpdir("ignore-test-");754 let td = tmpdir();755 mkdirp(td.path().join(".git/info"));756 wfile(td.path().join(".git/info/exclude"), "foo\n!bar");757758 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
4 unmodified lines
763 }764765 #[test]766 fn gitignore() {779 let td = tmpdir("ignore-test-");767 let td = tmpdir();768 mkdirp(td.path().join(".git"));769 wfile(td.path().join(".gitignore"), "foo\n!bar");770771 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
4 unmodified lines
776 }777778 #[test]779 fn gitignore_no_git() {792 let td = tmpdir("ignore-test-");780 let td = tmpdir();781 wfile(td.path().join(".gitignore"), "foo\n!bar");782783 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());784 assert!(err.is_none());
3 unmodified lines
788 }789790 #[test]791 fn ignore() {804 let td = tmpdir("ignore-test-");792 let td = tmpdir();793 wfile(td.path().join(".ignore"), "foo\n!bar");794795 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());796 assert!(err.is_none());
3 unmodified lines
800 }801802 #[test]803 fn custom_ignore() {816 let td = tmpdir("ignore-test-");804 let td = tmpdir();805 let custom_ignore = ".customignore";806 wfile(td.path().join(custom_ignore), "foo\n!bar");807808 let (ig, err) = IgnoreBuilder::new()809 .add_custom_ignore_filename(custom_ignore)822 .build().add_child(td.path());810 .build()811 .add_child(td.path());812 assert!(err.is_none());813 assert!(ig.matched("foo", false).is_ignore());814 assert!(ig.matched("bar", false).is_whitelist());815 assert!(ig.matched("baz", false).is_none());
1 unmodified line
817818 // Tests that a custom ignore file will override an .ignore.819 #[test]820 fn custom_ignore_over_ignore() {832 let td = tmpdir("ignore-test-");821 let td = tmpdir();822 let custom_ignore = ".customignore";823 wfile(td.path().join(".ignore"), "foo");824 wfile(td.path().join(custom_ignore), "!foo");825826 let (ig, err) = IgnoreBuilder::new()827 .add_custom_ignore_filename(custom_ignore)839 .build().add_child(td.path());828 .build()829 .add_child(td.path());830 assert!(err.is_none());831 assert!(ig.matched("foo", false).is_whitelist());832 }833834 // Tests that earlier custom ignore files have lower precedence than later.835 #[test]836 fn custom_ignore_precedence() {847 let td = tmpdir("ignore-test-");837 let td = tmpdir();838 let custom_ignore1 = ".customignore1";839 let custom_ignore2 = ".customignore2";840 wfile(td.path().join(custom_ignore1), "foo");841 wfile(td.path().join(custom_ignore2), "!foo");842843 let (ig, err) = IgnoreBuilder::new()844 .add_custom_ignore_filename(custom_ignore1)845 .add_custom_ignore_filename(custom_ignore2)856 .build().add_child(td.path());846 .build()847 .add_child(td.path());848 assert!(err.is_none());849 assert!(ig.matched("foo", false).is_whitelist());850 }851852 // Tests that an .ignore will override a .gitignore.853 #[test]854 fn ignore_over_gitignore() {864 let td = tmpdir("ignore-test-");855 let td = tmpdir();856 wfile(td.path().join(".gitignore"), "foo");857 wfile(td.path().join(".ignore"), "!foo");858859 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
3 unmodified lines
863864 // Tests that exclude has lower precedent than both .ignore and .gitignore.865 #[test]866 fn exclude_lowest() {876 let td = tmpdir("ignore-test-");867 let td = tmpdir();868 wfile(td.path().join(".gitignore"), "!foo");869 wfile(td.path().join(".ignore"), "!bar");870 mkdirp(td.path().join(".git/info"));871 wfile(td.path().join(".git/info/exclude"), "foo\nbar\nbaz");
6 unmodified lines
878 }879880 #[test]881 fn errored() {891 let td = tmpdir("ignore-test-");882 let td = tmpdir();883 wfile(td.path().join(".gitignore"), "{foo");884885 let (_, err) = IgnoreBuilder::new().build().add_child(td.path());886 assert!(err.is_some());887 }888889 #[test]890 fn errored_both() {900 let td = tmpdir("ignore-test-");891 let td = tmpdir();892 wfile(td.path().join(".gitignore"), "{foo");893 wfile(td.path().join(".ignore"), "{bar");894895 let (_, err) = IgnoreBuilder::new().build().add_child(td.path());
1 unmodified line
897 }898899 #[test]900 fn errored_partial() {910 let td = tmpdir("ignore-test-");901 let td = tmpdir();902 mkdirp(td.path().join(".git"));903 wfile(td.path().join(".gitignore"), "{foo\nbar");904905 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
2 unmodified lines
908 }909910 #[test]911 fn errored_partial_and_ignore() {921 let td = tmpdir("ignore-test-");912 let td = tmpdir();913 wfile(td.path().join(".gitignore"), "{foo\nbar");914 wfile(td.path().join(".ignore"), "!bar");915916 let (ig, err) = IgnoreBuilder::new().build().add_child(td.path());
2 unmodified lines
919 }920921 #[test]922 fn not_present_empty() {932 let td = tmpdir("ignore-test-");923 let td = tmpdir();924925 let (_, err) = IgnoreBuilder::new().build().add_child(td.path());926 assert!(err.is_none());927 }
1 unmodified line
929 #[test]930 fn stops_at_git_dir() {931 // This tests that .gitignore files beyond a .git barrier aren't932 // matched, but .ignore files are.942 let td = tmpdir("ignore-test-");933 let td = tmpdir();934 mkdirp(td.path().join(".git"));935 mkdirp(td.path().join("foo/.git"));936 wfile(td.path().join(".gitignore"), "foo");937 wfile(td.path().join(".ignore"), "bar");
12 unmodified lines
950 }951952 #[test]953 fn absolute_parent() {963 let td = tmpdir("ignore-test-");954 let td = tmpdir();955 mkdirp(td.path().join(".git"));956 mkdirp(td.path().join("foo"));957 wfile(td.path().join(".gitignore"), "bar");958
14 unmodified lines
973 }974975 #[test]976 fn absolute_parent_anchored() {986 let td = tmpdir("ignore-test-");977 let td = tmpdir();978 mkdirp(td.path().join(".git"));979 mkdirp(td.path().join("src/llvm"));980 wfile(td.path().join(".gitignore"), "/llvm/\nfoo");981
13 unmodified lines
modified:ignore/src/walk.rs+215 added-222 removed
2 unmodified lines
3use std::fmt;4use std::fs::{self, FileType, Metadata};5use std::io;6use std::path::{Path, PathBuf};7use std::sync::Arc;7use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};8use std::sync::Arc;9use std::thread;10use std::time::Duration;11use std::vec;12
168 unmodified lines
181 use self::DirEntryInner::*;182 match *self {183 Stdin => {184 let err = Error::Io(io::Error::new(185 io::ErrorKind::Other, "<stdin> has no metadata"));185 io::ErrorKind::Other,186 "<stdin> has no metadata",187 ));188 Err(err.with_path("<stdin>"))189 }188 Walkdir(ref x) => {189 x.metadata().map_err(|err| {190 Error::Io(io::Error::from(err)).with_path(x.path())191 })192 }190 Walkdir(ref x) => x191 .metadata()192 .map_err(|err| Error::Io(io::Error::from(err)).with_path(x.path())),193 Raw(ref x) => x.metadata(),194 }195 }196
25 unmodified lines
222 }223224 #[cfg(unix)]225 fn ino(&self) -> Option<u64> {226 use walkdir::DirEntryExt;226 use self::DirEntryInner::*;227 use walkdir::DirEntryExt;228 match *self {229 Stdin => None,230 Walkdir(ref x) => Some(x.ino()),231 Raw(ref x) => Some(x.ino()),
64 unmodified lines
296 if self.follow_link {297 fs::metadata(&self.path)298 } else {299 Ok(self.metadata.clone())300 }300 }.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))301 .map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))302 }303304 #[cfg(not(windows))]305 fn metadata_internal(&self) -> Result<fs::Metadata, Error> {306 if self.follow_link {307 fs::metadata(&self.path)308 } else {309 fs::symlink_metadata(&self.path)310 }309 }.map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))311 .map_err(|err| Error::Io(io::Error::from(err)).with_path(&self.path))312 }313314 fn file_type(&self) -> FileType {315 self.ty316 }317318 fn file_name(&self) -> &OsStr {319 self.path320 .file_name()317 self.path.file_name().unwrap_or_else(|| self.path.as_os_str())321 .unwrap_or_else(|| self.path.as_os_str())322 }323324 fn depth(&self) -> usize {325 self.depth
3 unmodified lines
329 fn ino(&self) -> u64 {330 self.ino331 }332329 fn from_entry(330 depth: usize,331 ent: &fs::DirEntry,332 ) -> Result<DirEntryRaw, Error> {333 fn from_entry(depth: usize, ent: &fs::DirEntry) -> Result<DirEntryRaw, Error> {334 let ty = ent.file_type().map_err(|err| {335 let err = Error::Io(io::Error::from(err)).with_path(ent.path());336 Error::WithDepth {337 depth: depth,
49 unmodified lines
387 ent: &fs::DirEntry,388 ty: fs::FileType,389 ) -> Result<DirEntryRaw, Error> {390 Err(Error::Io(io::Error::new(390 io::ErrorKind::Other, "unsupported platform")))391 io::ErrorKind::Other,392 "unsupported platform",393 )))394 }395396 #[cfg(windows)]394 fn from_path(395 depth: usize,396 pb: PathBuf,397 link: bool,398 ) -> Result<DirEntryRaw, Error> {399 let md = fs::metadata(&pb).map_err(|err| {400 Error::Io(err).with_path(&pb)401 })?;397 fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {398 let md = fs::metadata(&pb).map_err(|err| Error::Io(err).with_path(&pb))?;399 Ok(DirEntryRaw {400 path: pb,401 ty: md.file_type(),402 follow_link: link,
2 unmodified lines
405 })406 }407408 #[cfg(unix)]412 fn from_path(413 depth: usize,414 pb: PathBuf,415 link: bool,416 ) -> Result<DirEntryRaw, Error> {409 fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {410 use std::os::unix::fs::MetadataExt;411419 let md = fs::metadata(&pb).map_err(|err| {420 Error::Io(err).with_path(&pb)421 })?;412 let md = fs::metadata(&pb).map_err(|err| Error::Io(err).with_path(&pb))?;413 Ok(DirEntryRaw {414 path: pb,415 ty: md.file_type(),416 follow_link: link,
3 unmodified lines
420 }421422 // Placeholder implementation to allow compiling on non-standard platforms (e.g. wasm32).423 #[cfg(not(any(windows, unix)))]433 fn from_path(434 depth: usize,435 pb: PathBuf,436 link: bool,437 ) -> Result<DirEntryRaw, Error> {424 fn from_path(depth: usize, pb: PathBuf, link: bool) -> Result<DirEntryRaw, Error> {425 Err(Error::Io(io::Error::new(439 io::ErrorKind::Other, "unsupported platform")))426 io::ErrorKind::Other,427 "unsupported platform",428 )))429 }430}431432/// WalkBuilder builds a recursive directory iterator.
102 unmodified lines
535 pub fn build(&self) -> Walk {536 let follow_links = self.follow_links;537 let max_depth = self.max_depth;538 let sorter = self.sorter.clone();539 let its = self540 .paths541 .iter()550 let its = self.paths.iter().map(move |p| {551 if p == Path::new("-") {552 (p.to_path_buf(), None)553 } else {554 let mut wd = WalkDir::new(p);555 wd = wd.follow_links(follow_links || p.is_file());556 wd = wd.same_file_system(self.same_file_system);557 if let Some(max_depth) = max_depth {558 wd = wd.max_depth(max_depth);559 }560 if let Some(ref sorter) = sorter {561 match sorter.clone() {562 Sorter::ByName(cmp) => {563 wd = wd.sort_by(move |a, b| {564 cmp(a.file_name(), b.file_name())565 });542 .map(move |p| {543 if p == Path::new("-") {544 (p.to_path_buf(), None)545 } else {546 let mut wd = WalkDir::new(p);547 wd = wd.follow_links(follow_links || p.is_file());548 wd = wd.same_file_system(self.same_file_system);549 if let Some(max_depth) = max_depth {550 wd = wd.max_depth(max_depth);551 }552 if let Some(ref sorter) = sorter {553 match sorter.clone() {554 Sorter::ByName(cmp) => {555 wd = wd.sort_by(move |a, b| cmp(a.file_name(), b.file_name()));556 }557 Sorter::ByPath(cmp) => {558 wd = wd.sort_by(move |a, b| cmp(a.path(), b.path()));559 }560 }567 Sorter::ByPath(cmp) => {568 wd = wd.sort_by(move |a, b| {569 cmp(a.path(), b.path())570 });571 }561 }562 (p.to_path_buf(), Some(WalkEventIter::from(wd)))563 }574 (p.to_path_buf(), Some(WalkEventIter::from(wd)))575 }576 }).collect::<Vec<_>>().into_iter();564 })565 .collect::<Vec<_>>()566 .into_iter();567 let ig_root = self.ig_builder.build();568 Walk {569 its: its,570 it: None,
75 unmodified lines
646 let mut builder = GitignoreBuilder::new("");647 let mut errs = PartialErrorBuilder::default();648 errs.maybe_push(builder.add(path));649 match builder.build() {660 Ok(gi) => { self.ig_builder.add_ignore(gi); }661 Err(err) => { errs.push(err); }650 Ok(gi) => {651 self.ig_builder.add_ignore(gi);652 }653 Err(err) => {654 errs.push(err);655 }656 }657 errs.into_error_option()658 }659
4 unmodified lines
664 /// When specifying multiple names, earlier names have lower precedence than665 /// later names.666 pub fn add_custom_ignore_filename<S: AsRef<OsStr>>(667 &mut self,674 file_name: S668 file_name: S,669 ) -> &mut WalkBuilder {670 self.ig_builder.add_custom_ignore_filename(file_name);671 self672 }
128 unmodified lines
801 /// This method will override any previous sorter set by this method or802 /// by `sort_by_file_name`.803 ///804 /// Note that this is not used in the parallel iterator.811 pub fn sort_by_file_path<F>(812 &mut self,813 cmp: F,814 ) -> &mut WalkBuilder815 where F: Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static805 pub fn sort_by_file_path<F>(&mut self, cmp: F) -> &mut WalkBuilder806 where807 F: Fn(&Path, &Path) -> cmp::Ordering + Send + Sync + 'static,808 {809 self.sorter = Some(Sorter::ByPath(Arc::new(cmp)));810 self811 }
9 unmodified lines
821 /// by `sort_by_file_path`.822 ///823 /// Note that this is not used in the parallel iterator.824 pub fn sort_by_file_name<F>(&mut self, cmp: F) -> &mut WalkBuilder833 where F: Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static825 where826 F: Fn(&OsStr, &OsStr) -> cmp::Ordering + Send + Sync + 'static,827 {828 self.sorter = Some(Sorter::ByName(Arc::new(cmp)));829 self830 }
172 unmodified lines
1003}10041005impl From<WalkDir> for WalkEventIter {1006 fn from(it: WalkDir) -> WalkEventIter {1014 WalkEventIter { depth: 0, it: it.into_iter(), next: None }1007 WalkEventIter {1008 depth: 0,1009 it: it.into_iter(),1010 next: None,1011 }1012 }1013}10141015impl Iterator for WalkEventIter {
74 unmodified lines
1090impl WalkParallel {1091 /// Execute the parallel recursive directory iterator. `mkf` is called1092 /// for each thread used for iteration. The function produced by `mkf`1093 /// is then in turn called for each visited file path.1097 pub fn run<F>(1098 self,1099 mut mkf: F,1100 ) where F: FnMut() -> Box<dyn FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static> {1094 pub fn run<F>(self, mut mkf: F)1095 where1096 F: FnMut() -> Box<dyn FnMut(Result<DirEntry, Error>) -> WalkState + Send + 'static>,1097 {1098 let mut f = mkf();1099 let threads = self.threads();1100 // TODO: Figure out how to use a bounded channel here. With an1101 // unbounded channel, the workers can run away and fill up memory
8 unmodified lines
1110 // Send the initial set of root paths to the pool of workers.1111 // Note that we only send directories. For files, we send to them the1112 // callback directly.1113 for path in self.paths {1117 let (dent, root_device) =1118 if path == Path::new("-") {1119 (DirEntry::new_stdin(), None)1114 let (dent, root_device) = if path == Path::new("-") {1115 (DirEntry::new_stdin(), None)1116 } else {1117 let root_device = if !self.same_file_system {1118 None1119 } else {1121 let root_device =1122 if !self.same_file_system {1123 None1124 } else {1125 match device_num(&path) {1126 Ok(root_device) => Some(root_device),1120 match device_num(&path) {1121 Ok(root_device) => Some(root_device),1127 Err(err) => {1128 let err = Error::Io(err).with_path(path);1129 if f(Err(err)).is_quit() {1130 return;1131 }1132 continue;1133 }1134 }1135 };1136 match DirEntryRaw::from_path(0, path, false) {1137 Ok(dent) => {1138 (DirEntry::new_raw(dent, None), root_device)1139 }1122 Err(err) => {1123 let err = Error::Io(err).with_path(path);1124 if f(Err(err)).is_quit() {1125 return;1126 }1127 continue;1128 }1129 }1130 };1131 match DirEntryRaw::from_path(0, path, false) {1132 Ok(dent) => (DirEntry::new_raw(dent, None), root_device),1133 Err(err) => {1134 if f(Err(err)).is_quit() {1135 return;1136 }1137 continue;1138 }1139 }1140 };1141 tx.send(Message::Work(Work {1142 dent: dent,1143 ignore: self.ig_root.clone(),1144 root_device: root_device,1145 }))1152 })).unwrap();1146 .unwrap();1147 any_work = true;1148 }1149 // ... but there's no need to start workers if we don't need them.1150 if !any_work {
183 unmodified lines
1334 }1335 continue;1336 }1337 };1344 let descend =1345 if let Some(root_device) = work.root_device {1346 match is_same_file_system(root_device, work.dent.path()) {1347 Ok(true) => true,1348 Ok(false) => false,1349 Err(err) => {1350 if (self.f)(Err(err)).is_quit() {1351 self.quit_now();1352 return;1338 let descend = if let Some(root_device) = work.root_device {1339 match is_same_file_system(root_device, work.dent.path()) {1340 Ok(true) => true,1341 Ok(false) => false,1342 Err(err) => {1343 if (self.f)(Err(err)).is_quit() {1344 self.quit_now();1345 return;1353 }1354 false1346 }1347 false1348 }1349 }1357 } else {1358 true1359 };1350 } else {1351 true1352 };13531354 let depth = work.dent.depth();1355 match (self.f)(Ok(work.dent)) {1356 WalkState::Continue => {}
9 unmodified lines
1366 if self.max_depth.map_or(false, |max| depth >= max) {1367 continue;1368 }1369 for result in readdir {1377 let state = self.run_one(1378 &work.ignore,1379 depth + 1,1380 work.root_device,1381 result,1382 );1370 let state = self.run_one(&work.ignore, depth + 1, work.root_device, result);1371 if state.is_quit() {1372 self.quit_now();1373 return;1374 }
56 unmodified lines
1431 return WalkState::Continue;1432 }1433 }1434 let should_skip_path = should_skip_entry(ig, &dent);1447 let should_skip_filesize =1448 if self.max_filesize.is_some() && !dent.is_dir() {1449 skip_filesize(1450 self.max_filesize.unwrap(),1451 dent.path(),1452 &dent.metadata().ok(),1453 )1454 } else {1455 false1456 };1435 let should_skip_filesize = if self.max_filesize.is_some() && !dent.is_dir() {1436 skip_filesize(1437 self.max_filesize.unwrap(),1438 dent.path(),1439 &dent.metadata().ok(),1440 )1441 } else {1442 false1443 };14441445 if !should_skip_path && !should_skip_filesize {1446 self.tx1459 self.tx.send(Message::Work(Work {1460 dent: dent,1461 ignore: ig.clone(),1462 root_device: root_device,1463 })).unwrap();1447 .send(Message::Work(Work {1448 dent: dent,1449 ignore: ig.clone(),1450 root_device: root_device,1451 }))1452 .unwrap();1453 }1454 WalkState::Continue1455 }1456
121 unmodified lines
1578 child_path: &Path,1579 child_depth: usize,1580) -> Result<(), Error> {1581 let hchild = Handle::from_path(child_path).map_err(|err| {1593 Error::from(err).with_path(child_path).with_depth(child_depth)1582 Error::from(err)1583 .with_path(child_path)1584 .with_depth(child_depth)1585 })?;1595 for ig in ig_parent.parents().take_while(|ig| !ig.is_absolute_parent()) {1586 for ig in ig_parent1587 .parents()1588 .take_while(|ig| !ig.is_absolute_parent())1589 {1590 let h = Handle::from_path(ig.path()).map_err(|err| {1597 Error::from(err).with_path(child_path).with_depth(child_depth)1591 Error::from(err)1592 .with_path(child_path)1593 .with_depth(child_depth)1594 })?;1595 if hchild == h {1596 return Err(Error::Loop {1597 ancestor: ig.path().to_path_buf(),1598 child: child_path.to_path_buf(),1599 }1603 }.with_depth(child_depth));1600 .with_depth(child_depth));1601 }1602 }1603 Ok(())1604}16051606// Before calling this function, make sure that you ensure that is really1607// necessary as the arguments imply a file stat.1611fn skip_filesize(1612 max_filesize: u64,1613 path: &Path,1614 ent: &Option<Metadata>1615) -> bool {1608fn skip_filesize(max_filesize: u64, path: &Path, ent: &Option<Metadata>) -> bool {1609 let filesize = match *ent {1610 Some(ref md) => Some(md.len()),1618 None => None1611 None => None,1612 };16131614 if let Some(fs) = filesize {1615 if fs > max_filesize {
6 unmodified lines
1622 false1623 }1624}16251633fn should_skip_entry(1634 ig: &Ignore,1635 dent: &DirEntry,1636) -> bool {1626fn should_skip_entry(ig: &Ignore, dent: &DirEntry) -> bool {1627 let m = ig.matched_dir_entry(dent);1628 if m.is_ignore() {1629 debug!("ignoring {}: {:?}", dent.path().display(), m);1630 true
53 unmodified lines
16841685/// Returns true if and only if the given path is on the same device as the1686/// given root device.1687fn is_same_file_system(root_device: u64, path: &Path) -> Result<bool, Error> {1698 let dent_device = device_num(path)1699 .map_err(|err| Error::Io(err).with_path(path))?;1688 let dent_device = device_num(path).map_err(|err| Error::Io(err).with_path(path))?;1689 Ok(root_device == dent_device)1690}16911692#[cfg(unix)]1704fn device_num<P: AsRef<Path>>(path: P)-> io::Result<u64> {1693fn device_num<P: AsRef<Path>>(path: P) -> io::Result<u64> {1694 use std::os::unix::fs::MetadataExt;16951696 path.as_ref().metadata().map(|md| md.dev())1697}16981710 #[cfg(windows)]1699#[cfg(windows)]1700fn device_num<P: AsRef<Path>>(path: P) -> io::Result<u64> {1712 use winapi_util::{Handle, file};1701 use winapi_util::{file, Handle};17021703 let h = Handle::from_path_any(path)?;1704 file::information(h).map(|info| info.volume_serial_number())1705}17061707#[cfg(not(any(unix, windows)))]1719fn device_num<P: AsRef<Path>>(_: P)-> io::Result<u64> {1708fn device_num<P: AsRef<Path>>(_: P) -> io::Result<u64> {1709 Err(io::Error::new(1710 io::ErrorKind::Other,1711 "walkdir: same_file_system option not supported on this platform",1712 ))
5 unmodified lines
1718 use std::io::Write;1719 use std::path::Path;1720 use std::sync::{Arc, Mutex};17211733 use tests::TempDir;1722 use super::{DirEntry, WalkBuilder, WalkState};1723 use tests::TempDir;17241725 fn wfile<P: AsRef<Path>>(path: P, contents: &str) {1726 let mut file = File::create(path).unwrap();1727 file.write_all(contents.as_bytes()).unwrap();
38 unmodified lines
1766 paths.sort();1767 paths1768 }17691781 fn walk_collect_parallel(1782 prefix: &Path,1783 builder: &WalkBuilder,1784 ) -> Vec<String> {1770 fn walk_collect_parallel(prefix: &Path, builder: &WalkBuilder) -> Vec<String> {1771 let mut paths = vec![];1772 for dent in walk_collect_entries_parallel(builder) {1773 let path = dent.path().strip_prefix(prefix).unwrap();1774 if path.as_os_str().is_empty() {
26 unmodified lines
1801 paths.sort();1802 paths1803 }18041819 fn tmpdir(prefix: &str) -> TempDir {1805 fn tmpdir() -> TempDir {1806 TempDir::new().unwrap()1807 }18081823 fn assert_paths(1824 prefix: &Path,1825 builder: &WalkBuilder,1826 expected: &[&str],1827 ) {1809 fn assert_paths(prefix: &Path, builder: &WalkBuilder, expected: &[&str]) {1810 let got = walk_collect(prefix, builder);1811 assert_eq!(got, mkpaths(expected), "single threaded");1812 let got = walk_collect_parallel(prefix, builder);1813 assert_eq!(got, mkpaths(expected), "parallel");1814 }18151816 #[test]1817 fn no_ignores() {1836 let td = tmpdir("walk-test-");1818 let td = tmpdir();1819 mkdirp(td.path().join("a/b/c"));1820 mkdirp(td.path().join("x/y"));1821 wfile(td.path().join("a/b/foo"), "");1822 wfile(td.path().join("x/y/foo"), "");18231842 assert_paths(td.path(), &WalkBuilder::new(td.path()), &[1843 "x", "x/y", "x/y/foo", "a", "a/b", "a/b/foo", "a/b/c",1844 ]);1824 assert_paths(1825 td.path(),1826 &WalkBuilder::new(td.path()),1827 &["x", "x/y", "x/y/foo", "a", "a/b", "a/b/foo", "a/b/c"],1828 );1829 }18301831 #[test]1832 fn custom_ignore() {1849 let td = tmpdir("walk-test-");1833 let td = tmpdir();1834 let custom_ignore = ".customignore";1835 mkdirp(td.path().join("a"));1836 wfile(td.path().join(custom_ignore), "foo");1837 wfile(td.path().join("foo"), "");
7 unmodified lines
1845 }18461847 #[test]1848 fn custom_ignore_exclusive_use() {1865 let td = tmpdir("walk-test-");1849 let td = tmpdir();1850 let custom_ignore = ".customignore";1851 mkdirp(td.path().join("a"));1852 wfile(td.path().join(custom_ignore), "foo");1853 wfile(td.path().join("foo"), "");
11 unmodified lines
1865 }18661867 #[test]1868 fn gitignore() {1885 let td = tmpdir("walk-test-");1869 let td = tmpdir();1870 mkdirp(td.path().join(".git"));1871 mkdirp(td.path().join("a"));1872 wfile(td.path().join(".gitignore"), "foo");1873 wfile(td.path().join("foo"), "");1874 wfile(td.path().join("a/foo"), "");1875 wfile(td.path().join("bar"), "");1876 wfile(td.path().join("a/bar"), "");18771894 assert_paths(td.path(), &WalkBuilder::new(td.path()), &[1895 "bar", "a", "a/bar",1896 ]);1878 assert_paths(1879 td.path(),1880 &WalkBuilder::new(td.path()),1881 &["bar", "a", "a/bar"],1882 );1883 }18841885 #[test]1886 fn explicit_ignore() {1901 let td = tmpdir("walk-test-");1887 let td = tmpdir();1888 let igpath = td.path().join(".not-an-ignore");1889 mkdirp(td.path().join("a"));1890 wfile(&igpath, "foo");1891 wfile(td.path().join("foo"), "");
7 unmodified lines
1899 }19001901 #[test]1902 fn explicit_ignore_exclusive_use() {1917 let td = tmpdir("walk-test-");1903 let td = tmpdir();1904 let igpath = td.path().join(".not-an-ignore");1905 mkdirp(td.path().join("a"));1906 wfile(&igpath, "foo");1907 wfile(td.path().join("foo"), "");
3 unmodified lines
19111912 let mut builder = WalkBuilder::new(td.path());1913 builder.standard_filters(false);1914 assert!(builder.add_ignore(&igpath).is_none());1929 assert_paths(td.path(), &builder,1930 &[".not-an-ignore", "bar", "a", "a/bar"]);1915 assert_paths(1916 td.path(),1917 &builder,1918 &[".not-an-ignore", "bar", "a", "a/bar"],1919 );1920 }19211922 #[test]1923 fn gitignore_parent() {1935 let td = tmpdir("walk-test-");1924 let td = tmpdir();1925 mkdirp(td.path().join(".git"));1926 mkdirp(td.path().join("a"));1927 wfile(td.path().join(".gitignore"), "foo");1928 wfile(td.path().join("a/foo"), "");
4 unmodified lines
1933 }19341935 #[test]1936 fn max_depth() {1948 let td = tmpdir("walk-test-");1937 let td = tmpdir();1938 mkdirp(td.path().join("a/b/c"));1939 wfile(td.path().join("foo"), "");1940 wfile(td.path().join("a/foo"), "");1941 wfile(td.path().join("a/b/foo"), "");1942 wfile(td.path().join("a/b/c/foo"), "");19431944 let mut builder = WalkBuilder::new(td.path());1956 assert_paths(td.path(), &builder, &[1957 "a", "a/b", "a/b/c", "foo", "a/foo", "a/b/foo", "a/b/c/foo",1958 ]);1945 assert_paths(1946 td.path(),1947 &builder,1948 &["a", "a/b", "a/b/c", "foo", "a/foo", "a/b/foo", "a/b/c/foo"],1949 );1950 assert_paths(td.path(), builder.max_depth(Some(0)), &[]);1951 assert_paths(td.path(), builder.max_depth(Some(1)), &["a", "foo"]);1961 assert_paths(td.path(), builder.max_depth(Some(2)), &[1962 "a", "a/b", "foo", "a/foo",1963 ]);1952 assert_paths(1953 td.path(),1954 builder.max_depth(Some(2)),1955 &["a", "a/b", "foo", "a/foo"],1956 );1957 }19581959 #[test]1960 fn max_filesize() {1968 let td = tmpdir("walk-test-");1961 let td = tmpdir();1962 mkdirp(td.path().join("a/b"));1963 wfile_size(td.path().join("foo"), 0);1964 wfile_size(td.path().join("bar"), 400);1965 wfile_size(td.path().join("baz"), 600);
1 unmodified line
1967 wfile_size(td.path().join("a/bar"), 500);1968 wfile_size(td.path().join("a/baz"), 200);19691970 let mut builder = WalkBuilder::new(td.path());1978 assert_paths(td.path(), &builder, &[1979 "a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz",1980 ]);1981 assert_paths(td.path(), builder.max_filesize(Some(0)), &[1982 "a", "a/b", "foo"1983 ]);1984 assert_paths(td.path(), builder.max_filesize(Some(500)), &[1985 "a", "a/b", "foo", "bar", "a/bar", "a/baz"1986 ]);1987 assert_paths(td.path(), builder.max_filesize(Some(50000)), &[1988 "a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz",1989 ]);1971 assert_paths(1972 td.path(),1973 &builder,1974 &["a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz"],1975 );1976 assert_paths(1977 td.path(),1978 builder.max_filesize(Some(0)),1979 &["a", "a/b", "foo"],1980 );1981 assert_paths(1982 td.path(),1983 builder.max_filesize(Some(500)),1984 &["a", "a/b", "foo", "bar", "a/bar", "a/baz"],1985 );1986 assert_paths(1987 td.path(),1988 builder.max_filesize(Some(50000)),1989 &["a", "a/b", "foo", "bar", "baz", "a/foo", "a/bar", "a/baz"],1990 );1991 }19921993 #[cfg(unix)] // because symlinks on windows are weird1994 #[test]1995 fn symlinks() {1995 let td = tmpdir("walk-test-");1996 let td = tmpdir();1997 mkdirp(td.path().join("a/b"));1998 symlink(td.path().join("a/b"), td.path().join("z"));1999 wfile(td.path().join("a/b/foo"), "");20002001 let mut builder = WalkBuilder::new(td.path());2001 assert_paths(td.path(), &builder, &[2002 "a", "a/b", "a/b/foo", "z",2003 ]);2004 assert_paths(td.path(), &builder.follow_links(true), &[2005 "a", "a/b", "a/b/foo", "z", "z/foo",2006 ]);2002 assert_paths(td.path(), &builder, &["a", "a/b", "a/b/foo", "z"]);2003 assert_paths(2004 td.path(),2005 &builder.follow_links(true),2006 &["a", "a/b", "a/b/foo", "z", "z/foo"],2007 );2008 }20092010 #[cfg(unix)] // because symlinks on windows are weird2011 #[test]2012 fn first_path_not_symlink() {2012 let td = tmpdir("walk-test-");2013 let td = tmpdir();2014 mkdirp(td.path().join("foo"));20152016 let dents = WalkBuilder::new(td.path().join("foo"))2017 .build()
2 unmodified lines
2020 .unwrap();2021 assert_eq!(1, dents.len());2022 assert!(!dents[0].path_is_symlink());20232023 let dents = walk_collect_entries_parallel(2024 &WalkBuilder::new(td.path().join("foo")),2025 );2024 let dents = walk_collect_entries_parallel(&WalkBuilder::new(td.path().join("foo")));2025 assert_eq!(1, dents.len());2026 assert!(!dents[0].path_is_symlink());2027 }20282029 #[cfg(unix)] // because symlinks on windows are weird2030 #[test]2031 fn symlink_loop() {2033 let td = tmpdir("walk-test-");2032 let td = tmpdir();2033 mkdirp(td.path().join("a/b"));2034 symlink(td.path().join("a"), td.path().join("a/b/c"));20352036 let mut builder = WalkBuilder::new(td.path());2038 assert_paths(td.path(), &builder, &[2039 "a", "a/b", "a/b/c",2040 ]);2041 assert_paths(td.path(), &builder.follow_links(true), &[2042 "a", "a/b",2043 ]);2037 assert_paths(td.path(), &builder, &["a", "a/b", "a/b/c"]);2038 assert_paths(td.path(), &builder.follow_links(true), &["a", "a/b"]);2039 }20402041 // It's a little tricky to test the 'same_file_system' option since2042 // we need an environment with more than one file system. We adopt a
11 unmodified lines
2054 }20552056 // If our test directory actually isn't a different volume from /sys,2057 // then this test is meaningless and we shouldn't run it.2063 let td = tmpdir("walk-test-");2058 let td = tmpdir();2059 if device_num(td.path()).unwrap() == device_num("/sys").unwrap() {2060 return;2061 }2062
5 unmodified lines
2068 // permission error. Otherwise, it should just skip over the symlink2069 // completely.2070 let mut builder = WalkBuilder::new(td.path());2071 builder.follow_links(true).same_file_system(true);2077 assert_paths(td.path(), &builder, &[2078 "same_file", "same_file/alink",2079 ]);2072 assert_paths(td.path(), &builder, &["same_file", "same_file/alink"]);2073 }2074}