solidb 1.0.1

A lightweight, high-performance structured database server written in Rust.
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
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
use clap::Parser;
use fuser::{
    FileAttr, FileType, Filesystem, MountOption, ReplyAttr, ReplyData, ReplyDirectory, ReplyEntry,
    ReplyOpen, Request,
};
use libc::{EIO, ENOENT};
use serde::Deserialize;
use std::collections::{HashMap, HashSet};
use std::ffi::OsStr;
use std::sync::{Arc, Mutex};
use std::time::{Duration, UNIX_EPOCH};

use chrono::{DateTime, Datelike, Utc};
use tracing::{debug, error, info};
use uuid::Uuid;

const TTL: Duration = Duration::from_secs(1);
const BLOCK_SIZE: u64 = 512;

#[derive(Parser, Debug)]
#[command(name = "solidb-fuse")]
#[command(about = "Mount SolidB blob collections as a filesystem", long_about = None)]
struct Args {
    #[arg(long, default_value = "localhost")]
    host: String,

    #[arg(long, default_value_t = 6745)]
    port: u16,

    #[arg(long, default_value = "admin")]
    username: String,

    #[arg(long)]
    password: Option<String>,

    #[arg(long, help = "Mount point path")]
    mount: String,

    #[arg(long, default_value_t = false, help = "Run in foreground")]
    foreground: bool,

    #[arg(short, long, help = "Run as a daemon (background process)")]
    daemon: bool,

    #[arg(
        long,
        default_value = "./solidb-fuse.pid",
        help = "PID file path (used in daemon mode)"
    )]
    pid_file: String,

    #[arg(
        long,
        default_value = "./solidb-fuse.log",
        help = "Log file path (used in daemon mode)"
    )]
    log_file: String,
}

#[derive(Debug, Deserialize)]
struct DatabaseList {
    databases: Vec<String>,
}

#[derive(Debug, Deserialize)]
struct Collection {
    name: String,
    #[serde(default, rename = "type")]
    r#type: String,
}

#[derive(Debug, Deserialize)]
struct CollectionList {
    collections: Vec<Collection>,
}

#[derive(Debug, Deserialize, Clone)]
struct BlobMetadata {
    _key: String,
    #[serde(default, alias = "name")]
    filename: Option<String>,
    #[serde(default)]
    size: u64,
}

#[derive(Debug, Deserialize)]
struct CursorResponse<T> {
    result: Vec<T>,
}

#[derive(Debug, serde::Serialize)]
struct LoginRequest<'a> {
    username: &'a str,
    password: &'a str,
}

#[derive(Debug, Deserialize)]
struct LoginResponse {
    token: String,
}

#[derive(Debug)]
struct SolidBClient {
    client: reqwest::blocking::Client,
    base_url: String,
    username: String,
    password: Option<String>,
    token: Option<String>,
    // Cache for list_databases: (timestamp, data)
    db_list_cache: Option<(std::time::Instant, Vec<String>)>,
}

impl SolidBClient {
    fn new(host: &str, port: u16, username: &str, password: Option<&str>) -> Self {
        let base_url = format!("http://{}:{}", host, port);

        let mut client = Self {
            client: reqwest::blocking::Client::new(),
            base_url,
            username: username.to_string(),
            password: password.map(|s| s.to_string()),
            token: None,
            db_list_cache: None,
        };

        // Attempt login immediately
        if let Err(e) = client.authenticate() {
            error!(
                "Initial authentication failed: {}. Subsequent requests may fail.",
                e
            );
        }

        client
    }

    fn authenticate(&mut self) -> Result<(), anyhow::Error> {
        if let Some(pass) = &self.password {
            // Correct auth endpoint is /auth/login (not /_api/auth/login)
            let url = format!("{}/auth/login", self.base_url);
            let body = LoginRequest {
                username: &self.username,
                password: pass,
            };

            info!("Authenticating as {} to {}...", self.username, url);
            let resp = self.client.post(&url).json(&body).send()?;

            let status = resp.status();
            if !status.is_success() {
                let text = resp.text().unwrap_or_default();
                error!("Login failed with status {}. Response: {}", status, text);
                return Err(anyhow::anyhow!("Login failed: {}", status));
            }

            match resp.json::<LoginResponse>() {
                Ok(data) => {
                    self.token = Some(data.token);
                    info!("Authentication successful. Token received.");
                }
                Err(e) => {
                    error!("Failed to parse login response: {}", e);
                    return Err(e.into());
                }
            }
        }
        Ok(())
    }

