Advertisement
DOGGYWOOF

Untitled

Jan 29th, 2025 (edited)
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.33 KB | None | 0 0
  1. local protocol = "sync_protocol"
  2. local clientPath = "/disk/users/"
  3.  
  4. -- Get sync mode from arguments
  5. local args = {...}
  6. local syncMode = args[1] or "full" -- Default to full sync if no argument provided
  7.  
  8. if syncMode ~= "upload" and syncMode ~= "download" and syncMode ~= "full" then
  9. print("Invalid mode! Use: 'upload', 'download', or leave empty for full sync.")
  10. return
  11. end
  12.  
  13. -- Check if the DomainServiceEnabled directory exists
  14. local domainServicePath = "/disk/config/domain/DomainServiceEnabled.cfg/"
  15. if not fs.exists(domainServicePath) then
  16. print("Error: " .. domainServicePath .. " does not exist. Sync process will be ignored.")
  17. return
  18. end
  19.  
  20. -- Read the server ID from .domaincontrollerID file
  21. local function getServerID()
  22. local filePath = ".domaincontrollerID"
  23. if not fs.exists(filePath) then
  24. print("Error: .domaincontrollerID file not found.")
  25. return nil
  26. end
  27. local file = fs.open(filePath, "r")
  28. local serverID = tonumber(file.readAll())
  29. file.close()
  30. return serverID
  31. end
  32.  
  33. local serverID = getServerID()
  34. if not serverID then
  35. print("Invalid server ID.")
  36. return
  37. end
  38.  
  39. -- Open the modem
  40. rednet.open(peripheral.getName(peripheral.find("modem") or error("No modem found")))
  41.  
  42. -- Function to compute file hashes
  43. local function getFileHash(filePath)
  44. if not fs.exists(filePath) then return nil end
  45. local f = fs.open(filePath, "r")
  46. local content = f.readAll()
  47. f.close()
  48. return textutils.serialize(content)
  49. end
  50.  
  51. -- Get file list with hashes
  52. local function getFileList(path)
  53. local files = {}
  54. local function scan(dir)
  55. for _, file in pairs(fs.list(dir)) do
  56. local fullPath = dir .. file
  57. if fs.isDir(fullPath) then
  58. scan(fullPath .. "/")
  59. else
  60. files[fullPath:sub(#clientPath + 1)] = getFileHash(fullPath)
  61. end
  62. end
  63. end
  64. scan(path)
  65. return files
  66. end
  67.  
  68. -- Delete files that no longer exist on the server
  69. local function deleteMissingFiles(missingFiles)
  70. for _, file in pairs(missingFiles) do
  71. local filePath = clientPath .. file
  72. if fs.exists(filePath) then
  73. fs.delete(filePath)
  74. print("Deleted missing file:", file)
  75. end
  76. end
  77. end
  78.  
  79. -- Perform sync
  80. local function syncFiles()
  81. print("Connecting to server " .. serverID .. "...")
  82.  
  83. -- Send file list with hashes to server
  84. rednet.send(serverID, { action = "sync_request", mode = syncMode, files = getFileList(clientPath) }, protocol)
  85.  
  86. local id, response = rednet.receive(protocol, 5)
  87. if id == serverID and response.action == "sync_response" then
  88. if syncMode == "download" or syncMode == "full" then
  89. -- Download missing/updated files from server
  90. print("Downloading files from server...")
  91. for file, content in pairs(response.files_to_client) do
  92. local filePath = clientPath .. file
  93. local f = fs.open(filePath, "w")
  94. f.write(content)
  95. f.close()
  96. print("Updated from server:", file)
  97. end
  98. end
  99.  
  100. if syncMode == "upload" or syncMode == "full" then
  101. -- Send missing/updated files to server
  102. rednet.send(serverID, { action = "sync_upload", files_to_server = {} }, protocol)
  103. local id2, clientResponse = rednet.receive(protocol, 5)
  104. if id2 == serverID and clientResponse.action == "sync_upload_request" then
  105. for _, file in pairs(clientResponse.files_needed) do
  106. local filePath = clientPath .. file
  107. if fs.exists(filePath) then
  108. local f = fs.open(filePath, "r")
  109. local content = f.readAll()
  110. f.close()
  111. clientResponse.files_to_server[file] = content
  112. end
  113. end
  114. rednet.send(serverID, clientResponse, protocol)
  115. end
  116. end
  117.  
  118. -- If files were deleted on the client side (missing from server), delete them locally
  119. if response.files_to_delete then
  120. deleteMissingFiles(response.files_to_delete)
  121. end
  122. else
  123. print("No response from server.")
  124. end
  125. end
  126.  
  127. syncFiles()
  128. print("Sync complete.")
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement