@@ -356,10 +356,74 @@ function pathOf(t: JThread, file: JObject): string {
356356 return t . vm . jsString ( t . vm . getField ( file , "java/io/File" , "path" ) ) ;
357357}
358358
359+ // java.util.prefs lock files: fcntl locks, which only this VM's threads can
360+ // contend for here. path -> readers, or -1 while one writer holds it.
361+ interface PrefsLock { path : string ; shared : boolean }
362+ function prefsLocks ( t : JThread ) {
363+ let s = t . vm . state . get ( "prefsLocks" ) as { held : Map < string , number > ; fds : Map < number , PrefsLock > } | undefined ;
364+ if ( ! s ) t . vm . state . set ( "prefsLocks" , ( s = { held : new Map ( ) , fds : new Map ( ) } ) ) ;
365+ return s ;
366+ }
367+ // Linux errno numbers of the codes Node reports by name
368+ const ERRNO_NUMBER : Record < string , number > = {
369+ EPERM : 1 , ENOENT : 2 , EIO : 5 , EBADF : 9 , EAGAIN : 11 , ENOMEM : 12 , EACCES : 13 , ENOTDIR : 20 , EINVAL : 22 , ENFILE : 23 ,
370+ EMFILE : 24 , ENOSPC : 28 , EROFS : 30 , ENAMETOOLONG : 36 , ELOOP : 40 , ENOTSUP : 95 , EOPNOTSUPP : 95 , EDQUOT : 122 ,
371+ } ;
372+
373+ function intPair ( t : JThread , a : number , b : number ) : JArray {
374+ const arr = t . vm . newArray ( t . vm . primClass ( "I" ) , 2 ) ;
375+ ( arr . a as Int32Array ) . set ( [ a , b ] ) ;
376+ return arr ;
377+ }
378+
359379const FOS = "java/io/FileOutputStream" , FIS = "java/io/FileInputStream" , RAF = "java/io/RandomAccessFile" ;
360380const UFS = "java/io/UnixFileSystem" ;
361381
362382export const natives : Natives = {
383+ // the errno of chmod(2), or 0
384+ "java/util/prefs/FileSystemPreferences.chmod(Ljava/lang/String;I)I" : ( t , [ name , mode ] ) => {
385+ const abs = resolvePath ( t , t . vm . jsString ( name ) ) ;
386+ if ( ! t . vm . fs . stat ( abs ) ) return ERRNO_NUMBER . ENOENT ;
387+ const ok = t . vm . fs . chmod ( abs , ( m ) => ( m & ~ 0o7777 ) | ( mode & 0o7777 ) ) ;
388+ return ok === false ? ERRNO_NUMBER [ t . vm . fs . chmodError ( abs ) ?? "EIO" ] ?? ERRNO_NUMBER . EIO : 0 ;
389+ } ,
390+ // [fd, errno]; fd 0 when the file could not be opened or locked
391+ "java/util/prefs/FileSystemPreferences.lockFile0(Ljava/lang/String;IZ)[I" : ( t , [ name , mode , shared ] ) => {
392+ const abs = resolvePath ( t , t . vm . jsString ( name ) ) ;
393+ let fd : number ;
394+ try {
395+ fd = openFile ( t , abs , ! shared , false , false ) ;
396+ } catch {
397+ return intPair ( t , 0 , t . vm . fs . stat ( abs ) ? ERRNO_NUMBER . EACCES : ERRNO_NUMBER . ENOENT ) ;
398+ }
399+ // the requested mode, as HotSpot's fchmod gives it; a failure is ignored there too
400+ if ( ! shared ) t . vm . fs . chmod ( abs , ( m ) => ( m & ~ 0o7777 ) | ( mode & 0o7777 ) ) ;
401+ const { held, fds } = prefsLocks ( t ) ;
402+ const now = held . get ( abs ) ?? 0 ;
403+ if ( shared ? now < 0 : now !== 0 ) {
404+ closeFd ( t , fd ) ;
405+ return intPair ( t , 0 , ERRNO_NUMBER . EAGAIN ) ;
406+ }
407+ held . set ( abs , shared ? now + 1 : - 1 ) ;
408+ fds . set ( fd , { path : abs , shared : ! ! shared } ) ;
409+ return intPair ( t , fd , 0 ) ;
410+ } ,
411+ "java/util/prefs/FileSystemPreferences.unlockFile0(I)I" : ( t , [ fd ] ) => {
412+ const { held, fds } = prefsLocks ( t ) ;
413+ const lock = fds . get ( fd ) ;
414+ if ( lock ) {
415+ fds . delete ( fd ) ;
416+ const now = held . get ( lock . path ) ?? 0 ;
417+ if ( lock . shared && now > 1 ) held . set ( lock . path , now - 1 ) ;
418+ else held . delete ( lock . path ) ;
419+ }
420+ try {
421+ closeFd ( t , fd ) ;
422+ } catch {
423+ return ERRNO_NUMBER . EIO ;
424+ }
425+ return 0 ;
426+ } ,
363427 // serialization: the default serialVersionUID depends on whether the class
364428 // itself declares a static initializer
365429 "java/io/ObjectStreamClass.initNative()V" : ( ) => undefined ,
0 commit comments