From 988664ac8a9ae3f53341afd5a60d5e11d3f0ba7e Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 20 Mar 2014 13:59:07 -0700 Subject: [PATCH] rustdoc: Fix file locking on windows If the dwShareMode parameter is 0 on windows, it "prevents other processes from opening a file or device if they request delete, read, or write access", which is the opposite of what we want! This changes the 0 parameter to something which will allow multiple processes to open the file and then lock it. --- src/librustdoc/flock.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/librustdoc/flock.rs b/src/librustdoc/flock.rs index f5f755751133c..c1e5d66b1d214 100644 --- a/src/librustdoc/flock.rs +++ b/src/librustdoc/flock.rs @@ -135,6 +135,7 @@ mod imp { use std::libc; use std::mem; use std::os::win32::as_utf16_p; + use std::os; use std::ptr; static LOCKFILE_EXCLUSIVE_LOCK: libc::DWORD = 0x00000002; @@ -160,12 +161,20 @@ mod imp { impl Lock { pub fn new(p: &Path) -> Lock { let handle = as_utf16_p(p.as_str().unwrap(), |p| unsafe { - libc::CreateFileW(p, libc::GENERIC_READ, 0, ptr::mut_null(), + libc::CreateFileW(p, + libc::FILE_GENERIC_READ | + libc::FILE_GENERIC_WRITE, + libc::FILE_SHARE_READ | + libc::FILE_SHARE_DELETE | + libc::FILE_SHARE_WRITE, + ptr::mut_null(), libc::CREATE_ALWAYS, libc::FILE_ATTRIBUTE_NORMAL, ptr::mut_null()) }); - assert!(handle as uint != libc::INVALID_HANDLE_VALUE as uint); + if handle as uint == libc::INVALID_HANDLE_VALUE as uint { + fail!("create file error: {}", os::last_os_error()); + } let mut overlapped: libc::OVERLAPPED = unsafe { mem::init() }; let ret = unsafe { LockFileEx(handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 100, 0, @@ -173,7 +182,8 @@ mod imp { }; if ret == 0 { unsafe { libc::CloseHandle(handle); } - fail!("could not lock `{}`", p.display()) + fail!("could not lock `{}`: {}", p.display(), + os::last_os_error()) } Lock { handle: handle } }