braid-core 0.1.4

Unified Braid Protocol implementation in Rust, including Braid-HTTP, Antimatter CRDT, and BraidFS.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
use super::blob_handlers::{handle_get_blob, handle_put_blob};
use super::mapping;
use super::server_handlers::{handle_get_file, handle_get_file_api, handle_put_file};
use crate::core::server::BraidLayer;
use crate::core::Result;
use crate::fs::state::{Command, DaemonState};
use axum::{
    extract::State,
    routing::{delete, put},
    Json, Router,
};
use serde::Deserialize;
use std::net::SocketAddr;

#[derive(Deserialize)]
pub struct SyncParams {
    url: String,
}

#[derive(Deserialize)]
pub struct PushParams {
    url: String,
    content: String,
    content_type: Option<String>,
}

#[derive(Deserialize)]
pub struct CookieParams {
    pub domain: String,
    pub value: String,
}

#[derive(Deserialize)]
pub struct IdentityParams {
    pub domain: String,
    pub email: String,
}

#[derive(Deserialize)]
pub struct MountParams {
    pub port: Option<u16>,
    pub mount_point: Option<String>,
}

pub async fn run_server(port: u16, state: DaemonState) -> Result<()> {
    let mut app = Router::new()
        .route("/api/sync", put(handle_sync))
        .route("/api/sync", delete(handle_unsync))
        .route("/api/push", put(handle_push))
        .route("/api/get", axum::routing::get(handle_get_file_api))
        .route("/api/cookie", put(handle_cookie))
        .route("/api/identity", put(handle_identity));

    #[cfg(feature = "nfs")]
    {
        app = app
            .route("/api/mount", put(handle_mount))
            .route("/api/mount", delete(handle_unmount));
    }

    let app = app
        .route(
            "/.braidfs/config",
            axum::routing::get(handle_braidfs_config),
        )
        .route(
            "/.braidfs/errors",
            axum::routing::get(handle_braidfs_errors),
        )
        .route(
            "/.braidfs/get_version/{fullpath}/{hash}",
            axum::routing::get(handle_get_version),
        )
        .route(
            "/.braidfs/set_version/{fullpath}/{parents}",
            axum::routing::put(handle_set_version),
        )
        .route("/api/blob/{hash}", axum::routing::get(handle_get_blob))
        .route("/api/blob", put(handle_put_blob))
        .route("/{*path}", axum::routing::get(handle_get_file))
        .route("/{*path}", put(handle_put_file))
        .layer(BraidLayer::new().middleware())
        .with_state(state);

    let addr = SocketAddr::from(([127, 0, 0, 1], port));
    tracing::info!("Daemon API listening on {}", addr);

    let listener = tokio::net::TcpListener::bind(addr).await?;
    axum::serve(listener, app).await?;

    Ok(())
}

async fn handle_sync(
    State(state): State<DaemonState>,
    Json(params): Json<SyncParams>,
) -> Json<serde_json::Value> {
    tracing::info!("IPC Command: Sync {}", params.url);

    if let Err(e) = state
        .tx_cmd
        .send(Command::Sync {
            url: params.url.clone(),
        })
        .await
    {
        tracing::error!("Failed to send sync command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok", "url": params.url }))
}

async fn handle_unsync(
    State(state): State<DaemonState>,
    Json(params): Json<SyncParams>,
) -> Json<serde_json::Value> {
    tracing::info!("IPC Command: Unsync {}", params.url);

    if let Err(e) = state
        .tx_cmd
        .send(Command::Unsync {
            url: params.url.clone(),
        })
        .await
    {
        tracing::error!("Failed to send unsync command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok", "url": params.url }))
}

