Files
HyperionOS/prod/hysh/bin/losetup

2 lines
3.0 KiB
Plaintext

local a=syscall.getTask(syscall.getpid()).name;local b,c={},{d=false,l=false,i=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.." <path>")print(" "..a.." -i <path>")print(" "..a.." -d <id>")print(" "..a.." -l")print("")print("Manage loop devices.")print("")print(" <path> attach a directory (bind) or .hfs image file")print(" -i <path> force image mode for the given file")print(" -d <id> detach loop device by id (must be unmounted first)")print(" -l list all currently attached loop devices")print("")print("Requires root. Loop device ids look like loop0, loop1, …")return end;if c.l then local i,j=pcall(syscall.lolist)if not i then print(a..": "..tostring(j))syscall.exit(1)return end;local k=false;local l={}for m in pairs(j)do l[#l+1]=m end;table.sort(l)for d,m in ipairs(l)do k=true;local n=j[m]local o=type(n)=="table"and n.mode or"bind"local p=type(n)=="table"and n.path or tostring(n)local q=o=="image"and 0x00FFFF or 0x0000FF;syscall.devctl(1,"sfgc",0x00FF00)printInline(string.format("%-10s",m))syscall.devctl(1,"sfgc",q)printInline(string.format("%-7s","["..o.."]"))syscall.devctl(1,"sfgc",0xFFFFFF)print(" "..p)end;if not k then syscall.devctl(1,"sfgc",0x6D00FF)print(a..": no loop devices attached")syscall.devctl(1,"sfgc",0xFFFFFF)end;return end;if c.d then if#b<1 then print(a..": -d requires a loop device id")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;local m=b[1]local i,r=pcall(syscall.lodetach,m)if not i then local s=tostring(r)if s:find("EPERM")then s="Permission denied"elseif s:find("ENXIO")then s="no such loop device '"..m.."'"elseif s:find("EBUSY")then s="device '"..m.."' is still mounted, unmount first"end;print(a..": "..s)syscall.exit(1)return end;syscall.devctl(1,"sfgc",0x00FFFF)print(a..": detached "..m)syscall.devctl(1,"sfgc",0xFFFFFF)return end;if#b<1 then print(a..": missing path operand")print("try '"..a.." --help' for more information.")syscall.exit(1)return end;local p=b[1]if p:sub(1,1)~="/"then p=syscall.getcwd().."/"..p end;local t=syscall.type and syscall.type(p)if not(t=="file"or t=="directory")then print(a..": '"..b[1].."': no such file or directory")syscall.exit(1)return end;local i,u=pcall(syscall.losetup,p,c.i or nil)if not i then local s=tostring(u)if s:find("EPERM")then s="Permission denied"elseif s:find("ENOENT")then s="'"..b[1].."': no such file"elseif s:find("EINVAL")then s="'"..b[1].."': not a directory or .hfs image"elseif s:find("EIO")then s="'"..b[1].."': I/O error reading image"end;print(a..": "..s)syscall.exit(1)return end;print(u)