Skip to content

Play your Steam library's Windows games on a jailbroken PS5.

C++

Delete a folder with its contents from the Files page

Leonardo Lobatoa503557f148c
diff --git a/src/web/file_store.cpp b/src/web/file_store.cpp
index 1e87e30..7db2b6e 100644
--- a/src/web/file_store.cpp
+++ b/src/web/file_store.cpp
@@ -14,6 +14,7 @@
#include <cstring>
#include <dirent.h>
#include <fcntl.h>
+#include <set>
#include <sys/stat.h>
#include <thread>
#include <unistd.h>
@@ -30,6 +31,8 @@ constexpr std::uint64_t kSpaceMargin = 64ull * 1024 * 1024;
constexpr std::size_t kMaxZipEntries = 65535;
constexpr std::uint64_t kMaxZipBytes = 0x7fffffffu;
constexpr std::size_t kMaxZipDepth = 32;
+constexpr std::size_t kMaxTreeFolderEntries = 65536; // more stays in the folder until the next delete
+constexpr std::size_t kTreeBatch = 64; // files removed under one hold of the locks
const FileError kOk{};
FileError Busy() { return {503, "Playden is busy. Try again in a moment."}; }
@@ -54,7 +57,7 @@ struct RawEntry {
};
// Every regular file and folder a folder lists, links left out. Path operations: hold the lock.
-bool ReadFolder(const std::string& path, std::vector<RawEntry>* out, std::size_t limit, bool* truncated) {
+bool ReadFolder(const std::string& path, std::vector<RawEntry>* out, std::size_t limit, bool* truncated, std::uint64_t* others = nullptr) {
DIR* directory = ::opendir(path.c_str());
if (!directory) return false;
while (true) {
@@ -68,7 +71,10 @@ bool ReadFolder(const std::string& path, std::vector<RawEntry>* out, std::size_t
if (name == "." || name == "..") continue;
struct stat info{};
if (::lstat((path + "/" + name).c_str(), &info) != 0) continue;
- if (!S_ISREG(info.st_mode) && !S_ISDIR(info.st_mode)) continue;
+ if (!S_ISREG(info.st_mode) && !S_ISDIR(info.st_mode)) {
+ if (others) ++*others;
+ continue;
+ }
out->push_back({name, info});
}
::closedir(directory);
@@ -614,6 +620,126 @@ FileError FileStore::Delete(const FileRoot& root, const std::string& path) {
return kOk;
}
+FileError FileStore::DeleteTree(const FileRoot& root, const std::string& path, TreeSummary* out) { return WalkTree(root, path, true, out); }
+
+FileError FileStore::SummarizeTree(const FileRoot& root, const std::string& path, TreeSummary* out) { return WalkTree(root, path, false, out); }
+
+// Iterative, because worker stacks are small and a folder's depth is the player's: a folder is read and its files go,
+// then its folders are walked, then it goes itself if it is empty by then.
+FileError FileStore::WalkTree(const FileRoot& root, const std::string& path, bool remove, TreeSummary* out) {
+ *out = {};
+ const auto top = SplitRelativePath(path);
+ if (!top || top->empty() || PrivatePath(*top, root.private_paths)) return NotFound();
+ if (ReadOnlyPath(*top)) return {403, "Playden writes this folder itself, so it can't be changed here."};
+ const auto locks = [&](std::unique_lock<std::mutex>* commit) {
+ if (remove) *commit = std::unique_lock<std::mutex>(commit_mutex_);
+ return Hold();
+ };
+ struct Frame {
+ std::vector<std::string> parts;
+ bool read = false;
+ };
+ std::vector<Frame> stack;
+ stack.push_back({*top});
+ while (!stack.empty()) {
+ const auto current = stack.back().parts;
+ if (stack.back().read) {
+ stack.pop_back();
+ if (!remove) {
+ ++out->folders;
+ continue;
+ }
+ std::unique_lock<std::mutex> commit;
+ const auto held = locks(&commit);
+ if (!held) return Busy();
+ const std::vector<std::string> above(current.begin(), current.end() - 1);
+ std::optional<ManagedDirectory> parent;
+ if (auto error = Locate(root, above, true, false, &parent); !error.ok()) {
+ if (error.status == 404) continue;
+ return error;
+ }
+ if (!Step("tree-folder") || !parent->RemoveEmptyDirectory(current.back())) {
+ const int error = errno;
+ if (error == ENOENT || error == ENOTEMPTY || error == EEXIST) continue;
+ if (error == EACCES || error == EPERM) { ++out->left; continue; }
+ return FromErrno(error, "The folder can't be deleted");
+ }
+ if (!parent->Sync()) return FromErrno(errno, "The change can't be saved");
+ ++out->folders;
+ continue;
+ }
+ stack.back().read = true;
+ std::vector<RawEntry> raw;
+ bool truncated = false;
+ {
+ std::unique_lock<std::mutex> commit;
+ const auto held = locks(&commit);
+ if (!held) return Busy();
+ std::optional<ManagedDirectory> folder;
+ if (auto error = Locate(root, current, remove, false, &folder); !error.ok()) {
+ if (current.size() == top->size()) return error;
+ // A folder below that can't be opened, or a path too long to reach, stays with what's in it. The rest goes on.
+ if (error.status != 404) ++out->left;
+ stack.pop_back();
+ continue;
+ }
+ if (remove) Sweep(*folder);
+ if (!ReadFolder(folder->path().string(), &raw, kMaxTreeFolderEntries, &truncated, &out->left)) {
+ if (current.size() == top->size()) return FromErrno(errno, "This folder can't be listed");
+ ++out->left;
+ stack.pop_back();
+ continue;
+ }
+ }
+ if (truncated) ++out->left;
+ const std::string prefix = kPreviousPrefix;
+ std::set<std::string> beside;
+ for (const auto& entry : raw)
+ if (S_ISREG(entry.info.st_mode)) beside.insert(LowerAscii(entry.name));
+ std::vector<std::string> doomed;
+ auto child = current;
+ child.emplace_back();
+ for (const auto& entry : raw) {
+ const bool previous = S_ISREG(entry.info.st_mode) && entry.name.rfind(prefix, 0) == 0 && entry.name.size() > prefix.size();
+ const auto shown = previous ? entry.name.substr(prefix.size()) : entry.name;
+ child.back() = shown;
+ if (!UsableName(shown) || HiddenName(shown) || PrivatePath(child, root.private_paths) || HardLinked(entry.info)) {
+ ++out->left;
+ continue;
+ }
+ if (S_ISDIR(entry.info.st_mode)) {
+ if (ReadOnlyFolderName(shown)) ++out->left;
+ else stack.push_back({child});
+ continue;
+ }
+ out->bytes += static_cast<std::uint64_t>(entry.info.st_size);
+ // A previous version counts once, with its file; alone, the page lists it as a file.
+ if (!previous || !beside.count(LowerAscii(shown))) ++out->files;
+ doomed.push_back(entry.name);
+ }
+ if (!remove) continue;
+ for (std::size_t done = 0; done < doomed.size();) {
+ std::unique_lock<std::mutex> commit;
+ const auto held = locks(&commit);
+ if (!held) return Busy();
+ std::optional<ManagedDirectory> folder;
+ if (auto error = Locate(root, current, true, false, &folder); !error.ok()) {
+ if (error.status == 404) break;
+ return error;
+ }
+ for (const auto end = std::min(doomed.size(), done + kTreeBatch); done < end; ++done) {
+ if (Step("tree-file") && folder->RemoveRegularFile(doomed[done])) continue;
+ const int error = errno;
+ if (error == ENOENT) continue;
+ if (error == EACCES || error == EPERM) { ++out->left; continue; }
+ return FromErrno(error, "The file can't be deleted");
+ }
+ if (!folder->Sync()) return FromErrno(errno, "The change can't be saved");
+ }
+ }
+ return kOk;
+}
+
// Steps: file -> temporary; previous -> file; sync; temporary -> previous; sync. A crash leaves the previous version
// listed as Recoverable, or the swap done with a temporary the sweep removes when it is old.
FileError FileStore::Restore(const FileRoot& root, const std::string& path) {
diff --git a/src/web/file_store.h b/src/web/file_store.h
index 0abed9b..257493b 100644
--- a/src/web/file_store.h
+++ b/src/web/file_store.h
@@ -70,6 +70,14 @@ struct OpenedFile {
std::string name;
};
+// What a walk over a folder found, or what its delete did.
+struct TreeSummary {
+ std::uint64_t files = 0; // not counting a previous version beside its file
+ std::uint64_t folders = 0; // the folder itself too
+ std::uint64_t bytes = 0;
+ std::uint64_t left = 0; // what a delete leaves: see DeleteTree
+};
+
// A file read and hashed for the editor.
struct FileText {
std::string text; // as UTF-8, with any byte order mark left out
@@ -141,6 +149,13 @@ class FileStore {
FileError MakeFolder(const FileRoot& root, const std::string& path, bool parents = false);
// A file (with its previous version) or an empty folder.
FileError Delete(const FileRoot& root, const std::string& path);
+ // A folder and everything in it that the page can show. What stays, with the folders that hold it: hidden and secret
+ // names, read-only and private folders, links, files with another hard link, names the page can't show, temporary
+ // files still in use. A file or folder the console won't let this user remove stays too. Locks one batch at a time,
+ // so a long delete keeps nothing else waiting for long. Stopped by an error, a second run goes on with what is left.
+ FileError DeleteTree(const FileRoot& root, const std::string& path, TreeSummary* out);
+ // What DeleteTree would do, changing nothing.
+ FileError SummarizeTree(const FileRoot& root, const std::string& path, TreeSummary* out);
// Swaps a file with its previous version, or brings back a previous version with no file.
FileError Restore(const FileRoot& root, const std::string& path);
// A folder as a zip, its entries named from the folder's own name down. Before any byte goes out, a folder too
@@ -162,6 +177,7 @@ class FileStore {
std::optional<platform::ManagedDirectory>* out);
std::optional<platform::DirectoryPolicy> WritePolicy(const FileRoot& root);
bool Step(const std::string& step);
+ FileError WalkTree(const FileRoot& root, const std::string& path, bool remove, TreeSummary* out);
std::int64_t Now() const;
std::optional<std::string> RandomText();
void Sweep(const platform::ManagedDirectory& folder);
diff --git a/src/web/files_api.cpp b/src/web/files_api.cpp
index f4fca07..3902c0a 100644
--- a/src/web/files_api.cpp
+++ b/src/web/files_api.cpp
@@ -422,7 +422,37 @@ void FilesApi::Register(Router& router) {
if (!error.ok()) return Fail(response, error);
SendJson(response, 200, "{\"ok\":true}");
});
- router.Post("/api/files/delete", action(&FileStore::Delete));
+ const auto tree_json = [](const TreeSummary& tree) {
+ return "{" + Number("files", tree.files) + "," + Number("folders", tree.folders) + "," + Number("bytes", tree.bytes) + "," + Number("left", tree.left) + "}";
+ };
+ router.Post("/api/files/delete", [this, tree_json](const Request& request, const std::string&, Response& response) {
+ if (!RequirePaired(request, response) || !RequireAction(request, response)) return;
+ FileGame game;
+ FileRoot root;
+ if (!Find(request, response, &game, &root)) return;
+ const auto path = QueryValue(request.query, "path").value_or(std::string());
+ if (QueryValue(request.query, "recursive") == std::string("1")) {
+ Transfer transfer(transfers_);
+ if (!transfer.held()) return Fail(response, {503, "Other transfers are running. Try again in a moment."});
+ TreeSummary tree;
+ const auto error = store_.DeleteTree(root, path, &tree);
+ if (!error.ok()) return Fail(response, error);
+ return SendJson(response, 200, tree_json(tree));
+ }
+ const auto error = store_.Delete(root, path);
+ if (!error.ok()) return Fail(response, error);
+ SendJson(response, 200, "{\"ok\":true}");
+ });
+ router.Exact("/api/files/tree", [this, tree_json](const Request& request, const std::string&, Response& response) {
+ if (!RequirePaired(request, response)) return;
+ FileGame game;
+ FileRoot root;
+ if (!Find(request, response, &game, &root)) return;
+ TreeSummary tree;
+ const auto error = store_.SummarizeTree(root, QueryValue(request.query, "path").value_or(std::string()), &tree);
+ if (!error.ok()) return Fail(response, error);
+ SendJson(response, 200, tree_json(tree));
+ });
router.Post("/api/files/restore", action(&FileStore::Restore));
}
diff --git a/src/web/page/index.html b/src/web/page/index.html
index 02c8d99..07f70ca 100644
--- a/src/web/page/index.html
+++ b/src/web/page/index.html
@@ -787,21 +787,49 @@ main.dragging { outline: 2px dashed var(--accent); outline-offset: 8px; }
close = dialog("New folder", name, element("div", { class: "foot" }, button("Cancel", () => close()), button("Make folder", make, "primary")));
name.focus();
};
- const askDelete = (names) => {
+ const askDelete = async (names) => {
// Fixed now: the player can move to another folder or game while these go.
- const batch = { game: files.game.id, root: files.root, paths: names.map(relPath) };
+ const batch = { game: files.game.id, root: files.root };
+ const entries = names.map((name) => ({ name, path: relPath(name), entry: files.listing.entries.find((entry) => entry.name === name) }));
+ const inGameFolder = files.root === "game";
+ const folders = entries.filter((item) => item.entry && item.entry.directory);
+ const total = { files: entries.length - folders.length, folders: 0, bytes: entries.reduce((sum, item) => sum + (item.entry && !item.entry.directory ? item.entry.size : 0), 0), left: 0 };
+ try {
+ for (const { path } of folders) {
+ const tree = await api(`/api/files/tree?${q({ ...batch, path })}`);
+ total.files += tree.files;
+ total.folders += tree.folders;
+ total.bytes += tree.bytes;
+ total.left += tree.left;
+ }
+ } catch (error) { return report(error); }
let close;
const remove = async () => {
close();
- for (const path of batch.paths) {
- try { await act(`/api/files/delete?${q({ game: batch.game, root: batch.root, path })}`); }
- catch (error) { report(error); break; }
+ close = dialog("Deleting…", element("p", { text: "Keep this tab open until it finishes." }));
+ let left = 0;
+ for (const { path, entry } of entries) {
+ try {
+ const answer = await act(`/api/files/delete?${q({ ...batch, path, ...(entry && entry.directory ? { recursive: 1 } : {}) })}`);
+ left += answer.left || 0;
+ } catch (error) { report(error); break; }
}
+ close();
files.selected = new Set();
+ if (left) fail(`${left} ${left === 1 ? "item stays" : "items stay"}, because Playden protects ${left === 1 ? "it" : "them"} or can't remove ${left === 1 ? "it" : "them"} from here.`);
+ if (left) $("retry").hidden = true;
refreshFolder();
};
- close = dialog(`Delete ${names.length} ${names.length === 1 ? "item" : "items"}?`,
- element("p", { text: "They are deleted from the PS5 with any previous versions, and can't be restored from here. Only empty folders can be deleted." }),
+ const count = (number, word) => `${number} ${word}${number === 1 ? "" : "s"}`;
+ const title = folders.length
+ ? `Delete ${count(total.files, "file")}${total.bytes ? ` (${longSize(total.bytes)})` : ""} in ${count(total.folders, "folder")}?`
+ : `Delete ${count(names.length, "item")}?`;
+ close = dialog(title,
+ element("p", { text: "They are deleted from the PS5 with any previous versions, and can't be restored from here." }),
+ total.left ? element("p", { text: total.left === 1
+ ? "1 item will stay, because Playden protects it or can't remove it from here. The folders that hold it stay too."
+ : `${total.left} items will stay, because Playden protects them or can't remove them from here. The folders that hold them stay too.` }) : "",
+ folders.length && inGameFolder ? element("p", { text: "If these are the game's own files, the game may stop working until you install it again." }) : "",
element("ul", {}, ...names.map((name) => element("li", { text: name }))),
element("div", { class: "foot" }, button("Keep them", () => close()), button("Delete", remove, "primary")));
};
diff --git a/tests/test_web_files_smoke.py b/tests/test_web_files_smoke.py
index e3873c9..886647b 100644
--- a/tests/test_web_files_smoke.py
+++ b/tests/test_web_files_smoke.py
@@ -132,6 +132,11 @@ def run(port, server):
tree = ok(call("GET", "/api/files/list", path="Mods/Big/textures", **wolverine_place))
assert [entry["name"] for entry in tree["entries"]] == ["a.dds"] and "previous" in tree["entries"][0]
assert call("POST", "/api/files/mkdir", path="Mods/Big/textures/a.dds", parents=1, **wolverine_place)[0] == 409
+ # And a mod that was a mistake goes in one request.
+ counted = ok(call("GET", "/api/files/tree", path="Mods", **wolverine_place))
+ assert (counted["files"], counted["folders"], counted["left"]) == (1, 3, 0)
+ assert ok(call("POST", "/api/files/delete", path="Mods", recursive=1, **wolverine_place))["files"] == 1
+ assert call("GET", "/api/files/list", path="Mods", **wolverine_place)[0] == 404
ok(call("POST", "/api/files/upload", body=b"msi", path="_Redist/PhysX_9.09.msi", **wolverine_place))
wolverine = ok(call("GET", "/api/files/game", game="local-3fa9c1", redist=1))
assert next(folder for folder in wolverine["redist"] if folder["path"] == "_Redist")["physx_installer"] == "_Redist/PhysX_9.09.msi"
diff --git a/tests/web_file_store_test.cpp b/tests/web_file_store_test.cpp
index fd6c28f..147963f 100644
--- a/tests/web_file_store_test.cpp
+++ b/tests/web_file_store_test.cpp
@@ -593,6 +593,102 @@ int main() {
CHECK(store.Delete(root, "made").ok() && !fs::exists(folder / "made"));
CHECK(store.Delete(root, "made").status == 404);
+ // A folder with what's in it: gone, except what Playden protects and the folders that hold it.
+ {
+ const auto tree = folder / "tree";
+ Put(tree / "a.txt", "aaa");
+ Put(tree / ".playden-bak.a.txt", "bb");
+ Put(tree / ".playden-bak.lost.txt", "c");
+ Put(tree / "sub/deep/d.txt", "dddd");
+ fs::create_directories(tree / "sub/empty");
+ TreeSummary summary;
+ CHECK(store.SummarizeTree(root, "tree", &summary).ok());
+ CHECK(summary.files == 3 && summary.folders == 4 && summary.bytes == 10 && summary.left == 0 && fs::exists(tree / "a.txt"));
+ TreeSummary done;
+ CHECK(store.DeleteTree(root, "tree", &done).ok() && !fs::exists(tree));
+ CHECK(done.files == summary.files && done.folders == summary.folders && done.bytes == summary.bytes && done.left == 0);
+ CHECK(store.DeleteTree(root, "tree", &done).status == 404);
+
+ // Each protected kind keeps its parents, and the rest goes around it.
+ Put(tree / "go.txt", "x");
+ Put(tree / "secret/playden_online.txt", "token");
+ Put(tree / "secret/go.txt", "x");
+ Put(tree / "hidden/.playden-install.json", "{}");
+ Put(tree / "settings/steam_settings/configs.user.ini", "x");
+ Put(tree / "hiddendir/.playden-x/f", "x");
+ Put(tree / "linked/one", "x");
+ fs::create_hard_link(tree / "linked/one", base / "elsewhere-hard");
+ Put(tree / "kept-going/deep/deeper/go.txt", "x");
+ Put(base / "outside/target.txt", "outside");
+ fs::create_directories(tree / "withlink");
+ fs::create_symlink(base / "outside", tree / "withlink/out");
+ fs::create_symlink(base / "outside/target.txt", tree / "withlink/out.txt");
+ Put(tree / "busy/.playden-upload-new", "part");
+ CHECK(store.SummarizeTree(root, "tree", &summary).ok() && summary.left == 8 && fs::exists(tree / "go.txt"));
+ CHECK(store.DeleteTree(root, "tree", &done).ok());
+ CHECK(done.left == 8 && done.files == summary.files);
+ CHECK(!fs::exists(tree / "go.txt") && !fs::exists(tree / "secret/go.txt") && !fs::exists(tree / "kept-going"));
+ CHECK(Text(tree / "secret/playden_online.txt") == "token" && fs::exists(tree / "hidden/.playden-install.json"));
+ CHECK(fs::exists(tree / "settings/steam_settings/configs.user.ini") && fs::exists(tree / "hiddendir/.playden-x/f"));
+ CHECK(fs::exists(tree / "linked/one") && fs::exists(tree / "busy/.playden-upload-new"));
+ CHECK(fs::is_symlink(tree / "withlink/out") && fs::is_symlink(tree / "withlink/out.txt") && Text(base / "outside/target.txt") == "outside");
+ fs::remove_all(tree);
+ fs::remove_all(base / "outside");
+ fs::remove(base / "elsewhere-hard");
+
+ // A folder below that can't be opened stays, with its parents, and everything around it still goes.
+ Put(tree / "go.txt", "x");
+ Put(tree / "open/go.txt", "x");
+ Put(tree / "parent/locked/inside.txt", "kept");
+ Put(tree / "parent/go.txt", "x");
+ fs::permissions(tree / "parent/locked", fs::perms::none);
+ CHECK(store.SummarizeTree(root, "tree", &summary).ok() && summary.left == 1 && summary.files == 3);
+ CHECK(store.DeleteTree(root, "tree", &done).ok() && done.left == 1 && done.files == 3);
+ CHECK(!fs::exists(tree / "go.txt") && !fs::exists(tree / "open") && !fs::exists(tree / "parent/go.txt"));
+ fs::permissions(tree / "parent/locked", fs::perms::owner_all);
+ CHECK(Text(tree / "parent/locked/inside.txt") == "kept");
+ fs::remove_all(tree);
+
+ // Never the top of a root, a read-only or hidden folder, a path out of the root, or a link.
+ CHECK(store.DeleteTree(root, "", &done).status == 404 && store.SummarizeTree(root, "", &done).status == 404);
+ CHECK(store.DeleteTree(root, "../x", &done).status == 404 && store.DeleteTree(root, ".playden-x", &done).status == 404);
+ Put(folder / "steam_settings/k.ini", "x");
+ CHECK(store.DeleteTree(root, "steam_settings", &done).status == 403 && store.DeleteTree(root, "steam_settings/in", &done).status == 403);
+ CHECK(fs::exists(folder / "steam_settings/k.ini"));
+ Put(base / "outside/target.txt", "outside");
+ fs::create_symlink(base / "outside", folder / "linktop");
+ CHECK(!store.DeleteTree(root, "linktop", &done).ok() && Text(base / "outside/target.txt") == "outside");
+ fs::remove(folder / "linktop");
+ fs::remove_all(base / "outside");
+
+ // Deep enough to break anything recursive, and more files in a folder than one batch.
+ std::string deep = "tree";
+ for (int level = 0; level < 40; ++level) deep += "/d";
+ Put(folder / deep / "f.txt", "x");
+ for (int index = 0; index < 200; ++index) Put(tree / ("many" + std::to_string(index)), "x");
+ CHECK(store.DeleteTree(root, "tree", &done).ok() && !fs::exists(tree) && done.files == 201 && done.folders == 41);
+
+ // Stopped part way by a disk error or a busy lock: nothing is lost by running it again.
+ int allowed = 3;
+ auto faulty_options = options;
+ faulty_options.fault = [&](const std::string& step) { return step != "tree-file" || allowed-- > 0; };
+ FileStore faulty(faulty_options);
+ for (int index = 0; index < 10; ++index) Put(tree / ("f" + std::to_string(index)), "x");
+ CHECK(faulty.DeleteTree(root, "tree", &done).status == 500 && fs::exists(tree));
+ allowed = 1000;
+ CHECK(faulty.DeleteTree(root, "tree", &done).ok() && !fs::exists(tree));
+ auto busy_options = options;
+ busy_options.hold_tries = 1;
+ busy_options.hold_retry_ms = 1;
+ std::atomic<int> holds{4};
+ busy_options.try_hold = [&]() -> std::shared_ptr<void> { return holds-- > 0 ? std::make_shared<int>(0) : nullptr; };
+ FileStore busy(busy_options);
+ for (int index = 0; index < 3; ++index) Put(tree / ("sub" + std::to_string(index)) / "f", "x");
+ CHECK(busy.DeleteTree(root, "tree", &done).status == 503);
+ holds = 1000;
+ CHECK(busy.DeleteTree(root, "tree", &done).ok() && !fs::exists(tree));
+ }
+
// A root not made yet: empty to read, made by the first write, from an existing folder only.
{
const auto bottle = base / "bottle";
diff --git a/tests/web_files_api_test.cpp b/tests/web_files_api_test.cpp
index dc342a9..b31c8e6 100644
--- a/tests/web_files_api_test.cpp
+++ b/tests/web_files_api_test.cpp
@@ -250,6 +250,17 @@ int main() {
CHECK(Status(post("/api/files/mkdir?" + game + "game&path=_Redist2&parents=1", cookie)) == 200);
CHECK(Status(post("/api/files/mkdir?" + game + "game&path=tree/a/b&parents=1", cookie)) == 200 && fs::is_directory(game_folder / "tree/a/b"));
CHECK(Status(post("/api/files/mkdir?" + game + "game&path=game.exe/a&parents=1", cookie)) == 409);
+ // A folder with what's in it: counted first, then gone. Without the switch it still has to be empty.
+ Put(game_folder / "tree/a/b/f.txt", "12345");
+ CHECK(Status(post("/api/files/delete?" + game + "game&path=tree", cookie)) == 409 && fs::exists(game_folder / "tree/a/b/f.txt"));
+ {
+ const auto counted = get("/api/files/tree?" + game + "game&path=tree", cookie);
+ CHECK(counted.find("\"files\":1,\"folders\":3,\"bytes\":5,\"left\":0") != std::string::npos && fs::exists(game_folder / "tree/a/b/f.txt"));
+ CHECK(Status(get("/api/files/tree?" + game + "game&path=tree", "")) == 401);
+ CHECK(Status(post("/api/files/delete?" + game + "game&path=tree&recursive=1", cookie, "", "")) == 403 && fs::exists(game_folder / "tree"));
+ const auto gone = post("/api/files/delete?" + game + "game&path=tree&recursive=1", cookie);
+ CHECK(Status(gone) == 200 && gone.find("\"files\":1") != std::string::npos && !fs::exists(game_folder / "tree"));
+ }
CHECK(Status(post("/api/files/delete?" + game + "game&path=_Redist2", cookie)) == 200 && !fs::exists(game_folder / "_Redist2"));
CHECK(Status(post("/api/files/delete?" + game + "user&path=Documents/new.sav", cookie)) == 200 && !fs::exists(profile / "Documents/new.sav"));
// A POST from another origin never reaches these.