opfs 0.2.0

A Rust implementation of the Origin Private File System browser API.
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
use futures::Stream;
use std::sync::Arc;
use std::{io::SeekFrom, path::PathBuf};
use tokio::io::{AsyncSeekExt, AsyncWriteExt};
use tokio::sync::RwLock;

type DirectoryEntry = crate::DirectoryEntry<DirectoryHandle, FileHandle>;

#[derive(Clone, Debug)]
pub struct DirectoryHandle(PathBuf);

#[derive(Clone, Debug)]
pub struct FileHandle(PathBuf);

#[derive(Clone, Debug)]
pub struct WritableFileStream(Arc<RwLock<tokio::fs::File>>);

impl From<PathBuf> for DirectoryHandle {
    fn from(handle: PathBuf) -> Self {
        Self(handle)
    }
}

impl From<PathBuf> for FileHandle {
    fn from(handle: PathBuf) -> Self {
        Self(handle)
    }
}

impl From<tokio::fs::File> for WritableFileStream {
    fn from(handle: tokio::fs::File) -> Self {
        Self(Arc::new(RwLock::new(handle)))
    }
}

impl crate::private::Sealed for DirectoryHandle {}
impl crate::private::Sealed for FileHandle {}
impl crate::private::Sealed for WritableFileStream {}

impl crate::DirectoryHandle for DirectoryHandle {
    type Error = std::io::Error;
    type FileHandleT = FileHandle;

    async fn get_file_handle_with_options(
        &self,
        name: &str,
        options: &crate::GetFileHandleOptions,
    ) -> Result<Self::FileHandleT, Self::Error> {
        let mut path = self.0.clone();
        path.push(name);

        // Make sure the file exists
        let _ = tokio::fs::OpenOptions::new()
            .read(true)
            .write(true)
            .create(options.create)
            .open(&path)
            .await?;

        Ok(FileHandle(path))
    }

    async fn get_directory_handle_with_options(
        &self,
        name: &str,
        options: &crate::GetDirectoryHandleOptions,
    ) -> Result<Self, Self::Error> {
        let mut path = self.0.clone();
        path.push(name);

        if options.create {
            tokio::fs::create_dir_all(&path).await?;
        } else if !path.exists() {
            return Err(std::io::Error::new(
                std::io::ErrorKind::NotFound,
                format!("Directory '{}' not found", name),
            ));
        }

        Ok(DirectoryHandle(path))
    }

    async fn remove_entry(&mut self, name: &str) -> Result<(), Self::Error> {
        let mut path = self.0.clone();
        path.push(name);

        let metadata = tokio::fs::metadata(&path).await?;
        if metadata.is_file() {
            tokio::fs::remove_file(&path).await?;
        } else if metadata.is_dir() {
            tokio::fs::remove_dir(&path).await?;
        }

        Ok(())
    }

    async fn remove_entry_with_options(
        &mut self,
        name: &str,
        options: &crate::FileSystemRemoveOptions,
    ) -> Result<(), Self::Error> {
        let mut path = self.0.clone();
        path.push(name);

        let metadata = tokio::fs::metadata(&path).await?;
        if metadata.is_file() {
            tokio::fs::remove_file(&path).await?;
        } else if metadata.is_dir() {
            if options.recursive {
                tokio::fs::remove_dir_all(&path).await?;
            } else {
                tokio::fs::remove_dir(&path).await?;
            }
        }

        Ok(())
    }

    async fn entries(
        &self,
    ) -> Result<impl Stream<Item = Result<(String, DirectoryEntry), Self::Error>>, Self::Error>
    {
        let mut entries = Vec::new();
        let mut read_dir = tokio::fs::read_dir(&self.0).await?;

        while let Some(entry) = read_dir.next_entry().await? {
            let name = entry.file_name().to_string_lossy().to_string();
            let metadata = entry.metadata().await?;

            let dir_entry = if metadata.is_file() {
                DirectoryEntry::File(FileHandle(entry.path()))
            } else if metadata.is_dir() {
                DirectoryEntry::Directory(DirectoryHandle(entry.path()))
            } else {
                continue; // Skip other types like symlinks
            };

            entries.push(Ok((name, dir_entry)));
        }

        Ok(futures::stream::iter(entries))
    }
}

impl crate::FileHandle for FileHandle {
    type Error = std::io::Error;
    type WritableFileStreamT = WritableFileStream;

    async fn create_writable_with_options(
        &mut self,
        options: &crate::CreateWritableOptions,
    ) -> Result<Self::WritableFileStreamT, Self::Error> {
        let file = tokio::fs::OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(!options.keep_existing_data)
            .open(&self.0)
            .await?;

        Ok(WritableFileStream(Arc::new(RwLock::new(file))))
    }

    async fn read(&self) -> Result<Vec<u8>, Self::Error> {
        use tokio::io::AsyncReadExt;

        let mut file = tokio::fs::File::open(&self.0).await?;
        let mut buffer = Vec::new();
        file.read_to_end(&mut buffer).await?;
        Ok(buffer)
    }

