added file size to the json
This commit is contained in:
@@ -28,6 +28,7 @@ struct TreeNode {
|
|||||||
int ctime;
|
int ctime;
|
||||||
int atime;
|
int atime;
|
||||||
std::optional<std::vector<uint8_t>> content;
|
std::optional<std::vector<uint8_t>> content;
|
||||||
|
int size;
|
||||||
std::vector<TreeNode> children;
|
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.mtime = static_cast<int64_t>(buf.st_mtime);
|
||||||
node.atime = static_cast<int64_t>(buf.st_atime);
|
node.atime = static_cast<int64_t>(buf.st_atime);
|
||||||
node.ctime = static_cast<int64_t>(buf.st_ctime);
|
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) {
|
if(node.type == std::filesystem::file_type::directory) {
|
||||||
@@ -271,17 +273,40 @@ JsonValue to_json(const TreeNode& node) {
|
|||||||
childrenArray.push_back(to_json(child));
|
childrenArray.push_back(to_json(child));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
obj["size"] = node.size;
|
||||||
obj["children"] = std::move(childrenArray);
|
obj["children"] = std::move(childrenArray);
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
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";
|
std::cerr << "Wrong number of arguments passed\n";
|
||||||
return 1;
|
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])) {
|
if(!std::filesystem::exists(argv[1])) {
|
||||||
std::cerr << "The path doesnt exist\n";
|
std::cerr << "The path doesnt exist\n";
|
||||||
return 1;
|
return 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user