foxygit / Torlinkc Log in
commits tags

/include/torlinkc/engine/persist.hpp · 1.59 KB

raw
#pragma once

#include <cstdint>
#include <optional>
#include <string>
#include <vector>

#include "torlinkc/engine/types.hpp"

namespace torlinkc {

// We persist the user-meaningful seed states so a deliberate pause survives a
// restart (and stays paused), not just the seeding ids. "missing" is
// runtime-only and gets folded to paused on the way out so a gone file is
// never auto-seeded.
struct SeedRecord {
  std::string id;
  SeedStatus status = SeedStatus::Seeding;  // only Seeding or Paused is ever persisted
};

// Best-effort: never throws. A failed write is swallowed exactly like the
// original's `.catch(() => {})`, since losing one persistence tick is
// preferable to crashing the download loop over it.
void saveQueue(const std::vector<QueueItem>& items);
std::vector<QueueItem> loadQueue();

void saveSeeds(const std::vector<SeedRecord>& records);
std::vector<SeedRecord> loadSeeds();

// --- per-torrent .torrent metadata cache ------------------------------------

std::string torrentMetaPath(const std::string& id);
bool torrentMetaExists(const std::string& id);
std::string torrentExportName(const std::string& name, const std::string& id);
void saveTorrentMeta(const std::string& id, const std::string& data);
// Copies the cached .torrent into `dir` under a sanitized filename. Returns
// the exported path, or nullopt if no cached metadata exists / the copy fails.
std::optional<std::string> exportTorrentMeta(const std::string& id, const std::string& name,
                                              const std::string& dir);
void deleteTorrentMeta(const std::string& id);

}  // namespace torlinkc