added file size to the json

This commit is contained in:
2026-11-10 17:09:10 +01:00
parent 5842138bf4
commit 6a54131766
+26 -1
View File
@@ -28,6 +28,7 @@ struct TreeNode {
int ctime;
int atime;
std::optional<std::vector<uint8_t>> content;
int size;
std::vector<TreeNode> children;
};
@@ -57,6 +58,7 @@ TreeNode parse(const std::filesystem::path& p, int& next_inode, int& parent_inod
node.mtime = static_cast<int64_t>(buf.st_mtime);
node.atime = static_cast<int64_t>(buf.st_atime);
node.ctime = static_cast<int64_t>(buf.st_ctime);
node.size = static_cast<int>(buf.st_size);
}
if(node.type == std::filesystem::file_type::directory) {
@@ -271,17 +273,40 @@ JsonValue to_json(const TreeNode& node) {
childrenArray.push_back(to_json(child));
}
obj["size"] = node.size;
obj["children"] = std::move(childrenArray);
return obj;
}
int main(int argc, char* argv[]) {
if(argc != 2) {
std::cout << argc;
if(argc < 2 || argc > 3) {
std::cerr << "Wrong number of arguments passed\n";
return 1;
}
const std::string flag = "-c";
if(argv[1] == flag) {
std::cout << "Compact mode\n";
if(!std::filesystem::exists(argv[2])) {
std::cerr << "The path doesnt exist\n";
return 1;
}
const std::filesystem::path rootPath = argv[2];
TreeNode root = parse_tree(rootPath);
JsonValue json = to_json(root);
std::string json_str = serialize(json, "");
std::cout << json_str << "\n";
return 0;
}
if(!std::filesystem::exists(argv[1])) {
std::cerr << "The path doesnt exist\n";
return 1;