1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 15:24:46 -05:00

fs: add Deno.utime/Deno.utimeSync (#2241)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2019-05-01 02:08:12 -07:00 committed by Ryan Dahl
parent c36b5dd01c
commit 7237e9d34a
12 changed files with 322 additions and 1 deletions

12
Cargo.lock generated
View file

@ -242,6 +242,7 @@ dependencies = [
"tokio-rustls 0.9.2 (registry+https://github.com/rust-lang/crates.io-index)",
"tokio-threadpool 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)",
"url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
"utime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1338,6 +1339,16 @@ name = "utf8parse"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "utime"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
"libc 0.2.51 (registry+https://github.com/rust-lang/crates.io-index)",
"winapi 0.2.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "vec_map"
version = "0.8.1"
@ -1565,6 +1576,7 @@ dependencies = [
"checksum url 1.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "dd4e7c0d531266369519a4aa4f399d748bd37043b00bde1e4ff1f60a120b355a"
"checksum utf8-ranges 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "796f7e48bef87609f7ade7e06495a87d5cd06c7866e6a5cbfceffc558a243737"
"checksum utf8parse 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "8772a4ccbb4e89959023bc5b7cb8623a795caa7092d99f3aa9501b9484d4557d"
"checksum utime 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "055058552ca15c566082fc61da433ae678f78986a6f16957e33162d1b218792a"
"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a"
"checksum vlq 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "65dd7eed29412da847b0f78bcec0ac98588165988a8cfe41d4ea1d429f8ccfff"
"checksum void 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"

View file

@ -1714,6 +1714,30 @@ rust_crate("url") {
]
}
rust_crate("utime") {
edition = "2015"
source_root = "$cargo_home/registry/src/github.com-1ecc6299db9ec823/utime-0.2.1/src/lib.rs"
if (is_win) {
extern = [ ":kernel32" ]
extern_version = [
{
crate_name = "winapi"
crate_version = "0.2.8"
},
]
}
if (is_posix) {
extern = [ ":libc" ]
}
args = [
"--cap-lints",
"allow",
]
}
rust_crate("utf8_ranges") {
edition = "2015"
source_root = "$cargo_home/registry/src/github.com-1ecc6299db9ec823/utf8-ranges-1.0.2/src/lib.rs"

View file

@ -38,6 +38,7 @@ main_extern = [
"$rust_build:tokio_rustls",
"$rust_build:tokio_threadpool",
"$rust_build:url",
"$rust_build:utime",
]
if (is_win) {
main_extern += [ "$rust_build:winapi" ]
@ -105,6 +106,7 @@ ts_sources = [
"../js/url.ts",
"../js/url_search_params.ts",
"../js/util.ts",
"../js/utime.ts",
"../js/window.ts",
"../js/workers.ts",
"../js/write_file.ts",

View file

@ -47,6 +47,7 @@ tokio-process = "0.2.3"
tokio-rustls = "0.9.2"
tokio-threadpool = "0.1.14"
url = "1.7.2"
utime = "0.2.1"
[target.'cfg(windows)'.dependencies]
winapi = "0.3.7"

View file

@ -67,6 +67,7 @@ union Any {
StatRes,
Symlink,
Truncate,
Utime,
CreateWorker,
CreateWorkerRes,
HostGetWorkerClosed,
@ -434,6 +435,12 @@ table Truncate {
len: uint;
}
table Utime {
filename: string;
atime: uint64;
mtime: uint64;
}
table Open {
filename: string;
perm: uint;

View file

@ -50,6 +50,7 @@ use tokio::net::TcpListener;
use tokio::net::TcpStream;
use tokio_process::CommandExt;
use tokio_threadpool;
use utime;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
@ -202,6 +203,7 @@ pub fn op_selector_std(inner_type: msg::Any) -> Option<OpCreator> {
msg::Any::Stat => Some(op_stat),
msg::Any::Symlink => Some(op_symlink),
msg::Any::Truncate => Some(op_truncate),
msg::Any::Utime => Some(op_utime),
msg::Any::CreateWorker => Some(op_create_worker),
msg::Any::HostGetWorkerClosed => Some(op_host_get_worker_closed),
msg::Any::HostGetMessage => Some(op_host_get_message),
@ -1507,6 +1509,29 @@ fn op_truncate(
})
}
fn op_utime(
state: &ThreadSafeState,
base: &msg::Base<'_>,
data: deno_buf,
) -> Box<OpWithError> {
assert_eq!(data.len(), 0);
let inner = base.inner_as_utime().unwrap();
let filename = String::from(inner.filename().unwrap());
let atime = inner.atime();
let mtime = inner.mtime();
if let Err(e) = state.check_write(&filename) {
return odd_future(e);
}
blocking(base.sync(), move || {
debug!("op_utimes {} {} {}", filename, atime, mtime);
utime::set_file_times(filename, atime, mtime)?;
Ok(empty_buf())
})
}
fn op_listen(
state: &ThreadSafeState,
base: &msg::Base<'_>,

View file

@ -46,6 +46,7 @@ export {
MakeTempDirOptions
} from "./make_temp_dir";
export { chmodSync, chmod } from "./chmod";
export { utimeSync, utime } from "./utime";
export { removeSync, remove, RemoveOption } from "./remove";
export { renameSync, rename } from "./rename";
export { readFileSync, readFile } from "./read_file";

View file

@ -40,6 +40,7 @@ import "./timers_test.ts";
import "./truncate_test.ts";
import "./url_test.ts";
import "./url_search_params_test.ts";
import "./utime_test.ts";
import "./write_file_test.ts";
import "./performance_test.ts";
import "./permissions_test.ts";

View file

@ -192,3 +192,18 @@ export function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean {
}
return Object.prototype.hasOwnProperty.call(obj, v);
}
/**
* Split a number into two parts: lower 32 bit and higher 32 bit
* (as if the number is represented as uint64.)
*
* @param n Number to split.
* @internal
*/
export function splitNumberToParts(n: number): number[] {
// JS bitwise operators (OR, SHIFT) operate as if number is uint32.
const lower = n | 0;
// This is also faster than Math.floor(n / 0x100000000) in V8.
const higher = (n - lower) / 0x100000000;
return [lower, higher];
}

52
js/utime.ts Normal file
View file

@ -0,0 +1,52 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import * as msg from "gen/cli/msg_generated";
import * as flatbuffers from "./flatbuffers";
import * as dispatch from "./dispatch";
import * as util from "./util";
function req(
filename: string,
atime: number | Date,
mtime: number | Date
): [flatbuffers.Builder, msg.Any, flatbuffers.Offset] {
const atimeSec = atime instanceof Date ? Math.floor(+atime / 1000) : atime;
const mtimeSec = mtime instanceof Date ? Math.floor(+mtime / 1000) : mtime;
const builder = flatbuffers.createBuilder();
const filename_ = builder.createString(filename);
const atimeParts = util.splitNumberToParts(atimeSec);
const atimeMS_ = builder.createLong(atimeParts[0], atimeParts[1]);
const mtimeParts = util.splitNumberToParts(mtimeSec);
const mtimeMS_ = builder.createLong(mtimeParts[0], mtimeParts[1]);
const inner = msg.Utime.createUtime(builder, filename_, atimeMS_, mtimeMS_);
return [builder, msg.Any.Utime, inner];
}
/** Synchronously changes the access and modification times of a file system
* object referenced by `filename`. Given times are either in seconds
* (Unix epoch time) or as `Date` objects.
*
* Deno.utimeSync("myfile.txt", 1556495550, new Date());
*/
export function utimeSync(
filename: string,
atime: number | Date,
mtime: number | Date
): void {
dispatch.sendSync(...req(filename, atime, mtime));
}
/** Changes the access and modification times of a file system object
* referenced by `filename`. Given times are either in seconds
* (Unix epoch time) or as `Date` objects.
*
* await Deno.utime("myfile.txt", 1556495550, new Date());
*/
export async function utime(
filename: string,
atime: number | Date,
mtime: number | Date
): Promise<void> {
await dispatch.sendAsync(...req(filename, atime, mtime));
}

181
js/utime_test.ts Normal file
View file

@ -0,0 +1,181 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { testPerm, assert, assertEquals } from "./test_util.ts";
// Allow 10 second difference.
// Note this might not be enough for FAT (but we are not testing on such fs).
function assertFuzzyTimestampEquals(t1: number, t2: number): void {
assert(Math.abs(t1 - t2) < 10);
}
testPerm({ read: true, write: true }, function utimeSyncFileSuccess(): void {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
perm: 0o666
});
const atime = 1000;
const mtime = 50000;
Deno.utimeSync(filename, atime, mtime);
const fileInfo = Deno.statSync(filename);
assertFuzzyTimestampEquals(fileInfo.accessed, atime);
assertFuzzyTimestampEquals(fileInfo.modified, mtime);
});
testPerm(
{ read: true, write: true },
function utimeSyncDirectorySuccess(): void {
const testDir = Deno.makeTempDirSync();
const atime = 1000;
const mtime = 50000;
Deno.utimeSync(testDir, atime, mtime);
const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
}
);
testPerm({ read: true, write: true }, function utimeSyncDateSuccess(): void {
const testDir = Deno.makeTempDirSync();
const atime = 1000;
const mtime = 50000;
Deno.utimeSync(testDir, new Date(atime * 1000), new Date(mtime * 1000));
const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
});
testPerm(
{ read: true, write: true },
function utimeSyncLargeNumberSuccess(): void {
const testDir = Deno.makeTempDirSync();
// There are Rust side caps (might be fs relate),
// so JUST make them slightly larger than UINT32_MAX.
const atime = 0x100000001;
const mtime = 0x100000002;
Deno.utimeSync(testDir, atime, mtime);
const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
}
);
testPerm({ read: true, write: true }, function utimeSyncNotFound(): void {
const atime = 1000;
const mtime = 50000;
let caughtError = false;
try {
Deno.utimeSync("/baddir", atime, mtime);
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.NotFound);
assertEquals(e.name, "NotFound");
}
assert(caughtError);
});
testPerm({ read: true, write: false }, function utimeSyncPerm(): void {
const atime = 1000;
const mtime = 50000;
let caughtError = false;
try {
Deno.utimeSync("/some_dir", atime, mtime);
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
assert(caughtError);
});
testPerm(
{ read: true, write: true },
async function utimeFileSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const filename = testDir + "/file.txt";
Deno.writeFileSync(filename, new TextEncoder().encode("hello"), {
perm: 0o666
});
const atime = 1000;
const mtime = 50000;
await Deno.utime(filename, atime, mtime);
const fileInfo = Deno.statSync(filename);
assertFuzzyTimestampEquals(fileInfo.accessed, atime);
assertFuzzyTimestampEquals(fileInfo.modified, mtime);
}
);
testPerm(
{ read: true, write: true },
async function utimeDirectorySuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const atime = 1000;
const mtime = 50000;
await Deno.utime(testDir, atime, mtime);
const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
}
);
testPerm(
{ read: true, write: true },
async function utimeDateSuccess(): Promise<void> {
const testDir = Deno.makeTempDirSync();
const atime = 1000;
const mtime = 50000;
await Deno.utime(testDir, new Date(atime * 1000), new Date(mtime * 1000));
const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
}
);
testPerm({ read: true, write: true }, async function utimeNotFound(): Promise<
void
> {
const atime = 1000;
const mtime = 50000;
let caughtError = false;
try {
await Deno.utime("/baddir", atime, mtime);
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.NotFound);
assertEquals(e.name, "NotFound");
}
assert(caughtError);
});
testPerm({ read: true, write: false }, async function utimeSyncPerm(): Promise<
void
> {
const atime = 1000;
const mtime = 50000;
let caughtError = false;
try {
await Deno.utime("/some_dir", atime, mtime);
} catch (e) {
caughtError = true;
assertEquals(e.kind, Deno.ErrorKind.PermissionDenied);
assertEquals(e.name, "PermissionDenied");
}
assert(caughtError);
});

@ -1 +1 @@
Subproject commit 434ec0c4dafc6ec670984e8ce56d1a4d5f4d77ab
Subproject commit 69ae9358be5ba3918fa0515813d7cb678a6bc974