From 537776e1f8ece6f1ef650aac2e2d609721a91577 Mon Sep 17 00:00:00 2001 From: James Turner Date: Wed, 4 Nov 2020 11:22:48 +0000 Subject: [PATCH] TerraSync: fix local-file copying Avoid downloading data corresponding to files shipped in FGData. --- simgear/io/HTTPRepository.cxx | 64 +++++++++++++++++-------------- simgear/scene/tsync/terrasync.cxx | 17 +++++--- 2 files changed, 46 insertions(+), 35 deletions(-) diff --git a/simgear/io/HTTPRepository.cxx b/simgear/io/HTTPRepository.cxx index c197d27a..d7db9539 100644 --- a/simgear/io/HTTPRepository.cxx +++ b/simgear/io/HTTPRepository.cxx @@ -221,42 +221,48 @@ public: char* buf = nullptr; size_t bufSize = 0; - for (const auto& child : children) { - if (child.type != HTTPRepository::FileType) - continue; + for (auto &child : children) { + if (child.type != HTTPRepository::FileType) + continue; - if (child.path.exists()) - continue; + if (child.path.exists()) + continue; - SGPath cp = _repository->installedCopyPath; - cp.append(relativePath()); - cp.append(child.name); - if (!cp.exists()) { - continue; - } + SGPath cp = _repository->installedCopyPath; + cp.append(relativePath()); + cp.append(child.name); + if (!cp.exists()) { + continue; + } - SGBinaryFile src(cp); - SGBinaryFile dst(child.path); - src.open(SG_IO_IN); - dst.open(SG_IO_OUT); + SGBinaryFile src(cp); + SGBinaryFile dst(child.path); + src.open(SG_IO_IN); + dst.open(SG_IO_OUT); - if (bufSize < cp.sizeInBytes()) { - bufSize = cp.sizeInBytes(); - free(buf); - buf = (char*) malloc(bufSize); - if (!buf) { - continue; - } - } + if (bufSize < cp.sizeInBytes()) { + bufSize = cp.sizeInBytes(); + free(buf); + buf = (char *)malloc(bufSize); + if (!buf) { + continue; + } + } - src.read(buf, cp.sizeInBytes()); - dst.write(buf, cp.sizeInBytes()); - src.close(); - dst.close(); + src.read(buf, cp.sizeInBytes()); + dst.write(buf, cp.sizeInBytes()); + src.close(); + dst.close(); - } + // reset caching + child.path.set_cached(false); + child.path.set_cached(true); - free(buf); + std::string hash = computeHashForPath(child.path); + updatedFileContents(child.path, hash); + } + + free(buf); } void updateChildrenBasedOnHash() diff --git a/simgear/scene/tsync/terrasync.cxx b/simgear/scene/tsync/terrasync.cxx index d208456a..e9702fad 100644 --- a/simgear/scene/tsync/terrasync.cxx +++ b/simgear/scene/tsync/terrasync.cxx @@ -603,12 +603,6 @@ void SGTerraSync::WorkerThread::updateSyncSlot(SyncSlot &slot) beginNormalSync(slot); } - if (_installRoot.exists()) { - SGPath p = _installRoot; - p.append(slot.currentItem._dir); - slot.repository->setInstalledCopyPath(p); - } - try { slot.repository->update(); } catch (sg_exception& e) { @@ -676,6 +670,11 @@ void SGTerraSync::WorkerThread::beginSyncTile(SyncSlot& slot) slot.repository.reset(new HTTPRepository(path, &_http)); slot.repository->setBaseUrl(_httpServer + "/" + tileCategory); + if (_installRoot.exists()) { + SGPath p = _installRoot / tileCategory; + slot.repository->setInstalledCopyPath(p); + } + const auto dirPrefix = tenByTenDir + "/" + oneByOneDir; // filter callback to *only* sync the 1x1 dir we want, if it exists @@ -706,6 +705,12 @@ void SGTerraSync::WorkerThread::beginNormalSync(SyncSlot& slot) path.append(slot.currentItem._dir); slot.repository.reset(new HTTPRepository(path, &_http)); slot.repository->setBaseUrl(_httpServer + "/" + slot.currentItem._dir); + + if (_installRoot.exists()) { + SGPath p = _installRoot; + p.append(slot.currentItem._dir); + slot.repository->setInstalledCopyPath(p); + } } void SGTerraSync::WorkerThread::runInternal()