| ... | @@ -829,4 +829,24 @@ pub const File = struct { | ... | @@ -829,4 +829,24 @@ pub const File = struct { |
| 829 | pub fn seekableStream(file: File) SeekableStream { | 829 | pub fn seekableStream(file: File) SeekableStream { |
| 830 | return .{ .context = file }; | 830 | return .{ .context = file }; |
| 831 | } | 831 | } |
| | 832 | |
| | 833 | pub const SetLockError = os.FlockError; |
| | 834 | |
| | 835 | /// Blocks when an incompatible lock is held by another process. |
| | 836 | /// `non_blocking` may be used to make a non-blocking request, |
| | 837 | /// causing this function to possibly return `error.WouldBlock`. |
| | 838 | /// A process may hold only one type of lock (shared or exclusive) on |
| | 839 | /// a file. When a process terminates in any way, the lock is released. |
| | 840 | /// TODO: integrate with async I/O |
| | 841 | pub fn setLock(file: File, lock: Lock, non_blocking: bool) SetLockError!void { |
| | 842 | if (is_windows) { |
| | 843 | @compileError("TODO implement fs.File.setLock for Windows"); |
| | 844 | } |
| | 845 | const non_blocking_flag = if (non_blocking) os.LOCK_NB else @as(i32, 0); |
| | 846 | try os.flock(file.handle, switch (lock) { |
| | 847 | .None => os.LOCK_UN, |
| | 848 | .Shared => os.LOCK_SH | non_blocking_flag, |
| | 849 | .Exclusive => os.LOCK_EX | non_blocking_flag, |
| | 850 | }); |
| | 851 | } |
| 832 | }; | 852 | }; |