summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_data_structures/src/flock/windows.rs
blob: 43e6caaa18dcd1d6cf96f73cf6760ed9a195708d (plain)
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
use std::fs::{File, OpenOptions};
use std::io;
use std::mem;
use std::os::windows::prelude::*;
use std::path::Path;

use winapi::shared::winerror::ERROR_INVALID_FUNCTION;
use winapi::um::fileapi::LockFileEx;
use winapi::um::minwinbase::{LOCKFILE_EXCLUSIVE_LOCK, LOCKFILE_FAIL_IMMEDIATELY, OVERLAPPED};
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};

#[derive(Debug)]
pub struct Lock {
    _file: File,
}

impl Lock {
    pub fn new(p: &Path, wait: bool, create: bool, exclusive: bool) -> io::Result<Lock> {
        assert!(
            p.parent().unwrap().exists(),
            "Parent directory of lock-file must exist: {}",
            p.display()
        );

        let share_mode = FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE;

        let mut open_options = OpenOptions::new();
        open_options.read(true).share_mode(share_mode);

        if create {
            open_options.create(true).write(true);
        }

        debug!("attempting to open lock file `{}`", p.display());
        let file = match open_options.open(p) {
            Ok(file) => {
                debug!("lock file opened successfully");
                file
            }
            Err(err) => {
                debug!("error opening lock file: {}", err);
                return Err(err);
            }
        };

        let ret = unsafe {
            let mut overlapped: OVERLAPPED = mem::zeroed();

            let mut dwFlags = 0;
            if !wait {
                dwFlags |= LOCKFILE_FAIL_IMMEDIATELY;
            }

            if exclusive {
                dwFlags |= LOCKFILE_EXCLUSIVE_LOCK;
            }

            debug!("attempting to acquire lock on lock file `{}`", p.display());
            LockFileEx(file.as_raw_handle(), dwFlags, 0, 0xFFFF_FFFF, 0xFFFF_FFFF, &mut overlapped)
        };
        if ret == 0 {
            let err = io::Error::last_os_error();
            debug!("failed acquiring file lock: {}", err);
            Err(err)
        } else {
            debug!("successfully acquired lock");
            Ok(Lock { _file: file })
        }
    }

    pub fn error_unsupported(err: &io::Error) -> bool {
        err.raw_os_error() == Some(ERROR_INVALID_FUNCTION as i32)
    }
}

// Note that we don't need a Drop impl on Windows: The file is unlocked
// automatically when it's closed.