Skip to content

Commit

Permalink
feat: stabilize Deno.ftruncate and Deno.ftruncateSync (denoland#10126)
Browse files Browse the repository at this point in the history
This stabilizes Deno.ftruncate and Deno.ftruncateSync.

This is a well known system call and the interface is 
not going to change. Implicitly requires write permissions 
as the file has to be opened with write to be truncated.
  • Loading branch information
caspervonb committed Apr 11, 2021
1 parent c0b6e09 commit f5a9474
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 47 deletions.
2 changes: 0 additions & 2 deletions cli/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ const UNSTABLE_DENO_PROPS: &[&str] = &[
"formatDiagnostics",
"fstat",
"fstatSync",
"ftruncate",
"ftruncateSync",
"futime",
"futimeSync",
"hostname",
Expand Down
51 changes: 51 additions & 0 deletions cli/dts/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2335,4 +2335,55 @@ declare namespace Deno {
newpath: string,
options?: SymlinkOptions,
): Promise<void>;

/**
* Synchronously truncates or extends the specified file stream, to reach the
* specified `len`.
*
* If `len` is not specified then the entire file contents are truncated as if len was set to 0.
*
* if the file previously was larger than this new length, the extra data is lost.
*
* if the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').
*
* ```ts
* // truncate the entire file
* const file = Deno.open("my_file.txt", { read: true, write: true, truncate: true, create: true });
* Deno.ftruncateSync(file.rid);
*
* // truncate part of the file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* Deno.ftruncateSync(file.rid, 7);
* const data = new Uint8Array(32);
* Deno.readSync(file.rid, data);
* console.log(new TextDecoder().decode(data)); // Hello W
* ```
*/
export function ftruncateSync(rid: number, len?: number): void;

/**
* Truncates or extends the specified file stream, to reach the specified `len`.
*
* If `len` is not specified then the entire file contents are truncated as if len was set to 0.
*
* If the file previously was larger than this new length, the extra data is lost.
*
* If the file previously was shorter, it is extended, and the extended part reads as null bytes ('\0').
*
* ```ts
* // truncate the entire file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* await Deno.ftruncate(file.rid);
*
* // truncate part of the file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await Deno.ftruncate(file.rid, 7);
* const data = new Uint8Array(32);
* await Deno.read(file.rid, data);
* console.log(new TextDecoder().decode(data)); // Hello W
* ```
*/
export function ftruncate(rid: number, len?: number): Promise<void>;
}
41 changes: 0 additions & 41 deletions cli/dts/lib.deno.unstable.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1042,47 +1042,6 @@ declare namespace Deno {
*/
export function hostname(): string;

/** **UNSTABLE**: new API, yet to be vetted.
* Synchronously truncates or extends the specified file stream, to reach the
* specified `len`. If `len` is not specified then the entire file contents
* are truncated.
*
* ```ts
* // truncate the entire file
* const file = Deno.open("my_file.txt", { read: true, write: true, truncate: true, create: true });
* Deno.ftruncateSync(file.rid);
*
* // truncate part of the file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* Deno.ftruncateSync(file.rid, 7);
* const data = new Uint8Array(32);
* Deno.readSync(file.rid, data);
* console.log(new TextDecoder().decode(data)); // Hello W
* ```
*/
export function ftruncateSync(rid: number, len?: number): void;

/** **UNSTABLE**: new API, yet to be vetted.
* Truncates or extends the specified file stream, to reach the specified `len`. If
* `len` is not specified then the entire file contents are truncated.
*
* ```ts
* // truncate the entire file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* await Deno.ftruncate(file.rid);
*
* // truncate part of the file
* const file = Deno.open("my_file.txt", { read: true, write: true, create: true });
* await Deno.write(file.rid, new TextEncoder().encode("Hello World"));
* await Deno.ftruncate(file.rid, 7);
* const data = new Uint8Array(32);
* await Deno.read(file.rid, data);
* console.log(new TextDecoder().decode(data)); // Hello W
* ```
*/
export function ftruncate(rid: number, len?: number): Promise<void>;

/** **UNSTABLE**: New API, yet to be vetted.
* Synchronously returns a `Deno.FileInfo` for the given file stream.
*
Expand Down
4 changes: 2 additions & 2 deletions runtime/js/90_deno_ns.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
lstat: __bootstrap.fs.lstat,
truncateSync: __bootstrap.fs.truncateSync,
truncate: __bootstrap.fs.truncate,
ftruncateSync: __bootstrap.fs.ftruncateSync,
ftruncate: __bootstrap.fs.ftruncate,
errors: __bootstrap.errors.errors,
customInspect: __bootstrap.console.customInspect,
inspect: __bootstrap.console.inspect,
Expand Down Expand Up @@ -124,8 +126,6 @@
startTls: __bootstrap.tls.startTls,
fstatSync: __bootstrap.fs.fstatSync,
fstat: __bootstrap.fs.fstat,
ftruncateSync: __bootstrap.fs.ftruncateSync,
ftruncate: __bootstrap.fs.ftruncate,
umask: __bootstrap.fs.umask,
futime: __bootstrap.fs.futime,
futimeSync: __bootstrap.fs.futimeSync,
Expand Down
2 changes: 0 additions & 2 deletions runtime/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,6 @@ fn op_ftruncate_sync(
args: FtruncateArgs,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<(), AnyError> {
super::check_unstable(state, "Deno.ftruncate");
let rid = args.rid;
let len = args.len as u64;
StdFileResource::with(state, rid, |r| match r {
Expand All @@ -1314,7 +1313,6 @@ async fn op_ftruncate_async(
args: FtruncateArgs,
_zero_copy: Option<ZeroCopyBuf>,
) -> Result<(), AnyError> {
super::check_unstable2(&state, "Deno.ftruncate");
let rid = args.rid;
let len = args.len as u64;

Expand Down

0 comments on commit f5a9474

Please sign in to comment.