From cc5c78374e320bc7807f04391b70e43deaa54c94 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Mon, 18 Jul 2022 14:19:44 +0200 Subject: [PATCH] feat(file-storage): Support server name in URL on Windows --- doc/MANUAL.adoc | 12 +++++++----- src/storage/secondary/FileStorage.cpp | 23 ++++++++++++++++------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/doc/MANUAL.adoc b/doc/MANUAL.adoc index fc0eb1c35..be147f68c 100644 --- a/doc/MANUAL.adoc +++ b/doc/MANUAL.adoc @@ -1074,15 +1074,16 @@ on cache hits and misses: <> option. - === File storage backend -URL format: `+file:DIRECTORY+` or `+file://DIRECTORY+` +URL format: `+file:DIRECTORY+` or `+file://[HOST]DIRECTORY+` This backend stores data as separate files in a directory structure below -*DIRECTORY* (an absolute path), similar (but not identical) to the primary cache -storage. A typical use case for this backend would be sharing a cache on an NFS -directory. +*DIRECTORY*, similar (but not identical) to the primary cache storage. A typical +use case for this backend would be sharing a cache on an NFS directory. +*DIRECTORY* must start with a slash. *HOST* can be the empty string or +localhost. On Windows, *HOST* can also be the name of a server hosting a shared +folder. IMPORTANT: ccache will not perform any cleanup of the storage -- that has to be done by other means, for instance by running `ccache --trim-dir` periodically. @@ -1091,6 +1092,7 @@ Examples: * `+file:/shared/nfs/directory+` * `+file:///shared/nfs/directory|umask=002|update-mtime=true+` +* `+file://example.com/shared/folder+` Optional attributes: diff --git a/src/storage/secondary/FileStorage.cpp b/src/storage/secondary/FileStorage.cpp index f1d550c34..8807ab407 100644 --- a/src/storage/secondary/FileStorage.cpp +++ b/src/storage/secondary/FileStorage.cpp @@ -55,7 +55,7 @@ public: private: enum class Layout { flat, subdirs }; - const std::string m_dir; + std::string m_dir; std::optional m_umask; bool m_update_mtime = false; Layout m_layout = Layout::subdirs; @@ -64,15 +64,24 @@ private: }; FileStorageBackend::FileStorageBackend(const Params& params) - : m_dir(params.url.path()) { ASSERT(params.url.scheme() == "file"); - if (!params.url.host().empty()) { - throw core::Fatal(FMT( - "invalid file path \"{}\": specifying a host (\"{}\") is not supported", - params.url.str(), - params.url.host())); + + const auto& host = params.url.host(); +#ifdef _WIN32 + m_dir = util::replace_all(params.url.path(), "/", "\\"); + if (!host.empty()) { + m_dir = FMT("\\\\{}\\{}", host, m_dir); + } +#else + if (!host.empty() && host != "localhost") { + throw core::Fatal( + FMT("invalid file URL \"{}\": specifying a host other than localhost is" + " not supported", + params.url.str())); } + m_dir = params.url.path(); +#endif for (const auto& attr : params.attributes) { if (attr.key == "layout") { -- 2.47.3