30 std::lock_guard<std::mutex> lock(mutex_);
33 std::string normalizedPath = normalizePath(filePath);
36 auto it = pathToId_.find(normalizedPath);
37 if (it != pathToId_.end()) {
40 files_[
id - 1].refCount++;
46 record.path = std::move(normalizedPath);
50 FileID newId = files_.size() + 1;
53 files_.push_back(std::move(record));
54 pathToId_[files_.back().path] = newId;
60 std::lock_guard<std::mutex> lock(mutex_);
63 std::string normalizedPath = normalizePath(filePath);
65 auto it = pathToId_.find(normalizedPath);
66 if (it != pathToId_.end()) {
74 std::lock_guard<std::mutex> lock(mutex_);
77 static const std::string empty;
81 return files_[
id - 1].path;
85 std::lock_guard<std::mutex> lock(mutex_);
91 return files_[
id - 1].refCount;
95 std::lock_guard<std::mutex> lock(mutex_);
104std::string FileManager::normalizePath(std::string_view filePath)
const {
107 std::filesystem::path fsPath(filePath);
111 std::filesystem::path canonicalPath;
113 if (std::filesystem::exists(fsPath)) {
114 canonicalPath = std::filesystem::canonical(fsPath);
117 canonicalPath = std::filesystem::absolute(fsPath);
119 canonicalPath = canonicalPath.lexically_normal();
122 return canonicalPath.string();
123 }
catch (
const std::filesystem::filesystem_error &e) {
126 std::filesystem::path fsPath(filePath);
127 return std::filesystem::absolute(fsPath).lexically_normal().string();
130 return std::string(filePath);
Centralized file registry with path-to-FileID mapping.
std::size_t FileID
Type-safe identifier for registered files. 0 is reserved for invalid.
static constexpr FileID InvalidFileID
Reserved invalid FileID.
FileID registerFile(std::string_view filePath)
Register a file and return its FileID.
std::size_t getRefCount(FileID id) const
Get reference count for a file.
std::string_view getFilePath(FileID id) const
Get the canonical path for a FileID.
std::size_t getRegisteredFileCount() const
Get total number of registered files.
bool isValidFileId(FileID id) const
Check if a FileID is valid.
std::optional< FileID > tryGetFileId(std::string_view filePath) const
Look up FileID for a path without registering.