diff --git a/src/library_index.cpp b/src/library_index.cpp index bea9008..f15db9b 100644 --- a/src/library_index.cpp +++ b/src/library_index.cpp @@ -31,19 +31,34 @@ using File = LibraryIndex::File; namespace { -// library_index.db's layout (PRAGMA user_version); a file with another is made anew. -constexpr int kVersion = 1; +// The index is a disposable path cache, separate from playback history. +constexpr int kVersion = 2; constexpr const char* kSchema = R"( CREATE TABLE roots (path TEXT PRIMARY KEY, looked_at INTEGER NOT NULL); -CREATE TABLE folders (path TEXT PRIMARY KEY); +CREATE TABLE folders (path TEXT PRIMARY KEY, mtime INTEGER NOT NULL DEFAULT 0, + mtime_ns INTEGER NOT NULL DEFAULT 0, ctime INTEGER NOT NULL DEFAULT 0, + ctime_ns INTEGER NOT NULL DEFAULT 0, file_id INTEGER NOT NULL DEFAULT 0, + checked_at INTEGER NOT NULL DEFAULT 0); CREATE TABLE files (path TEXT PRIMARY KEY, size INTEGER NOT NULL, mtime INTEGER NOT NULL, added_at INTEGER NOT NULL, found_at INTEGER NOT NULL); CREATE TABLE todo (path TEXT NOT NULL); CREATE TABLE meta (key TEXT PRIMARY KEY, value INTEGER NOT NULL); +CREATE TABLE failed_roots (path TEXT PRIMARY KEY); +)"; +constexpr const char* kUpgrade = R"( +ALTER TABLE folders ADD COLUMN mtime INTEGER NOT NULL DEFAULT 0; +ALTER TABLE folders ADD COLUMN mtime_ns INTEGER NOT NULL DEFAULT 0; +ALTER TABLE folders ADD COLUMN ctime INTEGER NOT NULL DEFAULT 0; +ALTER TABLE folders ADD COLUMN ctime_ns INTEGER NOT NULL DEFAULT 0; +ALTER TABLE folders ADD COLUMN file_id INTEGER NOT NULL DEFAULT 0; +ALTER TABLE folders ADD COLUMN checked_at INTEGER NOT NULL DEFAULT 0; +CREATE TABLE failed_roots (path TEXT PRIMARY KEY); )"; // During a look, what it found is saved and shown every so often. constexpr auto kHandOver = std::chrono::seconds(2); +constexpr size_t kOutstanding = 32; +constexpr int64_t kVerifyAfter = 24 * 60 * 60; int64_t now() { return static_cast(std::time(nullptr)); } @@ -67,16 +82,29 @@ bool skipped(const std::string& name, bool folder) { return name.empty() || name == "." || name == ".." || (folder ? binOrLost(name) : name[0] == '.'); } +void readEntries(const std::string& dir, const std::vector& entries, + LibraryIndex::Listing& out) { + for (const auto& e : entries) { + if (skipped(e.name, e.folder)) continue; + if (e.folder) { + if (!e.link) out.folders.push_back({dir + "/" + e.name, e.stamp}); + } else if (isVideoFile(e.name)) { + out.videos.push_back({dir + "/" + e.name, {e.size, e.modified}}); + } + } +} + // Stash's generated folder, wherever its settings put it: Stash makes // screenshots/ (scene previews), vtt/, markers/, transcodes/, // interactive_heatmaps/ and download_stage/ in it at every start. A folder // with screenshots/ and two more of them is taken for one. -bool stashGenerated(const std::vector& folders) { +bool stashGenerated(const std::vector& folders) { static const char* const kMore[] = {"vtt", "markers", "transcodes", "interactive_heatmaps", "download_stage"}; bool screenshots = false; int more = 0; - for (const std::string& path : folders) { + for (const auto& folder : folders) { + const std::string& path = folder.path; const size_t slash = path.rfind('/'); const char* name = path.c_str() + (slash == std::string::npos ? 0 : slash + 1); if (std::strcmp(name, "screenshots") == 0) @@ -97,14 +125,7 @@ remote::Status LibraryIndex::list(const std::string& dir, Listing& out, if (remote::isRemote(dir)) { std::vector entries; const remote::Status status = remote::list(dir, entries); - for (const remote::Entry& e : entries) { - if (skipped(e.name, e.folder)) continue; - if (e.folder) { - if (!e.link) out.folders.push_back(dir + "/" + e.name); - } else if (isVideoFile(e.name)) { - out.videos.push_back({dir + "/" + e.name, {e.size, e.modified}}); - } - } + if (status == remote::Status::Ok) readEntries(dir, entries, out); return status; } DIR* d = opendir(dir.c_str()); @@ -123,7 +144,7 @@ remote::Status LibraryIndex::list(const std::string& dir, Listing& out, if (type == DT_UNKNOWN) type = ::lstat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode) ? DT_DIR : DT_REG; if (type == DT_DIR) { - if (!skipped(e->d_name, true)) out.folders.push_back(path); + if (!skipped(e->d_name, true)) out.folders.push_back({path, {}}); } else if ((type == DT_REG || type == DT_LNK) && !skipped(e->d_name, false) && isVideoFile(e->d_name) && ::stat(path.c_str(), &st) == 0 && S_ISREG(st.st_mode)) out.videos.push_back({path, {static_cast(st.st_size), st.st_mtime}}); @@ -150,17 +171,33 @@ struct LibraryIndex::Shared { struct Folder { std::unordered_set folders, files; // listed subfolders; videos + remote::DirectoryStamp stamp; + int64_t checkedAt = 0; + uint64_t revision = 0; + }; + struct Visit { + std::string path; + remote::DirectoryStamp stamp; + int64_t beganAt = 0; + uint64_t revision = 0; + }; + struct Pending { + Visit visit; + std::future result; }; sqlite3* db = nullptr; // none: kept in memory only (another instance has the file) std::map roots; // the pins (none inside another): last fully looked at std::unordered_map folders; // those listed std::unordered_map files; - std::vector todo; // folders this look has still to list, the next last + std::vector todo; // folders this look has still to list, the next last + std::vector pending; // also saved as todo until their results are applied std::unordered_set failed; // pins this look could not list std::shared_ptr stash; std::unordered_set generated; // Stash's generated folders this start found int64_t done = 0, total = -1; // folders this look listed, of those known before it (-1: not) bool pinsKnown = false; + bool fullLook = true; + int64_t verifiedAt = 0; std::unordered_set toMain, dirtyFiles, dirtyFolders, dirtyRoots; bool dirtyTodo = false; @@ -169,8 +206,16 @@ struct LibraryIndex::Shared { void handOver(); void save(); void setPins(std::vector pins); - void startLook(const std::vector& from); + void startLook(const std::vector& from, bool full = false); void lookNext(); + void received(const Visit& visit, remote::Status status, Listing& listing); + bool skip(const Listing::Directory& dir) const; + bool looking() const { return !todo.empty() || !pending.empty(); } + bool ready() const { + return std::any_of(pending.begin(), pending.end(), [](const Pending& p) { + return p.result.wait_for(std::chrono::seconds(0)) == std::future_status::ready; + }); + } void apply(const std::string& dir, const Listing& listing); void feed(const std::string& dir, const std::vector& entries); void dropFolder(const std::string& dir); @@ -209,10 +254,13 @@ void LibraryIndex::Shared::run() { std::vector> catalogs; { std::unique_lock lock(mutex); - wake.wait(lock, [this] { + auto work = [this] { return quit || pinsChanged || again || !listings.empty() || !deleted.empty() || - !stashes.empty() || (!todo.empty() && !videoOpen); - }); + !stashes.empty() || + (!todo.empty() && !videoOpen && pending.size() < kOutstanding) || ready(); + }; + if (pending.empty()) wake.wait(lock, work); + else wake.wait_for(lock, std::chrono::milliseconds(5), work); if (quit) break; pinsNew = std::exchange(pinsChanged, false); lookAgain = std::exchange(again, false); @@ -227,20 +275,19 @@ void LibraryIndex::Shared::run() { dropStashCopies(); } if (pinsNew) setPins(std::move(newPins)); - if (lookAgain && todo.empty()) { + if (lookAgain && !looking()) { std::vector all; for (const auto& root : roots) all.push_back(root.first); - startLook(all); + startLook(all, true); // explicit refresh always verifies every directory } for (const auto& [dir, entries] : listed) feed(dir, entries); for (const std::string& path : gone) { if (const auto folder = folders.find(parentOf(path)); folder != folders.end()) - folder->second.files.erase(path); + if (folder->second.files.erase(path)) ++folder->second.revision; if (files.erase(path)) touch(path); } - const bool open = videoIsOpen(); - if (!todo.empty() && !open) lookNext(); - if (pinsNew || lookAgain || todo.empty() || videoIsOpen() || + if (looking()) lookNext(); + if (pinsNew || lookAgain || !looking() || videoIsOpen() || std::chrono::steady_clock::now() - last >= kHandOver) { handOver(); last = std::chrono::steady_clock::now(); @@ -279,6 +326,16 @@ void LibraryIndex::Shared::load() { db = nullptr; return; } + if (version() == 1) { + if (!exec(db, "BEGIN") || !exec(db, kUpgrade) || + !exec(db, "PRAGMA user_version = 2") || !exec(db, "COMMIT")) { + exec(db, "ROLLBACK"); + std::fprintf(stderr, "Library index: cannot upgrade its cache; kept in memory only\n"); + sqlite3_close(db); + db = nullptr; + return; + } + } if (version() != kVersion) { // Another layout, or not a database: made anew (it is only a cache). sqlite3_close(db); @@ -306,7 +363,14 @@ void LibraryIndex::Shared::load() { }; rows("SELECT path, looked_at FROM roots", [this](std::string path, sqlite3_stmt* st) { roots[path] = sqlite3_column_int64(st, 1); }); - rows("SELECT path FROM folders", [this](std::string path, sqlite3_stmt*) { folders[path]; }); + rows("SELECT path, mtime, mtime_ns, ctime, ctime_ns, file_id, checked_at FROM folders", + [this](std::string path, sqlite3_stmt* st) { + Folder& folder = folders[path]; + folder.stamp = {sqlite3_column_int64(st, 1), sqlite3_column_int64(st, 2), + sqlite3_column_int64(st, 3), sqlite3_column_int64(st, 4), + static_cast(sqlite3_column_int64(st, 5))}; + folder.checkedAt = sqlite3_column_int64(st, 6); + }); for (auto& [path, folder] : folders) if (!roots.count(path)) if (const auto parent = folders.find(parentOf(path)); parent != folders.end()) @@ -322,16 +386,28 @@ void LibraryIndex::Shared::load() { toMain.insert(path); }); rows("SELECT path FROM todo ORDER BY rowid", - [this](std::string path, sqlite3_stmt*) { todo.push_back(std::move(path)); }); + [this](std::string path, sqlite3_stmt*) { todo.push_back({std::move(path), {}}); }); + for (const Visit& visit : todo) { + if (folders.try_emplace(visit.path).second) dirtyFolders.insert(visit.path); + if (roots.count(visit.path)) continue; // "/" is its own parent + if (const auto parent = folders.find(parentOf(visit.path)); parent != folders.end()) + parent->second.folders.insert(visit.path); + } rows("SELECT key, value FROM meta", [this](std::string key, sqlite3_stmt* st) { - (key == "done" ? done : total) = sqlite3_column_int64(st, 1); + const int64_t value = sqlite3_column_int64(st, 1); + if (key == "done") done = value; + else if (key == "total") total = value; + else if (key == "full") fullLook = value != 0; + else if (key == "verified_at") verifiedAt = value; }); + rows("SELECT path FROM failed_roots", + [this](std::string path, sqlite3_stmt*) { failed.insert(std::move(path)); }); } // What changed goes to the main thread and into the file. void LibraryIndex::Shared::handOver() { State s; - s.looking = !todo.empty(); + s.looking = looking(); bool all = !roots.empty(); int64_t oldest = 0; for (const auto& [path, lookedAt] : roots) { @@ -401,10 +477,17 @@ void LibraryIndex::Shared::save() { step(st); } }); - run("INSERT OR IGNORE INTO folders VALUES (?1)", [&](sqlite3_stmt* st) { + run("INSERT OR REPLACE INTO folders VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7)", [&](sqlite3_stmt* st) { for (const std::string& path : dirtyFolders) - if (folders.count(path)) { + if (const auto it = folders.find(path); it != folders.end()) { text(st, 1, path); + const Folder& f = it->second; + sqlite3_bind_int64(st, 2, f.stamp.modified); + sqlite3_bind_int64(st, 3, f.stamp.modifiedNs); + sqlite3_bind_int64(st, 4, f.stamp.changed); + sqlite3_bind_int64(st, 5, f.stamp.changedNs); + sqlite3_bind_int64(st, 6, static_cast(f.stamp.id)); + sqlite3_bind_int64(st, 7, f.checkedAt); step(st); } }); @@ -433,8 +516,13 @@ void LibraryIndex::Shared::save() { if (dirtyTodo) { ok = ok && exec(db, "DELETE FROM todo"); run("INSERT INTO todo VALUES (?1)", [&](sqlite3_stmt* st) { - for (const std::string& path : todo) { - text(st, 1, path); + // In-flight requests are not durable results. Revisit them after a restart. + for (const Pending& p : pending) { + text(st, 1, p.visit.path); + step(st); + } + for (const Visit& visit : todo) { + text(st, 1, visit.path); step(st); } }); @@ -443,12 +531,25 @@ void LibraryIndex::Shared::save() { sqlite3_bind_int64(st, 2, total); step(st); }); + run("INSERT OR REPLACE INTO meta VALUES ('full', ?1), ('verified_at', ?2)", [&](sqlite3_stmt* st) { + sqlite3_bind_int64(st, 1, fullLook); + sqlite3_bind_int64(st, 2, verifiedAt); + step(st); + }); + ok = ok && exec(db, "DELETE FROM failed_roots"); + run("INSERT INTO failed_roots VALUES (?1)", [&](sqlite3_stmt* st) { + for (const std::string& path : failed) { + text(st, 1, path); + step(st); + } + }); } ok = ok && exec(db, "COMMIT"); if (!ok) { // Only a cache: the next look puts it right. std::fprintf(stderr, "Library index: not saved (%s)\n", sqlite3_errmsg(db)); exec(db, "ROLLBACK"); + return; // retry the same dirty paths at the next handover } dirtyFiles.clear(); dirtyFolders.clear(); @@ -484,12 +585,12 @@ void LibraryIndex::Shared::setPins(std::vector pins) { if (!covered(folder.first)) left.push_back(folder.first); for (const std::string& dir : left) dropFolder(dir); const size_t before = todo.size(); - todo.erase(std::remove_if(todo.begin(), todo.end(), [&](const std::string& p) { return !covered(p); }), + todo.erase(std::remove_if(todo.begin(), todo.end(), [&](const Visit& p) { return !covered(p.path); }), todo.end()); dirtyTodo = dirtyTodo || todo.size() != before; } // Once per start: the look an earlier start did not finish goes on, else a new one. - if (!pinsKnown && todo.empty()) { + if (!pinsKnown && !looking()) { added.clear(); for (const auto& root : roots) added.push_back(root.first); } @@ -497,46 +598,122 @@ void LibraryIndex::Shared::setPins(std::vector pins) { startLook(added); } -void LibraryIndex::Shared::startLook(const std::vector& from) { +void LibraryIndex::Shared::startLook(const std::vector& from, bool full) { if (from.empty()) return; - if (todo.empty()) { + if (!looking()) { done = 0; total = static_cast(folders.size()); failed.clear(); + const int64_t t = now(); + fullLook = full || verifiedAt <= 0 || t < verifiedAt || t - verifiedAt >= kVerifyAfter; } for (const std::string& root : from) if (roots.at(root) == 0) total = -1; // never looked through: how many is not known - todo.insert(todo.end(), from.begin(), from.end()); + for (const std::string& path : from) todo.push_back({path, {}}); dirtyTodo = true; } void LibraryIndex::Shared::lookNext() { - const std::string dir = todo.back(); - todo.pop_back(); - dirtyTodo = true; - ++done; - Listing listing; - const remote::Status status = list(dir, listing); - const bool root = roots.count(dir) > 0; - if (status == remote::Status::Ok) { - sift(dir, listing); - apply(dir, listing); - todo.insert(todo.end(), listing.folders.begin(), listing.folders.end()); - } else if (status == remote::Status::Missing && !root) { - dropFolder(dir); - } else if (root) { - failed.insert(dir); // a share that doesn't answer, a drive taken out: kept as it was + // Apply every completed result, not just the oldest request. A slow folder + // must not hold up the rest of discovery or the connection's playback jobs. + for (size_t i = 0; i < pending.size();) { + if (pending[i].result.wait_for(std::chrono::seconds(0)) != std::future_status::ready) { + ++i; + continue; + } + auto result = pending[i].result.get(); + Visit visit = std::move(pending[i].visit); + pending.erase(pending.begin() + static_cast(i)); + Listing listing; + if (result.status == remote::Status::Ok) readEntries(visit.path, result.entries, listing); + received(visit, result.status, listing); + } + // Only submitting is paused. Results already in flight can finish and be + // saved without waiting on, or submitting work from, the render loop. + size_t local = 0; + while (!todo.empty() && pending.size() < kOutstanding && local < kOutstanding && !videoIsOpen()) { + Visit visit = std::move(todo.back()); + todo.pop_back(); + dirtyTodo = true; + if (!rootOf(visit.path)) continue; + if (!roots.count(visit.path) && !folders.count(visit.path)) continue; + visit.beganAt = now(); + if (const auto f = folders.find(visit.path); f != folders.end()) + visit.revision = f->second.revision; + if (visit.path.compare(0, 6, "smb://") == 0) { + auto future = remote::listAsync(visit.path); + pending.push_back({std::move(visit), std::move(future)}); + } else { + Listing listing; + const auto status = list(visit.path, listing); + received(visit, status, listing); + ++local; + // SFTP's backend lists synchronously. Queueing a window of those + // requests would put browsing behind the entire scan window. + if (remote::isRemote(visit.path)) break; + } } - if (!todo.empty()) return; + if (looking()) return; const int64_t t = now(); for (auto& [path, lookedAt] : roots) if (!failed.count(path)) { lookedAt = t; dirtyRoots.insert(path); } + if (fullLook && failed.empty()) verifiedAt = t; failed.clear(); done = 0; total = -1; + dirtyTodo = true; +} + +bool LibraryIndex::Shared::skip(const Listing::Directory& dir) const { + if (fullLook || !dir.stamp.usable()) return false; + const auto it = folders.find(dir.path); + if (it == folders.end()) return false; + const Folder& f = it->second; + const int64_t t = now(); + // Never skip branches: an unchanged ancestor says nothing about changes + // deeper down. Always list videos too, since in-place edits need not change + // their directory. Only a verified, unchanged, video-free leaf qualifies. + return f.files.empty() && f.folders.empty() && f.stamp == dir.stamp && + f.checkedAt > 0 && t >= f.checkedAt && t - f.checkedAt < kVerifyAfter && + f.stamp.modified < f.checkedAt - 2 && f.stamp.changed < f.checkedAt - 2; +} + +void LibraryIndex::Shared::received(const Visit& visit, remote::Status status, + Listing& listing) { + const std::string& dir = visit.path; + const auto owner = rootOf(dir); + if (!owner) return; // the pin was removed while its request was in flight + if (!roots.count(dir) && !folders.count(dir)) return; // its parent removed it + if (const auto f = folders.find(dir); + f != folders.end() && f->second.revision != visit.revision) { + todo.push_back({dir, {}}); // a newer browser listing/deletion won the race + dirtyTodo = true; + return; + } + ++done; + dirtyTodo = true; + const bool root = roots.count(dir) > 0; + if (status == remote::Status::Ok) { + sift(dir, listing); + apply(dir, listing); + const auto it = folders.find(dir); + if (it == folders.end()) return; // inside Stash's generated folder + Folder& folder = it->second; + folder.stamp = visit.stamp; + folder.checkedAt = visit.beganAt; + dirtyFolders.insert(dir); + for (const auto& child : listing.folders) { + if (skip(child)) ++done; + else todo.push_back({child.path, child.stamp}); + } + } else if (status == remote::Status::Missing && !root) { + dropFolder(dir); + } else { + failed.insert(owner->first); // partial/offline/denied trees are never marked up to date + } } // A folder's listing: its new videos recorded, those gone and its subfolders @@ -585,11 +762,18 @@ void LibraryIndex::Shared::apply(const std::string& dir, const Listing& listing) touch(*it); it = folder.files.erase(it); } - const std::unordered_set there(listing.folders.begin(), listing.folders.end()); + std::unordered_set there; + for (const auto& child : listing.folders) there.insert(child.path); std::vector gone; for (const std::string& sub : folder.folders) if (!there.count(sub)) gone.push_back(sub); for (const std::string& sub : gone) dropFolder(sub); + // Remember children even if their own listing later fails. Otherwise an + // unreadable child could make its parent look like a verified empty leaf. + for (const auto& child : listing.folders) { + folder.folders.insert(child.path); + if (folders.try_emplace(child.path).second) dirtyFolders.insert(child.path); + } } void LibraryIndex::Shared::feed(const std::string& dir, @@ -599,12 +783,20 @@ void LibraryIndex::Shared::feed(const std::string& dir, for (const FolderListing::Entry& e : entries) { if (skipped(e.name, e.directory)) continue; if (e.directory) - listing.folders.push_back(e.path); + listing.folders.push_back({e.path, {}}); else if (isVideoFile(e.name)) listing.videos.push_back({e.path, {e.size, e.modified}}); } sift(dir, listing); apply(dir, listing); + const auto it = folders.find(dir); + if (it == folders.end()) return; // inside Stash's generated folder + // Browser folder dates may be synthetic; only a scan's metadata can + // authorize skipping its own future enumeration. + it->second.stamp = {}; + it->second.checkedAt = 0; + ++it->second.revision; + dirtyFolders.insert(dir); } // Stash names a scene's preview (screenshots/.mp4) and transcode @@ -651,7 +843,7 @@ void LibraryIndex::Shared::sift(const std::string& dir, Listing& listing) { for (const std::string& sub : inside) dropFolder(sub); const size_t before = todo.size(); todo.erase(std::remove_if(todo.begin(), todo.end(), - [&](const std::string& p) { return p != dir && under(p, dir); }), + [&](const Visit& v) { return v.path != dir && under(v.path, dir); }), todo.end()); dirtyTodo = dirtyTodo || todo.size() != before; } diff --git a/src/library_index.h b/src/library_index.h index b4598d3..53cf091 100644 --- a/src/library_index.h +++ b/src/library_index.h @@ -46,7 +46,11 @@ class LibraryIndex { // May block on the network: workers only. Also first-run setup's count, // which may `stop` it between entries (cut short: Offline). struct Listing { - std::vector folders; + struct Directory { + std::string path; + remote::DirectoryStamp stamp; + }; + std::vector folders; std::vector> videos; }; static remote::Status list(const std::string& dir, Listing& out, diff --git a/src/remote.cpp b/src/remote.cpp index 18dfc26..f5bb810 100644 --- a/src/remote.cpp +++ b/src/remote.cpp @@ -273,6 +273,22 @@ Status list(const std::string& folder, std::vector& entries) { return c->call([&] { return c->backend().list(folder, entries); }); } +std::future listAsync(const std::string& folder) { + auto promise = std::make_shared>(); + auto future = promise->get_future(); + if (!isRemote(folder)) { + promise->set_value({Status::Missing, {}}); + return future; + } + auto c = connection(root(folder)); + c->post([c, folder, promise] { + c->backend().listAsync(folder, [promise](Listing result) { + promise->set_value(std::move(result)); + }); + }); + return future; +} + Status stat(const std::string& locator, Entry& entry) { if (!isRemote(locator)) return Status::Missing; auto c = connection(root(locator)); diff --git a/src/remote.h b/src/remote.h index d534da0..7cb2495 100644 --- a/src/remote.h +++ b/src/remote.h @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -61,16 +62,39 @@ enum class Status { KeyRefused, }; +// Directory identity and change times from a listing. Unknown/coarse metadata +// must not authorize skipping a directory. +struct DirectoryStamp { + int64_t modified = 0, modifiedNs = 0, changed = 0, changedNs = 0; + uint64_t id = 0; + bool usable() const { + return modified > 0 && changed > 0 && id != 0 && (modifiedNs != 0 || changedNs != 0); + } + bool operator==(const DirectoryStamp& b) const { + return modified == b.modified && modifiedNs == b.modifiedNs && changed == b.changed && + changedNs == b.changedNs && id == b.id; + } +}; + struct Entry { std::string name; bool folder = false; uint64_t size = 0; int64_t modified = 0; // seconds since 1970 bool link = false; // SFTP: a link to it, as what it points to + DirectoryStamp stamp; // SMB directories; unknown on other backends +}; +struct Listing { + Status status = Status::Offline; + std::vector entries; }; // smb://host: its shares (as folders; hidden "$" shares left out). // Any other locator: that folder's entries. Status list(const std::string& folder, std::vector& entries); +// A background listing on the same connection. Its future never blocks when +// destroyed; disconnects complete it with a failure, never a partial success. +// Callers bound their outstanding requests and stop submitting during playback. +std::future listAsync(const std::string& folder); Status stat(const std::string& locator, Entry& entry); // Deletes one file for good (docs/alpha/delete-file.md), never a folder and // never what a link points to, if it is still `size` bytes dated diff --git a/src/remote_connection.h b/src/remote_connection.h index 010e336..ab96b01 100644 --- a/src/remote_connection.h +++ b/src/remote_connection.h @@ -64,6 +64,12 @@ class Backend { // change to the login to keep (sftp.cpp). virtual Status signIn(Login& login) = 0; virtual Status list(const std::string& folder, std::vector& entries) = 0; + using Listed = std::function; + virtual void listAsync(const std::string& folder, Listed done) { + Listing result; + result.status = list(folder, result.entries); + done(std::move(result)); + } virtual Status stat(const std::string& locator, Entry& entry) = 0; virtual Status remove(const std::string& locator, uint64_t size, int64_t modified) = 0; diff --git a/src/smb.cpp b/src/smb.cpp index ccf1bd1..5c4a7f8 100644 --- a/src/smb.cpp +++ b/src/smb.cpp @@ -73,8 +73,13 @@ struct Result { }; Entry entry(const std::string& name, const smb2_stat_64& st) { - return {name, st.smb2_type == SMB2_TYPE_DIRECTORY, st.smb2_size, + Entry e{name, st.smb2_type == SMB2_TYPE_DIRECTORY, st.smb2_size, static_cast(st.smb2_mtime)}; + if (e.folder) + e.stamp = {static_cast(st.smb2_mtime), static_cast(st.smb2_mtime_nsec), + static_cast(st.smb2_ctime), static_cast(st.smb2_ctime_nsec), + st.smb2_ino}; + return e; } // All libsmb2 calls for a server. A call may wait for its one request; @@ -133,6 +138,38 @@ class Smb : public Backend { return statusOf(rc); } + void listAsync(const std::string& folder, Listed done) override { + const Location loc(folder); + if (loc.share.empty()) { + Backend::listAsync(folder, std::move(done)); + return; + } + const Status status = connect(loc.share); + if (status != Status::Ok) { + done({status, {}}); + return; + } + auto* d = new Directory{*this, std::move(done), serviceTree_}; + smb2_create_request req{}; + req.impersonation_level = SMB2_IMPERSONATION_IMPERSONATION; + req.desired_access = SMB2_FILE_LIST_DIRECTORY | SMB2_FILE_READ_ATTRIBUTES; + req.file_attributes = SMB2_FILE_ATTRIBUTE_DIRECTORY; + req.share_access = SMB2_FILE_SHARE_READ | SMB2_FILE_SHARE_WRITE | SMB2_FILE_SHARE_DELETE; + req.create_disposition = SMB2_FILE_OPEN; + req.create_options = SMB2_FILE_DIRECTORY_FILE; + req.name = loc.path.c_str(); + auto* pdu = smb2_cmd_create_async(ctx_, &req, Directory::opened, d); + if (!pdu) { + auto callback = std::move(d->done); + delete d; + callback({Status::Offline, {}}); + return; + } + ++awaited_; + lowWater(1); + smb2_queue_pdu(ctx_, pdu); + } + Status remove(const std::string& locator, uint64_t size, int64_t modified) override { const Location loc(locator); Status status = connect(loc.share); @@ -270,6 +307,7 @@ class Smb : public Backend { bool connected() const override { return ctx_; } void disconnect() override { + disconnecting_ = true; if (ctx_) { std::lock_guard lock(contextList); smb2_destroy_context(ctx_); // completes outstanding callbacks before freeing their data @@ -279,6 +317,8 @@ class Smb : public Backend { serviceTree_ = 0; awaited_ = 0; lowWater_ = 1; + disconnecting_ = false; + listsBroken_ = false; } int fd() const override { return ctx_ ? smb2_get_fd(ctx_) : -1; } @@ -290,12 +330,134 @@ class Smb : public Backend { // What follows a block's first part is read as it arrives; so is a reply // that is late, or shorter than what was asked for (an error). if (lowWater_ > 1 && ((revents & POLLIN) || Clock::now() >= patientUntil_)) lowWater(1); - return !ctx_ || smb2_service(ctx_, revents) >= 0; + return !ctx_ || (smb2_service(ctx_, revents) >= 0 && !listsBroken_); } void jobDone() override { serviceTree_ = 0; } private: + // Raw directory requests select their own tree before each continuation. + // Other jobs and playback may select another share between replies. + struct Directory { + Smb& smb; + Listed done; + uint32_t tree; + smb2_file_id file{}; + Listing result{Status::Ok, {}}; + + void finish(Status status) { + result.status = status; + if (status != Status::Ok) result.entries.clear(); + --smb.awaited_; + auto callback = std::move(done); + auto value = std::move(result); + delete this; + callback(std::move(value)); + } + bool aborted(uint32_t status) { + if (smb.disconnecting_) { + finish(Status::Offline); + return true; + } + if (connectionLost(-nterror_to_errno(status), status)) { + smb.listsBroken_ = true; + finish(Status::Offline); + return true; + } + return false; + } + void query() { + uint32_t previous = 0; + smb2_get_tree_id_for_pdu(smb.ctx_, nullptr, &previous); + smb2_select_tree_id(smb.ctx_, tree); + smb2_query_directory_request req{}; + req.file_information_class = SMB2_FILE_ID_FULL_DIRECTORY_INFORMATION; + std::memcpy(req.file_id, file, SMB2_FD_SIZE); + req.output_buffer_length = 64 * 1024; + req.name = "*"; + auto* pdu = smb2_cmd_query_directory_async(smb.ctx_, &req, queried, this); + smb2_select_tree_id(smb.ctx_, previous); + if (pdu) smb2_queue_pdu(smb.ctx_, pdu); + else close(Status::Offline); + } + void close(Status status) { + result.status = status; + uint32_t previous = 0; + smb2_get_tree_id_for_pdu(smb.ctx_, nullptr, &previous); + smb2_select_tree_id(smb.ctx_, tree); + smb2_close_request req{}; + std::memcpy(req.file_id, file, SMB2_FD_SIZE); + auto* pdu = smb2_cmd_close_async(smb.ctx_, &req, closed, this); + smb2_select_tree_id(smb.ctx_, previous); + if (pdu) smb2_queue_pdu(smb.ctx_, pdu); + else { + smb.listsBroken_ = true; // release the server handle on disconnect + finish(Status::Offline); + } + } + static void opened(smb2_context*, int status, void* data, void* opaque) { + auto* d = static_cast(opaque); + if (d->aborted(status)) return; + if (status != SMB2_STATUS_SUCCESS) { + d->finish(statusOf(-nterror_to_errno(status))); + return; + } + std::memcpy(d->file, static_cast(data)->file_id, SMB2_FD_SIZE); + d->query(); + } + static void queried(smb2_context* ctx, int status, void* data, void* opaque) { + auto* d = static_cast(opaque); + if (d->aborted(status)) return; + if (static_cast(status) == SMB2_STATUS_NO_MORE_FILES) { + d->close(Status::Ok); + return; + } + if (status != SMB2_STATUS_SUCCESS) { + // Only opening a nonexistent directory authorizes deleting cached paths. + d->close(Status::Offline); + return; + } + const auto& reply = *static_cast(data); + uint32_t offset = 0; + while (offset < reply.output_buffer_length) { + if (reply.output_buffer_length - offset < 80) { + d->close(Status::Offline); + return; + } + smb2_iovec v{reply.output_buffer + offset, reply.output_buffer_length - offset, nullptr}; + smb2_fileidfulldirectoryinformation f{}; + if (smb2_decode_fileidfulldirectoryinformation(ctx, &f, &v) < 0 || !f.name) { + d->close(Status::Offline); + return; + } + const std::string name = f.name ? f.name : ""; + free(const_cast(f.name)); + if (name != "." && name != "..") { + Entry e{name, bool(f.file_attributes & SMB2_FILE_ATTRIBUTE_DIRECTORY), + f.end_of_file, f.last_write_time.tv_sec}; + e.link = f.file_attributes & SMB2_FILE_ATTRIBUTE_REPARSE_POINT; + if (e.folder) + e.stamp = {f.last_write_time.tv_sec, f.last_write_time.tv_usec * 1000, + f.change_time.tv_sec, f.change_time.tv_usec * 1000, f.file_id}; + d->result.entries.push_back(std::move(e)); + } + if (!f.next_entry_offset) break; + if (f.next_entry_offset < 80 || f.next_entry_offset > reply.output_buffer_length - offset) { + d->close(Status::Offline); + return; + } + offset += f.next_entry_offset; + } + if (!reply.output_buffer_length) d->close(Status::Offline); + else d->query(); + } + static void closed(smb2_context*, int status, void*, void* opaque) { + auto* d = static_cast(opaque); + if (d->aborted(status)) return; + d->finish(status == SMB2_STATUS_SUCCESS ? d->result.status : Status::Offline); + } + }; + static smb2fh* fh(const Handle& h) { return static_cast(h.file); } static void readDone(smb2_context* ctx, int status, void*, void* opaque) { @@ -430,6 +592,7 @@ class Smb : public Backend { int awaited_ = 0; // replies awaited: blocks' and a job's int lowWater_ = 1; // the socket's (lowWater()) bool wholeReplies_ = true; + bool disconnecting_ = false, listsBroken_ = false; Clock::time_point patientUntil_{}; };