Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/jsc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,18 +1539,17 @@ pub type OpaqueCallback = unsafe extern "C" fn(current: *mut c_void);
/// under both names.
pub type Error = ErrorCode;

/// Maximum Date in JavaScript is less than Number.MAX_SAFE_INTEGER (u52).
pub const INIT_TIMESTAMP: JSTimeType = (1u64 << 52) - 1;
pub type JSTimeType = u64;
/// Milliseconds since the Unix epoch, signed like `fs.Stats.mtimeMs` (a file
/// can be dated before 1970).
Comment thread
robobun marked this conversation as resolved.
Resolved
pub type JSTimeType = i64;
/// "stat has not run yet" marker for `webcore::File::last_modified`.
pub const INIT_TIMESTAMP: JSTimeType = (1 << 52) - 1;

/// Compute in `i128` first so the
/// `sec * 1000` widening cannot overflow `isize`, then cast to `u64`
/// (non-negative inputs) before masking to 52 bits.
/// Truncates toward zero, like `new Date(stat.mtimeMs).getTime()`.
pub fn to_js_time(sec: isize, nsec: isize) -> JSTimeType {
const MS_PER_S: i128 = bun_core::time::MS_PER_S as i128;
let millisec = (nsec as i128) / bun_core::time::NS_PER_MS as i128;
let total = (sec as i128) * MS_PER_S + millisec;
(total as u64) & ((1u64 << 52) - 1)
let ns = (sec as i128) * bun_core::time::NS_PER_S as i128 + nsec as i128;
let ms = ns / bun_core::time::NS_PER_MS as i128;
ms.clamp(i64::MIN as i128, i64::MAX as i128) as i64
}

pub const MAX_SAFE_INTEGER: i64 = 9007199254740991;
Expand Down
22 changes: 10 additions & 12 deletions src/runtime/webcore/Blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5976,19 +5976,17 @@ pub(crate) unsafe extern "C" fn Blob__fromMmapWithType(
}
}

/// `stat.{st_mtime, st_mtime_nsec}` → JS epoch ms. `bun_sys::Stat` is
/// `libc::stat` on POSIX (fields) and `uv_stat_t` on Windows (`mtim` timespec);
/// cfg-split here so the call sites stay shared.
#[inline]
fn stat_to_js_mtime(stat: &bun_sys::Stat) -> jsc::JSTimeType {
#[cfg(not(windows))]
{
jsc::to_js_time(stat.st_mtime as isize, stat.st_mtime_nsec as isize)
}
/// `stat` mtime → JS epoch ms. Windows carries `uv_timespec_t.sec` in a
/// 32-bit `long`; read it back as unsigned, as node:fs does (`Stat.rs`
/// `timespec_parts`), so 2038..2106 survive and `lastModified` agrees with
/// `fs.statSync().mtimeMs` there too.
Comment thread
robobun marked this conversation as resolved.
Resolved
pub(crate) fn stat_to_js_mtime(stat: &bun_sys::Stat) -> jsc::JSTimeType {
let mtime = bun_sys::stat_mtime(stat);
#[cfg(windows)]
{
jsc::to_js_time(stat.mtim.sec as isize, stat.mtim.nsec as isize)
}
let sec = mtime.sec as i32 as u32 as i64;
#[cfg(not(windows))]
let sec = mtime.sec;
jsc::to_js_time(sec as isize, mtime.nsec as isize)
}

/// Window clamp shared by the `resolve_size`/`resolved_size` arms: only an
Expand Down
12 changes: 5 additions & 7 deletions src/runtime/webcore/blob/read_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use crate::webcore::blob::ClosingState;
#[cfg(windows)]
use crate::webcore::blob::store::Bytes as ByteStore;
use crate::webcore::blob::store::{Data, File as FileStore};
use crate::webcore::blob::{Blob, FileCloser, FileOpener, MAX_SIZE, SizeType, Store};
use crate::webcore::blob::{
Blob, FileCloser, FileOpener, MAX_SIZE, SizeType, Store, stat_to_js_mtime,
};
use crate::webcore::node_types::PathOrFileDescriptor;
#[cfg(windows)]
use bun_collections::ByteVecExt as _;
Expand Down Expand Up @@ -691,8 +693,7 @@ impl ReadFile {

if let Some(store) = &self.store {
if let Data::File(file) = Store::data_mut(store) {
let mtime = bun_sys::PosixStat::init(&stat).mtime();
file.last_modified = jsc::to_js_time(mtime.sec as isize, mtime.nsec as isize);
file.last_modified = stat_to_js_mtime(&stat);
}
}

Expand Down Expand Up @@ -1204,10 +1205,7 @@ impl<'a> ReadFileUV<'a> {

// keep in sync with resolveSizeAndLastModified
if let Data::File(file) = Store::data_mut(&this.store) {
// `uv_timespec_t` fields are `c_long` (i32 on Windows); widen to the
// platform-width `isize` `to_js_time` expects.
file.last_modified =
jsc::to_js_time(stat.mtime().sec as isize, stat.mtime().nsec as isize);
file.last_modified = stat_to_js_mtime(&stat);
}

if bun_sys::S::ISDIR(u32::try_from(stat.mode()).expect("int cast")) {
Expand Down
23 changes: 22 additions & 1 deletion test/js/bun/util/bun-file.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,29 @@
import { expect, test } from "bun:test";
import { statSync, utimesSync } from "fs";
import fsPromises from "fs/promises";
import { bunEnv, bunExe, tempDir } from "harness";
import { bunEnv, bunExe, isWindows, tempDir } from "harness";
import { join } from "path";

test("lastModified matches fs.statSync() for a file dated before 1970", async () => {
using dir = tempDir("bun-file-lastmodified-negative", { "old.txt": "x" });
const filename = join(dir, "old.txt");
const mtime = new Date("1960-01-01T00:00:00Z");
utimesSync(filename, mtime, mtime);

const expected = statSync(filename).mtime.getTime();
// Windows stat times are unsigned 32-bit seconds (as in Node), so only
// POSIX reports the date itself.
if (!isWindows) expect(expected).toBe(mtime.getTime());

// lazy stat path
expect(Bun.file(filename).lastModified).toBe(expected);

// fstat-during-read path
const file = Bun.file(filename);
expect(await file.text()).toBe("x");
expect(file.lastModified).toBe(expected);
});

test("delete() and stat() should work with unicode paths", async () => {
await using dir = tempDir("delete-stat-unicode-path", {
"another-file.txt": "HEY",
Expand Down
Loading