Merge branch 'worktree-agent-ab5c5cafd8f6ff470'
This commit is contained in:
@@ -3,6 +3,8 @@
|
||||
#include "realtime/webdav/server/resource_tree.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace molo::realtime::webdav::server {
|
||||
|
||||
@@ -159,25 +161,40 @@ bool ResourceTree::copy(const std::string& src, const std::string& dst,
|
||||
if (s == entries_.end() || src == dst) {
|
||||
return false;
|
||||
}
|
||||
// RFC 4918 9.8.3/9.9.3: a destination equal to, or nested inside, the
|
||||
// source collection must be rejected before anything is mutated.
|
||||
// Allowing it would insert entries (dst + the relative suffix) that
|
||||
// this same pass would then have to copy again, forever (MOLO-DAV-001).
|
||||
std::string prefix = src + "/";
|
||||
if (dst.compare(0, prefix.size(), prefix) == 0) {
|
||||
return false;
|
||||
}
|
||||
if (entries_.count(dst) > 0 && !overwrite) {
|
||||
return false;
|
||||
}
|
||||
std::string prefix = src + "/";
|
||||
// Collect every (target path, resource) to write in a first pass over
|
||||
// entries_, then apply them in a second pass below. entries_ must never
|
||||
// be mutated while it is being range-iterated: inserting into a
|
||||
// std::map mid-iteration is visible to that same iteration, and an
|
||||
// insert landing ahead of the cursor would be visited again.
|
||||
std::vector<std::pair<std::string, DavResource>> pending;
|
||||
for (const auto& [k, v] : entries_) {
|
||||
if (k == src || k.compare(0, prefix.size(), prefix) == 0) {
|
||||
std::string rel = k.substr(src.size());
|
||||
std::string target = dst + rel;
|
||||
if (entries_.count(target) == 0) {
|
||||
created = true;
|
||||
} else if (!entries_[target].is_collection) {
|
||||
total_bytes_ -= entries_[target].body.size();
|
||||
}
|
||||
entries_[target] = v;
|
||||
if (!v.is_collection) {
|
||||
total_bytes_ += v.body.size();
|
||||
}
|
||||
pending.emplace_back(dst + rel, v);
|
||||
}
|
||||
}
|
||||
for (auto& [target, v] : pending) {
|
||||
if (entries_.count(target) == 0) {
|
||||
created = true;
|
||||
} else if (!entries_[target].is_collection) {
|
||||
total_bytes_ -= entries_[target].body.size();
|
||||
}
|
||||
if (!v.is_collection) {
|
||||
total_bytes_ += v.body.size();
|
||||
}
|
||||
entries_[target] = std::move(v);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user