diff options
| author | ruki <[email protected]> | 2022-04-16 17:06:23 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-04-16 17:06:23 +0800 |
| commit | b625ced105994f15883c5ab6a5122738bfb8b922 (patch) | |
| tree | 11f7bfebe861b3cbb9367d42e69e050a2a443ca5 | |
| parent | 4d3e89832a34764cd081d7050fe43be0ce1b0240 (diff) | |
improve sync
| -rw-r--r-- | xmake/modules/private/service/remote_build/client.lua | 6 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/filesync.lua | 18 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_build/session.lua | 31 |
3 files changed, 46 insertions, 9 deletions
diff --git a/xmake/modules/private/service/remote_build/client.lua b/xmake/modules/private/service/remote_build/client.lua index 10cc3bc02..78b6ff683 100644 --- a/xmake/modules/private/service/remote_build/client.lua +++ b/xmake/modules/private/service/remote_build/client.lua @@ -174,6 +174,10 @@ function remote_build_client:sync() if not diff_files then break end + if not diff_files.changed then + ok = true + break + end -- archive diff files archive_diff_file, errors = self:_archive_diff_files(diff_files) @@ -357,7 +361,7 @@ end function remote_build_client:_diff_files(stream) assert(self:is_connected(), "%s: has been not connected!", self) local filesync = self:_filesync() - local manifest = filesync:reset() + local manifest = filesync:snapshot() local session_id = self:session_id() local result, errors if stream:send_msg(message.new_diff(session_id, manifest)) and stream:flush() then diff --git a/xmake/modules/private/service/remote_build/filesync.lua b/xmake/modules/private/service/remote_build/filesync.lua index 2c4024569..e0fc2c0d6 100644 --- a/xmake/modules/private/service/remote_build/filesync.lua +++ b/xmake/modules/private/service/remote_build/filesync.lua @@ -78,8 +78,8 @@ function filesync:manifest_file() return self._MANIFEST_FILE end --- reset all, it will re-scan all and update to manifest file -function filesync:reset() +-- do snapshot, it will re-scan all and update to manifest file +function filesync:snapshot() local rootdir = self:rootdir() assert(rootdir and os.isdir(rootdir), "reset %s failed, rootdir not found!", rootdir) local manifest = self:manifest() @@ -102,6 +102,20 @@ function filesync:reset() return manifest end +-- update file +function filesync:update(fileitem, filepath, sha256) + local mtime = os.mtime(filepath) + local manifest = self:manifest() + sha256 = sha256 or hash.sha256(filepath) + manifest[fileitem] = {sha256 = sha256, mtime = mtime} +end + +-- remove file +function filesync:remote(fileitem) + local manifest = self:manifest() + manifest[fileitem] = nil +end + -- load ignore files from .gitignore files function filesync:_ignorefiles_load(ignorefiles) local rootdir = self:rootdir() diff --git a/xmake/modules/private/service/remote_build/session.lua b/xmake/modules/private/service/remote_build/session.lua index d37facd9a..61caf0163 100644 --- a/xmake/modules/private/service/remote_build/session.lua +++ b/xmake/modules/private/service/remote_build/session.lua @@ -47,10 +47,7 @@ end -- open session function session:open() - local sourcedir = self:sourcedir() - if not os.isdir(sourcedir) then - os.mkdir(sourcedir) - end + self:_ensure_sourcedir() end -- close session @@ -71,8 +68,13 @@ end function session:diff(respmsg) local body = respmsg:body() vprint("%s: diff files in %s ..", self, self:sourcedir()) + + -- ensure sourcedir + self:_ensure_sourcedir() + + -- do snapshot local filesync = self:_filesync() - local manifest_server = assert(filesync:reset(), "server manifest not found!") + local manifest_server = assert(filesync:snapshot(), "server manifest not found!") local manifest_client = assert(body.manifest, "client manifest not found!") -- get all files @@ -88,22 +90,26 @@ function session:diff(respmsg) local removed = {} local modified = {} local inserted = {} + local changed = false for _, fileitem in fileitems:keys() do local manifest_info_client = manifest_client[fileitem] local manifest_info_server = manifest_server[fileitem] if manifest_info_client and manifest_info_server and manifest_info_client.sha256 ~= manifest_info_server.sha256 then table.insert(modified, fileitem) + changed = true vprint("[*]: %s", fileitem) elseif not manifest_info_server and manifest_info_client then table.insert(inserted, fileitem) + changed = true vprint("[+]: %s", fileitem) elseif not manifest_info_server and manifest_info_client then table.insert(removed, fileitem) + changed = true vprint("[-]: %s", fileitem) end end - body.manifest = {removed = removed, inserted = inserted, modified = modified} + body.manifest = {changed = changed, removed = removed, inserted = inserted, modified = modified} vprint("%s: diff files ok", self) end @@ -112,6 +118,7 @@ function session:sync(respmsg) local body = respmsg:body() local stream = self:stream() local manifest = assert(body.manifest, "manifest not found!") + local filesync = self:_filesync() local sourcedir = self:sourcedir() local archivefile = os.tmpfile() .. ".zip" local archivedir = archivefile .. ".dir" @@ -128,18 +135,22 @@ function session:sync(respmsg) local filepath_server = path.join(sourcedir, fileitem) local filepath_client = path.join(archivedir, fileitem) os.cp(filepath_client, filepath_server) + filesync:update(fileitem, filepath_server) end for _, fileitem in ipairs(manifest.modified) do vprint("[*]: %s", fileitem) local filepath_server = path.join(sourcedir, fileitem) local filepath_client = path.join(archivedir, fileitem) os.cp(filepath_client, filepath_server) + filesync:update(fileitem, filepath_server) end for _, fileitem in ipairs(manifest.removed) do vprint("[-]: %s", fileitem) local filepath_server = path.join(sourcedir, fileitem) os.rm(filepath_server) + filesync:remove(fileitem) end + filesync:manifest_save() else raise("receive files failed!") end @@ -202,6 +213,14 @@ function session:_filesync() return self._FILESYNC end +-- ensure source directory +function session:_ensure_sourcedir() + local sourcedir = self:sourcedir() + if not os.isdir(sourcedir) then + os.mkdir(sourcedir) + end +end + -- write data from pipe function session:_write_pipe(opt) local buff = bytes(256) |
