local a=syscall.getTask(syscall.getpid()).name;local b,c={},{x=false,help=false}for d,e in ipairs({...})do if e:sub(1,2)=="--"then local f=e:sub(3)if f=="help"then c.help=true else print(a..": unrecognised option '"..e.."'")print("try '"..a.." --help' for more information.")syscall.exit(1)return end elseif e:sub(1,1)=="-"then for g=2,#e do local h=e:sub(g,g)if c[h]~=nil then c[h]=true else print(a..": invalid option '-"..h.."'")print("try '"..a.." --help' for more information.")syscall.exit(1)return end end else table.insert(b,e)end end;if c.help then print("Usage: "..a.." ")print(" "..a.." -x ")print("")print("Pack a directory into a portable HFS image file, or extract one.")print("")print(" recursively pack srcdir into image.hfs")print(" -x extract image.hfs into dest (created if needed)")print("")print("HFS images can be mounted with:")print(" mount -o loop /path/to/image.hfs /mnt/point")print("")print("Requires root.")return end;local i=require("fs")if c.x then if#b<2 then print(a..": -x requires and ")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;local j=b[1]local k=b[2]if j:sub(1,1)~="/"then j=syscall.getcwd().."/"..j end;if k:sub(1,1)~="/"then k=syscall.getcwd().."/"..k end;local l="/tmp/._loimgcreate_"..tostring(math.random(100000,999999))local m,n=pcall(syscall.losetup,j,true)if not m then print(a..": losetup: "..tostring(n))syscall.exit(1)return end;local o,p=pcall(syscall.mount,l,n)if not o then pcall(syscall.lodetach,n)print(a..": mount: "..tostring(p))syscall.exit(1)return end;if not i.isDir(k)then local q,r=pcall(syscall.mkdir,k)if not q then pcall(syscall.umount,l)pcall(syscall.lodetach,n)print(a..": mkdir '"..b[2].."': "..tostring(r))syscall.exit(1)return end end;local s=0;local function t(u,v)local w=i.list(u)if not w then return end;for d,x in ipairs(w)do local y=u:gsub("/$","").."/"..x;local z=v:gsub("/$","").."/"..x;if i.isDir(y)then pcall(syscall.mkdir,z)t(y,z)else local A,B=pcall(syscall.open,y,"r")if A then local o,C=pcall(syscall.open,z,"w")if o then local q,D=pcall(syscall.read,B,65536*16)if q and D then pcall(syscall.write,C,D)end;pcall(syscall.close,C)s=s+1 end;pcall(syscall.close,B)end end end end;t(l,k)pcall(syscall.umount,l)pcall(syscall.lodetach,n)syscall.devctl(1,"sfgc",0x00FFFF)print(a..": extracted "..s.." file(s) to "..k)syscall.devctl(1,"sfgc",0xFFFFFF)return end;if#b<2 then print(a..": missing operands — need and ")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;local E=b[1]local j=b[2]if E:sub(1,1)~="/"then E=syscall.getcwd().."/"..E end;if j:sub(1,1)~="/"then j=syscall.getcwd().."/"..j end;if not i.isDir(E)then print(a..": '"..b[1].."': not a directory")syscall.exit(1)return end;local A,F=pcall(syscall.loimgcreate,E)if not A then local G=tostring(F)if G:find("EPERM")then G="Permission denied"elseif G:find("ENOTDIR")then G="'"..b[1].."': not a directory"end;print(a..": "..G)syscall.exit(1)return end;local o,H=pcall(syscall.loimgwrite,F,j)if not o then print(a..": write '"..b[2].."': "..tostring(H))syscall.exit(1)return end;local I=0;for d in F:gmatch("\n")do I=I+1 end;local J=#F;syscall.devctl(1,"sfgc",0x00FFFF)print(a..": image written to "..j)syscall.devctl(1,"sfgc",0x6D00FF)print(string.format(" %d records, %d bytes",I-1,J))syscall.devctl(1,"sfgc",0xFFFFFF)