fs_extra icon indicating copy to clipboard operation
fs_extra copied to clipboard

PermissionDenied error when using dir::move_dir on a directory containing a .git directory on Windows

Open WilliamVenner opened this issue 4 years ago • 5 comments
trafficstars

Hi, I have this code:

Code

static DIR_MOVE_OPTIONS: fs_extra::dir::CopyOptions = fs_extra::dir::CopyOptions {
	overwrite: false,
	skip_exist: true,
	buffer_size: 64000,
	copy_inside: true,
	content_only: true,
	depth: 0
};

static FILE_MOVE_OPTIONS: fs_extra::file::CopyOptions = fs_extra::file::CopyOptions {
	overwrite: false,
	skip_exist: true,
	buffer_size: 64000
};

...

fn move_items(&self, from: &Path, to: &Path) -> Result<(), String> {
	/*let mut renamed_from = from.to_owned();
	if from.file_name().unwrap() != to.file_name().unwrap() {
		// move_dir doesn't rename directories when moving(?), so we'll have to do it ourselves
		renamed_from.pop();
		renamed_from.push(&to.file_name().unwrap());
		match fs::rename(from, &renamed_from) {
			Err(error) => { return Err(String::from(format!("{:?}", &error))) },
			Ok(_) => {}
		}
	}*/

	match match from.is_dir() {
		true => fs_extra::dir::move_dir(from, to, &DIR_MOVE_OPTIONS),
		false => fs_extra::file::move_file(from, to, &FILE_MOVE_OPTIONS)
	} {
		Err(error) => Err(String::from(format!("{:?}", &error))),
		Ok(_) => Ok(())
	}
}

(#24 is why I have that comment block there)

Error

Error { kind: PermissionDenied, message: \"Access is denied. (os error 5)\" }
Backtrace

thread 'main' panicked at 'Blackhole PANIC: Failed to move file/folder to Blackhole ("Error { kind: PermissionDenied, message: \"Access is denied. (os error 5)\" }")
"D:\\Servers\\Garry\'s Mod\\GarrysMod\\addons\\rrrrrrrrrrrrrrrrrrrr\\_bkeypads-4.0.3" -> "C:\\Users\\billy\\$BLACKHOLE\\_bkeypads-4.0.3 (14)"', src\show.rs:7:13     
stack backtrace:
   0:     0x7ff6270dff49 - std::backtrace_rs::backtrace::dbghelp::trace
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\dbghelp.rs:98
   1:     0x7ff6270dff49 - std::backtrace_rs::backtrace::trace_unsynchronized
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\..\..\backtrace\src\backtrace\mod.rs:66    
   2:     0x7ff6270dff49 - std::sys_common::backtrace::_print_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:79
   3:     0x7ff6270dff49 - std::sys_common::backtrace::_print::{{impl}}::fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:58
   4:     0x7ff6270f69ab - core::fmt::write
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\core\src\fmt\mod.rs:1080
   5:     0x7ff6270db8f8 - std::io::Write::write_fmt<std::sys::windows::stdio::Stderr>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\io\mod.rs:1516
   6:     0x7ff6270e2a44 - std::sys_common::backtrace::_print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:61
   7:     0x7ff6270e2a44 - std::sys_common::backtrace::print
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:48
   8:     0x7ff6270e2a44 - std::panicking::default_hook::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:208
   9:     0x7ff6270e2628 - std::panicking::default_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:227
  10:     0x7ff6270e32ff - std::panicking::rust_panic_with_hook
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:577
  11:     0x7ff6270e2e65 - std::panicking::begin_panic_handler::{{closure}}
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:484
  12:     0x7ff6270e082f - std::sys_common::backtrace::__rust_end_short_backtrace<closure-0,!>
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\sys_common\backtrace.rs:153
  13:     0x7ff6270e2e19 - std::panicking::begin_panic_handler
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:483
  14:     0x7ff6270e2dcc - std::panicking::begin_panic_fmt
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:437
  15:     0x7ff62705044e - blackhole::show::show::Show::panic
                               at C:\Users\billy\Documents\GitHub\blackhole\src\show.rs:7
  16:     0x7ff627040761 - blackhole::blackhole::blackhole::Blackhole::send
                               at C:\Users\billy\Documents\GitHub\blackhole\src\blackhole.rs:149
  17:     0x7ff62703c731 - blackhole::main
                               at C:\Users\billy\Documents\GitHub\blackhole\src\main.rs:70
  18:     0x7ff627038b2b - core::ops::function::FnOnce::call_once<fn(),tuple<>>
                               at C:\Users\billy\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
  19:     0x7ff62704472b - std::sys_common::backtrace::__rust_begin_short_backtrace<fn(),tuple<>>
                               at C:\Users\billy\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\sys_common\backtrace.rs:137
  20:     0x7ff6270451f1 - std::rt::lang_start::{{closure}}<tuple<>>
                               at C:\Users\billy\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:66
  21:     0x7ff6270e3503 - core::ops::function::impls::{{impl}}::call_once
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\library\core\src\ops\function.rs:280
  22:     0x7ff6270e3503 - std::panicking::try::do_call
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:381
  23:     0x7ff6270e3503 - std::panicking::try
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panicking.rs:345
  24:     0x7ff6270e3503 - std::panic::catch_unwind
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\panic.rs:382
  25:     0x7ff6270e3503 - std::rt::lang_start_internal
                               at /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4\/library\std\src\rt.rs:51
  26:     0x7ff6270451c3 - std::rt::lang_start<tuple<>>
                               at C:\Users\billy\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\rt.rs:65
  27:     0x7ff62703c940 - main
  28:     0x7ff6270fb394 - invoke_main
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
  29:     0x7ff6270fb394 - __scrt_common_main_seh
                               at d:\agent\_work\63\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
  30:     0x7ffe75ec7c24 - BaseThreadInitThunk
  31:     0x7ffe77e6d721 - RtlUserThreadStart

Screenshot of the directory

image

What works

dir::move_dir successfully creates all the directories and nested directories:

image

The only file copied is the first file in .git/objects/00:

image

It does appear to contain data:

image

Notes

  • My program is running with administrative privileges
  • I initially thought it had something to do Windows Subsystem for Linux, but using cmd and Windows git to initialize .git still causes the issue
  • Deleting the .git folder stops the issue from happening
  • Moving the directory myself using Windows Explorer works with no issue, and does not prompt for anything like a file being in use

WilliamVenner avatar Feb 19 '21 04:02 WilliamVenner

Very interesting case, i will check it

webdesus avatar Mar 05 '21 09:03 webdesus

Is this still being worked on?

Frazzer951 avatar Jun 23 '22 03:06 Frazzer951

@Frazzer951 Do you have the same problem? Unfortunately, I can't reproduce it... And second question: Do you move the directory from one disk to another?

webdesus avatar Jun 25 '22 12:06 webdesus

@webdesus Yea it was the same issue, It was when moving a folder that contained a .git directory. It was moving between folders on my C drive. I.E. from C:\dev_1\folder to C:\dev_2\folder. I ended up just writing my own solution using the walkdir crate

Frazzer951 avatar Jun 25 '22 20:06 Frazzer951

I encountered the same problem. In my opinion, it was caused by hidden files in Windows. The target_path contains hidden files, and the original_path also has this file. An error of PermissionDenied is reported when overwriting occurs during the execution of move_file.

let option = fs_extra::file::CopyOptions::new().overwrite(true);
move_file(original_path, &target_path, &option)?;

mcthesw avatar Mar 29 '24 06:03 mcthesw