@@ -10,12 +10,12 @@ import { closeFd, DirectoryFile, newFd, fileInode, fileOf, flush, flushOrThrow,
1010import type { OpenFile } from "./io.ts" ;
1111import type { Natives } from "./util.ts" ;
1212
13- export const EPERM = 1 , ENOENT = 2 , EINTR = 4 , EIO = 5 , EBADF = 9 , ENOMEM = 12 , EACCES = 13 ,
13+ export const EPERM = 1 , ENOENT = 2 , EINTR = 4 , EIO = 5 , ENXIO = 6 , EBADF = 9 , ENOMEM = 12 , EACCES = 13 ,
1414 EEXIST = 17 , EXDEV = 18 , ENOTDIR = 20 , EISDIR = 21 , EINVAL = 22 , ENOSPC = 28 , EROFS = 30 ,
1515 ENOTEMPTY = 39 , ENOSYS = 38 , ENAMETOOLONG = 36 , ELOOP = 40 , ENOTSUP = 95 , ESTALE = 116 , EDQUOT = 122 ;
1616
1717const MESSAGES : Record < number , string > = {
18- [ EPERM ] : "Operation not permitted" , [ EINTR ] : "Interrupted system call" , [ EIO ] : "Input/output error" ,
18+ [ EPERM ] : "Operation not permitted" , [ ENXIO ] : "No such device or address" , [ EINTR ] : "Interrupted system call" , [ EIO ] : "Input/output error" ,
1919 [ ENOMEM ] : "Cannot allocate memory" , [ ENOSPC ] : "No space left on device" ,
2020 [ ELOOP ] : "Too many levels of symbolic links" , [ ENOTSUP ] : "Operation not supported" , [ ESTALE ] : "Stale file handle" ,
2121 [ ENAMETOOLONG ] : "File name too long" , [ EDQUOT ] : "Disk quota exceeded" ,
@@ -103,10 +103,12 @@ function renameAbs(t: JThread, from: string, to: string) {
103103}
104104
105105function openAbs ( t : JThread , abs : string , flags : number , mode : number ) : number {
106+ // before any stat that follows the link, which would touch its access time
107+ if ( flags & O_NOFOLLOW && ! ( flags & O_CREAT && flags & O_EXCL ) && t . vm . fs . lstat ( abs ) ?. link !== undefined ) unixError ( ELOOP ) ;
106108 const st = t . vm . fs . stat ( abs ) ;
107109 if ( ( flags & O_CREAT ) && ( flags & O_EXCL ) && ( st || t . vm . fs . lstat ( abs ) ) ) unixError ( EEXIST ) ;
110+ if ( st ?. special ) unixError ( ENXIO ) ;
108111 if ( ! st && ! ( flags & O_CREAT ) ) unixError ( ENOENT ) ;
109- if ( flags & O_NOFOLLOW && t . vm . fs . lstat ( abs ) ?. link !== undefined ) unixError ( ELOOP ) ;
110112 if ( flags & O_DIRECTORY && st && ! st . dir ) unixError ( ENOTDIR ) ;
111113 const write = ( flags & O_ACCMODE ) !== 0 ;
112114 if ( st ?. dir && write ) unixError ( EISDIR ) ;
@@ -162,25 +164,30 @@ function fillAttrs(t: JThread, attrs: JObject, p: string, follow = true): number
162164 return 0 ;
163165}
164166
167+ // seconds and nanoseconds of a time kept as milliseconds, or exactly as nanoseconds
168+ function splitTime ( ms : number , ns ?: bigint ) : [ bigint , bigint ] {
169+ if ( ns === undefined ) return [ BigInt ( Math . floor ( ms / 1000 ) ) , BigInt ( Math . floor ( ( ms % 1000 ) * 1e6 ) ) ] ;
170+ const sec = ns / 1_000_000_000n , nsec = ns % 1_000_000_000n ;
171+ // before 1970: whole seconds round down, nanoseconds stay positive
172+ return nsec < 0n ? [ sec - 1n , nsec + 1_000_000_000n ] : [ sec , nsec ] ;
173+ }
174+
165175function fillStat ( t : JThread , attrs : JObject , st : Stat , writable : boolean , ino : number , size = st . size , nlink = 1 ) {
166176 const vm = t . vm , A = "sun/nio/fs/UnixFileAttributes" ;
167177 const mode = st . device ? S_IFCHR | 0o666 : ( st . link !== undefined ? S_IFLNK : st . dir ? S_IFDIR : S_IFREG ) | ( st . mode ?? ( st . dir ? ( writable ? 0o755 : 0o555 ) : ( writable ? 0o644 : 0o444 ) ) ) ;
168- let sec = BigInt ( Math . floor ( st . mtime / 1000 ) ) , nsec = BigInt ( Math . floor ( ( st . mtime % 1000 ) * 1e6 ) ) ;
169- if ( st . mtimeNs !== undefined ) {
170- sec = st . mtimeNs / 1_000_000_000n ;
171- nsec = st . mtimeNs % 1_000_000_000n ;
172- // before 1970: whole seconds round down, nanoseconds stay positive
173- if ( nsec < 0n ) [ sec , nsec ] = [ sec - 1n , nsec + 1_000_000_000n ] ;
174- }
178+ const [ sec , nsec ] = splitTime ( st . mtime , st . mtimeNs ) ;
175179 vm . setField ( attrs , A , "st_mode" , mode ) ;
176180 vm . setField ( attrs , A , "st_ino" , BigInt ( ino ) ) ;
177181 vm . setField ( attrs , A , "st_dev" , 1n ) ;
178182 vm . setField ( attrs , A , "st_nlink" , nlink ) ;
179183 vm . setField ( attrs , A , "st_uid" , 1000 ) ;
180184 vm . setField ( attrs , A , "st_gid" , 1000 ) ;
181185 vm . setField ( attrs , A , "st_size" , BigInt ( size ) ) ;
182- for ( const k of [ "st_atime_sec" , "st_mtime_sec" , "st_ctime_sec" ] ) vm . setField ( attrs , A , k , sec ) ;
183- for ( const k of [ "st_atime_nsec" , "st_mtime_nsec" , "st_ctime_nsec" ] ) vm . setField ( attrs , A , k , nsec ) ;
186+ for ( const k of [ "st_mtime_sec" , "st_ctime_sec" ] ) vm . setField ( attrs , A , k , sec ) ;
187+ for ( const k of [ "st_mtime_nsec" , "st_ctime_nsec" ] ) vm . setField ( attrs , A , k , nsec ) ;
188+ const [ asec , ansec ] = splitTime ( st . atime ?? st . mtime , st . atimeNs ) ;
189+ vm . setField ( attrs , A , "st_atime_sec" , asec ) ;
190+ vm . setField ( attrs , A , "st_atime_nsec" , ansec ) ;
184191}
185192
186193// open mount table handles: whether the one entry has been read
@@ -310,15 +317,15 @@ export const natives: Natives = {
310317 [ `${ N } chown0(JII)V` ] : ( ) => undefined ,
311318 [ `${ N } lchown0(JII)V` ] : ( ) => undefined ,
312319 [ `${ N } fchown0(III)V` ] : ( ) => undefined ,
313- // times in nanoseconds; only the modification time is kept
314- [ `${ N } utimensat0(IJJJI)V` ] : ( t , [ dfd , p , , mtime , flags ] ) => {
320+ // times in nanoseconds; in-memory files keep only the modification time
321+ [ `${ N } utimensat0(IJJJI)V` ] : ( t , [ dfd , p , atime , mtime , flags ] ) => {
315322 const abs = pathAt ( t , dfd , p ) ;
316323 if ( ! t . vm . fs . lstat ( abs ) ) unixError ( ENOENT ) ;
317- if ( ! setMtime ( t , abs , Number ( mtime ) / 1e6 , BigInt ( mtime ) , ( flags & AT_SYMLINK_NOFOLLOW ) !== 0 ) ) unixError ( EROFS ) ;
324+ if ( ! setMtime ( t , abs , Number ( mtime ) / 1e6 , BigInt ( mtime ) , ( flags & AT_SYMLINK_NOFOLLOW ) !== 0 , { ms : Number ( atime ) / 1e6 , ns : BigInt ( atime ) } ) ) unixError ( EROFS ) ;
318325 } ,
319- [ `${ N } futimens0(IJJ)V` ] : ( t , [ fd , , mtime ] ) => {
326+ [ `${ N } futimens0(IJJ)V` ] : ( t , [ fd , atime , mtime ] ) => {
320327 const f = fileOf ( t , fd ) ;
321- if ( ! setMtime ( t , f . path , Number ( mtime ) / 1e6 , BigInt ( mtime ) ) ) unixError ( EROFS ) ;
328+ if ( ! setMtime ( t , f . path , Number ( mtime ) / 1e6 , BigInt ( mtime ) , false , { ms : Number ( atime ) / 1e6 , ns : BigInt ( atime ) } ) ) unixError ( EROFS ) ;
322329 } ,
323330 [ `${ N } opendir0(J)J` ] : ( t , [ p ] ) => {
324331 const abs = path ( t , p ) ;
0 commit comments