async fn handle_push(
    State(state): State<DaemonState>,
    Json(params): Json<PushParams>,
) -> Json<serde_json::Value> {
    tracing::info!(
        "IPC Command: Push {} ({} bytes)",
        params.url,
        params.content.len()
    );

    // 1. Write content to local file
    let path = match mapping::url_to_path(&params.url) {
        Ok(p) => p,
        Err(e) => {
            tracing::error!("Failed to map URL to path: {}", e);
            return Json(
                serde_json::json!({ "status": "error", "message": format!("Path mapping failed: {}", e) }),
            );
        }
    };

    if let Some(parent) = path.parent() {
        if let Err(e) = tokio::fs::create_dir_all(parent).await {
            tracing::error!("Failed to create parent directory: {}", e);
            return Json(
                serde_json::json!({ "status": "error", "message": format!("Directory creation failed: {}", e) }),
            );
        }
    }

    // 2. Get current version (parents)
    let parents = {
        let store = state.version_store.read().await;
        store
            .get(&params.url)
            .map(|v| v.current_version.clone())
            .unwrap_or_default()
    };

    // 3. Get original content for diff
    let original_content = {
        let cache = state.content_cache.read().await;
        cache.get(&params.url).cloned()
    };

    // 4. Push to remote FIRST (Confirm)
    match crate::fs::sync::sync_local_to_remote(
        &path,
        &params.url,
        &parents,
        original_content,
        params.content.clone(),
        params.content_type,
        state.clone(),
    )
    .await
    {
        Ok(()) => {
            tracing::info!("Successfully pushed {} to remote", params.url);

            // 5. Commit to local disk only after server confirmation
            if let Err(e) = tokio::fs::write(&path, &params.content).await {
                tracing::error!("Failed to write file after successful sync: {}", e);
                return Json(
                    serde_json::json!({ "status": "error", "message": format!("Server accepted but local write failed: {}", e) }),
                );
            }
            // Add to pending to avoid loop if we had a watcher trigger
            state.pending.add(path.clone());

            Json(serde_json::json!({ "status": "ok", "url": params.url }))
        }
        Err(e) => {
            tracing::error!("Push failed for {}: {}", params.url, e);
            let err_str = e.to_string();
            let status = if err_str.contains("401") || err_str.contains("Unauthorized") {
                "unauthorized"
            } else if err_str.contains("403") || err_str.contains("Forbidden") {
                "forbidden"
            } else {
                "error"
            };

            tracing::error!("Server error detail: {}", e);

            Json(serde_json::json!({
                "status": status,
                "message": format!("Push failed: {}", e),
                "domain": url::Url::parse(&params.url).ok().and_then(|u| u.domain().map(|d| d.to_string()))
            }))
        }
    }
}

async fn handle_cookie(
    State(state): State<DaemonState>,
    Json(params): Json<CookieParams>,
) -> Json<serde_json::Value> {
    tracing::info!("IPC Command: SetCookie {}={}", params.domain, params.value);

    if let Err(e) = state
        .tx_cmd
        .send(Command::SetCookie {
            domain: params.domain.clone(),
            value: params.value.clone(),
        })
        .await
    {
        tracing::error!("Failed to send cookie command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok", "domain": params.domain }))
}

async fn handle_identity(
    State(state): State<DaemonState>,
    Json(params): Json<IdentityParams>,
) -> Json<serde_json::Value> {
    tracing::info!(
        "IPC Command: SetIdentity {}={}",
        params.domain,
        params.email
    );

    if let Err(e) = state
        .tx_cmd
        .send(Command::SetIdentity {
            domain: params.domain.clone(),
            email: params.email.clone(),
        })
        .await
    {
        tracing::error!("Failed to send identity command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok", "domain": params.domain }))
}

/// Handle /.braidfs/config - returns the current configuration.
async fn handle_braidfs_config(State(state): State<DaemonState>) -> Json<serde_json::Value> {
    let config = state.config.read().await;
    Json(serde_json::json!({
        "sync": config.sync,
        "cookies": config.cookies,
        "port": config.port,
        "debounce_ms": config.debounce_ms,
        "ignore_patterns": config.ignore_patterns,
    }))
}

/// Error log storage (in-memory for now).
static ERRORS: std::sync::OnceLock<std::sync::Mutex<Vec<String>>> = std::sync::OnceLock::new();

