Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OrphanedObjects: detect object vs. multipart vs. unexpected file #2

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 75 additions & 6 deletions src/checks/orphaned_objects.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <filesystem>
#include <iostream>
#include <stack>
#include <regex>

OrphanedObjectsFix::OrphanedObjectsFix(
std::filesystem::path root, std::filesystem::path path
Expand All @@ -31,6 +32,7 @@ OrphanedObjectsFix::OrphanedObjectsFix(
}

void OrphanedObjectsFix::fix() {
// TODO: print a message of what was actually done when the fix was applied!
if (!std::filesystem::exists(
root_path / "lost+found" / obj_path.parent_path()
)) {
Expand Down Expand Up @@ -61,6 +63,21 @@ std::string OrphanedObjectsFix::to_string() const {
return "orphaned object: " + oid + " at " + obj_path.string();
}

UnexpectedFileFix::UnexpectedFileFix(
std::filesystem::path root, std::filesystem::path path
) {
root_path = root;
obj_path = path;
}

void UnexpectedFileFix::fix() {
// TODO: do we really want to move unexpected files to lost+found?
}

std::string UnexpectedFileFix::to_string() const {
return "Found unexpected mystery file: " + obj_path.string();
}

OrphanedObjectsCheck::OrphanedObjectsCheck(std::filesystem::path path) {
root_path = path;
metadata = std::make_unique<Database>(root_path / "s3gw.db");
Expand Down Expand Up @@ -91,15 +108,67 @@ int OrphanedObjectsCheck::check() {
if (std::filesystem::is_directory(entry.path())) {
stack.push(entry.path());
} else {
std::string filename(entry.path().filename());
std::filesystem::path rel =
std::filesystem::relative(cwd / entry.path(), root_path);
std::filesystem::path uuid_path =
std::filesystem::relative(cwd, root_path);
std::string uuid = uuid_path.string();
boost::erase_all(uuid, "/");
if (metadata->count_in_table("objects", "uuid=\"" + uuid + "\"") == 0) {
std::smatch match;
if (std::regex_match(filename, match, std::regex("^[0-9]+$"))) {
// It's a versioned object (their names are just integers)
// TODO: verify each version isn't orphaned
std::filesystem::path uuid_path =
std::filesystem::relative(cwd, root_path);
std::string uuid = uuid_path.string();
boost::erase_all(uuid, "/");
if (metadata->count_in_table("objects", "uuid=\"" + uuid + "\"") == 0) {
fixes.emplace_back(
std::make_shared<OrphanedObjectsFix>(root_path, rel.string())
);
orphan_count++;
}
} else if (std::regex_match(filename, match,
std::regex("^(([[:xdigit:]]{4}-){4}[[:xdigit:]]{12})-([0-9]+)$"))) {
// It's a multipart part. Their names are "$uuid_tail-$part_number",
// e.g.: "6c78-0c22-4c51-9a2b-4284724edd64-1"
// match[1] == uuid tail
// match[3] == multipart part number
std::filesystem::path uuid_base =
std::filesystem::relative(cwd, root_path);
std::string uuid = uuid_base.string() + std::string(match[1]);
boost::erase_all(uuid, "/");
std::string part_num = std::string(match[3]);

std::string query =
"SELECT COUNT(part_num) FROM multiparts_parts, multiparts "
"WHERE multiparts_parts.upload_id = multiparts.upload_id AND "
" part_num = " + part_num + " AND "
" object_uuid = '" + uuid + "'";
sqlite3_stmt* stm;
if (metadata->prepare(query, &stm) == SQLITE_OK) {
if (sqlite3_step(stm) == SQLITE_ROW && sqlite3_column_count(stm) > 0) {
int count = sqlite3_column_int(stm, 0);
if (count == 0) {
fixes.emplace_back(
// TODO: Consider making an OrphanedMultipartFix class.
// OrphanedOjectsFix works fine, but the messaging might
// be slightly misleading ("orphaned object: uuid-n ..."
// vs. what would be "orhpaned multipart part: ...")
std::make_shared<OrphanedObjectsFix>(root_path, rel.string())
);
orphan_count++;
}
} else {
std::cout << "This can't happen" << std::endl;
// TODO: You sure about that bro?
}
sqlite3_finalize(stm);
} else {
std::cout << "This shouldn't happen" << std::endl;
// TODO: What? Seriously? Do better with the error handling.
}
} else {
// This is something else
fixes.emplace_back(
std::make_shared<OrphanedObjectsFix>(root_path, rel.string())
std::make_shared<UnexpectedFileFix>(root_path, rel.string())
);
orphan_count++;
}
Expand Down
13 changes: 13 additions & 0 deletions src/checks/orphaned_objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,19 @@ class OrphanedObjectsFix : public Fix {
void fix();
};

class UnexpectedFileFix : public Fix {
private:
std::filesystem::path root_path;
std::filesystem::path obj_path; // relative to root_path

std::string to_string() const;

public:
UnexpectedFileFix(std::filesystem::path, std::filesystem::path);
operator std::string() const { return to_string(); };
void fix();
};

class OrphanedObjectsCheck : public Check {
private:
std::filesystem::path root_path;
Expand Down