communitas-core 0.1.22

Core business logic for Communitas - PQC collaboration with virtual disks
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
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
// Copyright (c) 2025 Saorsa Labs Limited
//
// This file is part of the Saorsa P2P network.
//
// Licensed under the AGPL-3.0 license:
// <https://www.gnu.org/licenses/agpl-3.0.html>

//! Entity Disk Service - Per-entity virtual disk management
//!
//! This module provides virtual disk functionality for entities (users, organizations,
//! groups, channels, projects). Each entity has three disk types:
//!
//! - **Private**: Encrypted, local-only storage (owner access only)
//! - **Public**: Content-addressed, distributed storage (world-readable)
//! - **Shared**: Group-accessible with shared encryption (members only)

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
use tokio::sync::RwLock;
use tracing::{debug, info, warn};

/// Type of virtual disk
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DiskType {
    /// Private: Encrypted, local-only storage (owner access only)
    Private,
    /// Public: Content-addressed, distributed storage (world-readable)
    Public,
    /// Shared: Group-accessible with shared encryption (members only)
    Shared,
}

impl DiskType {
    /// Get the directory name for this disk type
    pub fn as_dir_name(&self) -> &'static str {
        match self {
            DiskType::Private => "private",
            DiskType::Public => "public",
            DiskType::Shared => "shared",
        }
    }
}

impl std::fmt::Display for DiskType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            DiskType::Private => write!(f, "private"),
            DiskType::Public => write!(f, "public"),
            DiskType::Shared => write!(f, "shared"),
        }
    }
}

/// Information about a file or directory in a virtual disk
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileInfo {
    /// Full path within the disk (e.g., "/docs/readme.md")
    pub path: String,
    /// File or directory name
    pub name: String,
    /// True if this is a directory
    pub is_directory: bool,
    /// Size in bytes (0 for directories)
    pub size_bytes: u64,
    /// Last modified timestamp (Unix epoch seconds)
    pub modified_at: i64,
    /// BLAKE3 hash of contents (empty for directories)
    pub content_hash: String,
}

/// Storage statistics for a virtual disk
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiskStats {
    /// Entity ID this disk belongs to
    pub entity_id: String,
    /// Type of disk
    pub disk_type: DiskType,
    /// Total bytes used
    pub used_bytes: u64,
    /// Total number of files
    pub file_count: u32,
    /// Total number of directories
    pub dir_count: u32,
    /// Last modification timestamp
    pub last_modified: i64,
}

/// Internal metadata for tracking files
#[derive(Debug, Clone, Serialize, Deserialize)]
struct DiskFileMetadata {
    pub entity_id: String,
    pub disk_type: DiskType,
    pub path: String,
    pub name: String,
    pub is_directory: bool,
    pub size_bytes: u64,
    pub modified_at: i64,
    pub content_hash: String,
    pub file_path: PathBuf, // Actual filesystem path
}

/// Entity Disk Service - manages per-entity virtual disks
#[derive(Debug)]
pub struct EntityDiskService {
    /// Root directory for all disk storage
    root: PathBuf,
    /// Metadata index: (entity_id, disk_type, path) -> metadata
    index: RwLock<HashMap<String, DiskFileMetadata>>,
}

impl EntityDiskService {
    /// Create a new EntityDiskService with the given root directory
    pub async fn new<P: AsRef<Path>>(root: P) -> Result<Self> {
        let root = root.as_ref().to_path_buf();

        // Create root directory if it doesn't exist
        tokio::fs::create_dir_all(&root)
            .await
            .with_context(|| format!("Failed to create disk root: {}", root.display()))?;

        let service = Self {
            root,
            index: RwLock::new(HashMap::new()),
        };

        // Load existing metadata
        service.load_index().await?;

        info!(
            "EntityDiskService initialized at {}",
            service.root.display()
        );
        Ok(service)
    }

    /// Get the filesystem path for an entity's disk directory
    fn get_entity_disk_path(&self, entity_id: &str, disk_type: DiskType) -> PathBuf {
        self.root
            .join("entities")
            .join(entity_id)
            .join(disk_type.as_dir_name())
    }

    /// Get the filesystem path for a file within an entity's disk
    fn get_file_path(&self, entity_id: &str, disk_type: DiskType, path: &str) -> PathBuf {
        let disk_path = self.get_entity_disk_path(entity_id, disk_type);
        // Normalize path: remove leading slash and any .. components
        let clean_path = path.trim_start_matches('/').replace("..", "");
        disk_path.join(clean_path)
    }

    /// Generate the index key for a file
    fn index_key(entity_id: &str, disk_type: DiskType, path: &str) -> String {
        format!("{}:{}:{}", entity_id, disk_type, path)
    }

    /// Write a file to an entity's virtual disk
    pub async fn write_file(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
        data: &[u8],
    ) -> Result<FileInfo> {
        // Validate path
        if path.is_empty() || path == "/" {
            bail!("Invalid file path: cannot write to root");
        }

        let file_path = self.get_file_path(entity_id, disk_type, path);

        // Create parent directories
        if let Some(parent) = file_path.parent() {
            tokio::fs::create_dir_all(parent).await.with_context(|| {
                format!(
                    "Failed to create parent directories for {}",
                    file_path.display()
                )
            })?;
        }

        // Write the file
        tokio::fs::write(&file_path, data)
            .await
            .with_context(|| format!("Failed to write file: {}", file_path.display()))?;

        // Calculate hash
        let content_hash = blake3::hash(data).to_string();

        // Get file name
        let name = Path::new(path)
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| path.to_string());

        let now = chrono::Utc::now().timestamp();

        // Create metadata
        let metadata = DiskFileMetadata {
            entity_id: entity_id.to_string(),
            disk_type,
            path: path.to_string(),
            name: name.clone(),
            is_directory: false,
            size_bytes: data.len() as u64,
            modified_at: now,
            content_hash: content_hash.clone(),
            file_path: file_path.clone(),
        };

        // Update index
        {
            let key = Self::index_key(entity_id, disk_type, path);
            let mut index = self.index.write().await;
            index.insert(key, metadata);
        }

        // Persist index
        self.save_index().await?;

        debug!(
            "Wrote file {}:{}{} ({} bytes)",
            entity_id,
            disk_type,
            path,
            data.len()
        );

