From f5a9474952f459a5095e0aae68e0984fdd84b210 Mon Sep 17 00:00:00 2001 From: Casper Beyer Date: Sun, 11 Apr 2021 20:05:22 +0800 Subject: feat: stabilize Deno.ftruncate and Deno.ftruncateSync (#10126) 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. --- cli/dts/lib.deno.ns.d.ts | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'cli/dts/lib.deno.ns.d.ts') diff --git a/cli/dts/lib.deno.ns.d.ts b/cli/dts/lib.deno.ns.d.ts index 09f39d6b8..931d28877 100644 --- a/cli/dts/lib.deno.ns.d.ts +++ b/cli/dts/lib.deno.ns.d.ts @@ -2335,4 +2335,55 @@ declare namespace Deno { newpath: string, options?: SymlinkOptions, ): Promise; + + /** + * 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; } -- cgit v1.2.3