annotate rust/hg-core/src/revlog/file_io.rs @ 53042:cdd7bf612c7b stable tip

bundle-spec: properly format boolean parameter (issue6960) This was breaking automatic clone bundle generation. This changeset fixes it and add a test to catch it in the future.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Tue, 11 Mar 2025 02:29:42 +0100
parents 549b58b1ce72
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52155
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::{
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
4 cell::RefCell,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
5 io::{Read, Seek, SeekFrom, Write},
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
6 path::{Path, PathBuf},
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
7 sync::{Arc, Mutex},
52155
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
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
10 use crate::{
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
11 errors::{HgError, IoResultExt},
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
12 vfs::{Vfs, VfsFile},
52155
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
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
15 /// 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
16 /// 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
17 /// 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
18 /// file-based mmap.
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
19 ///
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
20 /// 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
21 /// 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
22 /// 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
23 /// 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
24 /// 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
25 /// 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
26 pub struct RandomAccessFile {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
27 /// 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
28 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
29 /// 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
30 pub filename: PathBuf,
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
31 /// The current read-only handle on the file, if any.
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
32 /// Specific to the current thread, since we don't want seeks to overlap
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
33 pub reading_handle: thread_local::ThreadLocal<RefCell<Option<FileHandle>>>,
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
34 /// The current read-write handle on the file, if any.
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
35 /// Specific to the current thread, since we don't want seeks to overlap,
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
36 /// and we can re-use the write handle for reading in certain contexts.
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
37 /// Logically, two concurrent writes are impossible because they are only
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
38 /// accessible through `&mut self` methods, which take a lock.
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
39 pub writing_handle: thread_local::ThreadLocal<RefCell<Option<FileHandle>>>,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
40 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
41
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
42 impl RandomAccessFile {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
43 /// Wrap a file for random access
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
44 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
45 assert!(filename.is_relative());
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
46 Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
47 vfs,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
48 filename,
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
49 reading_handle: thread_local::ThreadLocal::new(),
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
50 writing_handle: thread_local::ThreadLocal::new(),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
51 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
52 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
53
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
54 /// 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
55 pub fn read_chunk(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
56 &self,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
57 offset: usize,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
58 length: usize,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
59 ) -> Result<Vec<u8>, HgError> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
60 let mut handle = self.get_read_handle()?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
61 handle
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
62 .seek(SeekFrom::Start(offset as u64))
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
63 .when_reading_file(&self.filename)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
64 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
65 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
66
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
67 /// `pub` only for hg-cpython
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
68 #[doc(hidden)]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
69 pub fn get_read_handle(&self) -> Result<FileHandle, HgError> {
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
70 if let Some(handle) = &*self.writing_handle.get_or_default().borrow() {
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
71 // 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
72 // 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
73 // file.
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
74 // 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
75 // 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
76 return Ok(handle.clone());
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
77 }
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
78 if let Some(handle) = &*self.reading_handle.get_or_default().borrow() {
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
79 return Ok(handle.clone());
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
80 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
81 // 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
82 // 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
83 let new_handle = FileHandle::new(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
84 dyn_clone::clone_box(&*self.vfs),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
85 &self.filename,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
86 false,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
87 false,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
88 )?;
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
89 *self.reading_handle.get_or_default().borrow_mut() =
52765
162f4801ad39 rust-inner-revlog: move to `RwLock` instead of `RefCell`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52294
diff changeset
90 Some(new_handle.clone());
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
91 Ok(new_handle)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
92 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
93
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
94 /// `pub` only for hg-cpython
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
95 #[doc(hidden)]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
96 pub fn exit_reading_context(&self) {
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
97 self.reading_handle.get().map(|h| h.take());
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
98 }
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 // Returns whether this file currently open
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
101 pub fn is_open(&self) -> bool {
52766
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
102 self.reading_handle.get_or_default().borrow().is_some()
549b58b1ce72 rust-inner-revlog: use threadlocals for file handles
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52765
diff changeset
103 || self.writing_handle.get_or_default().borrow().is_some()
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
104 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
105 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
106
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
107 /// 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
108 /// 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
109 /// external processes.
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
110 #[derive(Clone, Debug, Default)]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
111 pub struct DelayedBuffer {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
112 // 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
113 pub(super) buffer: Vec<u8>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
114 /// 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
115 offset: u64,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
116 /// 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
117 file_size: u64,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
118 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
119
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
120 impl DelayedBuffer {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
121 /// 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
122 pub fn len(&self) -> u64 {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
123 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
124 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
125
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
126 pub fn is_empty(&self) -> bool {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
127 self.len() == 0
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
128 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
129 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
130
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
131 /// 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: 52167
diff changeset
132 /// 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: 52167
diff changeset
133 /// the disk, if relevant (in the changelog case), but reads are transparent.
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
134 pub struct FileHandle {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
135 /// The actual open file
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
136 pub file: VfsFile,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
137 /// 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
138 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
139 /// 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
140 filename: PathBuf,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
141 /// 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
142 /// 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
143 delayed_buffer: Option<Arc<Mutex<DelayedBuffer>>>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
144 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
145
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
146 impl std::fmt::Debug for FileHandle {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
147 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
148 f.debug_struct("FileHandle")
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
149 .field("filename", &self.filename)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
150 .field("delayed_buffer", &self.delayed_buffer)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
151 .field("file", &self.file)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
152 .finish()
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 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
156 impl Clone for FileHandle {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
157 fn clone(&self) -> Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
158 Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
159 vfs: dyn_clone::clone_box(&*self.vfs),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
160 filename: self.filename.clone(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
161 delayed_buffer: self.delayed_buffer.clone(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
162 // 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
163 // 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
164 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
165 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
166 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
167 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
168
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
169 impl FileHandle {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
170 /// 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
171 /// if `create` is `true`.
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
172 pub fn new(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
173 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
174 filename: impl AsRef<Path>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
175 create: bool,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
176 write: bool,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
177 ) -> Result<Self, HgError> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
178 let file = if create {
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
179 vfs.create(filename.as_ref(), false)?
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
180 } else if write {
52294
645d247d4c75 rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52181
diff changeset
181 vfs.open_write(filename.as_ref())?
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
182 } else {
52294
645d247d4c75 rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52181
diff changeset
183 vfs.open(filename.as_ref())?
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
184 };
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
185 Ok(Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
186 vfs,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
187 filename: filename.as_ref().to_owned(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
188 delayed_buffer: None,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
189 file,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
190 })
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
191 }
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 /// 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
194 pub fn new_delayed(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
195 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
196 filename: impl AsRef<Path>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
197 create: bool,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
198 delayed_buffer: Arc<Mutex<DelayedBuffer>>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
199 ) -> Result<Self, HgError> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
200 let mut file = if create {
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
201 vfs.create(filename.as_ref(), false)?
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
202 } else {
52294
645d247d4c75 rust-vfs: rename `open` to `open_write` and `open_read` to `open`
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52181
diff changeset
203 vfs.open_write(filename.as_ref())?
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
204 };
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
205 let size = vfs.file_size(&file)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
206 let offset = file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
207 .stream_position()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
208 .when_reading_file(filename.as_ref())?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
209
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
210 {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
211 let mut buf = delayed_buffer.lock().unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
212 buf.file_size = size;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
213 buf.offset = offset;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
214 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
215
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
216 Ok(Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
217 vfs,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
218 filename: filename.as_ref().to_owned(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
219 delayed_buffer: Some(delayed_buffer),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
220 file,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
221 })
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
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
224 /// Wrap an existing [`VfsFile`]
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
225 pub fn from_file(
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
226 file: VfsFile,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
227 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
228 filename: impl AsRef<Path>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
229 ) -> Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
230 Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
231 vfs,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
232 filename: filename.as_ref().to_owned(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
233 delayed_buffer: None,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
234 file,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
235 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
236 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
237
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
238 /// Wrap an existing [`VfsFile`], but writes go to a [`DelayedBuffer`].
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
239 pub fn from_file_delayed(
52181
8d35941689af rust-vfs: support checkambig
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52167
diff changeset
240 mut file: VfsFile,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
241 vfs: Box<dyn Vfs>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
242 filename: impl AsRef<Path>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
243 delayed_buffer: Arc<Mutex<DelayedBuffer>>,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
244 ) -> Result<Self, HgError> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
245 let size = vfs.file_size(&file)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
246 let offset = file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
247 .stream_position()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
248 .when_reading_file(filename.as_ref())?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
249
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
250 {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
251 let mut buf = delayed_buffer.lock().unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
252 buf.file_size = size;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
253 buf.offset = offset;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
254 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
255
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
256 Ok(Self {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
257 vfs,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
258 filename: filename.as_ref().to_owned(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
259 delayed_buffer: Some(delayed_buffer),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
260 file,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
261 })
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 /// 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
265 /// 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
266 /// 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
267 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
268 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
269 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
270 // 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
271 match pos {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
272 SeekFrom::Start(offset) => delay_buf.offset = offset,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
273 SeekFrom::End(offset) => {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
274 delay_buf.offset =
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
275 delay_buf.len().saturating_add_signed(offset)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
276 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
277 SeekFrom::Current(offset) => {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
278 delay_buf.offset =
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
279 delay_buf.offset.saturating_add_signed(offset);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
280 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
281 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
282 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
283 self.file.seek(pos)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
284 } else {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
285 Ok(delay_buf.offset)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
286 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
287 } else {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
288 self.file.seek(pos)
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 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
291
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
292 /// 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
293 /// 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
294 pub fn read_exact(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
295 &mut self,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
296 length: usize,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
297 ) -> Result<Vec<u8>, std::io::Error> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
298 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
299 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
300 let mut buf = vec![0; length];
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
301 let offset: isize =
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
302 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
303 let file_size: isize =
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
304 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
305 let span: isize = offset - file_size;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
306 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
307 let absolute_span: u64 =
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
308 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
309 if span < 0 {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
310 if length <= absolute_span {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
311 // We're only in the file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
312 self.file.read_exact(&mut buf)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
313 } else {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
314 // We're spanning file and buffer
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
315 self.file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
316 .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
317 delay_buf
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
318 .buffer
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
319 .take(length - absolute_span)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
320 .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
321 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
322 } else {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
323 // We're only in the buffer
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
324 delay_buf.buffer[absolute_span as usize..]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
325 .take(length)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
326 .read_exact(&mut buf)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
327 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
328 delay_buf.offset += length;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
329 Ok(buf.to_owned())
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 let mut buf = vec![0; length];
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
332 self.file.read_exact(&mut buf)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
333 Ok(buf)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
334 }
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
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
337 /// 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
338 /// delayed buffer, only the pending file changes.
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
339 pub fn flush(&mut self) -> Result<(), HgError> {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
340 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
341 }
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 /// Return the current position in the file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
344 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
345 self.file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
346 .stream_position()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
347 .when_reading_file(&self.filename)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
348 }
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 /// 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
351 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
352 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
353 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
354 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
355 delayed_buffer.buffer.extend_from_slice(data);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
356 delayed_buffer.offset += data.len() as u64;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
357 Ok(())
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
358 } else {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
359 self.file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
360 .write_all(data)
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
361 .when_writing_file(&self.filename)?;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
362 Ok(())
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
363 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
364 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
365 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
366
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
367 /// 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
368 #[derive(Debug)]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
369 pub struct WriteHandles {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
370 /// Handle to the index file
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
371 pub index_handle: FileHandle,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
372 /// 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
373 pub data_handle: Option<FileHandle>,
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
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
376 #[cfg(test)]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
377 mod tests {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
378 use std::io::ErrorKind;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
379
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
380 use crate::vfs::VfsImpl;
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
381
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
382 use super::*;
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 #[test]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
385 fn test_random_access_file() {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
386 let base = tempfile::tempdir().unwrap().into_path();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
387 let filename = Path::new("a");
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
388 let file_path = base.join(filename);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
389 let raf = RandomAccessFile::new(
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
390 Box::new(VfsImpl::new(base.clone(), true)),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
391 filename.to_owned(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
392 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
393
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
394 assert!(!raf.is_open());
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
395 assert_eq!(&raf.filename, &filename);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
396 // 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
397 match raf.get_read_handle().unwrap_err() {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
398 HgError::IoError { error, .. } => match error.kind() {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
399 std::io::ErrorKind::NotFound => {}
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
400 _ => panic!("should be not found"),
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 e => panic!("{}", e.to_string()),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
403 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
404
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
405 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
406
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
407 // 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
408 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
409 assert!(raf.is_open());
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
410 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
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 #[test]
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
414 fn test_file_handle() {
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
415 let base = tempfile::tempdir().unwrap().into_path();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
416 let filename = base.join("a");
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
417 // No `create` should fail
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
418 FileHandle::new(
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
419 Box::new(VfsImpl::new(base.clone(), false)),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
420 &filename,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
421 false,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
422 false,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
423 )
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
424 .unwrap_err();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
425 std::fs::write(&filename, b"1234567890").unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
426
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
427 let mut read_handle = FileHandle::new(
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
428 Box::new(VfsImpl::new(base.clone(), true)),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
429 &filename,
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 false,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
432 )
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
433 .unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
434 assert_eq!(&read_handle.filename, &filename);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
435 assert_eq!(read_handle.position().unwrap(), 0);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
436
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
437 // 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
438 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
439
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
440 // reading exactly n bytes should work
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
441 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
442 // and the position should be remembered
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
443 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
444
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
445 // Seeking should work
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
446 let position = read_handle.position().unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
447 read_handle.seek(SeekFrom::Current(-2)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
448 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
449
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
450 // Seeking too much data should fail
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
451 read_handle.read_exact(1000).unwrap_err();
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 // Open a write handle
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
454 let mut handle = FileHandle::new(
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
455 Box::new(VfsImpl::new(base.clone(), false)),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
456 &filename,
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
457 false,
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
458 true,
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
459 )
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
460 .unwrap();
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
461
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
462 // Now writing should succeed
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
463 handle.write_all(b"new data").unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
464 // 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
465 assert_eq!(handle.position().unwrap(), 8);
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
466 // We can still read
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
467 assert_eq!(handle.read_exact(2).unwrap(), b"90".to_vec());
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
468
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
469 let mut read_handle = FileHandle::new(
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
470 Box::new(VfsImpl::new(base.clone(), true)),
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
471 &filename,
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
472 false,
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
473 false,
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
474 )
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
475 .unwrap();
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
476 read_handle.seek(SeekFrom::Start(0)).unwrap();
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
477 // On-disk file contents should be changed
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
478 assert_eq!(
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
479 &read_handle.read_exact(10).unwrap(),
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
480 &b"new data90".to_vec(),
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
481 );
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
482 // Flushing doesn't do anything unexpected
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
483 handle.flush().unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
484
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
485 let delayed_buffer = Arc::new(Mutex::new(DelayedBuffer::default()));
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
486 let mut handle = FileHandle::new_delayed(
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
487 Box::new(VfsImpl::new(base.clone(), false)),
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
488 &filename,
52167
7be39c5110c9 hg-core: add a complete VFS
Rapha?l Gom?s <rgomes@octobus.net>
parents: 52155
diff changeset
489 false,
52155
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
490 delayed_buffer,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
491 )
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
492 .unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
493
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
494 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
495 handle
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
496 .delayed_buffer
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
497 .as_ref()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
498 .unwrap()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
499 .lock()
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 .file_size,
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
502 10
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
503 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
504 handle.seek(SeekFrom::End(0)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
505 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
506 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
507 handle
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
508 .delayed_buffer
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
509 .as_ref()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
510 .unwrap()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
511 .lock()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
512 .unwrap()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
513 .len(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
514 29
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
515 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
516 read_handle.seek(SeekFrom::Start(0)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
517 // On-disk file contents should be unchanged
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
518 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
519 read_handle.read_exact(10).unwrap(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
520 b"new data90".to_vec(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
521 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
522
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
523 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
524 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
525 ErrorKind::UnexpectedEof
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
526 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
527
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
528 handle.flush().unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
529 // 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
530 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
531 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
532 ErrorKind::UnexpectedEof
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
533 );
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 // Read from the buffer only
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
536 handle.seek(SeekFrom::End(-1)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
537 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
538
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
539 // 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
540 handle.seek(SeekFrom::Start(6)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
541 assert_eq!(
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
542 handle.read_exact(20).unwrap(),
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
543 b"ta90should go to buf".to_vec()
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
544 );
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
545
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
546 // Read from file only
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
547 handle.seek(SeekFrom::Start(0)).unwrap();
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
548 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
549 }
426696af24d3 rust-revlog: add file IO helpers
Rapha?l Gom?s <rgomes@octobus.net>
parents:
diff changeset
550 }