From d4b7e6baca5b036cb624eec3a7cbb863349ceda3 Mon Sep 17 00:00:00 2001 From: Lunaphied Date: Wed, 3 Apr 2024 20:45:47 -0600 Subject: build-remote: truncate+hash store URI used in lockfile paths Fixes: https://git.lix.systems/lix-project/lix/issues/157 Fixes: https://git.lix.systems/lix-project/lix/issues/221 Previously the entire escaped store URI was included. This would cause build failures if a very long or deeply nested path was being used in the store. Now, we use the first 48 characters of the URL (escaped), then 16 bytes of hash of the entire URL. This should never collide and limits the length of the file name to a bit over 64, which is fine. Change-Id: Ic1ba690a94e83749567c2c29460b8d1bcf2ac413 --- src/build-remote/build-remote.cc | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/build-remote/build-remote.cc b/src/build-remote/build-remote.cc index f7a159829..fb90403a0 100644 --- a/src/build-remote/build-remote.cc +++ b/src/build-remote/build-remote.cc @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #if __APPLE__ @@ -20,9 +21,9 @@ #include "local-store.hh" #include "legacy.hh" #include "experimental-features.hh" +#include "hash.hh" using namespace nix; -using std::cin; static void handleAlarm(int sig) { } @@ -35,9 +36,19 @@ std::string escapeUri(std::string uri) static std::string currentLoad; +static std::string makeLockFilename(const std::string & storeUri) { + // We include 48 bytes of escaped URI to give an idea of what the lock + // is on, then 16 bytes of hash to disambiguate. + // This avoids issues with the escaped URI being very long and causing + // path too long errors, while also avoiding any possibility of collision + // caused by simple truncation. + auto hash = hashString(HashType::htSHA256, storeUri).to_string(Base::Base32, false); + return escapeUri(storeUri).substr(0, 48) + "-" + hash.substr(0, 16); +} + static AutoCloseFD openSlotLock(const Machine & m, uint64_t slot) { - return openLockFile(fmt("%s/%s-%d", currentLoad, escapeUri(m.storeUri), slot), true); + return openLockFile(fmt("%s/%s-%d", currentLoad, makeLockFilename(m.storeUri), slot), true); } static bool allSupportedLocally(Store & store, const std::set& requiredFeatures) { @@ -263,7 +274,9 @@ connected: auto inputs = readStrings(source); auto wantedOutputs = readStrings(source); - AutoCloseFD uploadLock = openLockFile(currentLoad + "/" + escapeUri(storeUri) + ".upload-lock", true); + auto lockFileName = currentLoad + "/" + makeLockFilename(storeUri) + ".upload-lock"; + + AutoCloseFD uploadLock = openLockFile(lockFileName, true); { Activity act(*logger, lvlTalkative, actUnknown, fmt("waiting for the upload lock to '%s'", storeUri)); -- cgit v1.2.3