Skip to content

Commit

Permalink
Wip: rename FileInfo time fields and convert them to Date objects
Browse files Browse the repository at this point in the history
  • Loading branch information
piscisaureus committed Apr 27, 2020
1 parent c190a0d commit ea2bf48
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 69 deletions.
10 changes: 5 additions & 5 deletions cli/js/lib.deno.ns.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1316,15 +1316,15 @@ declare namespace Deno {
/** The last modification time of the file. This corresponds to the `mtime`
* field from `stat` on Linux/Mac OS and `ftLastWriteTime` on Windows. This
* may not be available on all platforms. */
modified: number | null;
mtime: Date | null;
/** The last access time of the file. This corresponds to the `atime`
* field from `stat` on Unix and `ftLastAccessTime` on Windows. This may not
* be available on all platforms. */
accessed: number | null;
atime: Date | null;
/** The creation time of the file. This corresponds to the `birthtime`
* field from `stat` on Mac/BSD and `ftCreationTime` on Windows. This may not
* be available on all platforms. */
created: number | null;
* field from `stat` on Mac/BSD and `ftCreationTime` on Windows. This may
* not be available on all platforms. */
birthtime: Date | null;
/** ID of the device containing the file.
*
* _Linux/Mac OS only._ */
Expand Down
18 changes: 9 additions & 9 deletions cli/js/ops/fs/stat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { build } from "../../build.ts";

export interface FileInfo {
size: number;
modified: number | null;
accessed: number | null;
created: number | null;
mtime: Date | null;
atime: Date | null;
birthtime: Date | null;
dev: number | null;
ino: number | null;
mode: number | null;
Expand All @@ -26,9 +26,9 @@ export interface StatResponse {
isDirectory: boolean;
isSymlink: boolean;
size: number;
modified: number;
accessed: number;
created: number;
mtime: number | null;
atime: number | null;
birthtime: number | null;
// Null for stat(), but exists for readdir().
name: string | null;
// Unix only members
Expand All @@ -51,9 +51,9 @@ export function parseFileInfo(response: StatResponse): FileInfo {
isDirectory: response.isDirectory,
isSymlink: response.isSymlink,
size: response.size,
modified: response.modified ? response.modified : null,
accessed: response.accessed ? response.accessed : null,
created: response.created ? response.created : null,
mtime: response.mtime != null ? new Date(response.mtime) : null,
atime: response.atime != null ? new Date(response.atime) : null,
birthtime: response.birthtime != null ? new Date(response.birthtime) : null,
// Only non-null if on Unix
dev: isUnix ? response.dev : null,
ino: isUnix ? response.ino : null,
Expand Down
16 changes: 14 additions & 2 deletions cli/js/tests/stat_test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
import { unitTest, assert, assertEquals } from "./test_util.ts";

// TODO Add tests for modified, accessed, and created fields once there is a way
// to create temp files.
unitTest({ perms: { read: true } }, function statSyncSuccess(): void {
const packageInfo = Deno.statSync("README.md");
assert(packageInfo.isFile);
Expand All @@ -15,6 +13,13 @@ unitTest({ perms: { read: true } }, function statSyncSuccess(): void {
const testsInfo = Deno.statSync("cli/tests");
assert(testsInfo.isDirectory);
assert(!testsInfo.isSymlink);

const tempFile = Deno.makeTempFileSync();
const tempInfo = Deno.statSync(tempFile);
const now = Date.now();
assert(now - tempInfo.atime.valueOf() < 1000);
assert(now - tempInfo.mtime.valueOf() < 1000);
assert(tempInfo.birthtime === null || now - tempInfo.mtime.valueOf() < 1000);
});

unitTest({ perms: { read: false } }, function statSyncPerm(): void {
Expand Down Expand Up @@ -97,6 +102,13 @@ unitTest({ perms: { read: true } }, async function statSuccess(): Promise<
const testsInfo = await Deno.stat("cli/tests");
assert(testsInfo.isDirectory);
assert(!testsInfo.isSymlink);

const tempFile = await Deno.makeTempFile();
const tempInfo = await Deno.stat(tempFile);
const now = Date.now();
assert(now - tempInfo.atime.valueOf() < 1000);
assert(now - tempInfo.mtime.valueOf() < 1000);
assert(tempInfo.birthtime === null || now - tempInfo.mtime.valueOf() < 1000);
});

unitTest({ perms: { read: false } }, async function statPerm(): Promise<void> {
Expand Down
68 changes: 27 additions & 41 deletions cli/js/tests/utime_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { unitTest, assert } from "./test_util.ts";
// Allow 10 second difference.
// Note this might not be enough for FAT (but we are not testing on such fs).
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function assertFuzzyTimestampEquals(t1: any, t2: number): void {
assert(typeof t1 === "number");
assert(Math.abs(t1 - t2) < 10);
function assertFuzzyTimestampEquals(t1: Date, t2: Date): void {
assert(t1 instanceof Date);
assert(Math.abs(t1.valueOf() - t2.valueOf()) < 10);
}

unitTest(
Expand All @@ -23,8 +23,8 @@ unitTest(
Deno.utimeSync(filename, atime, mtime);

const fileInfo = Deno.statSync(filename);
assertFuzzyTimestampEquals(fileInfo.accessed, atime);
assertFuzzyTimestampEquals(fileInfo.modified, mtime);
assertFuzzyTimestampEquals(fileInfo.atime, new Date(atime * 1000));
assertFuzzyTimestampEquals(fileInfo.mtime, new Date(mtime * 1000));
}
);

Expand All @@ -38,8 +38,8 @@ unitTest(
Deno.utimeSync(testDir, atime, mtime);

const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
}
);

Expand All @@ -48,13 +48,13 @@ unitTest(
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 atime = new Date(1000_000);
const mtime = new Date(50000_000);
Deno.utimeSync(testDir, atime, mtime);

const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
assertFuzzyTimestampEquals(dirInfo.atime, atime);
assertFuzzyTimestampEquals(dirInfo.mtime, mtime);
}
);

Expand All @@ -71,15 +71,8 @@ unitTest(
Deno.utimeSync(filename, atime, mtime);

const fileInfo = Deno.statSync(filename);
// atime and mtime must be scaled by a factor of 1000 to be recorded in seconds
assertFuzzyTimestampEquals(
fileInfo.accessed,
Math.trunc(atime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(
fileInfo.modified,
Math.trunc(mtime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(fileInfo.atime, atime);
assertFuzzyTimestampEquals(fileInfo.mtime, mtime);
}
);

Expand All @@ -95,8 +88,8 @@ unitTest(
Deno.utimeSync(testDir, atime, mtime);

const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
}
);

Expand Down Expand Up @@ -148,8 +141,8 @@ unitTest(
await Deno.utime(filename, atime, mtime);

const fileInfo = Deno.statSync(filename);
assertFuzzyTimestampEquals(fileInfo.accessed, atime);
assertFuzzyTimestampEquals(fileInfo.modified, mtime);
assertFuzzyTimestampEquals(fileInfo.atime, new Date(atime * 1000));
assertFuzzyTimestampEquals(fileInfo.mtime, new Date(mtime * 1000));
}
);

Expand All @@ -163,8 +156,8 @@ unitTest(
await Deno.utime(testDir, atime, mtime);

const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
assertFuzzyTimestampEquals(dirInfo.atime, new Date(atime * 1000));
assertFuzzyTimestampEquals(dirInfo.mtime, new Date(mtime * 1000));
}
);

Expand All @@ -173,13 +166,13 @@ unitTest(
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 atime = new Date(100_000);
const mtime = new Date(5000_000);
await Deno.utime(testDir, atime, mtime);

const dirInfo = Deno.statSync(testDir);
assertFuzzyTimestampEquals(dirInfo.accessed, atime);
assertFuzzyTimestampEquals(dirInfo.modified, mtime);
assertFuzzyTimestampEquals(dirInfo.atime, atime);
assertFuzzyTimestampEquals(dirInfo.mtime, mtime);
}
);

Expand All @@ -197,15 +190,8 @@ unitTest(
await Deno.utime(filename, atime, mtime);

const fileInfo = Deno.statSync(filename);
// The dates must be scaled by a factored of 1000 to make them seconds
assertFuzzyTimestampEquals(
fileInfo.accessed,
Math.trunc(atime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(
fileInfo.modified,
Math.trunc(mtime.valueOf() / 1000)
);
assertFuzzyTimestampEquals(fileInfo.atime, atime);
assertFuzzyTimestampEquals(fileInfo.mtime, mtime);
}
);

Expand Down
1 change: 1 addition & 0 deletions cli/op_error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::import_map::ImportMapError;
use deno_core::ErrBox;
use deno_core::ModuleResolutionError;
use rustyline::error::ReadlineError;
use serde_json;
use std::env::VarError;
use std::error::Error;
use std::fmt;
Expand Down
32 changes: 20 additions & 12 deletions cli/ops/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ use crate::state::State;
use deno_core::CoreIsolate;
use deno_core::ZeroCopyBuf;
use futures::future::FutureExt;
use serde_json;
use std::convert::From;
use std::env::{current_dir, set_current_dir, temp_dir};
use std::io;
use std::path::{Path, PathBuf};
use std::time::SystemTime;
use std::time::UNIX_EPOCH;

use rand::{thread_rng, Rng};
Expand Down Expand Up @@ -441,14 +444,19 @@ fn op_copy_file(
})
}

macro_rules! to_seconds {
($time:expr) => {{
// Unwrap is safe here as if the file is before the unix epoch
// something is very wrong.
$time
.and_then(|t| Ok(t.duration_since(UNIX_EPOCH).unwrap().as_secs()))
.unwrap_or(0)
}};
fn to_msec(maybe_time: Result<SystemTime, io::Error>) -> serde_json::Value {
match maybe_time {
Ok(time) => {
let msec = time
.duration_since(UNIX_EPOCH)
.map(|t| t.as_secs_f64() * 1000f64)
.unwrap_or_else(|err| err.duration().as_secs_f64() * -1000f64);
serde_json::Number::from_f64(msec)
.map(serde_json::Value::Number)
.unwrap_or(serde_json::Value::Null)
}
Err(_) => serde_json::Value::Null,
}
}

#[inline(always)]
Expand Down Expand Up @@ -477,10 +485,10 @@ fn get_stat_json(
"isDirectory": metadata.is_dir(),
"isSymlink": metadata.file_type().is_symlink(),
"size": metadata.len(),
// In seconds. Available on both Unix or Windows.
"modified":to_seconds!(metadata.modified()),
"accessed":to_seconds!(metadata.accessed()),
"created":to_seconds!(metadata.created()),
// In milliseconds, like JavaScript. Available on both Unix or Windows.
"mtime": to_msec(metadata.modified()),
"atime": to_msec(metadata.accessed()),
"birthtime": to_msec(metadata.created()),
// Following are only valid under Unix.
"dev": usm!(dev),
"ino": usm!(ino),
Expand Down

0 comments on commit ea2bf48

Please sign in to comment.