Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
29befa3
proto: add the history and restore bodies
Skyfuller1303 Nov 4, 2026
7c7096c
meta: add version history and restore of an old version
Skyfuller1303 Nov 4, 2026
d2a0c0f
server: add the history and restore routes
Skyfuller1303 Nov 4, 2026
2171728
docs: describe version history and restore
Skyfuller1303 Nov 4, 2026
ec83bd7
meta, docs: index the history query and correct the restore note
Skyfuller1303 Nov 4, 2026
48106b5
proto: add the trash bodies
Skyfuller1303 Nov 4, 2026
523ff28
meta: add the trash, with deletion times and a restore that works aro…
Skyfuller1303 Nov 4, 2026
aa7f082
server: add the trash routes and retention.trash_days
Skyfuller1303 Nov 4, 2026
4023e5c
docs: describe the trash
Skyfuller1303 Nov 4, 2026
135688a
meta, proto: bring a file back as a new version and keep restored fol…
Skyfuller1303 Nov 4, 2026
015f2de
sync: do not delete a folder twice after a delete whose answer was lost
Skyfuller1303 Nov 4, 2026
81224f0
docs, server: correct the agent compatibility of the trash restore
Skyfuller1303 Nov 4, 2026
b7e6cb1
sync: judge a folder whose delete was in doubt after the files it hel…
Skyfuller1303 Nov 4, 2026
28d7aa7
sync: settle a lost folder delete deepest first and on a relist
Skyfuller1303 Nov 4, 2026
2aad296
sync: pin folder rules after lost deletes
Skyfuller1303 Nov 4, 2026
f2f51b1
meta: add the reference index of what each manifest names
Skyfuller1303 Nov 4, 2026
d0ff336
server: add verify and index build, and finish a manifest's index bef…
Skyfuller1303 Nov 4, 2026
b70886d
docs: describe the reference index and verify
Skyfuller1303 Nov 4, 2026
54ff35c
meta: find negative ordinals and reset an index in bounded batches
Skyfuller1303 Nov 4, 2026
d7c6898
server, meta, store: verify only an existing server and check the siz…
Skyfuller1303 Nov 4, 2026
c8725af
test: cover reference index edge cases
Skyfuller1303 Nov 4, 2026
1f9610a
meta, server: make the reference index edge-case tests prove what the…
Skyfuller1303 Nov 4, 2026
84599b5
meta, server: open an existing SQLite database read-only so verify ca…
Skyfuller1303 Nov 5, 2026
25c859d
meta: add account storage estimates
Skyfuller1303 Nov 5, 2026
7b46036
proto, server: expose account usage route
Skyfuller1303 Nov 5, 2026
17968bc
docs: document account usage estimates
Skyfuller1303 Nov 5, 2026
205bafd
meta: leave saved versions out of the history estimate
Skyfuller1303 Nov 5, 2026
6913574
server, docs: describe the quota override without naming a pull request
Skyfuller1303 Nov 5, 2026
d8ad5aa
api: expose account usage through shared client
Skyfuller1303 Nov 5, 2026
c17709e
agent: proxy account usage from local API
Skyfuller1303 Nov 5, 2026
9078541
desktop: show account storage usage
Skyfuller1303 Nov 5, 2026
db99801
docs: document local usage route
Skyfuller1303 Nov 5, 2026
1fe8c6c
meta, server: a folder comes back and goes with everything of it in t…
Skyfuller1303 Nov 6, 2026
1dfad98
sync: tests keep a folder alone by taking what is below it out of the…
Skyfuller1303 Nov 6, 2026
2ddd75d
Merge trash: a folder comes back with what was deleted with it
Skyfuller1303 Nov 6, 2026
455353a
Merge gc-reference-index: a folder comes back with what was deleted w…
Skyfuller1303 Nov 6, 2026
aeb53fc
Merge account-usage: a folder comes back with what was deleted with it
Skyfuller1303 Nov 6, 2026
b61fcd6
meta: a folder restore or removal takes at most the cap below it, the…
Skyfuller1303 Nov 6, 2026
2c45e1b
Merge trash: a folder restore or removal takes at most the cap below it
Skyfuller1303 Nov 6, 2026
6dbb9f2
Merge gc-reference-index: a folder restore or removal takes at most t…
Skyfuller1303 Nov 6, 2026
159ed56
Merge account-usage: a folder restore or removal takes at most the ca…
Skyfuller1303 Nov 6, 2026
98020a2
meta tests: name the trash tests' sorting helper sorted_nodes
Skyfuller1303 Nov 6, 2026
19ca781
Merge trash: a folder restore or removal takes at most the cap below it
Skyfuller1303 Nov 6, 2026
8a23d1f
Merge gc-reference-index: a folder restore or removal takes at most t…
Skyfuller1303 Nov 6, 2026
bef5416
Merge account-usage: a folder restore or removal takes at most the ca…
Skyfuller1303 Nov 6, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions apps/craft-cloud-agent/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,11 @@ impl<C: Connection> HttpTransport<C> {
Ok(list)
}

