Skip to content

Commit 8cef1ef

Browse files
Rollup merge of rust-lang#132977 - cberner:fix_solaris, r=tgross35
Fix compilation error on Solaris due to flock usage PR 130999 added the file_lock feature, but libc does not define flock() for the Solaris platform leading to a compilation error. Additionally, I went through all the Tier 2 platforms and read through their documentation to see whether flock was implemented. This turned up 5 more Unix platforms where flock is not supported, even though it may exist in the libc crate. Fixes rust-lang#132921 Related to rust-lang#130999
2 parents daa9c43 + 44f376b commit 8cef1ef

File tree

2 files changed

+108
-0
lines changed

2 files changed

+108
-0
lines changed

std/src/fs/tests.rs

+28
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,13 @@ fn file_test_io_seek_and_write() {
204204
}
205205

206206
#[test]
207+
#[cfg(any(
208+
windows,
209+
target_os = "freebsd",
210+
target_os = "linux",
211+
target_os = "netbsd",
212+
target_vendor = "apple",
213+
))]
207214
fn file_lock_multiple_shared() {
208215
let tmpdir = tmpdir();
209216
let filename = &tmpdir.join("file_lock_multiple_shared_test.txt");
@@ -220,6 +227,13 @@ fn file_lock_multiple_shared() {
220227
}
221228

222229
#[test]
230+
#[cfg(any(
231+
windows,
232+
target_os = "freebsd",
233+
target_os = "linux",
234+
target_os = "netbsd",
235+
target_vendor = "apple",
236+
))]
223237
fn file_lock_blocking() {
224238
let tmpdir = tmpdir();
225239
let filename = &tmpdir.join("file_lock_blocking_test.txt");
@@ -237,6 +251,13 @@ fn file_lock_blocking() {
237251
}
238252

239253
#[test]
254+
#[cfg(any(
255+
windows,
256+
target_os = "freebsd",
257+
target_os = "linux",
258+
target_os = "netbsd",
259+
target_vendor = "apple",
260+
))]
240261
fn file_lock_drop() {
241262
let tmpdir = tmpdir();
242263
let filename = &tmpdir.join("file_lock_dup_test.txt");
@@ -251,6 +272,13 @@ fn file_lock_drop() {
251272
}
252273

253274
#[test]
275+
#[cfg(any(
276+
windows,
277+
target_os = "freebsd",
278+
target_os = "linux",
279+
target_os = "netbsd",
280+
target_vendor = "apple",
281+
))]
254282
fn file_lock_dup() {
255283
let tmpdir = tmpdir();
256284
let filename = &tmpdir.join("file_lock_dup_test.txt");

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

+80
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,54 @@ impl File {
12541254
}
12551255
}
12561256

1257+
#[cfg(any(
1258+
target_os = "freebsd",
1259+
target_os = "linux",
1260+
target_os = "netbsd",
1261+
target_vendor = "apple",
1262+
))]
12571263
pub fn lock(&self) -> io::Result<()> {
12581264
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
12591265
return Ok(());
12601266
}
12611267

1268+
#[cfg(not(any(
1269+
target_os = "freebsd",
1270+
target_os = "linux",
1271+
target_os = "netbsd",
1272+
target_vendor = "apple",
1273+
)))]
1274+
pub fn lock(&self) -> io::Result<()> {
1275+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock() not supported"))
1276+
}
1277+
1278+
#[cfg(any(
1279+
target_os = "freebsd",
1280+
target_os = "linux",
1281+
target_os = "netbsd",
1282+
target_vendor = "apple",
1283+
))]
12621284
pub fn lock_shared(&self) -> io::Result<()> {
12631285
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
12641286
return Ok(());
12651287
}
12661288

1289+
#[cfg(not(any(
1290+
target_os = "freebsd",
1291+
target_os = "linux",
1292+
target_os = "netbsd",
1293+
target_vendor = "apple",
1294+
)))]
1295+
pub fn lock_shared(&self) -> io::Result<()> {
1296+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
1297+
}
1298+
1299+
#[cfg(any(
1300+
target_os = "freebsd",
1301+
target_os = "linux",
1302+
target_os = "netbsd",
1303+
target_vendor = "apple",
1304+
))]
12671305
pub fn try_lock(&self) -> io::Result<bool> {
12681306
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
12691307
if let Err(ref err) = result {
@@ -1275,6 +1313,22 @@ impl File {
12751313
return Ok(true);
12761314
}
12771315

1316+
#[cfg(not(any(
1317+
target_os = "freebsd",
1318+
target_os = "linux",
1319+
target_os = "netbsd",
1320+
target_vendor = "apple",
1321+
)))]
1322+
pub fn try_lock(&self) -> io::Result<bool> {
1323+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock() not supported"))
1324+
}
1325+
1326+
#[cfg(any(
1327+
target_os = "freebsd",
1328+
target_os = "linux",
1329+
target_os = "netbsd",
1330+
target_vendor = "apple",
1331+
))]
12781332
pub fn try_lock_shared(&self) -> io::Result<bool> {
12791333
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
12801334
if let Err(ref err) = result {
@@ -1286,11 +1340,37 @@ impl File {
12861340
return Ok(true);
12871341
}
12881342

1343+
#[cfg(not(any(
1344+
target_os = "freebsd",
1345+
target_os = "linux",
1346+
target_os = "netbsd",
1347+
target_vendor = "apple",
1348+
)))]
1349+
pub fn try_lock_shared(&self) -> io::Result<bool> {
1350+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock_shared() not supported"))
1351+
}
1352+
1353+
#[cfg(any(
1354+
target_os = "freebsd",
1355+
target_os = "linux",
1356+
target_os = "netbsd",
1357+
target_vendor = "apple",
1358+
))]
12891359
pub fn unlock(&self) -> io::Result<()> {
12901360
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
12911361
return Ok(());
12921362
}
12931363

1364+
#[cfg(not(any(
1365+
target_os = "freebsd",
1366+
target_os = "linux",
1367+
target_os = "netbsd",
1368+
target_vendor = "apple",
1369+
)))]
1370+
pub fn unlock(&self) -> io::Result<()> {
1371+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
1372+
}
1373+
12941374
pub fn truncate(&self, size: u64) -> io::Result<()> {
12951375
let size: off64_t =
12961376
size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

0 commit comments

Comments
 (0)