Operating system for OpenComputers
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.1KB

  1. local fsmanager = {}
  2. fsmanager.filesystems = {}
  3. local run = true
  4. function fsmanager.mount(addr)
  5. for k,v in ipairs(fs.mounts()) do
  6. if fs.address(v) == addr then
  7. return
  8. end
  9. end
  10. dest = "/" .. (component.invoke(addr,"getLabel") or "mnt/"..addr:sub(1,3))
  11. syslog("Mounting "..addr.." to "..dest)
  12. fs.makeDirectory(dest)
  13. local w,r = fs.mount(dest,component.proxy(addr))
  14. if not w then
  15. syslog("Failed to mount: "..r)
  16. return false
  17. end
  18. fsmanager.filesystems[addr] = dest
  19. end
  20. function fsmanager.start()
  21. run = true
  22. return os.spawn(function()
  23. for addr, _ in component.list("filesystem") do
  24. fsmanager.mount(addr)
  25. end
  26. while run do
  27. local tE = {coroutine.yield()}
  28. if tE[1] == "component_added" and tE[3] == "filesystem" then
  29. fsmanager.mount(tE[2])
  30. elseif tE[1] == "component_removed" and fsmanager.filesystems[tE[2]] and tE[3] == "filesystem" then
  31. syslog("Unmounting "..tE[2].." from "..fsmanager.filesystems[tE[2]])
  32. fs.umount(fsmanager.filesystems[tE[2]])
  33. fsmanager.filesystems[tE[2]] = nil
  34. end
  35. end
  36. end,"fsmanager")
  37. end
  38. function fsmanager.stop()
  39. run = false
  40. end
  41. return fsmanager