Skip to content

Commit

Permalink
feat(ext/fs): stabilize Deno.FsFile.unlock[Sync]() and `Deno.FsFile…
Browse files Browse the repository at this point in the history
….lock[Sync]()` (denoland#23754)

Related denoland#22230
CC @dyedgreen
  • Loading branch information
iuioiua committed May 22, 2024
1 parent 8ea9370 commit 4908d45
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 20 deletions.
6 changes: 4 additions & 2 deletions cli/tools/test/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,12 @@ pub const OP_DETAILS: phf::Map<&'static str, [&'static str; 2]> = phf_map! {
"op_fs_events_poll" => ["get the next file system event", "breaking out of a for await loop looping over `Deno.FsEvents`"],
"op_fs_fdatasync_async" => ["flush pending data operations for a file to disk", "awaiting the result of a `Deno.fdatasync` or `Deno.FsFile.syncData` call"],
"op_fs_file_stat_async" => ["get file metadata", "awaiting the result of a `Deno.fstat` or `Deno.FsFile.stat` call"],
"op_fs_flock_async" => ["lock a file", "awaiting the result of a `Deno.flock` or `Deno.FsFile.lock` call"],
"op_fs_flock_async_unstable" => ["lock a file", "awaiting the result of a `Deno.flock` call"],
"op_fs_flock_async" => ["lock a file", "awaiting the result of a `Deno.FsFile.lock` call"],
"op_fs_fsync_async" => ["flush pending data operations for a file to disk", "awaiting the result of a `Deno.fsync` or `Deno.FsFile.sync` call"],
"op_fs_ftruncate_async" => ["truncate a file", "awaiting the result of a `Deno.ftruncate` or `Deno.FsFile.truncate` call"],
"op_fs_funlock_async" => ["unlock a file", "awaiting the result of a `Deno.funlock` or `Deno.FsFile.unlock` call"],
"op_fs_funlock_async_unstable" => ["unlock a file", "awaiting the result of a `Deno.funlock` call"],
"op_fs_funlock_async" => ["unlock a file", "awaiting the result of a `Deno.FsFile.unlock` call"],
"op_fs_futime_async" => ["change file timestamps", "awaiting the result of a `Deno.futime` or `Deno.FsFile.utime` call"],
"op_fs_link_async" => ["create a hard link", "awaiting the result of a `Deno.link` call"],
"op_fs_lstat_async" => ["get file metadata", "awaiting the result of a `Deno.lstat` call"],
Expand Down
12 changes: 4 additions & 8 deletions cli/tsc/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2682,27 +2682,23 @@ declare namespace Deno {
* ```
*/
setRaw(mode: boolean, options?: SetRawOptions): void;
/** **UNSTABLE**: New API, yet to be vetted.
*
/**
* Acquire an advisory file-system lock for the file.
*
* @param [exclusive=false]
*/
lock(exclusive?: boolean): Promise<void>;
/** **UNSTABLE**: New API, yet to be vetted.
*
/**
* Synchronously acquire an advisory file-system lock synchronously for the file.
*
* @param [exclusive=false]
*/
lockSync(exclusive?: boolean): void;
/** **UNSTABLE**: New API, yet to be vetted.
*
/**
* Release an advisory file-system lock for the file.
*/
unlock(): Promise<void>;
/** **UNSTABLE**: New API, yet to be vetted.
*
/**
* Synchronously release an advisory file-system lock for the file.
*/
unlockSync(): void;
Expand Down
12 changes: 8 additions & 4 deletions ext/fs/30_fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,17 @@ import {
op_fs_file_stat_async,
op_fs_file_stat_sync,
op_fs_flock_async,
op_fs_flock_async_unstable,
op_fs_flock_sync,
op_fs_flock_sync_unstable,
op_fs_fsync_async,
op_fs_fsync_sync,
op_fs_ftruncate_async,
op_fs_ftruncate_sync,
op_fs_funlock_async,
op_fs_funlock_async_unstable,
op_fs_funlock_sync,
op_fs_funlock_sync_unstable,
op_fs_futime_async,
op_fs_futime_sync,
op_fs_link_async,
Expand Down Expand Up @@ -577,19 +581,19 @@ async function fsync(rid) {
}

function flockSync(rid, exclusive) {
op_fs_flock_sync(rid, exclusive === true);
op_fs_flock_sync_unstable(rid, exclusive === true);
}

async function flock(rid, exclusive) {
await op_fs_flock_async(rid, exclusive === true);
await op_fs_flock_async_unstable(rid, exclusive === true);
}

function funlockSync(rid) {
op_fs_funlock_sync(rid);
op_fs_funlock_sync_unstable(rid);
}

async function funlock(rid) {
await op_fs_funlock_async(rid);
await op_fs_funlock_async_unstable(rid);
}

function seekSync(
Expand Down
8 changes: 6 additions & 2 deletions ext/fs/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,14 @@ deno_core::extension!(deno_fs,
op_fs_fsync_async,
op_fs_file_stat_sync,
op_fs_file_stat_async,
op_fs_flock_sync,
op_fs_flock_sync_unstable,
op_fs_flock_async_unstable,
op_fs_funlock_sync_unstable,
op_fs_funlock_async_unstable,
op_fs_flock_async,
op_fs_funlock_sync,
op_fs_flock_sync,
op_fs_funlock_async,
op_fs_funlock_sync,
op_fs_ftruncate_sync,
op_fs_ftruncate_async,
op_fs_futime_sync,
Expand Down
50 changes: 46 additions & 4 deletions ext/fs/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ pub async fn op_fs_file_stat_async(
}

#[op2(fast)]
pub fn op_fs_flock_sync(
pub fn op_fs_flock_sync_unstable(
state: &mut OpState,
#[smi] rid: ResourceId,
exclusive: bool,
Expand All @@ -1510,7 +1510,7 @@ pub fn op_fs_flock_sync(
}

#[op2(async)]
pub async fn op_fs_flock_async(
pub async fn op_fs_flock_async_unstable(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
exclusive: bool,
Expand All @@ -1522,7 +1522,7 @@ pub async fn op_fs_flock_async(
}

#[op2(fast)]
pub fn op_fs_funlock_sync(
pub fn op_fs_funlock_sync_unstable(
state: &mut OpState,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
Expand All @@ -1533,7 +1533,7 @@ pub fn op_fs_funlock_sync(
}

#[op2(async)]
pub async fn op_fs_funlock_async(
pub async fn op_fs_funlock_async_unstable(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
Expand All @@ -1543,6 +1543,48 @@ pub async fn op_fs_funlock_async(
Ok(())
}

#[op2(fast)]
pub fn op_fs_flock_sync(
state: &mut OpState,
#[smi] rid: ResourceId,
exclusive: bool,
) -> Result<(), AnyError> {
let file = FileResource::get_file(state, rid)?;
file.lock_sync(exclusive)?;
Ok(())
}

#[op2(async)]
pub async fn op_fs_flock_async(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
exclusive: bool,
) -> Result<(), AnyError> {
let file = FileResource::get_file(&state.borrow(), rid)?;
file.lock_async(exclusive).await?;
Ok(())
}

#[op2(fast)]
pub fn op_fs_funlock_sync(
state: &mut OpState,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
let file = FileResource::get_file(state, rid)?;
file.unlock_sync()?;
Ok(())
}

#[op2(async)]
pub async fn op_fs_funlock_async(
state: Rc<RefCell<OpState>>,
#[smi] rid: ResourceId,
) -> Result<(), AnyError> {
let file = FileResource::get_file(&state.borrow(), rid)?;
file.unlock_async().await?;
Ok(())
}

#[op2(fast)]
pub fn op_fs_ftruncate_sync(
state: &mut OpState,
Expand Down

0 comments on commit 4908d45

Please sign in to comment.