filethread.lua 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. --A thread to keep track of files being edited, and automatically update them in the appdata folder
  2. --So there would be no longer need to restart LIKO-12 for some tasks.
  3. --By default it only tracks the DiskOS folder.
  4. require("love.system")
  5. local reg = {}
  6. local tpath = "/OS/DiskOS/" --Tracking Path
  7. local dpath = "/drives/C/" --Destination Path
  8. local channel = love.thread.getChannel("BIOSFileThread") --Stop the thread when needed.
  9. local function checkDir(dir,r)
  10. local dir = dir or ""
  11. local r = r or reg
  12. local path = tpath..dir
  13. local items = love.filesystem.getDirectoryItems(path)
  14. for k, file in ipairs(items) do if file:sub(1,1) ~= "." then
  15. local fpath = path..file
  16. if love.filesystem.getInfo(fpath,"file") then --It's a file
  17. local fupdate = false --Should the file be updated ?
  18. if not love.filesystem.getInfo(dpath..dir..file) then --Add new file
  19. print("New file added")
  20. fupdate = true
  21. else --Check old file
  22. local info = love.filesystem.getInfo(fpath)
  23. local modtime, merr = info and info.modtime, ""
  24. if modtime then
  25. if r[file] then --It's registered
  26. if modtime > r[file] then --It has been edited !
  27. fupdate = true
  28. end
  29. else --It's not registered !
  30. r[file] = info.modtime --Register it.
  31. end
  32. else
  33. print("Error: failed to get modification time,",modtime)
  34. end
  35. end
  36. --Update the file
  37. if fupdate then
  38. local info = love.filesystem.getInfo(fpath)
  39. r[file] = info and info.modtime or false
  40. local data, rerr = love.filesystem.read(fpath)
  41. if data then
  42. local ok, werr = love.filesystem.write(dpath..dir..file,data)
  43. if not ok then r[file] = nil print("Error: Failed to write,",werr) else
  44. print("Updated File",fpath)
  45. end
  46. else
  47. print("Error: Failed to read,",rerr)
  48. end
  49. end
  50. else --Nested directory
  51. if not r[file] then r[file] = {} end
  52. checkDir(dir..file.."/",r[file])
  53. end
  54. end end
  55. end
  56. print("Started File Thread")
  57. while true do
  58. local shut = channel:pop()
  59. if shut then break end --Finish the thread.
  60. checkDir()
  61. end
  62. print("Finished File Thread")