Skip to content

Commit

Permalink
Rename FileInfo time fields and represent them as Date objects (denol…
Browse files Browse the repository at this point in the history
…and/deno#4932)

This patch also increases the resolution of reported file times to
sub-millisecond precision.
  • Loading branch information
piscisaureus authored and caspervonb committed Jan 24, 2021
1 parent 1fe6cb9 commit 4ddedae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 37 deletions.
7 changes: 3 additions & 4 deletions archive/tar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,9 @@ export class Tar {

const mode =
opts.fileMode || (info && info.mode) || parseInt("777", 8) & 0xfff,
mtime =
opts.mtime ||
(info && info.modified) ||
Math.floor(new Date().getTime() / 1000),
mtime = Math.floor(
opts.mtime ?? (info?.mtime ?? new Date()).valueOf() / 1000
),
uid = opts.uid || 0,
gid = opts.gid || 0;
if (typeof opts.owner === "string" && opts.owner.length >= 32) {
Expand Down
36 changes: 18 additions & 18 deletions fs/copy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ async function copyFile(
await Deno.copyFile(src, dest);
if (options.preserveTimestamps) {
const statInfo = await Deno.stat(src);
assert(statInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(statInfo.modified != null, `statInfo.modified is unavailable`);
await Deno.utime(dest, statInfo.accessed, statInfo.modified);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, statInfo.atime, statInfo.mtime);
}
}
/* copy file to dest synchronously */
Expand All @@ -96,9 +96,9 @@ function copyFileSync(src: string, dest: string, options: CopyOptions): void {
Deno.copyFileSync(src, dest);
if (options.preserveTimestamps) {
const statInfo = Deno.statSync(src);
assert(statInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(statInfo.modified != null, `statInfo.modified is unavailable`);
Deno.utimeSync(dest, statInfo.accessed, statInfo.modified);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
}
}

Expand All @@ -114,9 +114,9 @@ async function copySymLink(
await Deno.symlink(originSrcFilePath, dest, type);
if (options.preserveTimestamps) {
const statInfo = await Deno.lstat(src);
assert(statInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(statInfo.modified != null, `statInfo.modified is unavailable`);
await Deno.utime(dest, statInfo.accessed, statInfo.modified);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, statInfo.atime, statInfo.mtime);
}
}

Expand All @@ -132,9 +132,9 @@ function copySymlinkSync(
Deno.symlinkSync(originSrcFilePath, dest, type);
if (options.preserveTimestamps) {
const statInfo = Deno.lstatSync(src);
assert(statInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(statInfo.modified != null, `statInfo.modified is unavailable`);
Deno.utimeSync(dest, statInfo.accessed, statInfo.modified);
assert(statInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(statInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, statInfo.atime, statInfo.mtime);
}
}

Expand All @@ -152,9 +152,9 @@ async function copyDir(

if (options.preserveTimestamps) {
const srcStatInfo = await Deno.stat(src);
assert(srcStatInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(srcStatInfo.modified != null, `statInfo.modified is unavailable`);
await Deno.utime(dest, srcStatInfo.accessed, srcStatInfo.modified);
assert(srcStatInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(srcStatInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
await Deno.utime(dest, srcStatInfo.atime, srcStatInfo.mtime);
}

for await (const file of Deno.readdir(src)) {
Expand All @@ -180,9 +180,9 @@ function copyDirSync(src: string, dest: string, options: CopyOptions): void {

if (options.preserveTimestamps) {
const srcStatInfo = Deno.statSync(src);
assert(srcStatInfo.accessed != null, `statInfo.accessed is unavailable`);
assert(srcStatInfo.modified != null, `statInfo.modified is unavailable`);
Deno.utimeSync(dest, srcStatInfo.accessed, srcStatInfo.modified);
assert(srcStatInfo.atime instanceof Date, `statInfo.atime is unavailable`);
assert(srcStatInfo.mtime instanceof Date, `statInfo.mtime is unavailable`);
Deno.utimeSync(dest, srcStatInfo.atime, srcStatInfo.mtime);
}

for (const file of Deno.readdirSync(src)) {
Expand Down
24 changes: 12 additions & 12 deletions fs/copy_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ testCopy(

const srcStatInfo = await Deno.stat(srcFile);

assert(typeof srcStatInfo.accessed === "number");
assert(typeof srcStatInfo.modified === "number");
assert(srcStatInfo.atime instanceof Date);
assert(srcStatInfo.mtime instanceof Date);

// Copy with overwrite and preserve timestamps options.
await copy(srcFile, destFile, {
Expand All @@ -154,10 +154,10 @@ testCopy(

const destStatInfo = await Deno.stat(destFile);

assert(typeof destStatInfo.accessed === "number");
assert(typeof destStatInfo.modified === "number");
assertEquals(destStatInfo.accessed, srcStatInfo.accessed);
assertEquals(destStatInfo.modified, srcStatInfo.modified);
assert(destStatInfo.atime instanceof Date);
assert(destStatInfo.mtime instanceof Date);
assertEquals(destStatInfo.atime, srcStatInfo.atime);
assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
}
);

Expand Down Expand Up @@ -327,8 +327,8 @@ testCopySync(

const srcStatInfo = Deno.statSync(srcFile);

assert(typeof srcStatInfo.accessed === "number");
assert(typeof srcStatInfo.modified === "number");
assert(srcStatInfo.atime instanceof Date);
assert(srcStatInfo.mtime instanceof Date);

// Copy with overwrite and preserve timestamps options.
copySync(srcFile, destFile, {
Expand All @@ -338,12 +338,12 @@ testCopySync(

const destStatInfo = Deno.statSync(destFile);

assert(typeof destStatInfo.accessed === "number");
assert(typeof destStatInfo.modified === "number");
assert(destStatInfo.atime instanceof Date);
assert(destStatInfo.mtime instanceof Date);
// TODO: Activate test when https://github.com/denoland/deno/issues/2411
// is fixed
// assertEquals(destStatInfo.accessed, srcStatInfo.accessed);
// assertEquals(destStatInfo.modified, srcStatInfo.modified);
// assertEquals(destStatInfo.atime, srcStatInfo.atime);
// assertEquals(destStatInfo.mtime, srcStatInfo.mtime);
}
);

Expand Down
6 changes: 3 additions & 3 deletions node/_fs/_fs_dirent_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ class DirEntryMock implements Deno.DirEntry {
isDirectory = false;
isSymlink = false;
size = -1;
modified = -1;
accessed = -1;
created = -1;
mtime = new Date(-1);
atime = new Date(-1);
birthtime = new Date(-1);
name = "";
dev = -1;
ino = -1;
Expand Down

0 comments on commit 4ddedae

Please sign in to comment.