    fn list_databases(&mut self) -> Result<Vec<String>, anyhow::Error> {
        // Check cache (TTL 5 seconds)
        if let Some((ts, dbs)) = &self.db_list_cache {
            if ts.elapsed() < Duration::from_secs(5) {
                return Ok(dbs.clone());
            }
        }

        // Correct endpoint is /_api/databases (plural)
        let url = format!("{}/_api/databases", self.base_url);
        info!("Fetching databases from {}", url);

        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        } else {
            // Fallback or Basic? Server only supports Bearer probably.
            // Try Basic if no token and no password? No, token is key.
            if self.password.is_some() {
                // Try re-auth?
                // For now, if token is missing and we have password, maybe we failed init.
            }
        }

        let resp = req.send()?;

        if resp.status() == reqwest::StatusCode::UNAUTHORIZED {
            // Try re-login once
            info!("Got 401, attempting re-login...");
            self.authenticate()?;
            let mut req = self.client.get(&url);
            if let Some(token) = &self.token {
                req = req.bearer_auth(token);
            }
            let resp = req.send()?;
            if !resp.status().is_success() {
                error!(
                    "API Error listing databases after re-login: Status {}",
                    resp.status()
                );
            }
            let list = resp.json::<DatabaseList>()?;

            // Update cache
            self.db_list_cache = Some((std::time::Instant::now(), list.databases.clone()));
            return Ok(list.databases);
        }

        if !resp.status().is_success() {
            error!("API Error listing databases: Status {}", resp.status());
        }

        let list = resp.json::<DatabaseList>()?;

        // Update cache
        self.db_list_cache = Some((std::time::Instant::now(), list.databases.clone()));