fn get_errors() -> &'static std::sync::Mutex<Vec<String>> {
    ERRORS.get_or_init(|| std::sync::Mutex::new(Vec::new()))
}

/// Log an error to the in-memory error log.
pub fn log_error(text: &str) {
    tracing::error!("LOGGING ERROR: {}", text);
    if let Ok(mut errors) = get_errors().lock() {
        errors.push(format!(
            "{}: {}",
            chrono::Utc::now().format("%Y-%m-%d %H:%M:%S"),
            text
        ));
        // Keep only last 100 errors
        if errors.len() > 100 {
            errors.remove(0);
        }
    }
}

/// Handle /.braidfs/errors - returns the error log.
async fn handle_braidfs_errors() -> String {
    if let Ok(errors) = get_errors().lock() {
        errors.join("\n")
    } else {
        "Error reading error log".to_string()
    }
}

/// Handle /.braidfs/get_version/{fullpath}/{hash} - get version by content hash.
async fn handle_get_version(
    axum::extract::Path((fullpath, hash)): axum::extract::Path<(String, String)>,
    State(state): State<DaemonState>,
) -> Json<serde_json::Value> {
    use percent_encoding::percent_decode_str;
    let fullpath = percent_decode_str(&fullpath)
        .decode_utf8_lossy()
        .to_string();
    let hash = percent_decode_str(&hash).decode_utf8_lossy().to_string();

    tracing::debug!("get_version: {} hash={}", fullpath, hash);

    // Look up version in version store
    let versions = state.version_store.read().await;
    if let Some(version) = versions.get_version_by_hash(&fullpath, &hash) {
        Json(serde_json::json!(version))
    } else {
        Json(serde_json::json!(null))
    }
}

/// Handle /.braidfs/set_version/{fullpath}/{parents} - set version by content hash.
async fn handle_set_version(
    axum::extract::Path((fullpath, parents)): axum::extract::Path<(String, String)>,
    State(state): State<DaemonState>,
    body: String,
) -> Json<serde_json::Value> {
    use percent_encoding::percent_decode_str;
    let fullpath = percent_decode_str(&fullpath)
        .decode_utf8_lossy()
        .to_string();
    let parents_json = percent_decode_str(&parents).decode_utf8_lossy().to_string();

    let parents: Vec<String> = serde_json::from_str(&parents_json).unwrap_or_default();

    tracing::info!("set_version: {} parents={:?}", fullpath, parents);

    match mapping::path_to_url(std::path::Path::new(&fullpath)) {
        Ok(url) => {
            let mut store = state.version_store.write().await;
            let my_id = crate::fs::PEER_ID.read().await.clone();

            // Generate a new version ID
            let version_id = format!(
                "{}-{}",
                my_id,
                uuid::Uuid::new_v4().to_string()[..8].to_string()
            );

            store.update(
                &url,
                vec![crate::core::Version::new(&version_id)],
                parents
                    .into_iter()
                    .map(|p| crate::core::Version::new(&p))
                    .collect(),
            );
            let _ = store.save().await;

            // Also update content cache
            let mut cache = state.content_cache.write().await;
            cache.insert(url, body);

            Json(serde_json::json!({ "status": "ok", "version": version_id }))
        }
        Err(e) => {
            tracing::error!("Failed to map path to URL: {}", e);
            Json(serde_json::json!({ "status": "error", "message": e.to_string() }))
        }
    }
}

/// Check if a file is read-only.
/// Matches JS `is_read_only()` from braidfs/index.js.
#[cfg(unix)]
pub async fn is_read_only(path: &std::path::Path) -> std::io::Result<bool> {
    use std::os::unix::fs::PermissionsExt;
    let metadata = tokio::fs::metadata(path).await?;
    let mode = metadata.permissions().mode();
    // Check if write bit is set for owner
    Ok((mode & 0o200) == 0)
}

