Skip to content

Commit 353aa6d

Browse files
committed
use cfg_select for unix file lock implementations
1 parent b58125b commit 353aa6d

1 file changed

Lines changed: 123 additions & 178 deletions

File tree

library/std/src/sys/fs/unix.rs

Lines changed: 123 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,201 +1441,146 @@ impl File {
14411441
}
14421442
}
14431443

1444-
#[cfg(any(
1445-
target_os = "freebsd",
1446-
target_os = "fuchsia",
1447-
target_os = "hurd",
1448-
target_os = "linux",
1449-
target_os = "netbsd",
1450-
target_os = "openbsd",
1451-
target_os = "cygwin",
1452-
target_os = "illumos",
1453-
target_os = "aix",
1454-
target_os = "android",
1455-
target_vendor = "apple",
1456-
))]
14571444
pub fn lock(&self) -> io::Result<()> {
1458-
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
1459-
return Ok(());
1460-
}
1461-
1462-
#[cfg(not(any(
1463-
target_os = "freebsd",
1464-
target_os = "fuchsia",
1465-
target_os = "hurd",
1466-
target_os = "linux",
1467-
target_os = "netbsd",
1468-
target_os = "openbsd",
1469-
target_os = "cygwin",
1470-
target_os = "illumos",
1471-
target_os = "aix",
1472-
target_os = "android",
1473-
target_vendor = "apple",
1474-
)))]
1475-
pub fn lock(&self) -> io::Result<()> {
1476-
Err(io::const_error!(io::ErrorKind::Unsupported, "lock() not supported"))
1477-
}
1478-
1479-
#[cfg(any(
1480-
target_os = "freebsd",
1481-
target_os = "fuchsia",
1482-
target_os = "hurd",
1483-
target_os = "linux",
1484-
target_os = "netbsd",
1485-
target_os = "openbsd",
1486-
target_os = "cygwin",
1487-
target_os = "illumos",
1488-
target_os = "aix",
1489-
target_os = "android",
1490-
target_vendor = "apple",
1491-
))]
1492-
pub fn lock_shared(&self) -> io::Result<()> {
1493-
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
1494-
return Ok(());
1445+
cfg_select! {
1446+
any(
1447+
target_os = "freebsd",
1448+
target_os = "fuchsia",
1449+
target_os = "hurd",
1450+
target_os = "linux",
1451+
target_os = "netbsd",
1452+
target_os = "openbsd",
1453+
target_os = "cygwin",
1454+
target_os = "illumos",
1455+
target_os = "aix",
1456+
target_os = "android",
1457+
target_vendor = "apple",
1458+
) => {
1459+
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
1460+
return Ok(());
1461+
}
1462+
_ => {
1463+
Err(io::const_error!(io::ErrorKind::Unsupported, "lock() not supported"))
1464+
}
1465+
}
14951466
}
14961467

