@@ -54,7 +54,7 @@ const closesHosts = new WeakSet<JVM>();
5454
5555// A snapshot cannot hold a host handle, nor the opener a file may still need.
5656export function streams ( f : OpenFile ) : boolean {
57- return ! ! f . contents . host || hostOpeners . has ( f . contents ) ;
57+ return f instanceof DirectoryFile || ! ! f . contents . host || hostOpeners . has ( f . contents ) ;
5858}
5959
6060// Host handles of files the program leaves open close when its VM exits.
@@ -120,17 +120,19 @@ export class OpenFile {
120120 return n ;
121121 }
122122
123- write ( src : Uint8Array ) {
123+ write ( src : Uint8Array , atPosition = false ) {
124+ if ( this . append && ! atPosition ) this . pos = this . size ;
124125 const host = this . contents . host ;
125126 if ( host ) {
126127 hostCall ( ( ) => host . write ( src , this . pos ) ) ;
127128 this . pos += src . length ;
128129 if ( this . pos > this . size ) this . size = this . pos ;
129130 return ;
130131 }
132+ const end = this . pos + src . length ;
133+ if ( end >= STREAM_MIN && this . stream ( ) ) return this . write ( src , atPosition ) ;
131134 // past the end, the gap reads as zeros (the buffer may hold bytes from before a truncation)
132135 if ( this . pos > this . size ) this . data . fill ( 0 , this . size , Math . min ( this . pos , this . data . length ) ) ;
133- const end = this . pos + src . length ;
134136 if ( end > this . data . length ) {
135137 const next = new Uint8Array ( Math . max ( end , this . data . length * 2 , 64 ) ) ;
136138 next . set ( this . data . subarray ( 0 , this . size ) ) ;
@@ -140,7 +142,6 @@ export class OpenFile {
140142 this . pos = end ;
141143 if ( end > this . size ) this . size = end ;
142144 this . dirty = true ;
143- if ( this . size >= STREAM_MIN ) this . stream ( ) ;
144145 }
145146
146147 setLength ( n : number ) {
@@ -197,6 +198,18 @@ export class OpenFile {
197198 close ( ) { this . release ( ) ; }
198199}
199200
201+ // A read-only directory descriptor can be opened, but cannot be read as bytes.
202+ class DirectoryFile extends OpenFile {
203+ constructor ( path : string , size : number ) {
204+ super ( path , new Uint8Array ( 0 ) , false ) ;
205+ this . size = size ;
206+ }
207+
208+ override read ( ) : number {
209+ throw new PendingThrow ( "java/io/IOException" , "Is a directory" ) ;
210+ }
211+ }
212+
200213// Reads and writes a character device as a stream.
201214class DeviceFile extends OpenFile {
202215 constructor ( path : string ) {
@@ -284,20 +297,25 @@ function notFound(path: string, why = "No such file or directory"): never {
284297}
285298
286299// Opens a path and returns its descriptor number.
287- export function openFile ( t : JThread , path : string , write : boolean , truncate : boolean , append : boolean , readable = true ) : number {
300+ export function openFile ( t : JThread , path : string , write : boolean , truncate : boolean , append : boolean , readable = true , allowDirectory = false ) : number {
288301 const fs = t . vm . fs ;
289302 const abs = resolvePath ( t , path ) ;
290303 syncPath ( t , abs ) ;
291304 const st = fs . stat ( abs ) ;
292- if ( st ?. dir ) notFound ( path , "Is a directory" ) ;
305+ if ( st ?. dir ) {
306+ if ( ! allowDirectory || write ) notFound ( path , "Is a directory" ) ;
307+ const tab = table ( t ) ;
308+ tab . files . set ( tab . next , new DirectoryFile ( abs , st . size ) ) ;
309+ return tab . next ++ ;
310+ }
293311 const shared = st ? [ ...table ( t ) . files . values ( ) ] . find ( ( o ) => o . path === abs && o . constructor === OpenFile ) ?. contents : undefined ;
294312 if ( st ?. device || ( st && ! shared && ! write && st . size >= LAZY_MIN && fs . canReadRange ( abs ) ) ) {
295313 const tab = table ( t ) ;
296314 tab . files . set ( tab . next , st . device ? new DeviceFile ( abs ) : new LazyFile ( abs , st . size , fs ) ) ;
297315 return tab . next ++ ;
298316 }
299317 if ( write && fs . denied ( abs ) ) notFound ( path , "Permission denied" ) ;
300- if ( st && ! shared && ! truncate && st . size >= STREAM_MIN && fs . canOpenFile ( abs ) ) {
318+ if ( st && ! shared && ! truncate && st . size >= STREAM_MIN && fs . canOpenFile ( abs , write ) ) {
301319 const host = openHost ( fs , abs , write , readable ) ;
302320 if ( host ) {
303321 closeHostsOnExit ( t . vm ) ;
@@ -330,7 +348,7 @@ export function openFile(t: JThread, path: string, write: boolean, truncate: boo
330348 }
331349 const f = new OpenFile ( abs , data , write , shared ) ;
332350 // only a writer can grow a file, so read-only files never hold an opener
333- if ( write && ! hostOpeners . has ( f . contents ) && fs . canOpenFile ( abs ) ) {
351+ if ( write && ! hostOpeners . has ( f . contents ) && fs . canOpenFile ( abs , true ) ) {
334352 const vm = t . vm ;
335353 const c = f . contents ;
336354 hostOpeners . set ( c , ( current ) => {
0 commit comments