1#ifndef INODE_MAPPER_HPP
2#define INODE_MAPPER_HPP
13#include <unordered_map>
17#define LIB_SYNC_NAME "lib.sync"
18#define LIB_BIN_NAME "lib.bin"
23 unordered_map<ino_t, string> inodeToPath;
29 if (useCacheFile ==
"true")
31 syncFile.open(syncFileName, std::ios::app);
35 syncFile.open(syncFileName);
38 if (!syncFile.is_open())
40 cerr <<
"Error: Could not open file " << syncFileName << endl;
47 if (syncFile.is_open())
53 void addMapping(ino_t inode,
const string& filePath,
bool writeSyncFile)
55 if (inodeToPath.find(inode) == inodeToPath.end())
57 inodeToPath[inode] = filePath;
58 if (writeSyncFile && syncFile.is_open())
60 syncFile << inode << endl;
61 syncFile << filePath << endl;
68 for (
const auto& pair : inodeToPath)
70 cout <<
"Inode: " << pair.first <<
" -> " << pair.second << endl;
77 DIR* dir = opendir(dirPath.c_str());
80 cerr <<
"Error: Could not open directory " << dirPath << endl;
85 while ((entry = readdir(dir)) !=
nullptr)
87 if (strcmp(entry->d_name,
".") == 0 || strcmp(entry->d_name,
"..") == 0)
92 string fullPath = dirPath +
"/" + entry->d_name;
94 if (stat(fullPath.c_str(), &fileStat) == 0)
96 rbt.
insert(fileStat.st_ino);
97 mapper.
addMapping(fileStat.st_ino, fullPath,
true);
101 cerr <<
"Warning: Could not stat file " << fullPath << endl;
111 int fd = open(cacheFilePath.c_str(), O_RDONLY);
114 throw std::runtime_error(
"Could not open cache file " + cacheFilePath);
119 if (fstat(fd, &sb) == -1)
122 throw std::runtime_error(
"Could not get file size");
127 static_cast<const char*
>(mmap(
nullptr, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0));
129 if (data == MAP_FAILED)
132 throw std::runtime_error(
"Could not memory map file");
135 const char* current = data;
136 const char* end = data + sb.st_size;
138 while (current < end)
141 const char* lineEnd =
static_cast<const char*
>(memchr(current,
'\n', end - current));
147 const char* inodeStart = current;
148 while (current < lineEnd && *current >=
'0' && *current <=
'9')
150 inode = inode * 10 + (*current -
'0');
155 current = lineEnd + 1;
160 lineEnd =
static_cast<const char*
>(memchr(current,
'\n', end - current));
165 std::string filePath(current, lineEnd - current);
167 if (inode > 0 && !filePath.empty())
170 mapper.
addMapping(inode, std::move(filePath),
false);
173 current = lineEnd + 1;
177 munmap(
const_cast<char*
>(data), sb.st_size);
Definition inode_mapper.hpp:21
void addMapping(ino_t inode, const string &filePath, bool writeSyncFile)
Definition inode_mapper.hpp:53
~InodeFileMapper()
Definition inode_mapper.hpp:45
InodeFileMapper(const string &syncFileName, string useCacheFile)
Definition inode_mapper.hpp:27
void printMappings() const
Definition inode_mapper.hpp:66
void insert(ino_t data)
Definition rbtree.hpp:178
void processDirectory(const string &dirPath, RedBlackTree &rbt, InodeFileMapper &mapper)
Definition inode_mapper.hpp:75
void processCacheFile(const std::string &cacheFilePath, RedBlackTree &rbt, InodeFileMapper &mapper)
Definition inode_mapper.hpp:108