Mercurial > public > mercurial-scm > hg-stable
annotate rust/hg-core/src/revlog/file_io.rs @ 52779:162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
We need to make `InnerRevlog` `Sync`, since we want to iterate on and read it
in parallel. An uncontended `RwLock` has no measurable overhead over a
`RefCell` in our contexts (to be verified in benchmarks, but I'm pretty sure).
This change assumes that writes/reads to/from the uncompressed chunk cache
can fail if contended, since it's just a cache, but the rest of operations
(which are on FileHandles) should wait for the lock and bubble up the panic
in case of lock poisoning. If a program tries to write from multiple threads
to a revlog, it had better have good reasons, so I'm not too worried.
author | Rapha?l Gom?s <rgomes@octobus.net> |
---|---|
date | Fri, 24 Jan 2025 12:05:23 -0500 |
parents | 645d247d4c75 |
children | 549b58b1ce72 |
rev | line source |
---|---|
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
1 //! Helpers for revlog file reading and writing. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
2 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
3 use std::{ |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
4 io::{Read, Seek, SeekFrom, Write}, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
5 path::{Path, PathBuf}, |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
6 sync::{Arc, Mutex, RwLock}, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
7 }; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
8 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
9 use crate::{ |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
10 errors::{HgError, IoResultExt}, |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
11 vfs::{Vfs, VfsFile}, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
12 }; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
13 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
14 /// Wraps accessing arbitrary chunks of data within a file and reusing handles. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
15 /// This is currently useful for accessing a revlog's data file, only reading |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
16 /// the ranges that are currently relevant, like a sort of basic and manual |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
17 /// file-based mmap. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
18 /// |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
19 /// XXX should this just be replaced with `mmap` + `madvise` ranges? |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
20 /// The upcoming `UncompressedChunkCache` will make up for most of the slowness |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
21 /// of re-reading the same chunks, so this might not be as useful. Aside from |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
22 /// the major benefit of having less code to take care of, using `mmap` will |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
23 /// allow multiple processes to share the same pages, especially for the |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
24 /// changelog and manifest, which would make a difference in server contexts. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
25 pub struct RandomAccessFile { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
26 /// The current store VFS to pass it to [`FileHandle`] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
27 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
28 /// Filename of the open file, relative to the vfs root |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
29 pub filename: PathBuf, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
30 /// The current read-only handle on the file, if any |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
31 pub reading_handle: RwLock<Option<FileHandle>>, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
32 /// The current read-write handle on the file, if any |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
33 pub writing_handle: RwLock<Option<FileHandle>>, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
34 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
35 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
36 impl RandomAccessFile { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
37 /// Wrap a file for random access |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
38 pub fn new(vfs: Box<dyn Vfs>, filename: PathBuf) -> Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
39 assert!(filename.is_relative()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
40 Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
41 vfs, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
42 filename, |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
43 reading_handle: RwLock::new(None), |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
44 writing_handle: RwLock::new(None), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
45 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
46 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
47 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
48 /// Read a chunk of bytes from the file. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
49 pub fn read_chunk( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
50 &self, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
51 offset: usize, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
52 length: usize, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
53 ) -> Result<Vec<u8>, HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
54 let mut handle = self.get_read_handle()?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
55 handle |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
56 .seek(SeekFrom::Start(offset as u64)) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
57 .when_reading_file(&self.filename)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
58 handle.read_exact(length).when_reading_file(&self.filename) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
59 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
60 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
61 /// `pub` only for hg-cpython |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
62 #[doc(hidden)] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
63 pub fn get_read_handle(&self) -> Result<FileHandle, HgError> { |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
64 if let Some(handle) = |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
65 &*self.writing_handle.write().expect("lock is poisoned") |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
66 { |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
67 // Use a file handle being actively used for writes, if available. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
68 // There is some danger to doing this because reads will seek the |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
69 // file. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
70 // However, [`Revlog::write_entry`] performs a `SeekFrom::End(0)` |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
71 // before all writes, so we should be safe. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
72 return Ok(handle.clone()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
73 } |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
74 if let Some(handle) = |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
75 &*self.reading_handle.read().expect("lock is poisoned") |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
76 { |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
77 return Ok(handle.clone()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
78 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
79 // early returns done to work around borrowck being overzealous |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
80 // See https://github.com/rust-lang/rust/issues/103108 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
81 let new_handle = FileHandle::new( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
82 dyn_clone::clone_box(&*self.vfs), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
83 &self.filename, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
84 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
85 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
86 )?; |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
87 *self.reading_handle.write().expect("lock is poisoned") = |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
88 Some(new_handle.clone()); |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
89 Ok(new_handle) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
90 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
91 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
92 /// `pub` only for hg-cpython |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
93 #[doc(hidden)] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
94 pub fn exit_reading_context(&self) { |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
95 self.reading_handle |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
96 .write() |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
97 .expect("lock is poisoned") |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
98 .take(); |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
99 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
100 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
101 // Returns whether this file currently open |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
102 pub fn is_open(&self) -> bool { |
52779
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
103 self.reading_handle |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
104 .read() |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
105 .expect("lock is poisoned") |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
106 .is_some() |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
107 || self |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
108 .writing_handle |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
109 .read() |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
110 .expect("lock is poisoned") |
162f4801ad39
rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52330
diff
changeset
|
111 .is_some() |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
112 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
113 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
114 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
115 /// A buffer that holds new changelog index data that needs to be written |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
116 /// after the manifest and filelogs so that the repo is updated atomically to |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
117 /// external processes. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
118 #[derive(Clone, Debug, Default)] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
119 pub struct DelayedBuffer { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
120 // The actual in-memory bytes storing the delayed writes |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
121 pub(super) buffer: Vec<u8>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
122 /// The current offset into the virtual file composed of file + buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
123 offset: u64, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
124 /// The size of the file at the time of opening |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
125 file_size: u64, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
126 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
127 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
128 impl DelayedBuffer { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
129 /// Returns the length of the full data (on-disk + buffer length). |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
130 pub fn len(&self) -> u64 { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
131 self.buffer.len() as u64 + self.file_size |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
132 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
133 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
134 pub fn is_empty(&self) -> bool { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
135 self.len() == 0 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
136 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
137 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
138 |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
139 /// Holds an open [`VfsFile`] and the related data. This can be used for |
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
140 /// reading and writing. Writes can be delayed to a buffer before touching |
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
141 /// the disk, if relevant (in the changelog case), but reads are transparent. |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
142 pub struct FileHandle { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
143 /// The actual open file |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
144 pub file: VfsFile, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
145 /// The VFS with which the file was opened |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
146 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
147 /// Filename of the open file, relative to the repo root |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
148 filename: PathBuf, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
149 /// Buffer of delayed entry writes to the changelog index. This points |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
150 /// back to the buffer inside the revlog this handle refers to. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
151 delayed_buffer: Option<Arc<Mutex<DelayedBuffer>>>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
152 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
153 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
154 impl std::fmt::Debug for FileHandle { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
155 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
156 f.debug_struct("FileHandle") |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
157 .field("filename", &self.filename) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
158 .field("delayed_buffer", &self.delayed_buffer) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
159 .field("file", &self.file) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
160 .finish() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
161 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
162 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
163 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
164 impl Clone for FileHandle { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
165 fn clone(&self) -> Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
166 Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
167 vfs: dyn_clone::clone_box(&*self.vfs), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
168 filename: self.filename.clone(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
169 delayed_buffer: self.delayed_buffer.clone(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
170 // This can only fail if the OS doesn't have the file handle |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
171 // anymore, so we're not going to do anything useful anyway. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
172 file: self.file.try_clone().expect("couldn't clone file handle"), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
173 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
174 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
175 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
176 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
177 impl FileHandle { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
178 /// Get a (read or write) file handle to `filename`. Only creates the file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
179 /// if `create` is `true`. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
180 pub fn new( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
181 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
182 filename: impl AsRef<Path>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
183 create: bool, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
184 write: bool, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
185 ) -> Result<Self, HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
186 let file = if create { |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
187 vfs.create(filename.as_ref(), false)? |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
188 } else if write { |
52330
645d247d4c75
rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52311
diff
changeset
|
189 vfs.open_write(filename.as_ref())? |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
190 } else { |
52330
645d247d4c75
rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52311
diff
changeset
|
191 vfs.open(filename.as_ref())? |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
192 }; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
193 Ok(Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
194 vfs, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
195 filename: filename.as_ref().to_owned(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
196 delayed_buffer: None, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
197 file, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
198 }) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
199 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
200 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
201 /// Get a file handle to `filename`, but writes go to a [`DelayedBuffer`]. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
202 pub fn new_delayed( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
203 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
204 filename: impl AsRef<Path>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
205 create: bool, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
206 delayed_buffer: Arc<Mutex<DelayedBuffer>>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
207 ) -> Result<Self, HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
208 let mut file = if create { |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
209 vfs.create(filename.as_ref(), false)? |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
210 } else { |
52330
645d247d4c75
rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52311
diff
changeset
|
211 vfs.open_write(filename.as_ref())? |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
212 }; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
213 let size = vfs.file_size(&file)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
214 let offset = file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
215 .stream_position() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
216 .when_reading_file(filename.as_ref())?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
217 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
218 { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
219 let mut buf = delayed_buffer.lock().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
220 buf.file_size = size; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
221 buf.offset = offset; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
222 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
223 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
224 Ok(Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
225 vfs, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
226 filename: filename.as_ref().to_owned(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
227 delayed_buffer: Some(delayed_buffer), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
228 file, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
229 }) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
230 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
231 |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
232 /// Wrap an existing [`VfsFile`] |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
233 pub fn from_file( |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
234 file: VfsFile, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
235 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
236 filename: impl AsRef<Path>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
237 ) -> Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
238 Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
239 vfs, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
240 filename: filename.as_ref().to_owned(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
241 delayed_buffer: None, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
242 file, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
243 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
244 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
245 |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
246 /// Wrap an existing [`VfsFile`], but writes go to a [`DelayedBuffer`]. |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
247 pub fn from_file_delayed( |
52311
8d35941689af
rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52297
diff
changeset
|
248 mut file: VfsFile, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
249 vfs: Box<dyn Vfs>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
250 filename: impl AsRef<Path>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
251 delayed_buffer: Arc<Mutex<DelayedBuffer>>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
252 ) -> Result<Self, HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
253 let size = vfs.file_size(&file)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
254 let offset = file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
255 .stream_position() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
256 .when_reading_file(filename.as_ref())?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
257 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
258 { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
259 let mut buf = delayed_buffer.lock().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
260 buf.file_size = size; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
261 buf.offset = offset; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
262 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
263 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
264 Ok(Self { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
265 vfs, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
266 filename: filename.as_ref().to_owned(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
267 delayed_buffer: Some(delayed_buffer), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
268 file, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
269 }) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
270 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
271 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
272 /// Move the position of the handle to `pos`, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
273 /// spanning the [`DelayedBuffer`] if defined. Will return an error if |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
274 /// an invalid seek position is asked, or for any standard io error. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
275 pub fn seek(&mut self, pos: SeekFrom) -> Result<u64, std::io::Error> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
276 if let Some(delay_buf) = &self.delayed_buffer { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
277 let mut delay_buf = delay_buf.lock().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
278 // Virtual file offset spans real file and data |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
279 match pos { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
280 SeekFrom::Start(offset) => delay_buf.offset = offset, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
281 SeekFrom::End(offset) => { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
282 delay_buf.offset = |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
283 delay_buf.len().saturating_add_signed(offset) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
284 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
285 SeekFrom::Current(offset) => { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
286 delay_buf.offset = |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
287 delay_buf.offset.saturating_add_signed(offset); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
288 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
289 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
290 if delay_buf.offset < delay_buf.file_size { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
291 self.file.seek(pos) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
292 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
293 Ok(delay_buf.offset) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
294 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
295 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
296 self.file.seek(pos) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
297 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
298 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
299 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
300 /// Read exactly `length` bytes from the current position. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
301 /// Errors are the same as [`std::io::Read::read_exact`]. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
302 pub fn read_exact( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
303 &mut self, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
304 length: usize, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
305 ) -> Result<Vec<u8>, std::io::Error> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
306 if let Some(delay_buf) = self.delayed_buffer.as_mut() { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
307 let mut delay_buf = delay_buf.lock().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
308 let mut buf = vec![0; length]; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
309 let offset: isize = |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
310 delay_buf.offset.try_into().expect("buffer too large"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
311 let file_size: isize = |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
312 delay_buf.file_size.try_into().expect("file too large"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
313 let span: isize = offset - file_size; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
314 let length = length.try_into().expect("too large of a length"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
315 let absolute_span: u64 = |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
316 span.unsigned_abs().try_into().expect("length too large"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
317 if span < 0 { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
318 if length <= absolute_span { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
319 // We're only in the file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
320 self.file.read_exact(&mut buf)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
321 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
322 // We're spanning file and buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
323 self.file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
324 .read_exact(&mut buf[..absolute_span as usize])?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
325 delay_buf |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
326 .buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
327 .take(length - absolute_span) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
328 .read_exact(&mut buf[absolute_span as usize..])?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
329 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
330 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
331 // We're only in the buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
332 delay_buf.buffer[absolute_span as usize..] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
333 .take(length) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
334 .read_exact(&mut buf)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
335 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
336 delay_buf.offset += length; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
337 Ok(buf.to_owned()) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
338 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
339 let mut buf = vec![0; length]; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
340 self.file.read_exact(&mut buf)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
341 Ok(buf) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
342 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
343 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
344 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
345 /// Flush the in-memory changes to disk. This does *not* write the |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
346 /// delayed buffer, only the pending file changes. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
347 pub fn flush(&mut self) -> Result<(), HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
348 self.file.flush().when_writing_file(&self.filename) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
349 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
350 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
351 /// Return the current position in the file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
352 pub fn position(&mut self) -> Result<u64, HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
353 self.file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
354 .stream_position() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
355 .when_reading_file(&self.filename) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
356 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
357 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
358 /// Append `data` to the file, or to the [`DelayedBuffer`], if any. |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
359 pub fn write_all(&mut self, data: &[u8]) -> Result<(), HgError> { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
360 if let Some(buf) = &mut self.delayed_buffer { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
361 let mut delayed_buffer = buf.lock().expect("propagate the panic"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
362 assert_eq!(delayed_buffer.offset, delayed_buffer.len()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
363 delayed_buffer.buffer.extend_from_slice(data); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
364 delayed_buffer.offset += data.len() as u64; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
365 Ok(()) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
366 } else { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
367 self.file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
368 .write_all(data) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
369 .when_writing_file(&self.filename)?; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
370 Ok(()) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
371 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
372 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
373 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
374 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
375 /// Write handles to a given revlog (index + maybe data) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
376 #[derive(Debug)] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
377 pub struct WriteHandles { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
378 /// Handle to the index file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
379 pub index_handle: FileHandle, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
380 /// Handle to the data file, if the revlog is non-inline |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
381 pub data_handle: Option<FileHandle>, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
382 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
383 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
384 #[cfg(test)] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
385 mod tests { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
386 use std::io::ErrorKind; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
387 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
388 use crate::vfs::VfsImpl; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
389 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
390 use super::*; |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
391 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
392 #[test] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
393 fn test_random_access_file() { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
394 let base = tempfile::tempdir().unwrap().into_path(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
395 let filename = Path::new("a"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
396 let file_path = base.join(filename); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
397 let raf = RandomAccessFile::new( |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
398 Box::new(VfsImpl::new(base.clone(), true)), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
399 filename.to_owned(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
400 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
401 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
402 assert!(!raf.is_open()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
403 assert_eq!(&raf.filename, &filename); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
404 // Should fail to read a non-existing file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
405 match raf.get_read_handle().unwrap_err() { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
406 HgError::IoError { error, .. } => match error.kind() { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
407 std::io::ErrorKind::NotFound => {} |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
408 _ => panic!("should be not found"), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
409 }, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
410 e => panic!("{}", e.to_string()), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
411 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
412 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
413 std::fs::write(file_path, b"1234567890").unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
414 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
415 // Should be able to open an existing file |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
416 let mut handle = raf.get_read_handle().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
417 assert!(raf.is_open()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
418 assert_eq!(handle.read_exact(10).unwrap(), b"1234567890".to_vec()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
419 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
420 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
421 #[test] |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
422 fn test_file_handle() { |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
423 let base = tempfile::tempdir().unwrap().into_path(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
424 let filename = base.join("a"); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
425 // No `create` should fail |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
426 FileHandle::new( |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
427 Box::new(VfsImpl::new(base.clone(), false)), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
428 &filename, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
429 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
430 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
431 ) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
432 .unwrap_err(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
433 std::fs::write(&filename, b"1234567890").unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
434 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
435 let mut read_handle = FileHandle::new( |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
436 Box::new(VfsImpl::new(base.clone(), true)), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
437 &filename, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
438 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
439 false, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
440 ) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
441 .unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
442 assert_eq!(&read_handle.filename, &filename); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
443 assert_eq!(read_handle.position().unwrap(), 0); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
444 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
445 // Writing to an explicit read handle should fail |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
446 read_handle.write_all(b"some data").unwrap_err(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
447 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
448 // reading exactly n bytes should work |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
449 assert_eq!(read_handle.read_exact(3).unwrap(), b"123".to_vec()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
450 // and the position should be remembered |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
451 assert_eq!(read_handle.read_exact(2).unwrap(), b"45".to_vec()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
452 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
453 // Seeking should work |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
454 let position = read_handle.position().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
455 read_handle.seek(SeekFrom::Current(-2)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
456 assert_eq!(position - 2, read_handle.position().unwrap()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
457 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
458 // Seeking too much data should fail |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
459 read_handle.read_exact(1000).unwrap_err(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
460 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
461 // Open a write handle |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
462 let mut handle = FileHandle::new( |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
463 Box::new(VfsImpl::new(base.clone(), false)), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
464 &filename, |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
465 false, |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
466 true, |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
467 ) |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
468 .unwrap(); |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
469 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
470 // Now writing should succeed |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
471 handle.write_all(b"new data").unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
472 // Opening or writing does not seek, so we should be at the start |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
473 assert_eq!(handle.position().unwrap(), 8); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
474 // We can still read |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
475 assert_eq!(handle.read_exact(2).unwrap(), b"90".to_vec()); |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
476 |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
477 let mut read_handle = FileHandle::new( |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
478 Box::new(VfsImpl::new(base.clone(), true)), |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
479 &filename, |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
480 false, |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
481 false, |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
482 ) |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
483 .unwrap(); |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
484 read_handle.seek(SeekFrom::Start(0)).unwrap(); |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
485 // On-disk file contents should be changed |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
486 assert_eq!( |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
487 &read_handle.read_exact(10).unwrap(), |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
488 &b"new data90".to_vec(), |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
489 ); |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
490 // Flushing doesn't do anything unexpected |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
491 handle.flush().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
492 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
493 let delayed_buffer = Arc::new(Mutex::new(DelayedBuffer::default())); |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
494 let mut handle = FileHandle::new_delayed( |
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
495 Box::new(VfsImpl::new(base.clone(), false)), |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
496 &filename, |
52297
7be39c5110c9
hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents:
52285
diff
changeset
|
497 false, |
52285
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
498 delayed_buffer, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
499 ) |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
500 .unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
501 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
502 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
503 handle |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
504 .delayed_buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
505 .as_ref() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
506 .unwrap() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
507 .lock() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
508 .unwrap() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
509 .file_size, |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
510 10 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
511 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
512 handle.seek(SeekFrom::End(0)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
513 handle.write_all(b"should go to buffer").unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
514 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
515 handle |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
516 .delayed_buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
517 .as_ref() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
518 .unwrap() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
519 .lock() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
520 .unwrap() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
521 .len(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
522 29 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
523 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
524 read_handle.seek(SeekFrom::Start(0)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
525 // On-disk file contents should be unchanged |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
526 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
527 read_handle.read_exact(10).unwrap(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
528 b"new data90".to_vec(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
529 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
530 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
531 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
532 read_handle.read_exact(1).unwrap_err().kind(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
533 ErrorKind::UnexpectedEof |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
534 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
535 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
536 handle.flush().unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
537 // On-disk file contents should still be unchanged after a flush |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
538 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
539 read_handle.read_exact(1).unwrap_err().kind(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
540 ErrorKind::UnexpectedEof |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
541 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
542 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
543 // Read from the buffer only |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
544 handle.seek(SeekFrom::End(-1)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
545 assert_eq!(handle.read_exact(1).unwrap(), b"r".to_vec()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
546 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
547 // Read from an overlapping section of file and buffer |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
548 handle.seek(SeekFrom::Start(6)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
549 assert_eq!( |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
550 handle.read_exact(20).unwrap(), |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
551 b"ta90should go to buf".to_vec() |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
552 ); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
553 |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
554 // Read from file only |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
555 handle.seek(SeekFrom::Start(0)).unwrap(); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
556 assert_eq!(handle.read_exact(8).unwrap(), b"new data".to_vec()); |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
557 } |
426696af24d3
rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff
changeset
|
558 } |