Advanced File System Indexer and Search Tool in C++
Build a highly efficient file system indexer that recursively scans directories, indexes file metadata, and supports advanced search queries with filters and sorting, all implemented in C++.
Challenge prompt
Create a C++ program that recursively scans a directory and all its subdirectories to index metadata of each file (including file name, path, size, last modified timestamp). Store this indexed data in an efficient in-memory data structure. Then implement a command-line interface that supports searching the indexed files based on multiple criteria such as partial file name match, size range, and modification date range. The search results should be sortable by name, size, or date. Optimize for performance by avoiding redundant scans and supporting fast queries. Additionally, implement a feature to export the current index snapshot to a JSON file and import it back to resume searching without re-scanning.
Guidance
- • Use recursive directory traversal with appropriate platform-specific or cross-platform APIs (e.g., <filesystem> in C++17).
- • Design an index data structure that allows efficient multi-criteria filtering and sorting, such as storing file metadata objects in vectors and using algorithms for queries.
- • Implement a flexible command parser to support user input for complex queries and sorting options.
- • Ensure your program can serialize and deserialize the index to/from JSON efficiently to avoid repeated filesystem scans.
Hints
- • Investigate std::filesystem for directory traversal and file metadata extraction.
- • Consider std::vector with custom filtering and sorting using std::ranges or algorithms for query processing.
- • Use a JSON serialization library like nlohmann/json for saving and loading the index state.
Starter code
#include <iostream>
#include <vector>
#include <string>
#include <filesystem>
#include <chrono>
struct FileInfo {
std::string name;
std::string path;
uintmax_t size;
std::filesystem::file_time_type last_modified;
};
class FileIndexer {
public:
void scanDirectory(const std::string& directoryPath);
std::vector<FileInfo> search(const std::string& nameFilter = "", uintmax_t minSize = 0, uintmax_t maxSize = UINTMAX_MAX);
void exportToJson(const std::string& jsonFilePath);
void importFromJson(const std::string& jsonFilePath);
private:
std::vector<FileInfo> index;
};
int main() {
// Implement CLI to scan directory, perform searches, interact with export/import
std::cout << "File Indexer CLI (implement your solution here)" << std::endl;
return 0;
}Expected output
Example run: > scan /home/user/docs Indexed 1024 files. > search name=report size>1000 sort=size desc Found 15 files matching criteria: 1) report_summary.pdf - 2345 bytes - 2024-05-20 2) annual_report.docx - 1230 bytes - 2024-05-18 ... > export index.json Index successfully saved to index.json > import index.json Index loaded from index.json with 1024 files.
Core concepts
Challenge a Friend
Send this duel to someone else and see if they can solve it.