    async fn read_range<R: std::ops::RangeBounds<usize> + Send>(
        &self,
        range: R,
    ) -> Result<Vec<u8>, Self::Error> {
        use std::ops::Bound;
        use tokio::io::{AsyncReadExt, AsyncSeekExt};

        let mut file = tokio::fs::File::open(&self.0).await?;
        let file_size = file.metadata().await?.len() as usize;

        let start = match range.start_bound() {
            Bound::Included(&n) => n,
            Bound::Excluded(&n) => n + 1,
            Bound::Unbounded => 0,
        };

        let end = match range.end_bound() {
            Bound::Included(&n) => n + 1,
            Bound::Excluded(&n) => n,
            Bound::Unbounded => file_size,
        };

        if start >= file_size {
            return Ok(Vec::new());
        }

        let actual_end = end.min(file_size);
        let bytes_to_read = actual_end.saturating_sub(start);

        if bytes_to_read == 0 {
            return Ok(Vec::new());
        }

        file.seek(SeekFrom::Start(start as u64)).await?;
        let mut buffer = vec![0; bytes_to_read];
        file.read_exact(&mut buffer).await?;
        Ok(buffer)
    }

    async fn size(&self) -> Result<usize, Self::Error> {
        let metadata = tokio::fs::metadata(&self.0).await?;
        Ok(metadata.len() as usize)
    }
}

impl crate::WritableFileStream for WritableFileStream {
    type Error = std::io::Error;

    async fn write_at_cursor_pos(&mut self, data: &[u8]) -> Result<(), Self::Error> {
        let mut file = self.0.write().await;
        file.write_all(data).await?;
        Ok(())
    }

    async fn write_with_params(&mut self, params: &crate::WriteParams) -> Result<(), Self::Error> {
        use crate::WriteCommandType;

        let mut file = self.0.write().await;

        match params.command_type {
            WriteCommandType::Write => {
                if let Some(data) = &params.data {
                    if let Some(position) = params.position {
                        file.seek(SeekFrom::Start(position as u64)).await?;
                    }
                    file.write_all(data).await?;
                } else {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Write command requires data",
                    ));
                }
            }
            WriteCommandType::Seek => {
                if let Some(position) = params.position {
                    file.seek(SeekFrom::Start(position as u64)).await?;
                } else {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Seek command requires position",
                    ));
                }
            }
            WriteCommandType::Truncate => {
                if let Some(size) = params.size {
                    file.set_len(size as u64).await?;
                } else {
                    return Err(std::io::Error::new(
                        std::io::ErrorKind::InvalidInput,
                        "Truncate command requires size",
                    ));
                }
            }
        }
        Ok(())
    }

    async fn truncate(&mut self, size: usize) -> Result<(), Self::Error> {
        let file = self.0.write().await;
        file.set_len(size as u64).await?;
        Ok(())
    }

    async fn close(&mut self) -> Result<(), Self::Error> {
        let mut file = self.0.write().await;
        file.shutdown().await?;
        Ok(())
    }

    async fn seek(&mut self, offset: usize) -> Result<(), Self::Error> {
        let mut file = self.0.write().await;
        file.seek(SeekFrom::Start(offset as u64)).await?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        CreateWritableOptions, DirectoryHandle as _, FileHandle as _, GetFileHandleOptions,
        WritableFileStream as _,
    };
    use futures::StreamExt;
    use tempfile::TempDir;

    async fn setup_temp_dir() -> (TempDir, DirectoryHandle) {
        let temp_dir = TempDir::new().unwrap();
        let dir_handle = DirectoryHandle(temp_dir.path().to_path_buf());
        (temp_dir, dir_handle)
    }

    #[tokio::test]
    async fn test_create_and_read_file() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();

        let data = b"Hello, world!";
        writer.write_at_cursor_pos(data).await.unwrap();
        writer.close().await.unwrap();

        let read_data = file.read().await.unwrap();
        assert_eq!(read_data, data);
        assert_eq!(file.size().await.unwrap(), data.len());
    }

    #[tokio::test]
    async fn test_file_not_found() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: false };

        let result = dir
            .get_file_handle_with_options("nonexistent.txt", &options)
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_remove_entry() {
        let (_temp_dir, mut dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let _file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        dir.remove_entry("test.txt").await.unwrap();

        let result = dir
            .get_file_handle_with_options("test.txt", &GetFileHandleOptions { create: false })
            .await;
        assert!(result.is_err());
    }

    #[tokio::test]
    async fn test_entries_empty() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let entries_stream = dir.entries().await.unwrap();
        let entries: Vec<_> = entries_stream.collect().await;
        assert!(entries.is_empty());
    }

    #[tokio::test]
    async fn test_entries_with_files() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let _file1 = dir
            .get_file_handle_with_options("file1.txt", &options)
            .await
            .unwrap();
        let _file2 = dir
            .get_file_handle_with_options("file2.txt", &options)
            .await
            .unwrap();

        let entries_stream = dir.entries().await.unwrap();
        let entries: Vec<_> = entries_stream.collect().await;

        assert_eq!(entries.len(), 2);

        let mut names: Vec<_> = entries.into_iter().map(|r| r.unwrap().0).collect();
        names.sort();
        assert_eq!(names, vec!["file1.txt", "file2.txt"]);
    }

    #[tokio::test]
    async fn test_entries_with_subdirectory() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        // Create a file
        let _file = dir
            .get_file_handle_with_options("file.txt", &options)
            .await
            .unwrap();

        // Create a subdirectory
        let mut subdir_path = dir.0.clone();
        subdir_path.push("subdir");
        tokio::fs::create_dir(&subdir_path).await.unwrap();

        let entries_stream = dir.entries().await.unwrap();
        let entries: Vec<_> = entries_stream.collect().await;

        assert_eq!(entries.len(), 2);

        let mut items: Vec<_> = entries
            .into_iter()
            .map(|r| {
                let (name, entry) = r.unwrap();
                let is_dir = matches!(entry, DirectoryEntry::Directory(_));
                (name, is_dir)
            })
            .collect();
        items.sort_by(|a, b| a.0.cmp(&b.0));

        assert_eq!(items[0].0, "file.txt");
        assert!(!items[0].1); // is file
        assert_eq!(items[1].0, "subdir");
        assert!(items[1].1); // is directory
    }

    #[tokio::test]
    async fn test_seek_and_write() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();

        writer.write_at_cursor_pos(b"Hello").await.unwrap();
        writer.seek(0).await.unwrap();
        writer.write_at_cursor_pos(b"Hi").await.unwrap();
        writer.close().await.unwrap();

        let data = file.read().await.unwrap();
        assert_eq!(data, b"Hillo"); // "Hi" overwrites first 2 chars
    }

    #[tokio::test]
    async fn test_keep_existing_data() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        // Write initial data
        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();
        writer.write_at_cursor_pos(b"Hello").await.unwrap();
        writer.close().await.unwrap();

        // Write more data keeping existing
        let keep_options = CreateWritableOptions {
            keep_existing_data: true,
        };
        let mut writer2 = file
            .create_writable_with_options(&keep_options)
            .await
            .unwrap();
        writer2.write_at_cursor_pos(b" World").await.unwrap();
        writer2.close().await.unwrap();

        let data = file.read().await.unwrap();
        assert_eq!(data, b" World"); // Overwrites from beginning when keeping data
    }

    #[tokio::test]
    async fn test_truncate_existing_data() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        // Write initial data
        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();
        writer.write_at_cursor_pos(b"Hello World").await.unwrap();
        writer.close().await.unwrap();

        // Truncate and write new data
        let truncate_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer2 = file
            .create_writable_with_options(&truncate_options)
            .await
            .unwrap();
        writer2.write_at_cursor_pos(b"Hi").await.unwrap();
        writer2.close().await.unwrap();

        let data = file.read().await.unwrap();
        assert_eq!(data, b"Hi");
    }

    #[tokio::test]
    async fn test_read_range() {
        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();
        writer.write_at_cursor_pos(b"Hello, World!").await.unwrap();
        writer.close().await.unwrap();

        // Test various range types
        assert_eq!(file.read_range(0..5).await.unwrap(), b"Hello");
        assert_eq!(file.read_range(7..).await.unwrap(), b"World!");
        assert_eq!(file.read_range(2..9).await.unwrap(), b"llo, Wo");
        assert_eq!(file.read_range(100..).await.unwrap(), b"");
        assert_eq!(file.read_range(0..=4).await.unwrap(), b"Hello");
        assert_eq!(file.read_range(..).await.unwrap(), b"Hello, World!");
    }

    #[tokio::test]
    async fn test_truncate_and_write_params() {
        use crate::{WriteCommandType, WriteParams};

        let (_temp_dir, dir) = setup_temp_dir().await;
        let options = GetFileHandleOptions { create: true };

        let mut file = dir
            .get_file_handle_with_options("test.txt", &options)
            .await
            .unwrap();

        let write_options = CreateWritableOptions {
            keep_existing_data: false,
        };
        let mut writer = file
            .create_writable_with_options(&write_options)
            .await
            .unwrap();

        // Write initial data
        writer.write_at_cursor_pos(b"Hello, World!").await.unwrap();

        // Truncate using WriteParams
        let truncate_params = WriteParams {
            command_type: WriteCommandType::Truncate,
            data: None,
            position: None,
            size: Some(5),
        };
        writer.write_with_params(&truncate_params).await.unwrap();

        writer.close().await.unwrap();

        let data = file.read().await.unwrap();
        assert_eq!(data, b"Hello");
    }
}