Skip to content

Commit

Permalink
Add collision number to same file names
Browse files Browse the repository at this point in the history
now if two files have the same name, the 2nd file is changed to "originalFileName(1)"
  • Loading branch information
juuz0 committed Apr 29, 2024
1 parent 717afc0 commit 93ab559
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/zimfuse/node.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct Node {
Node* parent;
std::string fullPath;
std::string originalPath;
int collisionCount = 0;
std::vector<Node*> children;

void addChild(Node* child)
Expand Down
11 changes: 8 additions & 3 deletions src/zimfuse/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,18 @@ std::string sanitize(const std::string& name)
return name;
}

std::pair<Node*, bool> Tree::attachFile(const std::string& name, Node* parent)
std::pair<Node*, bool> Tree::attachFile(const std::string& name, Node* parent, int collisionCount = 0)
{
auto sanitizedName = sanitize(name);
if (collisionCount)
sanitizedName += '(' + std::to_string(collisionCount) + ')';
auto fullPath = parent->fullPath + '/' + sanitizedName;
auto originalPath = parent->originalPath + '/' + name;
if (mappedNodes.count(fullPath))
return {mappedNodes[fullPath].get(), false};
if (mappedNodes.count(fullPath)) {
auto node = mappedNodes[fullPath].get();
node->collisionCount++;
return attachFile(name, parent, node->collisionCount);
}

auto n = Node::Ptr(new Node{.name = sanitizedName,
.isDir = false,
Expand Down
2 changes: 1 addition & 1 deletion src/zimfuse/tree.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Tree
public:
Tree(const std::string &path);
std::pair<Node*, bool> attachNode(const std::string& path, Node* parent);
std::pair<Node*, bool> attachFile(const std::string& path, Node* parent);
std::pair<Node*, bool> attachFile(const std::string& path, Node* parent, int collisionCount);
Node* findNode(const std::string& name);
zim::Archive* getArchive() { return &zimArchive; }
~Tree();
Expand Down

0 comments on commit 93ab559

Please sign in to comment.