Skip to content

Commit

Permalink
flock: Use fcntl constants directly from libc crate on Unix targets
Browse files Browse the repository at this point in the history
Since the values for the fcntl constants can vary from architecture
to architecture, it is better to use the values defined in the libc
crate instead of assigning literals in the flock code which would
make the assumption that all architectures use the same values.

Fixes #57007
  • Loading branch information
glaubitz committed Jan 6, 2019
1 parent 1e903c3 commit 1c4823e
Showing 1 changed file with 5 additions and 41 deletions.
46 changes: 5 additions & 41 deletions src/librustc_data_structures/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,6 @@ cfg_if! {
// not actually here, but brings in line with freebsd
pub l_sysid: libc::c_int,
}

pub const F_RDLCK: libc::c_short = 0;
pub const F_WRLCK: libc::c_short = 1;
pub const F_UNLCK: libc::c_short = 2;
pub const F_SETLK: libc::c_int = 6;
pub const F_SETLKW: libc::c_int = 7;
}

#[cfg(target_os = "freebsd")]
Expand All @@ -52,12 +46,6 @@ cfg_if! {
pub l_whence: libc::c_short,
pub l_sysid: libc::c_int,
}

pub const F_RDLCK: libc::c_short = 1;
pub const F_UNLCK: libc::c_short = 2;
pub const F_WRLCK: libc::c_short = 3;
pub const F_SETLK: libc::c_int = 12;
pub const F_SETLKW: libc::c_int = 13;
}

#[cfg(any(target_os = "dragonfly",
Expand All @@ -78,12 +66,6 @@ cfg_if! {
// not actually here, but brings in line with freebsd
pub l_sysid: libc::c_int,
}

pub const F_RDLCK: libc::c_short = 1;
pub const F_UNLCK: libc::c_short = 2;
pub const F_WRLCK: libc::c_short = 3;
pub const F_SETLK: libc::c_int = 8;
pub const F_SETLKW: libc::c_int = 9;
}

#[cfg(target_os = "haiku")]
Expand All @@ -101,12 +83,6 @@ cfg_if! {
// not actually here, but brings in line with freebsd
pub l_sysid: libc::c_int,
}

pub const F_RDLCK: libc::c_short = 0x0040;
pub const F_UNLCK: libc::c_short = 0x0200;
pub const F_WRLCK: libc::c_short = 0x0400;
pub const F_SETLK: libc::c_int = 0x0080;
pub const F_SETLKW: libc::c_int = 0x0100;
}

#[cfg(any(target_os = "macos", target_os = "ios"))]
Expand All @@ -124,12 +100,6 @@ cfg_if! {
// not actually here, but brings in line with freebsd
pub l_sysid: libc::c_int,
}

pub const F_RDLCK: libc::c_short = 1;
pub const F_UNLCK: libc::c_short = 2;
pub const F_WRLCK: libc::c_short = 3;
pub const F_SETLK: libc::c_int = 8;
pub const F_SETLKW: libc::c_int = 9;
}

#[cfg(target_os = "solaris")]
Expand All @@ -145,12 +115,6 @@ cfg_if! {
pub l_sysid: libc::c_int,
pub l_pid: libc::pid_t,
}

pub const F_RDLCK: libc::c_short = 1;
pub const F_WRLCK: libc::c_short = 2;
pub const F_UNLCK: libc::c_short = 3;
pub const F_SETLK: libc::c_int = 6;
pub const F_SETLKW: libc::c_int = 7;
}

#[derive(Debug)]
Expand Down Expand Up @@ -182,9 +146,9 @@ cfg_if! {
}

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

let flock = os::flock {
Expand All @@ -195,7 +159,7 @@ cfg_if! {
l_type: lock_type,
l_sysid: 0,
};
let cmd = if wait { os::F_SETLKW } else { os::F_SETLK };
let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
let ret = unsafe {
libc::fcntl(fd, cmd, &flock)
};
Expand All @@ -216,11 +180,11 @@ cfg_if! {
l_len: 0,
l_pid: 0,
l_whence: libc::SEEK_SET as libc::c_short,
l_type: os::F_UNLCK,
l_type: libc::F_UNLCK as libc::c_short,
l_sysid: 0,
};
unsafe {
libc::fcntl(self.fd, os::F_SETLK, &flock);
libc::fcntl(self.fd, libc::F_SETLK, &flock);
libc::close(self.fd);
}
}
Expand Down

0 comments on commit 1c4823e

Please sign in to comment.