Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local protocol = "sync_protocol"
- local clientPath = "/disk/users/"
- -- Get sync mode from arguments
- local args = {...}
- local syncMode = args[1] or "full" -- Default to full sync if no argument provided
- if syncMode ~= "upload" and syncMode ~= "download" and syncMode ~= "full" then
- print("Invalid mode! Use: 'upload', 'download', or leave empty for full sync.")
- return
- end
- -- Check if the DomainServiceEnabled directory exists
- local domainServicePath = "/disk/config/domain/DomainServiceEnabled.cfg/"
- if not fs.exists(domainServicePath) then
- print("Error: " .. domainServicePath .. " does not exist. Sync process will be ignored.")
- return
- end
- -- Read the server ID from .domaincontrollerID file
- local function getServerID()
- local filePath = ".domaincontrollerID"
- if not fs.exists(filePath) then
- print("Error: .domaincontrollerID file not found.")
- return nil
- end
- local file = fs.open(filePath, "r")
- local serverID = tonumber(file.readAll())
- file.close()
- return serverID
- end
- local serverID = getServerID()
- if not serverID then
- print("Invalid server ID.")
- return
- end
- -- Open the modem
- rednet.open(peripheral.getName(peripheral.find("modem") or error("No modem found")))
- -- Function to compute file hashes
- local function getFileHash(filePath)
- if not fs.exists(filePath) then return nil end
- local f = fs.open(filePath, "r")
- local content = f.readAll()
- f.close()
- return textutils.serialize(content)
- end
- -- Get file list with hashes
- local function getFileList(path)
- local files = {}
- local function scan(dir)
- for _, file in pairs(fs.list(dir)) do
- local fullPath = dir .. file
- if fs.isDir(fullPath) then
- scan(fullPath .. "/")
- else
- files[fullPath:sub(#clientPath + 1)] = getFileHash(fullPath)
- end
- end
- end
- scan(path)
- return files
- end
- -- Delete files that no longer exist on the server
- local function deleteMissingFiles(missingFiles)
- for _, file in pairs(missingFiles) do
- local filePath = clientPath .. file
- if fs.exists(filePath) then
- fs.delete(filePath)
- print("Deleted missing file:", file)
- end
- end
- end
- -- Perform sync
- local function syncFiles()
- print("Connecting to server " .. serverID .. "...")
- -- Send file list with hashes to server
- rednet.send(serverID, { action = "sync_request", mode = syncMode, files = getFileList(clientPath) }, protocol)
- local id, response = rednet.receive(protocol, 5)
- if id == serverID and response.action == "sync_response" then
- if syncMode == "download" or syncMode == "full" then
- -- Download missing/updated files from server
- print("Downloading files from server...")
- for file, content in pairs(response.files_to_client) do
- local filePath = clientPath .. file
- local f = fs.open(filePath, "w")
- f.write(content)
- f.close()
- print("Updated from server:", file)
- end
- end
- if syncMode == "upload" or syncMode == "full" then
- -- Send missing/updated files to server
- rednet.send(serverID, { action = "sync_upload", files_to_server = {} }, protocol)
- local id2, clientResponse = rednet.receive(protocol, 5)
- if id2 == serverID and clientResponse.action == "sync_upload_request" then
- for _, file in pairs(clientResponse.files_needed) do
- local filePath = clientPath .. file
- if fs.exists(filePath) then
- local f = fs.open(filePath, "r")
- local content = f.readAll()
- f.close()
- clientResponse.files_to_server[file] = content
- end
- end
- rednet.send(serverID, clientResponse, protocol)
- end
- end
- -- If files were deleted on the client side (missing from server), delete them locally
- if response.files_to_delete then
- deleteMissingFiles(response.files_to_delete)
- end
- else
- print("No response from server.")
- end
- end
- syncFiles()
- print("Sync complete.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement