Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add advisory file locking support. #69

Merged
merged 5 commits into from
Dec 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 142 additions & 0 deletions wasi-filesystem.abi.md
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,145 @@ Size: 16, Alignment: 8

- result<_, [`errno`](#errno)>

----

#### <a href="#descriptor_lock_shared" name="descriptor_lock_shared"></a> `descriptor::lock-shared`

Request a shared advisory lock for an open file.

This requests a *shared* lock; more than one shared lock can be held for
a file at the same time.

If the open file has an exclusive lock, this function downgrades the lock
to a shared lock. If it has a shared lock, this function has no effect.

This requests an *advisory* lock, meaning that the file could be accessed
by other programs that don't hold the lock.

It is unspecified how shared locks interact with locks acquired by
non-WASI programs.

This function blocks until the lock can be acquired.

Not all filesystems support locking; on filesystems which don't support
locking, this function returns `errno::notsup`.

Note: This is similar to `flock(fd, LOCK_SH)` in Unix.
##### Params

- <a href="#descriptor_lock_shared.self" name="descriptor_lock_shared.self"></a> `self`: handle<descriptor>
##### Results

- result<_, [`errno`](#errno)>

----

#### <a href="#descriptor_lock_exclusive" name="descriptor_lock_exclusive"></a> `descriptor::lock-exclusive`

Request an exclusive advisory lock for an open file.

This requests an *exclusive* lock; no other locks may be held for the
file while an exclusive lock is held.

If the open file has a shared lock and there are no exclusive locks held
for the fhile, this function upgrades the lock to an exclusive lock. If the
open file already has an exclusive lock, this function has no effect.

This requests an *advisory* lock, meaning that the file could be accessed
by other programs that don't hold the lock.

It is unspecified whether this function succeeds if the file descriptor
is not opened for writing. It is unspecified how exclusive locks interact
with locks acquired by non-WASI programs.

This function blocks until the lock can be acquired.

Not all filesystems support locking; on filesystems which don't support
locking, this function returns `errno::notsup`.

Note: This is similar to `flock(fd, LOCK_EX)` in Unix.
##### Params

- <a href="#descriptor_lock_exclusive.self" name="descriptor_lock_exclusive.self"></a> `self`: handle<descriptor>
##### Results

- result<_, [`errno`](#errno)>

----

#### <a href="#descriptor_try_lock_shared" name="descriptor_try_lock_shared"></a> `descriptor::try-lock-shared`

Request a shared advisory lock for an open file.

This requests a *shared* lock; more than one shared lock can be held for
a file at the same time.

If the open file has an exclusive lock, this function downgrades the lock
to a shared lock. If it has a shared lock, this function has no effect.

This requests an *advisory* lock, meaning that the file could be accessed
by other programs that don't hold the lock.

It is unspecified how shared locks interact with locks acquired by
non-WASI programs.

This function returns `errno::wouldblock` if the lock cannot be acquired.

Not all filesystems support locking; on filesystems which don't support
locking, this function returns `errno::notsup`.

Note: This is similar to `flock(fd, LOCK_SH | LOCK_NB)` in Unix.
##### Params

- <a href="#descriptor_try_lock_shared.self" name="descriptor_try_lock_shared.self"></a> `self`: handle<descriptor>
##### Results

- result<_, [`errno`](#errno)>

----

#### <a href="#descriptor_try_lock_exclusive" name="descriptor_try_lock_exclusive"></a> `descriptor::try-lock-exclusive`

Request an exclusive advisory lock for an open file.

This requests an *exclusive* lock; no other locks may be held for the
file while an exclusive lock is held.

If the open file has a shared lock and there are no exclusive locks held
for the fhile, this function upgrades the lock to an exclusive lock. If the
open file already has an exclusive lock, this function has no effect.

This requests an *advisory* lock, meaning that the file could be accessed
by other programs that don't hold the lock.

It is unspecified whether this function succeeds if the file descriptor
is not opened for writing. It is unspecified how exclusive locks interact
with locks acquired by non-WASI programs.

This function returns `errno::wouldblock` if the lock cannot be acquired.

Not all filesystems support locking; on filesystems which don't support
locking, this function returns `errno::notsup`.

Note: This is similar to `flock(fd, LOCK_EX | LOCK_NB)` in Unix.
##### Params

- <a href="#descriptor_try_lock_exclusive.self" name="descriptor_try_lock_exclusive.self"></a> `self`: handle<descriptor>
##### Results

- result<_, [`errno`](#errno)>

----

#### <a href="#descriptor_unlock" name="descriptor_unlock"></a> `descriptor::unlock`

Release a shared or exclusive lock on an open file.

Note: This is similar to `flock(fd, LOCK_UN)` in Unix.
##### Params

- <a href="#descriptor_unlock.self" name="descriptor_unlock.self"></a> `self`: handle<descriptor>
##### Results

- result<_, [`errno`](#errno)>

112 changes: 112 additions & 0 deletions wasi-filesystem.wit.md
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,118 @@ change-directory-permissions-at: func(
) -> result<_, errno>
```

## `lock-shared`
```wit
/// Request a shared advisory lock for an open file.
///
/// This requests a *shared* lock; more than one shared lock can be held for
/// a file at the same time.
///
/// If the open file has an exclusive lock, this function downgrades the lock
/// to a shared lock. If it has a shared lock, this function has no effect.
///
/// This requests an *advisory* lock, meaning that the file could be accessed
/// by other programs that don't hold the lock.
///
/// It is unspecified how shared locks interact with locks acquired by
/// non-WASI programs.
///
/// This function blocks until the lock can be acquired.
///
/// Not all filesystems support locking; on filesystems which don't support
/// locking, this function returns `errno::notsup`.
///
/// Note: This is similar to `flock(fd, LOCK_SH)` in Unix.
lock-shared: func() -> result<_, errno>
```

## `lock-exclusive`
```wit
/// Request an exclusive advisory lock for an open file.
///
/// This requests an *exclusive* lock; no other locks may be held for the
/// file while an exclusive lock is held.
///
/// If the open file has a shared lock and there are no exclusive locks held
/// for the fhile, this function upgrades the lock to an exclusive lock. If the
/// open file already has an exclusive lock, this function has no effect.
///
/// This requests an *advisory* lock, meaning that the file could be accessed
/// by other programs that don't hold the lock.
///
/// It is unspecified whether this function succeeds if the file descriptor
/// is not opened for writing. It is unspecified how exclusive locks interact
/// with locks acquired by non-WASI programs.
///
/// This function blocks until the lock can be acquired.
///
/// Not all filesystems support locking; on filesystems which don't support
/// locking, this function returns `errno::notsup`.
///
/// Note: This is similar to `flock(fd, LOCK_EX)` in Unix.
lock-exclusive: func() -> result<_, errno>
```

## `try-lock-shared`
```wit
/// Request a shared advisory lock for an open file.
///
/// This requests a *shared* lock; more than one shared lock can be held for
/// a file at the same time.
///
/// If the open file has an exclusive lock, this function downgrades the lock
/// to a shared lock. If it has a shared lock, this function has no effect.
///
/// This requests an *advisory* lock, meaning that the file could be accessed
/// by other programs that don't hold the lock.
///
/// It is unspecified how shared locks interact with locks acquired by
/// non-WASI programs.
///
/// This function returns `errno::wouldblock` if the lock cannot be acquired.
///
/// Not all filesystems support locking; on filesystems which don't support
/// locking, this function returns `errno::notsup`.
///
/// Note: This is similar to `flock(fd, LOCK_SH | LOCK_NB)` in Unix.
try-lock-shared: func() -> result<_, errno>
```

## `try-lock-exclusive`
```wit
/// Request an exclusive advisory lock for an open file.
///
/// This requests an *exclusive* lock; no other locks may be held for the
/// file while an exclusive lock is held.
///
/// If the open file has a shared lock and there are no exclusive locks held
/// for the fhile, this function upgrades the lock to an exclusive lock. If the
/// open file already has an exclusive lock, this function has no effect.
///
/// This requests an *advisory* lock, meaning that the file could be accessed
/// by other programs that don't hold the lock.
///
/// It is unspecified whether this function succeeds if the file descriptor
/// is not opened for writing. It is unspecified how exclusive locks interact
/// with locks acquired by non-WASI programs.
///
/// This function returns `errno::wouldblock` if the lock cannot be acquired.
///
/// Not all filesystems support locking; on filesystems which don't support
/// locking, this function returns `errno::notsup`.
///
/// Note: This is similar to `flock(fd, LOCK_EX | LOCK_NB)` in Unix.
try-lock-exclusive: func() -> result<_, errno>
```

## `unlock`
```wit
/// Release a shared or exclusive lock on an open file.
///
/// Note: This is similar to `flock(fd, LOCK_UN)` in Unix.
unlock: func() -> result<_, errno>
```

```wit
}
```