inLimbo
TUI Music Player that keeps you in Limbo.
 
Loading...
Searching...
No Matches
inode_mapper.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "rbtree.hpp"
4#include <cstring>
5#include <dirent.h>
6#include <fcntl.h>
7#include <fstream>
8#include <iostream>
9#include <sys/mman.h>
10#include <sys/stat.h>
11#include <unistd.h>
12#include <unordered_map>
13
14using namespace std;
15
16#define LIB_BIN_NAME "lib.bin"
17
19{
20private:
21 unordered_map<ino_t, string> inodeToPath;
22 ofstream syncFile;
23
24public:
25 void addMapping(ino_t inode, const string& filePath, bool writeSyncFile)
26 {
27 if (inodeToPath.find(inode) == inodeToPath.end())
28 {
29 inodeToPath[inode] = filePath;
30 if (writeSyncFile && syncFile.is_open())
31 {
32 syncFile << inode << endl;
33 syncFile << filePath << endl;
34 }
35 }
36 }
37
38 void printMappings() const
39 {
40 for (const auto& pair : inodeToPath)
41 {
42 cout << "Inode: " << pair.first << " -> " << pair.second << endl;
43 }
44 }
45};
46
47void processDirectory(const string& dirPath, RedBlackTree& rbt, InodeFileMapper& mapper)
48{
49 DIR* dir = opendir(dirPath.c_str());
50 if (!dir)
51 {
52 cerr << "Error: Could not open directory " << dirPath << endl;
53 exit(EXIT_FAILURE);
54 }
55
56 struct dirent* entry;
57 while ((entry = readdir(dir)) != nullptr)
58 {
59 if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0)
60 {
61 continue;
62 }
63
64 string fullPath = dirPath + "/" + entry->d_name;
65 struct stat fileStat;
66 if (stat(fullPath.c_str(), &fileStat) == 0)
67 {
68 rbt.insert(fileStat.st_ino);
69 mapper.addMapping(fileStat.st_ino, fullPath, true);
70 }
71 else
72 {
73 cerr << "Warning: Could not stat file " << fullPath << endl;
74 }
75 }
76
77 closedir(dir);
78}
Definition inode_mapper.hpp:19
void addMapping(ino_t inode, const string &filePath, bool writeSyncFile)
Definition inode_mapper.hpp:25
void printMappings() const
Definition inode_mapper.hpp:38
Definition rbtree.hpp:34
void insert(ino_t data)
Definition rbtree.hpp:174
void processDirectory(const string &dirPath, RedBlackTree &rbt, InodeFileMapper &mapper)
Definition inode_mapper.hpp:47