1- _G ._OSNAME = " MiniOS "
2- _G ._OSVER = " 0.5.9.5 "
1+ _G ._OSNAME = " miniOS "
2+ _G ._OSVER = " 0.5.9.7 "
33_G ._OSVERSION = _OSNAME .. " " .. _OSVER
44
55-- component code
@@ -228,6 +228,10 @@ function text_code()
228228
229229 ---- ---------------------------------------------------------------------------
230230
231+ function text .endswith (s , send )
232+ return # s >= # send and s :find (send , # s -# send + 1 , true ) and true or false
233+ end
234+
231235 return text
232236end
233237-- event code
@@ -419,24 +423,44 @@ function event_code()
419423
420424 return event
421425end
422- -- Simple filesystem code
426+ -- filesystem code
423427function fs_code ()
424428 local fs = {}
425429 fs .drive = {}
426- fs .drive ._map = { [" A" ]= computer .getBootAddress (), [" X" ]= computer .tmpAddress () } -- Z: will be the unix style root
430+ -- drive mapping table, initilized later
431+ fs .drive ._map = {}
432+ -- converts a drive letter into a proxy
433+ function fs .drive .letterToProxy (letter )
434+ return fs .drive ._map [letter ]
435+ end
436+ -- finds the proxy associated with the letter
437+ function fs .drive .proxyToLetter (proxy )
438+ for l ,p in pairs (fs .drive ._map ) do
439+ if p == proxy then return l end
440+ end
441+ return nil
442+ end
443+ -- maps a proxy to a letter
444+ function fs .drive .mapProxy (letter , proxy )
445+ fs .drive ._map [letter ] = proxy
446+ end
447+ -- finds the address of a drive letter.
427448 function fs .drive .toAddress (letter )
428- return fs .drive ._map [letter ]
449+ return fs .drive ._map [letter ]. address
429450 end
451+ -- finds the drive letter mapped to an address
430452 function fs .drive .toLetter (address )
431- for l ,a in pairs (fs .drive ._map ) do
432- if a == address then return l end
453+ for l ,p in pairs (fs .drive ._map ) do
454+ if p . address == address then return l end
433455 end
434456 return nil
435457 end
436458 function fs .drive .mapAddress (letter , address )
437- fs .drive ._map [letter ] = address
459+ -- print("mapAddress")
460+ fs .drive ._map [letter ] = fs .proxy (address )
438461 end
439462 function fs .drive .autoMap (address ) -- returns the letter if mapped OR already mapped, false if not.
463+ -- print("autoMap")
440464 -- we get the address and see if it already is mapped...
441465 local l = fs .drive .toLetter (address )
442466 if l then return l end
@@ -450,28 +474,38 @@ function fs_code()
450474 if l == " _" then return false end
451475 end
452476 end
453- function fs .drive .list ()
477+ function fs .drive .listProxy ()
454478 local t = fs .drive ._map
455- local a = {}
456- for n in pairs (t ) do table.insert (a , n ) end
457- table.sort (a , f )
479+ local p = {}
480+ for n in pairs (t ) do table.insert (p , n ) end
481+ table.sort (p , f )
458482 local i = 0 -- iterator variable
459483 local iter = function () -- iterator function
460484 i = i + 1
461- if a [i ] == nil then return nil
462- else return a [i ], t [a [i ]]
485+ if p [i ] == nil then return nil
486+ else return p [i ], t [p [i ]]
463487 end
464488 end
465489 return iter
466490 end
491+ function fs .drive .list ()
492+ local i = 0 -- iterator variable
493+ local proxyIter = fs .drive .listProxy ()
494+ local iter = function () -- iterator function
495+ l , p = proxyIter ()
496+ if not l then return nil end
497+ return l , p .address
498+ end
499+ return iter
500+ end
467501 fs .drive ._current = " A" -- as the boot drive is A:
468502 function fs .drive .setcurrent (letter )
469503 letter = letter :upper ()
470504 if not fs .drive ._map [letter ] then error (" Invalid Drive" , 2 ) end
471505 fs .drive ._current = letter
472506 end
473507 function fs .drive .getcurrent () return fs .drive ._current end
474- function fs .invoke (method , ...) return component . invoke ( fs .drive .toAddress ( fs .drive ._current ), method , ... ) end
508+ function fs .invoke (method , ...) return fs .drive ._map [ fs .drive ._current ][ method ]( ... ) end
475509 function fs .proxy (filter )
476510 checkArg (1 , filter , " string" )
477511 local address
@@ -499,6 +533,39 @@ function fs_code()
499533 function fs .close (handle ) return fs .invoke (" close" , handle ) end
500534 function fs .isDirectory (path ) return fs .invoke (" isDirectory" , path ) end
501535 function fs .exists (path ) return fs .invoke (" exists" , path ) end
536+ function fs .remove (path ) return fs .invoke (" remove" , path ) end
537+ function fs .copy (fromPath , toPath )
538+ if fs .isDirectory (fromPath ) then
539+ return nil , " cannot copy folders"
540+ end
541+ local input , reason = fs .open (fromPath , " rb" )
542+ if not input then
543+ return nil , reason
544+ end
545+ local output , reason = fs .open (toPath , " wb" )
546+ if not output then
547+ fs .close (input )
548+ return nil , reason
549+ end
550+ repeat
551+ local buffer , reason = filesystem .read (input )
552+ if not buffer and reason then
553+ return nil , reason
554+ elseif buffer then
555+ local result , reason = filesystem .write (output , buffer )
556+ if not result then
557+ filesystem .close (input )
558+ filesystem .close (output )
559+ return nil , reason
560+ end
561+ end
562+ until not buffer
563+ filesystem .close (input )
564+ filesystem .close (output )
565+ return true
566+ end
567+ function fs .rename (path1 , path2 ) return fs .invoke (" rename" , path1 , path2 ) end
568+ function fs .makeDirectory (path ) return fs .invoke (" makeDirectory" , path ) end
502569 function fs .list (path )
503570 local i = 0
504571 local t = fs .invoke (" list" , path )
@@ -517,12 +584,18 @@ function fs_code()
517584 end
518585 end
519586 local function onComponentRemoved (_ , address , componentType )
520- if componentType == " filesystem" then
521- fs .drive .mapAddress (fs .drive .toLetter (address ), nil )
587+ if componentType == " filesystem" then
588+ fs .drive .mapAddress (fs .drive .toLetter (address ), nil )
589+ end
522590 end
523- end
524591 event .listen (" component_added" , onComponentAdded )
525592 event .listen (" component_removed" , onComponentRemoved )
593+ local function driveInit ()
594+ local boot = fs .proxy (computer .getBootAddress ())
595+ local temp = fs .proxy (computer .tmpAddress ())
596+ fs .drive ._map = { [" A" ]= boot , [" X" ]= temp }
597+ end
598+ driveInit ()
526599 -- return the API
527600 return fs
528601end
@@ -1043,15 +1116,14 @@ end
10431116
10441117print (" Starting " .. _OSNAME .. " ...\n " )
10451118
1119+ -- clean up libs
1120+ event_code , component_code , text_code , fs_code , terminal_code = nil , nil , nil , nil , nil
1121+
10461122-- map the drives
10471123for address , componentType in component .list () do
10481124 if componentType == " filesystem" then filesystem .drive .autoMap (address ) end
10491125end
10501126
1051- function text .endswith (s , send )
1052- return # s >= # send and s :find (send , # s -# send + 1 , true ) and true or false
1053- end
1054-
10551127miniOS = {}
10561128local function interrupt (data )
10571129 if data [2 ] == " RUN" then miniOS .runfile (data [3 ], table.unpack (data [4 ])) end
0 commit comments