<commit_before>//! Simple file-locking apis for each OS.
//!
//! This is not meant to be in the standard library, it does nothing with
//! green/native threading. This is just a bare-bones enough solution for
//! librustdoc, it is not production quality at all.

#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]

use std::io;
use std::path::Path;

cfg_if! {
    if #[cfg(unix)] {
        use std::ffi::{CString, OsStr};
        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,
        }

        impl Lock {
            pub fn new(p: &Path,
                       wait: bool,
                       create: bool,
                       exclusive: bool)
                       -> io::Result<Lock> {
                let os: &OsStr = p.as_ref();
                let buf = CString::new(os.as_bytes()).unwrap();
                let open_flags = if create {
                    libc::O_RDWR | libc::O_CREAT
                } else {
                    libc::O_RDWR
                };

                let fd = unsafe {
                    libc::open(buf.as_ptr(), open_flags,
                               libc::S_IRWXU as libc::c_int)
                };

                if fd < 0 {
                    return Err(io::Error::last_os_error());
                }

                let lock_type = if exclusive {
                    libc::F_WRLCK as libc::c_short
                } else {
                    libc::F_RDLCK as libc::c_short
                };

                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 cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
                let ret = unsafe {
                    libc::fcntl(fd, cmd, &flock)
                };
                if ret == -1 {
                    let err = io::Error::last_os_error();
                    unsafe { libc::close(fd); }
                    Err(err)
                } else {
                    Ok(Lock { fd })
                }
            }
        }

        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,
                };
                unsafe {
                    libc::fcntl(self.fd, libc::F_SETLK, &flock);
                    libc::close(self.fd);
                }<commit_msg>remove bespoke flock bindings<commit_after>//! Simple file-locking apis for each OS.
//!
//! This is not meant to be in the standard library, it does nothing with
//! green/native threading. This is just a bare-bones enough solution for
//! librustdoc, it is not production quality at all.

#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]

use std::io;
use std::path::Path;

cfg_if! {
    if #[cfg(unix)] {
        use std::ffi::{CString, OsStr};
        use std::mem;
        use std::os::unix::prelude::*;

        #[derive(Debug)]
        pub struct Lock {
            fd: libc::c_int,
        }

        impl Lock {
            pub fn new(p: &Path,
                       wait: bool,
                       create: bool,
                       exclusive: bool)
                       -> io::Result<Lock> {
                let os: &OsStr = p.as_ref();
                let buf = CString::new(os.as_bytes()).unwrap();
                let open_flags = if create {
                    libc::O_RDWR | libc::O_CREAT
                } else {
                    libc::O_RDWR
                };

                let fd = unsafe {
                    libc::open(buf.as_ptr(), open_flags,
                               libc::S_IRWXU as libc::c_int)
                };

                if fd < 0 {
                    return Err(io::Error::last_os_error());
                }

                let lock_type = if exclusive {
                    libc::F_WRLCK
                } else {
                    libc::F_RDLCK
                };

                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)
                };
                if ret == -1 {
                    let err = io::Error::last_os_error();
                    unsafe { libc::close(fd); }
                    Err(err)
                } else {
                    Ok(Lock { fd })
                }
            }
        }

        impl Drop for Lock {
            fn drop(&mut self) {
                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);
                }
{'fraction_non_alphanumeric': 0.1075880430836258, 'fraction_numerical': 0.0019363427326636814, 'mean_word_length': 1.0784708249496981, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4212983', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2341, 'n_words': 569}