2021-03-26 19:52:25 +00:00
|
|
|
/* Copyright 2021, Michele Santullo
|
|
|
|
* This file is part of remarkable_tool.
|
|
|
|
*
|
|
|
|
* remarkable_tool is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Remarkable_tool is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with remarkable_tool. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2021-03-25 15:12:46 +00:00
|
|
|
#include <iostream>
|
|
|
|
#include <simdjson.h>
|
|
|
|
#include <string_view>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <vector>
|
|
|
|
#include <unordered_map>
|
|
|
|
#include <cstdint>
|
2021-03-26 19:52:25 +00:00
|
|
|
#include <stdexcept>
|
|
|
|
#include <numeric>
|
|
|
|
#include <utility>
|
2021-03-25 15:12:46 +00:00
|
|
|
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
#define BASE_PATH "/run/media/duckz/Sabrent/deleme/remarkable_20210324/xochitl/"
|
|
|
|
//#define BASE_PATH "/home/duckz/dev/code/cpp/remarkable_tool/lib/lines-are-beautiful/share/rmlab/examples/"
|
|
|
|
|
|
|
|
struct NotebookInfo {
|
|
|
|
std::string visible_name;
|
|
|
|
std::string parent;
|
|
|
|
std::string type;
|
|
|
|
std::vector<std::string> files;
|
|
|
|
std::uint64_t version = 0;
|
|
|
|
std::uintmax_t total_size = 0;
|
|
|
|
bool deleted = false;
|
2021-03-26 19:53:20 +00:00
|
|
|
bool isolated = true;
|
2021-03-25 15:12:46 +00:00
|
|
|
};
|
|
|
|
|
2021-03-26 19:52:25 +00:00
|
|
|
struct Node {
|
|
|
|
Node() :
|
|
|
|
info(nullptr),
|
|
|
|
parent(nullptr)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
Node (std::string_view name, const NotebookInfo* payload, Node* parent) :
|
|
|
|
name(name),
|
|
|
|
info(payload),
|
|
|
|
parent(parent)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
std::vector<Node> children;
|
|
|
|
std::string_view name;
|
|
|
|
const NotebookInfo* info;
|
|
|
|
const Node* parent;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef std::unordered_map<std::string, NotebookInfo> NotebookMapType;
|
|
|
|
|
|
|
|
std::ostream& operator<< (std::ostream& str, const NotebookMapType::value_type& v) {
|
2021-03-25 15:12:46 +00:00
|
|
|
auto& key = v.first;
|
|
|
|
auto& val = v.second;
|
|
|
|
str << key << " (\"" << val.visible_name << "\", " << val.type <<
|
|
|
|
"): deleted=" << std::boolalpha << val.deleted <<
|
|
|
|
", parent=\"" << val.parent <<
|
|
|
|
"\", version=" << val.version <<
|
|
|
|
", files=";
|
|
|
|
|
|
|
|
std::string_view sep("");
|
|
|
|
for (const auto& f : val.files) {
|
|
|
|
str << sep << f;
|
|
|
|
sep = ";";
|
|
|
|
}
|
|
|
|
|
|
|
|
str << ", size=" << std::setprecision(2) <<
|
|
|
|
static_cast<double>(val.total_size) / (1024.0 * 1024.0) << "MiB";
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2021-03-26 19:52:25 +00:00
|
|
|
template <typename T>
|
|
|
|
std::vector<T>& operator+= (std::vector<T>& dst, const std::vector<T>& src) {
|
|
|
|
dst.insert(dst.end(), src.begin(), src.end());
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
NotebookMapType build_notebook_infos (
|
2021-03-25 15:12:46 +00:00
|
|
|
const fs::path& from_path
|
|
|
|
) {
|
|
|
|
simdjson::dom::parser parser;
|
2021-03-26 19:52:25 +00:00
|
|
|
NotebookMapType retval;
|
2021-03-25 15:12:46 +00:00
|
|
|
|
|
|
|
for (const auto& entry : fs::directory_iterator(from_path)) {
|
|
|
|
NotebookInfo& curr_info = retval[entry.path().stem()];
|
|
|
|
|
|
|
|
if (entry.path().extension() == ".metadata") {
|
|
|
|
simdjson::dom::object json_doc = parser.load(entry.path());
|
|
|
|
|
|
|
|
curr_info.deleted = json_doc["deleted"];
|
|
|
|
curr_info.parent = json_doc["parent"];
|
|
|
|
curr_info.type = json_doc["type"];
|
|
|
|
curr_info.version = json_doc["version"];
|
|
|
|
curr_info.visible_name = json_doc["visibleName"];
|
2021-03-26 19:53:20 +00:00
|
|
|
curr_info.isolated = false;
|
2021-03-25 15:12:46 +00:00
|
|
|
}
|
|
|
|
else if (fs::is_directory(entry)) {
|
|
|
|
for (const auto& subentry : fs::recursive_directory_iterator(entry)) {
|
|
|
|
curr_info.files.push_back(fs::relative(subentry, from_path));
|
|
|
|
if (fs::is_regular_file(subentry))
|
|
|
|
curr_info.total_size += fs::file_size(subentry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
curr_info.files.push_back(fs::relative(entry, from_path));
|
|
|
|
if (fs::is_regular_file(entry))
|
|
|
|
curr_info.total_size += fs::file_size(entry);
|
|
|
|
}
|
|
|
|
|
|
|
|
return retval;
|
|
|
|
}
|
2021-03-26 19:52:25 +00:00
|
|
|
|
|
|
|
std::vector<Node> to_tree (const NotebookMapType& nbs) {
|
|
|
|
std::unordered_map<std::string_view, std::pair<unsigned int, Node>> parents;
|
|
|
|
for (const auto& nb : nbs) {
|
|
|
|
auto it_ins = parents.emplace(nb.second.parent, std::make_pair(0U, Node{nb.second.parent, nullptr, nullptr}));
|
|
|
|
it_ins.first->second.first++;
|
|
|
|
}
|
|
|
|
for (const auto& nb : nbs) {
|
|
|
|
auto it_parent = parents.find(nb.first);
|
|
|
|
if (parents.cend() != it_parent) {
|
|
|
|
Node& node = it_parent->second.second;
|
|
|
|
node.info = &nb.second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (auto& par : parents) {
|
|
|
|
Node& node = par.second.second;
|
|
|
|
const unsigned int children_count = par.second.first;
|
|
|
|
node.children.reserve(children_count);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto& nb : nbs) {
|
|
|
|
if (0 == parents.count(nb.first)) {
|
|
|
|
Node& parent = parents[nb.second.parent].second;
|
|
|
|
parent.children.emplace_back(nb.first, &nb.second, &parent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<Node> roots;
|
|
|
|
std::unordered_map<std::string_view, Node*> grouped_nodes;
|
|
|
|
for (auto it = parents.begin(), it_end = parents.end(); it != it_end; it = parents.erase(it)) {
|
|
|
|
auto& node = it->second.second;
|
|
|
|
if (node.info) {
|
|
|
|
auto it_parent = parents.find(node.info->parent);
|
|
|
|
Node* dst_node = (parents.end() == it_parent ? grouped_nodes[node.info->parent] : &it_parent->second.second);
|
|
|
|
dst_node->children.push_back(std::move(node));
|
|
|
|
grouped_nodes[dst_node->children.back().name] = &dst_node->children.back();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
roots.push_back(std::move(node));
|
|
|
|
grouped_nodes[node.name] = &roots.back();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return roots;
|
|
|
|
}
|
2021-03-26 19:53:20 +00:00
|
|
|
|
2021-03-26 19:52:25 +00:00
|
|
|
void print_tree (const Node& tree, std::string_view desc={}, std::string indent={}) {
|
|
|
|
std::cout << indent << desc << " (\"" << tree.name << "\")";
|
2021-03-26 19:53:20 +00:00
|
|
|
if (tree.info && tree.info->isolated)
|
|
|
|
std::cout << " ISOLATED";
|
|
|
|
else if (tree.info and tree.info->deleted)
|
2021-03-26 19:52:25 +00:00
|
|
|
std::cout << " DELETED";
|
|
|
|
std::cout << '\n';
|
|
|
|
|
|
|
|
for (const auto& child : tree.children) {
|
|
|
|
print_tree(child, child.info->visible_name, indent + "\t");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::uintmax_t recursive_size (const Node& tree) {
|
|
|
|
std::uintmax_t retval = 0;
|
|
|
|
if (tree.info)
|
|
|
|
retval = tree.info->total_size;
|
|
|
|
|
|
|
|
for (const auto& child : tree.children) {
|
|
|
|
retval += recursive_size(child);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const NotebookInfo*> to_flat_notebook_list (const Node& tree) {
|
|
|
|
std::vector<const NotebookInfo*> retval;
|
|
|
|
for (const auto& child : tree.children) {
|
|
|
|
retval += to_flat_notebook_list(child);
|
|
|
|
}
|
|
|
|
if (tree.info)
|
|
|
|
retval.push_back(tree.info);
|
|
|
|
return retval;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const NotebookInfo*> make_dele_list (const Node& tree) {
|
2021-03-26 19:53:20 +00:00
|
|
|
if (tree.info and (tree.info->deleted or tree.info->isolated))
|
2021-03-26 19:52:25 +00:00
|
|
|
return to_flat_notebook_list(tree);
|
|
|
|
|
|
|
|
std::vector<const NotebookInfo*> retval;
|
|
|
|
for (const auto& child : tree.children) {
|
|
|
|
retval += make_dele_list(child);
|
|
|
|
}
|
|
|
|
return retval;
|
|
|
|
}
|
2021-03-25 15:12:46 +00:00
|
|
|
} //unnamed namespace
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
using std::string_view;
|
|
|
|
|
|
|
|
auto notebooks = build_notebook_infos(BASE_PATH);
|
|
|
|
|
2021-03-26 19:52:25 +00:00
|
|
|
//naive linear delete
|
|
|
|
//std::uintmax_t total = 0;
|
|
|
|
//std::uintmax_t freed = 0;
|
|
|
|
//std::set<std::string_view> dele_list;
|
|
|
|
//for (auto& nb : notebooks) {
|
|
|
|
// if (nb.second.deleted) {
|
|
|
|
// dele_list.insert(nb.first);
|
|
|
|
// for (const auto& file : nb.second.files) {
|
|
|
|
// std::cout << BASE_PATH << file << '\n';
|
|
|
|
// }
|
|
|
|
// freed += nb.second.total_size;
|
|
|
|
// }
|
|
|
|
// total += nb.second.total_size;
|
|
|
|
//}
|
|
|
|
|
|
|
|
//for (auto& nb : notebooks) {
|
|
|
|
// if (dele_list.count(nb.second.parent) and not nb.second.deleted) {
|
|
|
|
// std::cerr << "Warning: " << nb.second.parent << " deleted but it's the parent of " << nb.first << "!\n";
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
//
|
|
|
|
//std::cout << "Found " << notebooks.size() << " notebooks\n";
|
|
|
|
//std::cout << "Total size " << std::setprecision(2) <<
|
|
|
|
// static_cast<double>(total) / (1024.0 * 1024.0 * 1024.0) << "GiB\n";
|
|
|
|
//std::cout << "Would delete " << dele_list.size() << " notebooks\n";
|
|
|
|
//std::cout << "Would free " << std::setprecision(2) <<
|
|
|
|
// static_cast<double>(freed) / (1024.0 * 1024.0 * 1024.0) << "GiB\n";
|
|
|
|
|
|
|
|
std::vector<Node> roots = to_tree(notebooks);
|
|
|
|
//std::cout << "Found " << notebooks.size() << " notebooks\n";
|
|
|
|
//std::cout << "Collection has " << roots.size() << " roots\n";
|
|
|
|
//std::cout << " ---------\n";
|
|
|
|
//for (const auto& root : roots) {
|
|
|
|
// std::cout << "\tTree \"" << root.name << "\" has " << root.children.size() << " nodes\n\n";
|
|
|
|
//}
|
|
|
|
|
|
|
|
//std::uintmax_t total_mem1 = 0;
|
|
|
|
std::vector<const NotebookInfo*> dele_list;
|
|
|
|
for (const auto& root : roots) {
|
|
|
|
//std::string_view desc;
|
|
|
|
//if (root.name == "trash")
|
|
|
|
// desc = "rubbish bin";
|
|
|
|
//else if (root.name == "")
|
|
|
|
// desc = "root";
|
|
|
|
|
|
|
|
//print_tree(root, desc);
|
|
|
|
//total_mem1 += recursive_size(root);
|
|
|
|
dele_list += make_dele_list(root);
|
2021-03-25 15:12:46 +00:00
|
|
|
}
|
|
|
|
|
2021-03-26 19:52:25 +00:00
|
|
|
//std::cout << "Total size 1: " << total_mem1 << " bytes\n";
|
|
|
|
//const std::uintmax_t total_mem2 = std::accumulate(notebooks.cbegin(), notebooks.cend(), std::uintmax_t{}, [](std::uintmax_t a, const auto& b) {return a + b.second.total_size;});
|
|
|
|
//std::cout << "Total size 2: " << total_mem2 << " bytes\n";
|
|
|
|
|
|
|
|
for (const NotebookInfo* nb : dele_list) {
|
|
|
|
for (const auto& f : nb->files) {
|
|
|
|
std::cout << "/home/root/.local/share/remarkable/xochitl/" << f << '\n';
|
|
|
|
}
|
|
|
|
}
|
2021-03-25 15:12:46 +00:00
|
|
|
return 0;
|
|
|
|
}
|