/// The account's stored bytes and labeled estimates, fetched without caching.
pub async fn usage(&self) -> Result<craft_cloud_proto::api::AccountUsage, TransportError> {
json(self.send(self.request(Method::GET, "account/usage", MAX_SMALL_JSON_BYTES)).await?, MAX_SMALL_JSON_BYTES).await
}

/// A new add-a-computer code from this device (replacing its open one).
pub async fn create_pairing(&self) -> Result<PairingCreated, TransportError> {
let created: PairingCreated = json(self.send(self.request(Method::POST, "pairings", 0)).await?, MAX_SMALL_JSON_BYTES).await?;
Expand Down
9 changes: 8 additions & 1 deletion apps/craft-cloud-agent/src/local_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use axum::response::{IntoResponse, Response};
use axum::routing::{delete, get, post};
use axum::{Json, Router};
use craft_cloud_proto::DeviceId;
use craft_cloud_proto::api::{DeviceInfo, ErrorBody, PairingStatus, codes as api_codes};
use craft_cloud_proto::api::{AccountUsage, DeviceInfo, ErrorBody, PairingStatus, codes as api_codes};
use craft_cloud_proto::join::{Code, JoinString, MAX_JOIN_BYTES, PAIRING_CHARS};
use craft_cloud_proto::local::{
AutostartRequest, JoinRequest, LOCAL_API_FILE, LOCAL_API_MAX, LOCAL_API_SCHEMA, LocalApiFile, LoginRequest, LogoutRequest, MAX_LOCAL_JSON_BYTES,
Expand Down Expand Up @@ -350,6 +350,7 @@ fn router(state: Api) -> Router {
.route(paths::PAIRINGS, post(create_pairing))
.route(&format!("{}/{{id}}", paths::PAIRINGS), get(pairing_status).delete(delete_pairing))
.route(paths::DEVICES, get(devices))
.route(paths::USAGE, get(usage))
.route(&format!("{}/{{id}}", paths::DEVICES), delete(remove_device))
.route(paths::AUTOSTART, post(set_autostart))
.route(paths::PAUSE, post(pause))
Expand Down Expand Up @@ -526,6 +527,12 @@ async fn devices(State(state): State<Api>) -> Result<Json<Vec<DeviceInfo>>, Loca
Ok(Json(transport.devices().await.map_err(AgentError::from)?))
}

/// `GET /v1/usage`: asks the signed-in account's server each time; nothing is cached.
async fn usage(State(state): State<Api>) -> Result<Json<AccountUsage>, LocalError> {
let (_, transport) = server_api(&state)?;
Ok(Json(transport.usage().await.map_err(AgentError::from)?))
}

/// Removes (revokes) another computer of the account; answered with the list as it is then. This computer can't be
/// removed this way: that is signing out.
async fn remove_device(State(state): State<Api>, id: Result<Path<DeviceId>, PathRejection>) -> Result<Json<Vec<DeviceInfo>>, LocalError> {
Expand Down
37 changes: 37 additions & 0 deletions apps/craft-cloud-agent/tests/end_to_end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,43 @@ async fn a_first_sync_goes_both_ways() {
assert_eq!(a.files(), b.files());
}

/// Deleting a folder with files in it and restoring it from the trash: the folder and everything that was in the trash
/// below it, also a file deleted on its own before, come back to the disk of both computers by their normal pass.
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn a_folder_restored_from_the_trash_comes_back_with_its_files() {
let server = server().await;
let a = Agent::signed_in(&server, "desktop").await;
a.write("dir1/child.txt", "deleted with the folder");
a.write("dir1/early.txt", "deleted first");
a.write("dir1/sub/deep.txt", "deep");
a.write("keep.txt", "stays");
assert_eq!(a.sync().await, Exit::Done);
let b = Agent::signed_in(&server, "laptop").await;
assert_eq!(b.sync().await, Exit::Done);
assert_eq!(b.files().len(), 4);

std::fs::remove_file(a.path("dir1/early.txt")).unwrap();
assert_eq!(a.sync().await, Exit::Done);
std::fs::remove_dir_all(a.path("dir1")).unwrap();
assert_eq!(a.sync().await, Exit::Done);
assert_eq!(b.sync().await, Exit::Done);
assert_eq!(a.files(), ["keep.txt"]);
assert_eq!(b.files(), ["keep.txt"]);

let ana = server.meta.user_by_name("ana").await.unwrap().unwrap();
let listed = server.meta.trash(ana.space, None, 200, 0).await.unwrap().entries;
assert_eq!(listed.iter().map(|e| e.name.as_str()).collect::<Vec<_>>(), ["dir1"], "the folder is listed alone");
let device = server.meta.list_devices(ana.id).await.unwrap().into_iter().find(|d| d.name == "desktop").unwrap().id;
server.meta.restore_from_trash(ana.space, device, listed[0].id, 0).await.unwrap();

for agent in [&a, &b] {
assert_eq!(agent.sync().await, Exit::Done);
assert_eq!(agent.files(), ["dir1/child.txt", "dir1/early.txt", "dir1/sub/deep.txt", "keep.txt"]);
assert_eq!(agent.read("dir1/sub/deep.txt").as_deref(), Some("deep"));
assert_eq!(agent.read("dir1/early.txt").as_deref(), Some("deleted first"));
}
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn big_files_travel_in_chunks_and_keep_their_exec_bit() {
let server = server().await;
Expand Down
53 changes: 51 additions & 2 deletions apps/craft-cloud-agent/tests/local_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const PATIENCE: Duration = Duration::from_secs(30);
struct Server {
url: String,
meta: Meta,
task: tokio::task::JoinHandle<std::io::Result<()>>,
_dirs: (tempfile::TempDir, tempfile::TempDir),
}

Expand All @@ -34,8 +35,8 @@ async fn server() -> Server {
let state = AppState::new(meta.clone(), Store::new(DiskBackend::new(blobs.path()).unwrap()), vec![]).unwrap();
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let url = format!("http://{}", listener.local_addr().unwrap());
tokio::spawn(craft_cloud_server::serve(state, listener, std::future::pending()));
Server { url, meta, _dirs: (db, blobs) }
let task = tokio::spawn(craft_cloud_server::serve(state, listener, std::future::pending()));
Server { url, meta, task, _dirs: (db, blobs) }
}

fn quick() -> RunOptions {
Expand Down Expand Up @@ -172,6 +173,7 @@ const ROUTES: &[(&str, &str)] = &[
("GET", "/v1/pairings/0192f0c4-0000-7000-8000-000000000000"),
("DELETE", "/v1/pairings/0192f0c4-0000-7000-8000-000000000000"),
("GET", "/v1/devices"),
("GET", "/v1/usage"),
("DELETE", "/v1/devices/0192f0c4-0000-7000-8000-000000000000"),
("POST", "/v1/autostart"),
];
Expand Down Expand Up @@ -522,6 +524,53 @@ async fn signed_in(server: &Server, data: &DataDir, folder: &Path) -> (Running,
(running, api)
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn usage_reads_through_to_the_server_without_caching() {
let server = server().await;
let data = DataDir::new(tempfile::tempdir().unwrap().keep());
let folder = tempfile::tempdir().unwrap();
let (_running, api) = signed_in(&server, &data, folder.path()).await;
let account = data.load_account().unwrap().unwrap();
let transport = craft_cloud_agent::http::HttpTransport::new(account.server.clone(), &account.token).unwrap();
std::fs::write(folder.path().join("usage-first.bin"), b"first payload").unwrap();
reaches_the_server(&server, "usage-first.bin").await;
let server_first = transport.usage().await.unwrap();
let (code, body) = call(&api, "GET", "/v1/usage", "");
assert_eq!(code, 200, "{body}");
let local_first: craft_cloud_proto::api::AccountUsage = serde_json::from_str(&body).unwrap();
assert_eq!(local_first, server_first, "the local API passes nonzero numbers through unchanged");
assert!(local_first.stored_bytes > 0);

std::fs::write(folder.path().join("usage-second.bin"), b"a different payload for fresh usage").unwrap();
reaches_the_server(&server, "usage-second.bin").await;
let server_second = transport.usage().await.unwrap();
let (code, body) = call(&api, "GET", "/v1/usage", "");
assert_eq!(code, 200, "{body}");
let local_second: craft_cloud_proto::api::AccountUsage = serde_json::from_str(&body).unwrap();
assert_eq!(local_second, server_second, "a second request sees server changes; no cache");
assert!(local_second.stored_bytes > local_first.stored_bytes);
}

#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
async fn usage_refuses_signed_out_and_reports_server_unreachable() {
let server = server().await;
let data = DataDir::new(tempfile::tempdir().unwrap().keep());
let running = Running::start(&data);
let api = api_file(data.path()).await;
let (code, body) = call(&api, "GET", "/v1/usage", "");
assert_eq!(code, 409, "{body}");
assert_eq!(error_code(&body), codes::NOT_SIGNED_IN);
let _ = running.stop().await;

let folder = tempfile::tempdir().unwrap();
let (running, api) = signed_in(&server, &data, folder.path()).await;
server.task.abort();
let (code, body) = call(&api, "GET", "/v1/usage", "");
assert_eq!(code, 503, "{body}");
assert_eq!(error_code(&body), codes::OFFLINE);
drop(running);
}

async fn reaches_the_server(server: &Server, name: &str) {
let deadline = Instant::now() + PATIENCE;
while !server_has(server, name).await {
Expand Down
29 changes: 27 additions & 2 deletions apps/craft-cloud-desktop/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::sync::mpsc::{Receiver, Sender, channel};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;

use craft_cloud_app::{Client, DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, PairingStatus, Status, Uuid};
use craft_cloud_app::{AccountUsage, Client, DeviceId, DeviceInfo, Error, JoinRequest, PairingInfo, PairingStatus, Status, Uuid};

use crate::model::{Effect, Event};

Expand All @@ -28,6 +28,7 @@ pub trait Agent: Send + Sync {
fn pairing_state(&self, id: Uuid) -> Result<PairingStatus, Error>;
fn cancel_pairing(&self, id: Uuid) -> Result<(), Error>;
fn devices(&self) -> Result<Vec<DeviceInfo>, Error>;
fn usage(&self) -> Result<AccountUsage, Error>;
fn remove_device(&self, id: DeviceId) -> Result<Vec<DeviceInfo>, Error>;
fn pause(&self) -> Result<Status, Error>;
fn resume(&self) -> Result<Status, Error>;
Expand Down Expand Up @@ -98,6 +99,9 @@ impl Agent for ClientAgent {
fn devices(&self) -> Result<Vec<DeviceInfo>, Error> {
self.with(Client::devices)
}
fn usage(&self) -> Result<AccountUsage, Error> {
self.with(Client::usage)
}
fn remove_device(&self, id: DeviceId) -> Result<Vec<DeviceInfo>, Error> {
self.with(|c| c.remove_device(id))
}
Expand Down Expand Up @@ -198,6 +202,7 @@ pub fn call(agent: &dyn Agent, start: &Starter, effect: Effect) -> Option<Event>
return None;
}
Effect::Devices => Event::Devices(agent.devices()),
Effect::Usage(request_id) => Event::Usage { request_id, result: agent.usage() },
Effect::RemoveDevice(id) => Event::Devices(agent.remove_device(id)),
Effect::Logout { local } => Event::SignedOut(agent.logout(local)),
Effect::Pause => Event::Answered(agent.pause()),
Expand All @@ -223,12 +228,19 @@ pub(crate) mod fake {
pub gone: AtomicBool,
/// What `join` answers, in turn (then the status).
pub joins: Mutex<Vec<Result<Status, Error>>>,
pub usage_result: Mutex<Option<Result<AccountUsage, Error>>>,
pub asked: Mutex<Vec<String>>,
}

impl FakeAgent {
pub fn new(status: Status) -> Self {
Self { status: Mutex::new(status), gone: AtomicBool::new(false), joins: Mutex::new(Vec::new()), asked: Mutex::new(Vec::new()) }
Self {
status: Mutex::new(status),
gone: AtomicBool::new(false),
joins: Mutex::new(Vec::new()),
usage_result: Mutex::new(None),
asked: Mutex::new(Vec::new()),
}
}

fn answer<T>(&self, what: &str, ok: impl FnOnce(&Status) -> T) -> Result<T, Error> {
Expand Down Expand Up @@ -272,6 +284,19 @@ pub(crate) mod fake {
fn devices(&self) -> Result<Vec<DeviceInfo>, Error> {
self.answer("devices", |_| Vec::new())
}
fn usage(&self) -> Result<AccountUsage, Error> {
self.answer("usage", |_| ())?;
if let Some(result) = self.usage_result.lock().unwrap().clone() {
return result;
}
Ok(AccountUsage {
stored_bytes: 0,
quota_bytes: None,
trash_bytes: Some(0),
history_bytes: Some(0),
estimates: craft_cloud_proto::api::UsageEstimates { index_complete: true, reason: None },
})
}
fn remove_device(&self, _id: DeviceId) -> Result<Vec<DeviceInfo>, Error> {
self.answer("remove_device", |_| Vec::new())
}
Expand Down
Loading