Skip to content

Commit c4b9ec4

Browse files
committed
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.
1 parent 81eef2d commit c4b9ec4

File tree

2 files changed

+132
-0
lines changed

2 files changed

+132
-0
lines changed

library/std/src/fs/tests.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,14 @@ fn file_test_io_seek_and_write() {
204204
}
205205

206206
#[test]
207+
#[cfg(not(any(
208+
target_os = "android",
209+
target_os = "emscripten",
210+
target_os = "fuchsia",
211+
target_os = "illumos",
212+
target_os = "redox",
213+
target_os = "solaris"
214+
)))]
207215
fn file_lock_multiple_shared() {
208216
let tmpdir = tmpdir();
209217
let filename = &tmpdir.join("file_lock_multiple_shared_test.txt");
@@ -220,6 +228,14 @@ fn file_lock_multiple_shared() {
220228
}
221229

222230
#[test]
231+
#[cfg(not(any(
232+
target_os = "android",
233+
target_os = "emscripten",
234+
target_os = "fuchsia",
235+
target_os = "illumos",
236+
target_os = "redox",
237+
target_os = "solaris"
238+
)))]
223239
fn file_lock_blocking() {
224240
let tmpdir = tmpdir();
225241
let filename = &tmpdir.join("file_lock_blocking_test.txt");
@@ -237,6 +253,14 @@ fn file_lock_blocking() {
237253
}
238254

239255
#[test]
256+
#[cfg(not(any(
257+
target_os = "android",
258+
target_os = "emscripten",
259+
target_os = "fuchsia",
260+
target_os = "illumos",
261+
target_os = "redox",
262+
target_os = "solaris"
263+
)))]
240264
fn file_lock_drop() {
241265
let tmpdir = tmpdir();
242266
let filename = &tmpdir.join("file_lock_dup_test.txt");
@@ -251,6 +275,14 @@ fn file_lock_drop() {
251275
}
252276

253277
#[test]
278+
#[cfg(not(any(
279+
target_os = "android",
280+
target_os = "emscripten",
281+
target_os = "fuchsia",
282+
target_os = "illumos",
283+
target_os = "redox",
284+
target_os = "solaris"
285+
)))]
254286
fn file_lock_dup() {
255287
let tmpdir = tmpdir();
256288
let filename = &tmpdir.join("file_lock_dup_test.txt");

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,16 +1254,64 @@ impl File {
12541254
}
12551255
}
12561256

1257+
#[cfg(not(any(
1258+
target_os = "android",
1259+
target_os = "emscripten",
1260+
target_os = "fuchsia",
1261+
target_os = "illumos",
1262+
target_os = "redox",
1263+
target_os = "solaris"
1264+
)))]
12571265
pub fn lock(&self) -> io::Result<()> {
12581266
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX) })?;
12591267
return Ok(());
12601268
}
12611269

1270+
#[cfg(any(
1271+
target_os = "android",
1272+
target_os = "emscripten",
1273+
target_os = "fuchsia",
1274+
target_os = "illumos",
1275+
target_os = "redox",
1276+
target_os = "solaris"
1277+
))]
1278+
pub fn lock(&self) -> io::Result<()> {
1279+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock() not supported"))
1280+
}
1281+
1282+
#[cfg(not(any(
1283+
target_os = "android",
1284+
target_os = "emscripten",
1285+
target_os = "fuchsia",
1286+
target_os = "illumos",
1287+
target_os = "redox",
1288+
target_os = "solaris"
1289+
)))]
12621290
pub fn lock_shared(&self) -> io::Result<()> {
12631291
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH) })?;
12641292
return Ok(());
12651293
}
12661294

1295+
#[cfg(any(
1296+
target_os = "android",
1297+
target_os = "emscripten",
1298+
target_os = "fuchsia",
1299+
target_os = "illumos",
1300+
target_os = "redox",
1301+
target_os = "solaris"
1302+
))]
1303+
pub fn lock_shared(&self) -> io::Result<()> {
1304+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "lock_shared() not supported"))
1305+
}
1306+
1307+
#[cfg(not(any(
1308+
target_os = "android",
1309+
target_os = "emscripten",
1310+
target_os = "fuchsia",
1311+
target_os = "illumos",
1312+
target_os = "redox",
1313+
target_os = "solaris"
1314+
)))]
12671315
pub fn try_lock(&self) -> io::Result<bool> {
12681316
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_EX | libc::LOCK_NB) });
12691317
if let Err(ref err) = result {
@@ -1275,6 +1323,26 @@ impl File {
12751323
return Ok(true);
12761324
}
12771325

1326+
#[cfg(any(
1327+
target_os = "android",
1328+
target_os = "emscripten",
1329+
target_os = "fuchsia",
1330+
target_os = "illumos",
1331+
target_os = "redox",
1332+
target_os = "solaris"
1333+
))]
1334+
pub fn try_lock(&self) -> io::Result<bool> {
1335+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock() not supported"))
1336+
}
1337+
1338+
#[cfg(not(any(
1339+
target_os = "android",
1340+
target_os = "emscripten",
1341+
target_os = "fuchsia",
1342+
target_os = "illumos",
1343+
target_os = "redox",
1344+
target_os = "solaris"
1345+
)))]
12781346
pub fn try_lock_shared(&self) -> io::Result<bool> {
12791347
let result = cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_SH | libc::LOCK_NB) });
12801348
if let Err(ref err) = result {
@@ -1286,11 +1354,43 @@ impl File {
12861354
return Ok(true);
12871355
}
12881356

1357+
#[cfg(any(
1358+
target_os = "android",
1359+
target_os = "emscripten",
1360+
target_os = "fuchsia",
1361+
target_os = "illumos",
1362+
target_os = "redox",
1363+
target_os = "solaris"
1364+
))]
1365+
pub fn try_lock_shared(&self) -> io::Result<bool> {
1366+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "try_lock_shared() not supported"))
1367+
}
1368+
1369+
#[cfg(not(any(
1370+
target_os = "android",
1371+
target_os = "emscripten",
1372+
target_os = "fuchsia",
1373+
target_os = "illumos",
1374+
target_os = "redox",
1375+
target_os = "solaris"
1376+
)))]
12891377
pub fn unlock(&self) -> io::Result<()> {
12901378
cvt(unsafe { libc::flock(self.as_raw_fd(), libc::LOCK_UN) })?;
12911379
return Ok(());
12921380
}
12931381

1382+
#[cfg(any(
1383+
target_os = "android",
1384+
target_os = "emscripten",
1385+
target_os = "fuchsia",
1386+
target_os = "illumos",
1387+
target_os = "redox",
1388+
target_os = "solaris"
1389+
))]
1390+
pub fn unlock(&self) -> io::Result<()> {
1391+
Err(io::const_io_error!(io::ErrorKind::Unsupported, "unlock() not supported"))
1392+
}
1393+
12941394
pub fn truncate(&self, size: u64) -> io::Result<()> {
12951395
let size: off64_t =
12961396
size.try_into().map_err(|e| io::Error::new(io::ErrorKind::InvalidInput, e))?;

0 commit comments

Comments
 (0)