        Ok(list.databases)
    }

    fn list_collections(&self, db: &str) -> Result<Vec<String>, anyhow::Error> {
        let url = format!("{}/_api/database/{}/collection", self.base_url, db);
        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req.send()?.json::<CollectionList>()?;

        Ok(resp
            .collections
            .into_iter()
            .filter(|c| c.r#type == "blob")
            .map(|c| c.name)
            .collect())
    }

    fn list_blobs(&self, db: &str, coll: &str) -> Result<Vec<BlobMetadata>, anyhow::Error> {
        // Endpoint is /_api/database/:db/cursor
        let url = format!("{}/_api/database/{}/cursor", self.base_url, db);
        let query = format!("FOR doc IN {} RETURN doc", coll);
        let body = serde_json::json!({
            "query": query,
            "database": db
        });

        let mut req = self.client.post(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req
            .json(&body)
            .send()?
            .json::<CursorResponse<BlobMetadata>>()?;

        Ok(resp.result)
    }

    fn get_blob_content(&self, db: &str, coll: &str, key: &str) -> Result<Vec<u8>, anyhow::Error> {
        let url = format!("{}/_api/blob/{}/{}/{}", self.base_url, db, coll, key);
        let mut req = self.client.get(&url);
        if let Some(token) = &self.token {
            req = req.bearer_auth(token);
        }

        let resp = req.send()?;

        if resp.status().is_success() {
            Ok(resp.bytes()?.to_vec())
        } else {
            Err(anyhow::anyhow!("Failed to fetch blob: {}", resp.status()))
        }
    }
}

#[derive(Debug, Clone)]
enum InodeType {
    Root,
    Database(String),
    Collection(String, String),         // db, coll
    Year(String, String, i32),          // db, coll, year
    Month(String, String, i32, u32),    // db, coll, year, month
    Day(String, String, i32, u32, u32), // db, coll, year, month, day
    Blob(String, String, BlobMetadata), // db, coll, metadata
}

struct SolidBFS {
    client: Arc<Mutex<SolidBClient>>,
    inodes: HashMap<u64, InodeType>,
    next_inode: u64,
    uid: u32,
    gid: u32,
}

impl SolidBFS {
    fn new(client: SolidBClient) -> Self {
        let mut inodes = HashMap::new();
        inodes.insert(1, InodeType::Root);
        unsafe {
            Self {
                client: Arc::new(Mutex::new(client)),
                inodes,
                next_inode: 2,
                uid: libc::getuid(),
                gid: libc::getgid(),
            }
        }
    }

    fn allocate_inode(&mut self, kind: InodeType) -> u64 {
        // Linear search to deduplicate would be slow.
        // For now, simpler: just allocate. Kernel handles lookup caching.
        // If we want stable inodes, we need a reverse map.
        // Let's rely on readdir/lookup being consistent during a session.
        let ino = self.next_inode;
        self.next_inode += 1;
        self.inodes.insert(ino, kind);
        ino
    }

    fn get_inode(&self, ino: u64) -> Option<&InodeType> {
        self.inodes.get(&ino)
    }

    fn get_file_attr(&self, ino: u64) -> Option<FileAttr> {
        let kind = self.get_inode(ino)?;
        Some(match kind {
            InodeType::Root
            | InodeType::Database(_)
            | InodeType::Collection(_, _)
            | InodeType::Year(_, _, _)
            | InodeType::Month(_, _, _, _)
            | InodeType::Day(_, _, _, _, _) => FileAttr {
                ino,
                size: 0,
                blocks: 0,
                atime: UNIX_EPOCH,
                mtime: UNIX_EPOCH,
                ctime: UNIX_EPOCH,
                crtime: UNIX_EPOCH,
                kind: FileType::Directory,
                perm: 0o755,
                nlink: 2,
                uid: self.uid,
                gid: self.gid,
                rdev: 0,
                flags: 0,
                blksize: 512,
            },
            InodeType::Blob(_, _, meta) => {
                FileAttr {
                    ino,
                    size: meta.size,
                    blocks: meta.size.div_ceil(BLOCK_SIZE),
                    atime: UNIX_EPOCH,
                    mtime: UNIX_EPOCH, // Could extract from UUID if desired
                    ctime: UNIX_EPOCH,
                    crtime: UNIX_EPOCH,
                    kind: FileType::RegularFile,
                    perm: 0o644,
                    nlink: 1,
                    uid: self.uid,
                    gid: self.gid,
                    rdev: 0,
                    flags: 0,
                    blksize: 512,
                }
            }
        })
    }
}

impl Filesystem for SolidBFS {
    fn lookup(&mut self, _req: &Request, parent: u64, name: &OsStr, reply: ReplyEntry) {
        let name_str = match name.to_str() {
            Some(s) => s,
            None => {
                reply.error(ENOENT);
                return;
            }
        };

        // This is inefficient: we reconstruct children to find the matching one.
        // In a real optimized FS we would cache children.
        // Here we just "simulate" finding it.

        let parent_kind = match self.get_inode(parent) {
            Some(k) => k.clone(),
            None => {
                reply.error(ENOENT);
                return;
            }
        };

        let client_arc = self.client.clone();
        let mut client = client_arc.lock().unwrap();

        // debug!("FUSE: lookup called for {} in parent {}", name_str, parent);

        match parent_kind {
            InodeType::Root => {
                // Check databases
                if let Ok(dbs) = client.list_databases() {
                    if dbs.contains(&name_str.to_string()) {
                        drop(client); // Release lock before mutating
                        let ino = self.allocate_inode(InodeType::Database(name_str.to_string()));
                        reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                        return;
                    }
                }
            }
            InodeType::Database(db) => {
                // Check collections
                if let Ok(colls) = client.list_collections(&db) {
                    if colls.contains(&name_str.to_string()) {
                        drop(client);
                        let ino =
                            self.allocate_inode(InodeType::Collection(db, name_str.to_string()));
                        reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                        return;
                    }
                }
            }
            InodeType::Collection(db, coll) => {
                // Looking for Year?
                if let Ok(year) = name_str.parse::<i32>() {
                    // Verify if any blob exists in this year?
                    // Optimization: Just assume it exists or fetch all blobs to check.
                    // Fetched blobs are needed for hierarchy anyway.
                    if let Ok(blobs) = client.list_blobs(&db, &coll) {
                        let has_year = blobs.iter().any(|b| {
                            if let Ok(uuid) = Uuid::parse_str(&b._key) {
                                if let Some(ts) = uuid.get_timestamp() {
                                    let (secs, _) = ts.to_unix();
                                    let dt = DateTime::<Utc>::from(
                                        UNIX_EPOCH + Duration::from_secs(secs),
                                    );
                                    return dt.year() == year;
                                }
                            }
                            false
                        });
                        if has_year {
                            drop(client);
                            let ino = self.allocate_inode(InodeType::Year(db, coll, year));
                            reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                            return;
                        }
                    }
                }
            }
            InodeType::Year(db, coll, year) => {
                if let Ok(month) = name_str.parse::<u32>() {
                    // Check month
                    if let Ok(blobs) = client.list_blobs(&db, &coll) {
                        let has_month = blobs.iter().any(|b| {
                            if let Ok(uuid) = Uuid::parse_str(&b._key) {
                                if let Some(ts) = uuid.get_timestamp() {
                                    let (secs, _) = ts.to_unix();
                                    let dt = DateTime::<Utc>::from(
                                        UNIX_EPOCH + Duration::from_secs(secs),
                                    );
                                    return dt.year() == year && dt.month() == month;
                                }
                            }
                            false
                        });
                        if has_month {
                            drop(client);
                            let ino = self.allocate_inode(InodeType::Month(db, coll, year, month));
                            reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                            return;
                        }
                    }
                }
            }
            InodeType::Month(db, coll, year, month) => {
                if let Ok(day) = name_str.parse::<u32>() {
                    // Check day
                    if let Ok(blobs) = client.list_blobs(&db, &coll) {
                        let has_day = blobs.iter().any(|b| {
                            if let Ok(uuid) = Uuid::parse_str(&b._key) {
                                if let Some(ts) = uuid.get_timestamp() {
                                    let (secs, _) = ts.to_unix();
                                    let dt = DateTime::<Utc>::from(
                                        UNIX_EPOCH + Duration::from_secs(secs),
                                    );
                                    return dt.year() == year
                                        && dt.month() == month
                                        && dt.day() == day;
                                }
                            }
                            false
                        });
                        if has_day {
                            drop(client);
                            let ino =
                                self.allocate_inode(InodeType::Day(db, coll, year, month, day));
                            reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                            return;
                        }
                    }
                }
            }
            InodeType::Day(db, coll, year, month, day) => {
                // Find file
                if let Ok(blobs) = client.list_blobs(&db, &coll) {
                    if let Some(blob) = blobs.into_iter().find(|b| {
                        let target_name = b.filename.as_deref().unwrap_or(&b._key);
                        if target_name != name_str {
                            return false;
                        }

                        if let Ok(uuid) = Uuid::parse_str(&b._key) {
                            if let Some(ts) = uuid.get_timestamp() {
                                let (secs, _) = ts.to_unix();
                                let dt =
                                    DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(secs));
                                return dt.year() == year && dt.month() == month && dt.day() == day;
                            }
                        }
                        false
                    }) {
                        drop(client);
                        let ino = self.allocate_inode(InodeType::Blob(db, coll, blob));
                        reply.entry(&TTL, &self.get_file_attr(ino).unwrap(), 0);
                        return;
                    }
                }
            }
            _ => {}
        }

        reply.error(ENOENT);
    }

    fn getattr(&mut self, _req: &Request, ino: u64, _fh: Option<u64>, reply: ReplyAttr) {
        if let Some(attr) = self.get_file_attr(ino) {
            reply.attr(&TTL, &attr);
        } else {
            reply.error(ENOENT);
        }
    }

    fn readdir(
        &mut self,
        _req: &Request,
        ino: u64,
        _fh: u64,
        offset: i64,
        mut reply: ReplyDirectory,
    ) {
        let parent_kind = match self.get_inode(ino) {
            Some(k) => k.clone(),
            None => {
                reply.error(ENOENT);
                return;
            }
        };

        if offset == 0 {
            if reply.add(ino, 0, FileType::Directory, ".") {
                return;
            }
            if reply.add(ino, 1, FileType::Directory, "..") {
                return;
            }
        }

        let client_arc = self.client.clone();
        let mut client = client_arc.lock().unwrap();
        let mut entries = Vec::new();

        // 0 and 1 are already sent.
        // We accumulate entries then skip based on offset (simplification)
        // Offset logic in FUSE is tricky. We'll use index + 2 as offset.

        // debug!("FUSE: readdir called on inode {}", ino);

        match parent_kind {
            InodeType::Root => {
                match client.list_databases() {
                    Ok(dbs) => {
                        // debug!("FUSE: Found databases: {:?}", dbs);
                        info!("Listing databases: {:?}", dbs);
                        for db in dbs {
                            let child_ino = self.allocate_inode(InodeType::Database(db.clone()));
                            entries.push((child_ino, FileType::Directory, db));
                        }
                    }
                    Err(e) => error!("Failed to list databases: {:?}", e),
                }
            }
            InodeType::Database(db) => match client.list_collections(&db) {
                Ok(colls) => {
                    debug!("Listing collections for {}: {:?}", db, colls);
                    for coll in colls {
                        let child_ino =
                            self.allocate_inode(InodeType::Collection(db.clone(), coll.clone()));
                        entries.push((child_ino, FileType::Directory, coll));
                    }
                }
                Err(e) => error!("Failed to list collections for {}: {:?}", db, e),
            },
            InodeType::Collection(db, coll) => {
                if let Ok(blobs) = client.list_blobs(&db, &coll) {
                    let mut years = HashSet::new();
                    for b in blobs {
                        if let Ok(uuid) = Uuid::parse_str(&b._key) {
                            if let Some(ts) = uuid.get_timestamp() {
                                let (secs, _) = ts.to_unix();
                                let dt =
                                    DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(secs));
                                years.insert(dt.year());
                            }
                        }
                    }
                    let mut sorted_years: Vec<_> = years.into_iter().collect();
                    sorted_years.sort();
                    for year in sorted_years {
                        let child_ino =
                            self.allocate_inode(InodeType::Year(db.clone(), coll.clone(), year));
                        entries.push((child_ino, FileType::Directory, year.to_string()));
                    }
                }
            }
            InodeType::Year(db, coll, year) => {
                if let Ok(blobs) = client.list_blobs(&db, &coll) {
                    let mut months = HashSet::new();
                    for b in blobs {
                        if let Ok(uuid) = Uuid::parse_str(&b._key) {
                            if let Some(ts) = uuid.get_timestamp() {
                                let (secs, _) = ts.to_unix();
                                let dt =
                                    DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(secs));
                                if dt.year() == year {
                                    months.insert(dt.month());
                                }
                            }
                        }
                    }
                    let mut sorted_months: Vec<_> = months.into_iter().collect();
                    sorted_months.sort();
                    for month in sorted_months {
                        let child_ino = self.allocate_inode(InodeType::Month(
                            db.clone(),
                            coll.clone(),
                            year,
                            month,
                        ));
                        // Pad month 01, 02...
                        entries.push((child_ino, FileType::Directory, format!("{:02}", month)));
                    }
                }
            }
            InodeType::Month(db, coll, year, month) => {
                if let Ok(blobs) = client.list_blobs(&db, &coll) {
                    let mut days = HashSet::new();
                    for b in blobs {
                        if let Ok(uuid) = Uuid::parse_str(&b._key) {
                            if let Some(ts) = uuid.get_timestamp() {
                                let (secs, _) = ts.to_unix();
                                let dt =
                                    DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(secs));
                                if dt.year() == year && dt.month() == month {
                                    days.insert(dt.day());
                                }
                            }
                        }
                    }
                    let mut sorted_days: Vec<_> = days.into_iter().collect();
                    sorted_days.sort();
                    for day in sorted_days {
                        let child_ino = self.allocate_inode(InodeType::Day(
                            db.clone(),
                            coll.clone(),
                            year,
                            month,
                            day,
                        ));
                        entries.push((child_ino, FileType::Directory, format!("{:02}", day)));
                    }
                }
            }
            InodeType::Day(db, coll, year, month, day) => {
                if let Ok(blobs) = client.list_blobs(&db, &coll) {
                    for b in blobs {
                        if let Ok(uuid) = Uuid::parse_str(&b._key) {
                            if let Some(ts) = uuid.get_timestamp() {
                                let (secs, _) = ts.to_unix();
                                let dt =
                                    DateTime::<Utc>::from(UNIX_EPOCH + Duration::from_secs(secs));
                                if dt.year() == year && dt.month() == month && dt.day() == day {
                                    let name = b.filename.clone().unwrap_or_else(|| b._key.clone());
                                    let child_ino = self.allocate_inode(InodeType::Blob(
                                        db.clone(),
                                        coll.clone(),
                                        b,
                                    ));
                                    entries.push((child_ino, FileType::RegularFile, name));
                                }
                            }
                        }
                    }
                }
            }
            _ => {}
        }

        // Apply offset
        let start_idx = if offset > 1 { (offset - 2) as usize } else { 0 };

        for (i, (ino, kind, name)) in entries.into_iter().enumerate().skip(start_idx) {
            if reply.add(ino, (i + 3) as i64, kind, name) {
                break;
            }
        }

        reply.ok();
    }

    fn open(&mut self, _req: &Request, _ino: u64, _flags: i32, reply: ReplyOpen) {
        // Read-only check? flags & O_ACCMODE == O_RDONLY
        // Minimal impl: allow everything, read() will fail if it's directory
        reply.opened(0, 0);
    }

    fn read(
        &mut self,
        _req: &Request,
        ino: u64,
        _fh: u64,
        offset: i64,
        size: u32,
        _flags: i32,
        _lock_owner: Option<u64>,
        reply: ReplyData,
    ) {
        let inode_kind = match self.get_inode(ino) {
            Some(k) => k.clone(),
            None => {
                reply.error(ENOENT);
                return;
            }
        };

        match inode_kind {
            InodeType::Blob(db, coll, meta) => {
                let client = self.client.lock().unwrap();
                match client.get_blob_content(&db, &coll, &meta._key) {
                    Ok(data) => {
                        let start = offset as usize;
                        if start >= data.len() {
                            reply.data(&[]);
                        } else {
                            let end = (start + size as usize).min(data.len());
                            reply.data(&data[start..end]);
                        }
                    }
                    Err(_) => reply.error(EIO),
                }
            }
            _ => reply.error(EIO), // Should contain EISDIR equivalent
        }
    }
}

