@@ -1293,13 +1293,19 @@ impl File {
1293
1293
return Ok ( ( ) ) ;
1294
1294
}
1295
1295
1296
+ #[ cfg( any( target_os = "solaris" , ) ) ]
1297
+ pub fn lock ( & self ) -> io:: Result < ( ) > {
1298
+ self . emulate_flock ( libc:: F_WRLCK , libc:: F_SETLK ) ;
1299
+ }
1300
+
1296
1301
#[ cfg( not( any(
1297
1302
target_os = "freebsd" ,
1298
1303
target_os = "fuchsia" ,
1299
1304
target_os = "linux" ,
1300
1305
target_os = "netbsd" ,
1301
1306
target_os = "openbsd" ,
1302
1307
target_os = "cygwin" ,
1308
+ target_os = "solaris" ,
1303
1309
target_vendor = "apple" ,
1304
1310
) ) ) ]
1305
1311
pub fn lock ( & self ) -> io:: Result < ( ) > {
@@ -1320,13 +1326,19 @@ impl File {
1320
1326
return Ok ( ( ) ) ;
1321
1327
}
1322
1328
1329
+ #[ cfg( target_os = "solaris" ) ]
1330
+ pub fn lock_shared ( & self ) -> io:: Result < ( ) > {
1331
+ self . emulate_flock ( libc:: F_RDLCK , libc:: F_SETLK ) ;
1332
+ }
1333
+
1323
1334
#[ cfg( not( any(
1324
1335
target_os = "freebsd" ,
1325
1336
target_os = "fuchsia" ,
1326
1337
target_os = "linux" ,
1327
1338
target_os = "netbsd" ,
1328
1339
target_os = "openbsd" ,
1329
1340
target_os = "cygwin" ,
1341
+ target_os = "solaris" ,
1330
1342
target_vendor = "apple" ,
1331
1343
) ) ) ]
1332
1344
pub fn lock_shared ( & self ) -> io:: Result < ( ) > {
@@ -1355,13 +1367,28 @@ impl File {
1355
1367
}
1356
1368
}
1357
1369
1370
+ #[ cfg( target_os = "solaris" ) ]
1371
+ pub fn try_lock ( & self ) -> io:: Result < ( ) > {
1372
+ let result = self . emulate_flock ( libc:: F_WRLCK , libc:: F_SETLKW ) ;
1373
+ if let Err ( err) = result {
1374
+ if err. kind ( ) == io:: ErrorKind :: WouldBlock {
1375
+ Err ( TryLockError :: WouldBlock )
1376
+ } else {
1377
+ Err ( TryLockError :: Error ( err) )
1378
+ }
1379
+ } else {
1380
+ Ok ( ( ) )
1381
+ }
1382
+ }
1383
+
1358
1384
#[ cfg( not( any(
1359
1385
target_os = "freebsd" ,
1360
1386
target_os = "fuchsia" ,
1361
1387
target_os = "linux" ,
1362
1388
target_os = "netbsd" ,
1363
1389
target_os = "openbsd" ,
1364
1390
target_os = "cygwin" ,
1391
+ target_os = "solaris" ,
1365
1392
target_vendor = "apple" ,
1366
1393
) ) ) ]
1367
1394
pub fn try_lock ( & self ) -> Result < ( ) , TryLockError > {
@@ -1393,13 +1420,28 @@ impl File {
1393
1420
}
1394
1421
}
1395
1422
1423
+ #[ cfg( target_os = "solaris" ) ]
1424
+ pub fn try_lock_shared ( & self ) -> io:: Result < ( ) > {
1425
+ let result = self . emulate_flock ( libc:: F_RDLCK , libc:: F_SETLKW ) ;
1426
+ if let Err ( err) = result {
1427
+ if err. kind ( ) == io:: ErrorKind :: WouldBlock {
1428
+ Err ( TryLockError :: WouldBlock )
1429
+ } else {
1430
+ Err ( TryLockError :: Error ( err) )
1431
+ }
1432
+ } else {
1433
+ Ok ( ( ) )
1434
+ }
1435
+ }
1436
+
1396
1437
#[ cfg( not( any(
1397
1438
target_os = "freebsd" ,
1398
1439
target_os = "fuchsia" ,
1399
1440
target_os = "linux" ,
1400
1441
target_os = "netbsd" ,
1401
1442
target_os = "openbsd" ,
1402
1443
target_os = "cygwin" ,
1444
+ target_os = "solaris" ,
1403
1445
target_vendor = "apple" ,
1404
1446
) ) ) ]
1405
1447
pub fn try_lock_shared ( & self ) -> Result < ( ) , TryLockError > {
@@ -1423,19 +1465,34 @@ impl File {
1423
1465
return Ok ( ( ) ) ;
1424
1466
}
1425
1467
1468
+ #[ cfg( target_os = "solaris" ) ]
1469
+ pub fn unlock ( & self ) -> io:: Result < ( ) > {
1470
+ self . emulate_flock ( libc:: F_UNLCK , libc:: F_SETLK )
1471
+ }
1472
+
1426
1473
#[ cfg( not( any(
1427
1474
target_os = "freebsd" ,
1428
1475
target_os = "fuchsia" ,
1429
1476
target_os = "linux" ,
1430
1477
target_os = "netbsd" ,
1431
1478
target_os = "openbsd" ,
1432
1479
target_os = "cygwin" ,
1480
+ target_os = "solaris" ,
1433
1481
target_vendor = "apple" ,
1434
1482
) ) ) ]
1435
1483
pub fn unlock ( & self ) -> io:: Result < ( ) > {
1436
1484
Err ( io:: const_error!( io:: ErrorKind :: Unsupported , "unlock() not supported" ) )
1437
1485
}
1438
1486
1487
+ /// Emulates flock by `fcntl`.
1488
+ #[ cfg( any( target_os = "solaris" , ) ) ]
1489
+ fn emulate_flock ( & self , lock_type : libc:: c_int , cmd : libc:: c_int ) -> Result < ( ) > {
1490
+ let mut flock: libc:: flock = unsafe { mem:: zeroed ( ) } ;
1491
+ flock. l_whence = libc:: SEEK_SET ;
1492
+ flock. l_type = lock_type;
1493
+ cvt ( unsafe { libc:: fcntl ( file. as_raw_fd ( ) , cmd, & flock) } )
1494
+ }
1495
+
1439
1496
pub fn truncate ( & self , size : u64 ) -> io:: Result < ( ) > {
1440
1497
let size: off64_t =
1441
1498
size. try_into ( ) . map_err ( |e| io:: Error :: new ( io:: ErrorKind :: InvalidInput , e) ) ?;
0 commit comments