        Ok(FileInfo {
            path: path.to_string(),
            name,
            is_directory: false,
            size_bytes: data.len() as u64,
            modified_at: now,
            content_hash,
        })
    }

    /// Read a file from an entity's virtual disk
    pub async fn read_file(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
    ) -> Result<Vec<u8>> {
        let key = Self::index_key(entity_id, disk_type, path);

        // Get metadata
        let metadata = {
            let index = self.index.read().await;
            index.get(&key).cloned()
        };

        let file_path = match metadata {
            Some(meta) => {
                if meta.is_directory {
                    bail!("Cannot read directory as file: {}", path);
                }
                meta.file_path
            }
            None => {
                // Try direct path lookup
                let fp = self.get_file_path(entity_id, disk_type, path);
                if !fp.exists() {
                    bail!("File not found: {}:{}{}", entity_id, disk_type, path);
                }
                fp
            }
        };

        // Read the file
        let data = tokio::fs::read(&file_path)
            .await
            .with_context(|| format!("Failed to read file: {}", file_path.display()))?;

        debug!(
            "Read file {}:{}{} ({} bytes)",
            entity_id,
            disk_type,
            path,
            data.len()
        );

        Ok(data)
    }

    /// List files in a directory within an entity's virtual disk
    pub async fn list_files(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
    ) -> Result<Vec<FileInfo>> {
        let dir_path = if path.is_empty() || path == "/" {
            self.get_entity_disk_path(entity_id, disk_type)
        } else {
            self.get_file_path(entity_id, disk_type, path)
        };

        // Create directory if it doesn't exist
        if !dir_path.exists() {
            tokio::fs::create_dir_all(&dir_path)
                .await
                .with_context(|| format!("Failed to create directory: {}", dir_path.display()))?;
        }

        if !dir_path.is_dir() {
            bail!("Path is not a directory: {}", path);
        }

        let mut entries = Vec::new();
        let mut read_dir = tokio::fs::read_dir(&dir_path)
            .await
            .with_context(|| format!("Failed to read directory: {}", dir_path.display()))?;

        while let Some(entry) = read_dir.next_entry().await? {
            let entry_path = entry.path();
            let metadata = entry.metadata().await?;

            let name = entry.file_name().to_string_lossy().to_string();

            // Skip hidden files and index files
            if name.starts_with('.') || name == "disk_index.json" {
                continue;
            }

            let entry_relative_path = if path.is_empty() || path == "/" {
                format!("/{}", name)
            } else {
                format!("{}/{}", path.trim_end_matches('/'), name)
            };

            let (content_hash, size_bytes) = if metadata.is_file() {
                let data = tokio::fs::read(&entry_path).await?;
                (blake3::hash(&data).to_string(), data.len() as u64)
            } else {
                (String::new(), 0)
            };

            let modified_at = metadata
                .modified()
                .ok()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| d.as_secs() as i64)
                .unwrap_or(0);

            entries.push(FileInfo {
                path: entry_relative_path,
                name,
                is_directory: metadata.is_dir(),
                size_bytes,
                modified_at,
                content_hash,
            });
        }

        // Sort by name
        entries.sort_by(|a, b| a.name.cmp(&b.name));

        debug!(
            "Listed {}:{}{} - {} entries",
            entity_id,
            disk_type,
            path,
            entries.len()
        );

        Ok(entries)
    }

    /// Delete a file from an entity's virtual disk
    pub async fn delete_file(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
    ) -> Result<()> {
        if path.is_empty() || path == "/" {
            bail!("Cannot delete root directory");
        }

        let file_path = self.get_file_path(entity_id, disk_type, path);

        if !file_path.exists() {
            bail!("File not found: {}:{}{}", entity_id, disk_type, path);
        }

        if file_path.is_dir() {
            tokio::fs::remove_dir_all(&file_path)
                .await
                .with_context(|| format!("Failed to delete directory: {}", file_path.display()))?;
        } else {
            tokio::fs::remove_file(&file_path)
                .await
                .with_context(|| format!("Failed to delete file: {}", file_path.display()))?;
        }

        // Remove from index
        {
            let key = Self::index_key(entity_id, disk_type, path);
            let mut index = self.index.write().await;
            index.remove(&key);
        }

        // Persist index
        self.save_index().await?;

        debug!("Deleted {}:{}{}", entity_id, disk_type, path);

        Ok(())
    }

    /// Get storage statistics for an entity's disk
    pub async fn get_stats(&self, entity_id: &str, disk_type: DiskType) -> Result<DiskStats> {
        let disk_path = self.get_entity_disk_path(entity_id, disk_type);

        let mut used_bytes: u64 = 0;
        let mut file_count: u32 = 0;
        let mut dir_count: u32 = 0;
        let mut last_modified: i64 = 0;

        if disk_path.exists() {
            self.calculate_stats_recursive(
                &disk_path,
                &mut used_bytes,
                &mut file_count,
                &mut dir_count,
                &mut last_modified,
            )
            .await?;
        }

        Ok(DiskStats {
            entity_id: entity_id.to_string(),
            disk_type,
            used_bytes,
            file_count,
            dir_count,
            last_modified,
        })
    }

    /// Recursively calculate storage statistics
    async fn calculate_stats_recursive(
        &self,
        path: &Path,
        used_bytes: &mut u64,
        file_count: &mut u32,
        dir_count: &mut u32,
        last_modified: &mut i64,
    ) -> Result<()> {
        let mut read_dir = tokio::fs::read_dir(path).await?;

        while let Some(entry) = read_dir.next_entry().await? {
            let entry_path = entry.path();
            let metadata = entry.metadata().await?;

            // Skip hidden files
            let name = entry.file_name().to_string_lossy().to_string();
            if name.starts_with('.') || name == "disk_index.json" {
                continue;
            }

            let modified = metadata
                .modified()
                .ok()
                .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
                .map(|d| d.as_secs() as i64)
                .unwrap_or(0);

            if modified > *last_modified {
                *last_modified = modified;
            }

            if metadata.is_dir() {
                *dir_count += 1;
                Box::pin(self.calculate_stats_recursive(
                    &entry_path,
                    used_bytes,
                    file_count,
                    dir_count,
                    last_modified,
                ))
                .await?;
            } else {
                *file_count += 1;
                *used_bytes += metadata.len();
            }
        }

        Ok(())
    }

    /// Create a directory in an entity's virtual disk
    pub async fn create_directory(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
    ) -> Result<FileInfo> {
        if path.is_empty() || path == "/" {
            bail!("Cannot create root directory");
        }

        let dir_path = self.get_file_path(entity_id, disk_type, path);

        tokio::fs::create_dir_all(&dir_path)
            .await
            .with_context(|| format!("Failed to create directory: {}", dir_path.display()))?;

        let name = Path::new(path)
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| path.to_string());

        let now = chrono::Utc::now().timestamp();

        // Create metadata
        let metadata = DiskFileMetadata {
            entity_id: entity_id.to_string(),
            disk_type,
            path: path.to_string(),
            name: name.clone(),
            is_directory: true,
            size_bytes: 0,
            modified_at: now,
            content_hash: String::new(),
            file_path: dir_path,
        };

        // Update index
        {
            let key = Self::index_key(entity_id, disk_type, path);
            let mut index = self.index.write().await;
            index.insert(key, metadata);
        }

        // Persist index
        self.save_index().await?;

        debug!("Created directory {}:{}{}", entity_id, disk_type, path);

        Ok(FileInfo {
            path: path.to_string(),
            name,
            is_directory: true,
            size_bytes: 0,
            modified_at: now,
            content_hash: String::new(),
        })
    }

    /// Check if a file exists
    pub async fn file_exists(&self, entity_id: &str, disk_type: DiskType, path: &str) -> bool {
        let file_path = self.get_file_path(entity_id, disk_type, path);
        file_path.exists()
    }

    /// Get file info without reading the contents
    pub async fn get_file_info(
        &self,
        entity_id: &str,
        disk_type: DiskType,
        path: &str,
    ) -> Result<FileInfo> {
        let file_path = self.get_file_path(entity_id, disk_type, path);

        if !file_path.exists() {
            bail!("File not found: {}:{}{}", entity_id, disk_type, path);
        }

        let metadata = tokio::fs::metadata(&file_path).await?;
        let is_directory = metadata.is_dir();

        let name = Path::new(path)
            .file_name()
            .map(|s| s.to_string_lossy().to_string())
            .unwrap_or_else(|| path.to_string());

        let modified_at = metadata
            .modified()
            .ok()
            .and_then(|t| t.duration_since(std::time::UNIX_EPOCH).ok())
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);

        let (size_bytes, content_hash) = if is_directory {
            (0, String::new())
        } else {
            let data = tokio::fs::read(&file_path).await?;
            (data.len() as u64, blake3::hash(&data).to_string())
        };

        Ok(FileInfo {
            path: path.to_string(),
            name,
            is_directory,
            size_bytes,
            modified_at,
            content_hash,
        })
    }

    /// Load the metadata index from disk
    async fn load_index(&self) -> Result<()> {
        let index_path = self.root.join("disk_index.json");

        if !index_path.exists() {
            debug!("No existing disk index found, starting fresh");
            return Ok(());
        }

        match tokio::fs::read_to_string(&index_path).await {
            Ok(data) => match serde_json::from_str::<HashMap<String, DiskFileMetadata>>(&data) {
                Ok(stored_index) => {
                    let count = stored_index.len();
                    let mut index = self.index.write().await;
                    *index = stored_index;
                    info!("Loaded {} entries from disk index", count);
                }
                Err(e) => {
                    warn!("Failed to parse disk index, starting fresh: {}", e);
                }
            },
            Err(e) => {
                warn!("Failed to read disk index, starting fresh: {}", e);
            }
        }

        Ok(())
    }

    /// Save the metadata index to disk
    async fn save_index(&self) -> Result<()> {
        let index_path = self.root.join("disk_index.json");
        let temp_path = self.root.join(".disk_index.tmp");

        let data = {
            let index = self.index.read().await;
            serde_json::to_string_pretty(&*index).context("Failed to serialize disk index")?
        };

        // Write to temp file first
        tokio::fs::write(&temp_path, &data)
            .await
            .context("Failed to write temp index file")?;

        // Atomically move to final location
        tokio::fs::rename(&temp_path, &index_path)
            .await
            .context("Failed to move index file")?;

        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::tempdir;

    #[tokio::test]
    async fn test_write_and_read_file() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";
        let data = b"Hello, World!";

        // Write file
        let info = service
            .write_file(entity_id, DiskType::Private, "/docs/test.txt", data)
            .await
            .unwrap();

        assert_eq!(info.name, "test.txt");
        assert_eq!(info.size_bytes, 13);
        assert!(!info.is_directory);

        // Read file
        let read_data = service
            .read_file(entity_id, DiskType::Private, "/docs/test.txt")
            .await
            .unwrap();

        assert_eq!(read_data, data);
    }

    #[tokio::test]
    async fn test_list_files() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";

        // Write some files
        service
            .write_file(entity_id, DiskType::Public, "/file1.txt", b"one")
            .await
            .unwrap();
        service
            .write_file(entity_id, DiskType::Public, "/file2.txt", b"two")
            .await
            .unwrap();

        // List files
        let files = service
            .list_files(entity_id, DiskType::Public, "/")
            .await
            .unwrap();

        assert_eq!(files.len(), 2);
    }

    #[tokio::test]
    async fn test_delete_file() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";

        // Write file
        service
            .write_file(entity_id, DiskType::Shared, "/to_delete.txt", b"delete me")
            .await
            .unwrap();

        // Verify it exists
        assert!(
            service
                .file_exists(entity_id, DiskType::Shared, "/to_delete.txt")
                .await
        );

        // Delete it
        service
            .delete_file(entity_id, DiskType::Shared, "/to_delete.txt")
            .await
            .unwrap();

        // Verify it's gone
        assert!(
            !service
                .file_exists(entity_id, DiskType::Shared, "/to_delete.txt")
                .await
        );
    }

    #[tokio::test]
    async fn test_get_stats() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";

        // Write some files
        service
            .write_file(entity_id, DiskType::Private, "/file1.txt", b"hello")
            .await
            .unwrap();
        service
            .write_file(entity_id, DiskType::Private, "/dir/file2.txt", b"world")
            .await
            .unwrap();

        // Get stats
        let stats = service
            .get_stats(entity_id, DiskType::Private)
            .await
            .unwrap();

        assert_eq!(stats.file_count, 2);
        assert_eq!(stats.used_bytes, 10); // "hello" + "world"
        assert_eq!(stats.dir_count, 1); // "dir"
    }

    #[tokio::test]
    async fn test_create_directory() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";

        // Create directory
        let info = service
            .create_directory(entity_id, DiskType::Public, "/my-folder")
            .await
            .unwrap();

        assert_eq!(info.name, "my-folder");
        assert!(info.is_directory);

        // Verify it exists
        assert!(
            service
                .file_exists(entity_id, DiskType::Public, "/my-folder")
                .await
        );
    }

    #[tokio::test]
    async fn test_disk_types() {
        let temp = tempdir().unwrap();
        let service = EntityDiskService::new(temp.path()).await.unwrap();

        let entity_id = "test-entity-one-two";

        // Write to each disk type
        service
            .write_file(entity_id, DiskType::Private, "/private.txt", b"private")
            .await
            .unwrap();
        service
            .write_file(entity_id, DiskType::Public, "/public.txt", b"public")
            .await
            .unwrap();
        service
            .write_file(entity_id, DiskType::Shared, "/shared.txt", b"shared")
            .await
            .unwrap();

        // Verify isolation
        assert!(
            service
                .file_exists(entity_id, DiskType::Private, "/private.txt")
                .await
        );
        assert!(
            !service
                .file_exists(entity_id, DiskType::Public, "/private.txt")
                .await
        );
        assert!(
            !service
                .file_exists(entity_id, DiskType::Shared, "/private.txt")
                .await
        );
    }
}