]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_data_structures/flock.rs
Auto merge of #68248 - JohnTitor:rollup-x0kml5f, r=JohnTitor
[rust.git] / src / librustc_data_structures / flock.rs
index 01f25a054f0a7949635a74a703080f0528c7a287..2a0139fa90d5a2b742cf9c0c11fd3372b3834066 100644 (file)
 cfg_if! {
     if #[cfg(unix)] {
         use std::ffi::{CString, OsStr};
+        use std::mem;
         use std::os::unix::prelude::*;
 
-        #[cfg(any(target_os = "linux", target_os = "android"))]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_pid: libc::pid_t,
-
-                // not actually here, but brings in line with freebsd
-                pub l_sysid: libc::c_int,
-            }
-        }
-
-        #[cfg(target_os = "freebsd")]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_pid: libc::pid_t,
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-                pub l_sysid: libc::c_int,
-            }
-        }
-
-        #[cfg(any(target_os = "dragonfly",
-                  target_os = "netbsd",
-                  target_os = "openbsd"))]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_pid: libc::pid_t,
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-
-                // not actually here, but brings in line with freebsd
-                pub l_sysid: libc::c_int,
-            }
-        }
-
-        #[cfg(target_os = "haiku")]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_pid: libc::pid_t,
-
-                // not actually here, but brings in line with freebsd
-                pub l_sysid: libc::c_int,
-            }
-        }
-
-        #[cfg(any(target_os = "macos", target_os = "ios"))]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_pid: libc::pid_t,
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-
-                // not actually here, but brings in line with freebsd
-                pub l_sysid: libc::c_int,
-            }
-        }
-
-        #[cfg(target_os = "solaris")]
-        mod os {
-            #[repr(C)]
-            pub struct flock {
-                pub l_type: libc::c_short,
-                pub l_whence: libc::c_short,
-                pub l_start: libc::off_t,
-                pub l_len: libc::off_t,
-                pub l_sysid: libc::c_int,
-                pub l_pid: libc::pid_t,
-            }
-        }
-
         #[derive(Debug)]
         pub struct Lock {
             fd: libc::c_int,
@@ -132,19 +45,17 @@ pub fn new(p: &Path,
                 }
 
                 let lock_type = if exclusive {
-                    libc::F_WRLCK as libc::c_short
+                    libc::F_WRLCK
                 } else {
-                    libc::F_RDLCK as libc::c_short
+                    libc::F_RDLCK
                 };
 
-                let flock = os::flock {
-                    l_start: 0,
-                    l_len: 0,
-                    l_pid: 0,
-                    l_whence: libc::SEEK_SET as libc::c_short,
-                    l_type: lock_type,
-                    l_sysid: 0,
-                };
+                let mut flock: libc::flock = unsafe { mem::zeroed() };
+                flock.l_type = lock_type as libc::c_short;
+                flock.l_whence = libc::SEEK_SET as libc::c_short;
+                flock.l_start = 0;
+                flock.l_len = 0;
+
                 let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
                 let ret = unsafe {
                     libc::fcntl(fd, cmd, &flock)
@@ -161,14 +72,12 @@ pub fn new(p: &Path,
 
         impl Drop for Lock {
             fn drop(&mut self) {
-                let flock = os::flock {
-                    l_start: 0,
-                    l_len: 0,
-                    l_pid: 0,
-                    l_whence: libc::SEEK_SET as libc::c_short,
-                    l_type: libc::F_UNLCK as libc::c_short,
-                    l_sysid: 0,
-                };
+                let mut flock: libc::flock = unsafe { mem::zeroed() };
+                flock.l_type = libc::F_UNLCK as libc::c_short;
+                flock.l_whence = libc::SEEK_SET as libc::c_short;
+                flock.l_start = 0;
+                flock.l_len = 0;
+
                 unsafe {
                     libc::fcntl(self.fd, libc::F_SETLK, &flock);
                     libc::close(self.fd);
@@ -178,39 +87,11 @@ fn drop(&mut self) {
     } else if #[cfg(windows)] {
         use std::mem;
         use std::os::windows::prelude::*;
-        use std::os::windows::raw::HANDLE;
         use std::fs::{File, OpenOptions};
-        use std::os::raw::{c_ulong, c_int};
-
-        type DWORD = c_ulong;
-        type BOOL = c_int;
-        type ULONG_PTR = usize;
-
-        type LPOVERLAPPED = *mut OVERLAPPED;
-        const LOCKFILE_EXCLUSIVE_LOCK: DWORD = 0x0000_0002;
-        const LOCKFILE_FAIL_IMMEDIATELY: DWORD = 0x0000_0001;
 
-        const FILE_SHARE_DELETE: DWORD = 0x4;
-        const FILE_SHARE_READ: DWORD = 0x1;
-        const FILE_SHARE_WRITE: DWORD = 0x2;
-
-        #[repr(C)]
-        struct OVERLAPPED {
-            Internal: ULONG_PTR,
-            InternalHigh: ULONG_PTR,
-            Offset: DWORD,
-            OffsetHigh: DWORD,
-            hEvent: HANDLE,
-        }
-
-        extern "system" {
-            fn LockFileEx(hFile: HANDLE,
-                          dwFlags: DWORD,
-                          dwReserved: DWORD,
-                          nNumberOfBytesToLockLow: DWORD,
-                          nNumberOfBytesToLockHigh: DWORD,
-                          lpOverlapped: LPOVERLAPPED) -> BOOL;
-        }
+        use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
+        use winapi::um::fileapi::LockFileEx;
+        use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};
 
         #[derive(Debug)]
         pub struct Lock {