fn main() -> anyhow::Result<()> {
    let args = Args::parse();

    #[cfg(unix)]
    if args.daemon {
        use solidb::daemon::Daemonize;
        use std::fs::File;
        use std::path::Path;
        use std::time::Duration;
        use sysinfo::{Pid, System};

        // Check if PID file exists and kill existing process
        if Path::new(&args.pid_file).exists() {
            match std::fs::read_to_string(&args.pid_file) {
                Ok(pid_str) => {
                    if let Ok(pid) = pid_str.trim().parse::<i32>() {
                        let mut sys = System::new_all();
                        sys.refresh_all();

                        let sys_pid = Pid::from(pid as usize);
                        if let Some(proc) = sys.process(sys_pid) {
                            let proc_name = proc.name().to_string_lossy();
                            if proc_name != "solidb-fuse" && proc_name != "solidb" {
                                eprintln!("SECURITY ERROR: Process with PID {} is named '{}', not 'solidb-fuse'. Refusing to kill potential mismatch.", pid, proc_name);
                                return Ok(());
                            }
                        }

                        eprintln!(
                            "Found existing FUSE process with PID {}. Stopping it...",
                            pid
                        );
                        unsafe {
                            libc::kill(pid, libc::SIGTERM);
                        }
                        // Give it a moment to cleanup mounts
                        std::thread::sleep(Duration::from_millis(500));
                        let _ = std::fs::remove_file(&args.pid_file);
                    }
                }
                Err(e) => eprintln!("Warning: Could not read PID file: {}", e),
            }
        }

        let stdout = File::create(&args.log_file)?;
        let stderr = File::create(&args.log_file)?;

        let daemonize = Daemonize::new()
            .pid_file(&args.pid_file)
            // Note: working directory not changed to avoid breaking relative mount paths
            .stdout(stdout)
            .stderr(stderr);

        match daemonize.start() {
            Ok(_) => {
                // We're now in the daemon process
            }
            Err(e) => {
                eprintln!("Error starting daemon: {}", e);
                std::process::exit(1);
            }
        }
    }

    #[cfg(not(unix))]
    if args.daemon {
        eprintln!("Daemon mode is only supported on Unix systems");
        std::process::exit(1);
    }

    tracing_subscriber::fmt()
        .with_env_filter(
            tracing_subscriber::EnvFilter::from_default_env()
                .add_directive(tracing::Level::INFO.into()),
        )
        .init();

    let mountpoint = args.mount;

    // Check mountpoint
    if !std::path::Path::new(&mountpoint).exists() {
        // error!("Mount point '{}' does not exist. Attempting to create it...", mountpoint);
        std::fs::create_dir_all(&mountpoint)?;
    }

    // Options
    let options = vec![MountOption::RO, MountOption::FSName("solidb".to_string())];
    #[cfg(target_os = "macos")]
    let mut options = options;
    #[cfg(target_os = "macos")]
    options.push(MountOption::AutoUnmount);

    println!("Attempting to mount at {}", mountpoint);
    let client = SolidBClient::new(
        &args.host,
        args.port,
        &args.username,
        args.password.as_deref(),
    );
    let fs = SolidBFS::new(client);

    // We always use spawn_mount2 (background thread) and keep the main thread
    // alive with Tokio to handle signals and explicit unmounting.

    let runtime = tokio::runtime::Runtime::new()?;
    runtime.block_on(async {
        // Spawn the filesystem in a background thread
        let session = fuser::spawn_mount2(fs, &mountpoint, &options)?;

        info!("Mount session started successfully.");
        println!("Filesystem mounted at {}.", mountpoint);
        println!("Press Ctrl+C to unmount.");

        // Wait for Ctrl+C (or SIGTERM if daemon)
        let ctrl_c = tokio::signal::ctrl_c();
        let sig_term = async {
            #[cfg(unix)]
            {
                let mut stream =
                    tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
                        .unwrap();
                stream.recv().await;
            }
            #[cfg(not(unix))]
            std::future::pending::<()>().await;
        };

        tokio::select! {
            _ = ctrl_c => { println!("\nReceived Ctrl+C"); },
            _ = sig_term => { println!("\nReceived SIGTERM"); },
        }

        println!("Unmounting {}...", mountpoint);
        info!("Unmounting {}...", mountpoint);

        // Explicitly run system unmount command to be sure
        #[cfg(target_os = "linux")]
        {
            let status = std::process::Command::new("fusermount")
                .arg("-u")
                .arg(&mountpoint)
                .status();

            match status {
                Ok(s) if s.success() => info!("'fusermount -u' executed successfully."),
                Ok(s) => error!("'fusermount -u' failed with exit code: {:?}", s.code()),
                Err(e) => error!("Failed to execute 'fusermount -u': {}", e),
            }
        }

        #[cfg(target_os = "macos")]
        {
            let status = std::process::Command::new("umount")
                .arg(&mountpoint)
                .status();

            match status {
                Ok(s) if s.success() => info!("'umount' executed successfully."),
                Ok(s) => error!("'umount' failed with exit code: {:?}", s.code()),
                Err(e) => error!("Failed to execute 'umount': {}", e),
            }
        }

        // Dropping the session also triggers unmount (if not already done)
        drop(session);

        // Give it a moment to clean up
        tokio::time::sleep(Duration::from_millis(500)).await;

        Ok::<(), anyhow::Error>(())
    })?;

    println!("Exiting.");
    Ok(())
}