diff options
author | Casper Beyer <caspervonb@pm.me> | 2020-06-20 21:46:10 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-20 09:46:10 -0400 |
commit | 86f92e04c79be81f98e5899638cb6fdb29a4fa64 (patch) | |
tree | b106b4551d9ec2e842beefdb4d0f921e27b03aa1 /cli/js | |
parent | bdf2d26ba1879b2ebeffd4b3a52e23e9254d4f05 (diff) |
feat(unstable): add Deno.ftruncate and ftruncateSync (#6243)
Diffstat (limited to 'cli/js')
-rw-r--r-- | cli/js/deno_unstable.ts | 1 | ||||
-rw-r--r-- | cli/js/lib.deno.unstable.d.ts | 39 | ||||
-rw-r--r-- | cli/js/ops/fs/truncate.ts | 8 |
3 files changed, 48 insertions, 0 deletions
diff --git a/cli/js/deno_unstable.ts b/cli/js/deno_unstable.ts index 192137e1f..991df9955 100644 --- a/cli/js/deno_unstable.ts +++ b/cli/js/deno_unstable.ts @@ -12,6 +12,7 @@ export { applySourceMap, formatDiagnostics } from "./ops/errors.ts"; export { signal, signals, Signal, SignalStream } from "./signals.ts"; export { setRaw } from "./ops/tty.ts"; export { utimeSync, utime } from "./ops/fs/utime.ts"; +export { ftruncateSync, ftruncate } from "./ops/fs/truncate.ts"; export { ShutdownMode, shutdown } from "./net.ts"; export { listen, listenDatagram, connect } from "./net_unstable.ts"; export { startTls } from "./tls.ts"; diff --git a/cli/js/lib.deno.unstable.d.ts b/cli/js/lib.deno.unstable.d.ts index 0a80eb2c6..dd8de2eb6 100644 --- a/cli/js/lib.deno.unstable.d.ts +++ b/cli/js/lib.deno.unstable.d.ts @@ -1248,4 +1248,43 @@ declare namespace Deno { /** **UNSTABLE**: The URL of the file that was originally executed from the command-line. */ export const mainModule: string; + + /** 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; + + /** 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>; } diff --git a/cli/js/ops/fs/truncate.ts b/cli/js/ops/fs/truncate.ts index 861e843f8..2b805e5ac 100644 --- a/cli/js/ops/fs/truncate.ts +++ b/cli/js/ops/fs/truncate.ts @@ -13,6 +13,14 @@ function coerceLen(len?: number): number { return len; } +export function ftruncateSync(rid: number, len?: number): void { + sendSync("op_ftruncate", { rid, len: coerceLen(len) }); +} + +export async function ftruncate(rid: number, len?: number): Promise<void> { + await sendAsync("op_ftruncate", { rid, len: coerceLen(len) }); +} + export function truncateSync(path: string, len?: number): void { sendSync("op_truncate", { path, len: coerceLen(len) }); } |