1497-
#[cfg(not(any(
1498-
target_os = "freebsd",
1499-
target_os = "fuchsia",
1500-
target_os = "hurd",
1501-
target_os = "linux",
1502-
target_os = "netbsd",
1503-
target_os = "openbsd",
1504-
target_os = "cygwin",
1505-
target_os = "illumos",
1506-
target_os = "aix",
1507-
target_os = "android",
1508-
target_vendor = "apple",
1509-
)))]
15101468
pub fn lock_shared(&self) -> io::Result<()> {
1511-
Err(io::const_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
1512-
}
1513-
1514-
#[cfg(any(
1515-
target_os = "freebsd",
1516-
target_os = "fuchsia",
1517-
target_os = "hurd",
1518-
target_os = "linux",
1519-
target_os = "netbsd",
1520-
target_os = "openbsd",
1521-
target_os = "cygwin",
1522-
target_os = "illumos",
1523-
target_os = "aix",
1524-
target_os = "android",
1525-
target_vendor = "apple",
1526-
))]
1527-
pub fn try_lock(&self) -> Result<(), TryLockError> {
1528-
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
1529-
if let Err(err) = result {
1530-
if err.kind() == io::ErrorKind::WouldBlock {
1531-
Err(TryLockError::WouldBlock)
1532-
} else {
1533-
Err(TryLockError::Error(err))
1469+
cfg_select! {
1470+
any(
1471+
target_os = "freebsd",
1472+
target_os = "fuchsia",
1473+
target_os = "hurd",
1474+
target_os = "linux",
1475+
target_os = "netbsd",
1476+
target_os = "openbsd",
1477+
target_os = "cygwin",
1478+
target_os = "illumos",
1479+
target_os = "aix",
1480+
target_os = "android",
1481+
target_vendor = "apple",
1482+
) => {
1483+
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
1484+
return Ok(());
1485+
}
1486+
_ => {
1487+
Err(io::const_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
15341488
}
1535-
} else {
1536-
Ok(())
15371489
}
15381490
}
15391491

1540-
#[cfg(not(any(
1541-
target_os = "freebsd",
1542-
target_os = "fuchsia",
1543-
target_os = "hurd",
1544-
target_os = "linux",
1545-
target_os = "netbsd",
1546-
target_os = "openbsd",
1547-
target_os = "cygwin",
1548-
target_os = "illumos",
1549-
target_os = "aix",
1550-
target_os = "android",
1551-
target_vendor = "apple",
1552-
)))]
15531492
pub fn try_lock(&self) -> Result<(), TryLockError> {
1554-
Err(TryLockError::Error(io::const_error!(
1555-
io::ErrorKind::Unsupported,
1556-
"try_lock() not supported"
1557-
)))
1558-
}
1559-
1560-
#[cfg(any(
1561-
target_os = "freebsd",
1562-
target_os = "fuchsia",
1563-
target_os = "hurd",
1564-
target_os = "linux",
1565-
target_os = "netbsd",
1566-
target_os = "openbsd",
1567-
target_os = "cygwin",
1568-
target_os = "illumos",
1569-
target_os = "aix",
1570-
target_os = "android",
1571-
target_vendor = "apple",
1572-
))]
1573-
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
1574-
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
1575-
if let Err(err) = result {
1576-
if err.kind() == io::ErrorKind::WouldBlock {
1577-
Err(TryLockError::WouldBlock)
1578-
} else {
1579-
Err(TryLockError::Error(err))
1493+
cfg_select! {
1494+
any(
1495+
target_os = "freebsd",
1496+
target_os = "fuchsia",
1497+
target_os = "hurd",
1498+
target_os = "linux",
1499+
target_os = "netbsd",
1500+
target_os = "openbsd",
1501+
target_os = "cygwin",
1502+
target_os = "illumos",
1503+
target_os = "aix",
1504+
target_os = "android",
1505+
target_vendor = "apple",
1506+
) => {
1507+
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
1508+
if let Err(err) = result {
1509+
if err.kind() == io::ErrorKind::WouldBlock {
1510+
Err(TryLockError::WouldBlock)
1511+
} else {
1512+
Err(TryLockError::Error(err))
1513+
}
1514+
} else {
1515+
Ok(())
1516+
}
1517+
}
1518+
_ => {
1519+
Err(TryLockError::Error(io::const_error!(
1520+
io::ErrorKind::Unsupported,
1521+
"try_lock() not supported"
1522+
)))
15801523
}
1581-
} else {
1582-
Ok(())
15831524
}
15841525
}
15851526

1586-
#[cfg(not(any(
1587-
target_os = "freebsd",
1588-
target_os = "fuchsia",
1589-
target_os = "hurd",
1590-
target_os = "linux",
1591-
target_os = "netbsd",
1592-
target_os = "openbsd",
1593-
target_os = "cygwin",
1594-
target_os = "illumos",
1595-
target_os = "aix",
1596-
target_os = "android",
1597-
target_vendor = "apple",
1598-
)))]
15991527
pub fn try_lock_shared(&self) -> Result<(), TryLockError> {
1600-
Err(TryLockError::Error(io::const_error!(
1601-
io::ErrorKind::Unsupported,
1602-
"try_lock_shared() not supported"
1603-
)))
1604-
}
1605-
1606-
#[cfg(any(
1607-
target_os = "freebsd",
1608-
target_os = "fuchsia",
1609-
target_os = "hurd",
1610-
target_os = "linux",
1611-
target_os = "netbsd",
1612-
target_os = "openbsd",
1613-
target_os = "cygwin",
1614-
target_os = "illumos",
1615-
target_os = "aix",
1616-
target_os = "android",
1617-
target_vendor = "apple",
1618-
))]
1619-
pub fn unlock(&self) -> io::Result<()> {
1620-
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
1621-
return Ok(());
1528+
cfg_select! {
1529+
any(
1530+
target_os = "freebsd",
1531+
target_os = "fuchsia",
1532+
target_os = "hurd",
1533+
target_os = "linux",
1534+
target_os = "netbsd",
1535+
target_os = "openbsd",
1536+
target_os = "cygwin",
1537+
target_os = "illumos",
1538+
target_os = "aix",
1539+
target_os = "android",
1540+
target_vendor = "apple",
1541+
) => {
1542+
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
1543+
if let Err(err) = result {
1544+
if err.kind() == io::ErrorKind::WouldBlock {
1545+
Err(TryLockError::WouldBlock)
1546+
} else {
1547+
Err(TryLockError::Error(err))
1548+
}
1549+
} else {
1550+
Ok(())
1551+
}
1552+
}
1553+
_ => {
1554+
Err(TryLockError::Error(io::const_error!(
1555+
io::ErrorKind::Unsupported,
1556+
"try_lock_shared() not supported"
1557+
)))
1558+
}
1559+
}
16221560
}
16231561

1624-
#[cfg(not(any(
1625-
target_os = "freebsd",
1626-
target_os = "fuchsia",
1627-
target_os = "hurd",
1628-
target_os = "linux",
1629-
target_os = "netbsd",
1630-
target_os = "openbsd",
1631-
target_os = "cygwin",
1632-
target_os = "illumos",
1633-
target_os = "aix",
1634-
target_os = "android",
1635-
target_vendor = "apple",
1636-
)))]
16371562
pub fn unlock(&self) -> io::Result<()> {
1638-
Err(io::const_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
1563+
cfg_select! {
1564+
any(
1565+
target_os = "freebsd",
1566+
target_os = "fuchsia",
1567+
target_os = "hurd",
1568+
target_os = "linux",
1569+
target_os = "netbsd",
1570+
target_os = "openbsd",
1571+
target_os = "cygwin",
1572+
target_os = "illumos",
1573+
target_os = "aix",
1574+
target_os = "android",
1575+
target_vendor = "apple",
1576+
) => {
1577+
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
1578+
return Ok(());
1579+
}
1580+
_ => {
1581+
Err(io::const_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
1582+
}
1583+
}
16391584
}
16401585

16411586
pub fn truncate(&self, size: u64) -> io::Result<()> {

0 commit comments

Comments
 (0)