#[cfg(windows)]
pub async fn is_read_only(path: &std::path::Path) -> std::io::Result<bool> {
    let metadata = tokio::fs::metadata(path).await?;
    Ok(metadata.permissions().readonly())
}

/// Set a file to read-only or writable.
/// Matches JS `set_read_only()` from braidfs/index.js.
#[cfg(unix)]
pub async fn set_read_only(path: &std::path::Path, read_only: bool) -> std::io::Result<()> {
    use std::os::unix::fs::PermissionsExt;
    let metadata = tokio::fs::metadata(path).await?;
    let mut perms = metadata.permissions();
    let mode = perms.mode();

    let new_mode = if read_only {
        mode & !0o222 // Remove write bits
    } else {
        mode | 0o200 // Add owner write bit
    };

    perms.set_mode(new_mode);
    tokio::fs::set_permissions(path, perms).await
}

#[cfg(windows)]
pub async fn set_read_only(path: &std::path::Path, read_only: bool) -> std::io::Result<()> {
    let metadata = tokio::fs::metadata(path).await?;
    let mut perms = metadata.permissions();
    perms.set_readonly(read_only);
    tokio::fs::set_permissions(path, perms).await
}

#[cfg(feature = "nfs")]
async fn handle_mount(
    State(state): State<DaemonState>,
    Json(params): Json<MountParams>,
) -> Json<serde_json::Value> {
    let port = params.port.unwrap_or(2049);
    tracing::info!("IPC Command: Mount on port {}", port);

    if let Err(e) = state
        .tx_cmd
        .send(Command::Mount {
            port,
            mount_point: params.mount_point,
        })
        .await
    {
        tracing::error!("Failed to send mount command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok", "port": port }))
}

#[cfg(feature = "nfs")]
async fn handle_unmount(State(state): State<DaemonState>) -> Json<serde_json::Value> {
    tracing::info!("IPC Command: Unmount");

    if let Err(e) = state.tx_cmd.send(Command::Unmount).await {
        tracing::error!("Failed to send unmount command: {}", e);
        return Json(serde_json::json!({ "status": "error", "message": "Internal channel error" }));
    }

    Json(serde_json::json!({ "status": "ok" }))
}
async fn handle_push_binary(
    State(state): State<DaemonState>,
    query: axum::extract::Query<SyncParams>,
    headers: axum::http::HeaderMap,
    body: axum::body::Bytes,
) -> Json<serde_json::Value> {
    tracing::info!(
        "IPC Command: Push Binary {} ({} bytes)",
        query.url,
        body.len()
    );

    // 1. Map URL to path
    let path = match mapping::url_to_path(&query.url) {
        Ok(p) => p,
        Err(e) => {
            return Json(
                serde_json::json!({ "status": "error", "message": format!("Path mapping failed: {}", e) }),
            );
        }
    };

    if let Some(parent) = path.parent() {
        let _ = tokio::fs::create_dir_all(parent).await;
    }

    // 2. Parents/Version
    let parents = {
        let store = state.version_store.read().await;
        store
            .get(&query.url)
            .map(|v| v.current_version.clone())
            .unwrap_or_default()
    };

    let content_type = headers
        .get(axum::http::header::CONTENT_TYPE)
        .and_then(|v| v.to_str().ok())
        .map(|s| s.to_string());

    // 3. Push to remote
    match crate::fs::sync::sync_binary_to_remote(
        &path,
        &query.url,
        &parents,
        body.clone(),
        content_type,
        state.clone(),
    )
    .await
    {
        Ok(()) => {
            // 4. Commit to local disk
            if let Err(e) = tokio::fs::write(&path, &body).await {
                return Json(
                    serde_json::json!({ "status": "error", "message": format!("Server accepted but local write failed: {}", e) }),
                );
            }
            state.pending.add(path);

            Json(serde_json::json!({ "status": "ok", "url": query.url }))
        }
        Err(e) => Json(
            serde_json::json!({ "status": "error", "message": format!("Binary push failed: {}", e) }),
        ),
    }
}