1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
//! This rust crate allow to take a part of an object that implement ``Read`` + ``Seek`` (typically a file), by specifying it's offset and lenght. It can also build similar item with an Arc<Mutex<File>>, ensuring coherency of the pointer in the file, allowing to access the same file concurrently (althougth it isn't optimized for speed, as it have to unlock the Mutex and seek to the good position). //! //! # Examples //! ``` //! use std::io::{Cursor, Read}; //! use io_partition::Partition; //! let file = Cursor::new(&[0, 2, 4, 6, 8, 10, 12]); //! //! let mut sub_file = Partition::new(file, 2, 3).unwrap(); //! let mut buffer = [0, 0, 0, 0, 0]; //! assert_eq!(sub_file.read(&mut buffer).unwrap(), 3); //! assert_eq!(buffer, [4, 6, 8, 0, 0]); //! ``` //TODO: impl stream_len when seek_convenience is stabilized use io::ErrorKind; use std::io::{Cursor, Read, Seek, SeekFrom, Write}; use std::sync::{Arc, Mutex}; use std::{io, sync::MutexGuard}; use thiserror::Error; const ERROR_MESSAGE_SEEK_PRE_START: &str = "can't seek before the beggining of the partition"; const ERROR_MESSAGE_OVERFLOW_POSITION_UNSIGNED: &str = "position cant be more than 2^64."; const ERROR_MESSAGE_OVERFLOW_POSITION_SIGNED: &str = "position cant be more than 2^63."; const ERROR_MESSAGE_START_LENGHT_OVERFLOW: &str = "the sum of the input start + lenght is superior to the maximum representatble value in a 64 bit number."; fn partition_read<T: Read + Seek>( buf: &mut [u8], file: &mut T, _start: u64, end: u64, mut pointer: u64, seek_is_correct: bool, ) -> (u64, io::Result<usize>) { if !seek_is_correct { match file.seek(SeekFrom::Start(pointer)) { Ok(_) => (), Err(err) => return (pointer, Err(err)), } } let end_byte_absolute = match pointer.checked_add(buf.len() as u64) { Some(value) => value, None => { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_OVERFLOW_POSITION_UNSIGNED, )), ) } }; if end_byte_absolute >= end { if end < pointer { return (pointer, Ok(0)); }; let loop_total_nb = end - pointer; let mut buffer1 = [0]; for loop_nb in 0..loop_total_nb { match file.read_exact(&mut buffer1) { Ok(()) => (), Err(err) => { let _ = file.seek(SeekFrom::Start(pointer)); return (pointer, Err(err)); } } pointer += 1; buf[loop_nb as usize] = buffer1[0]; } (pointer, Ok(loop_total_nb as usize)) } else { match file.read(buf) { Ok(value) => (pointer + value as u64, Ok(value)), Err(err) => (pointer, Err(err)), } } } fn partition_seek<T: Read + Seek>( file: &mut T, start: u64, end: u64, pointer: u64, target: SeekFrom, ) -> (u64, io::Result<u64>) { let new_real_pos: u64 = match target { SeekFrom::Start(nb) => match start.checked_add(nb) { Some(position) => position, None => { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_OVERFLOW_POSITION_UNSIGNED, )), ) } }, SeekFrom::End(nb) => { let result_i64 = match (end as i64).checked_add(nb) { Some(position) => position, None => { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_OVERFLOW_POSITION_SIGNED, )), ) } }; if result_i64 < start as i64 { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_SEEK_PRE_START, )), ); }; result_i64 as u64 } SeekFrom::Current(nb) => { let result_i64 = match (pointer as i64).checked_add(nb) { Some(position) => position, None => { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_OVERFLOW_POSITION_SIGNED, )), ) } }; if result_i64 < start as i64 { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_SEEK_PRE_START, )), ); }; result_i64 as u64 } }; if new_real_pos < start { return ( pointer, Err(io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_SEEK_PRE_START, )), ); }; // do not block seeking post-partition, as it will be caught by read match file.seek(SeekFrom::Start(new_real_pos as u64)) { Ok(_) => (), Err(err) => return (pointer, Err(err)), }; (new_real_pos, Ok(new_real_pos - start)) } fn check_seek_end_valid<T: Read + Seek>(reader: &mut T, start: u64, end: u64) -> io::Result<()> { let real_end = reader.seek(SeekFrom::End(0))?; if real_end < end { return Err(io::Error::new( ErrorKind::Other, "the end of the file is after the real file end (determined via SeekFrom::end(0))", )); }; reader.seek(SeekFrom::Start(start))?; Ok(()) } /// A ``Partition`` allow you to refer to a part of the file. It consume the input file. /// /// The input offset is the first byte that will be accessible. The user of the ``Partition`` won't be able to seek before it, and it will be considered the offset 0 of the ``Partition`` /// The input lenght is the number of byte that can be read with this ``Partition``. The last readable byte from the input file is input_offset + input_len /// /// # Examples /// ``` /// use std::io::{Cursor, Read, Seek, SeekFrom}; /// use io_partition::Partition; /// /// let some_value = (0..30).collect::<Vec<u8>>(); /// let input_file = Cursor::new(&some_value); //0, 1, 2, 3 ... 99 /// /// let mut partition = Partition::new(input_file, 10, 20).unwrap(); /// /// let mut buffer = [0]; /// partition.read_exact(&mut buffer).unwrap(); /// assert_eq!(buffer, [10]); /// partition.read_exact(&mut buffer).unwrap(); /// assert_eq!(buffer, [11]); /// /// assert!(partition.seek(SeekFrom::Current(-10)).is_err()); /// partition.seek(SeekFrom::End(-1)).unwrap(); /// partition.read_exact(&mut buffer).unwrap(); /// assert_eq!(buffer, [29]); /// /// partition.seek(SeekFrom::End(-3)); /// let mut buffer_large = [0; 6]; /// assert_eq!(partition.read(&mut buffer_large).unwrap(), 3); /// assert_eq!(buffer_large, [27, 28, 29, 0, 0, 0]); /// ``` #[derive(Debug)] pub struct Partition<T: Read + Seek> { file: T, /// The offset of the first byte that should be included start: u64, pointer: u64, /// The offset of the first byte that should be NOT included end: u64, } impl<T: Read + Seek> Partition<T> { /// Create new ``Partition``, with the specified input file, start and lenght /// /// This will check that the file is big enought at creation, and the cursor will be located at the beggining of the file. pub fn new(mut file: T, start: u64, lenght: u64) -> io::Result<Partition<T>> { let end = start.checked_add(lenght).ok_or_else(|| { io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_START_LENGHT_OVERFLOW, ) })?; check_seek_end_valid(&mut file, start, end)?; let result = Partition { file, start, pointer: start, end, }; Ok(result) } } impl<T: Read + Seek> Read for Partition<T> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { let (new_pointer_pos, result) = partition_read( buf, &mut self.file, self.start, self.end, self.pointer, true, ); self.pointer = new_pointer_pos; result } } impl<T: Seek + Read> Seek for Partition<T> { fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { let (new_pointer_pos, result) = partition_seek(&mut self.file, self.start, self.end, self.pointer, pos); self.pointer = new_pointer_pos; result } } impl<T: Read + Seek> Write for Partition<T> { /// Do not use this write function. It will always fail. It is just here because some library require this to have the ``Write`` trait to make this work with this (rust_vfs) fn write(&mut self, _: &[u8]) -> io::Result<usize> { Err(io::Error::from(io::ErrorKind::PermissionDenied)) } /// Always suceed. It is useless to call it fn flush(&mut self) -> io::Result<()> { Ok(()) } } #[derive(Debug, Error)] /// An error that may occur by calling [`PartitionMutex::lock`] pub enum LockPartitionError { #[error("an io error occured")] IOError(#[from] io::Error), #[error("A thread panicked while holding this lock")] PoisonError, } /// A ``PartitionMutex`` allow you to refer to a part of the file. It consume the input file. /// /// As the input file is an ``Arc<Mutex<_>>``, multiple ``PartitionMutex`` can be created by file, and ``PartitionMutex`` can be cloned. /// /// Note that all function (but the write one) will lock the Mutex and release it before the function end (except for ``lock``). On calling function of this, you must ensure the input file isn't already locked by this thread. /// /// The input offset is the first byte that will be accessible. The user of the ``PartitionMutex`` won't be able to seek before it, and it will be considered the offset 0 of the ``PartitionMutex`` /// The input lenght is the number of byte that can be read with this ``PartitionMutex``. The last readable byte from the input file is input_offset + input_len /// /// It is possible to lock the mutex with [`PartitionMutex::lock`]. You need to take care when using it, or a panic may occur. Please read the documentation of the function. /// /// # Examples /// ``` /// use std::io::{Cursor, Read, Seek, SeekFrom}; /// use io_partition::PartitionMutex; /// use std::sync::{Mutex, Arc}; /// use std::thread; /// /// let mut some_value = (0..100).collect::<Vec<u8>>(); /// let some_file = Arc::new(Mutex::new(Cursor::new(some_value))); /// /// let mut first_partition = PartitionMutex::new(some_file.clone(), 10, 20).unwrap(); /// let mut second_partition = PartitionMutex::new(some_file.clone(), 40, 30).unwrap(); /// /// let mut buf = [0]; /// /// first_partition.seek(SeekFrom::Start(10)).unwrap(); /// second_partition.seek(SeekFrom::Start(5)).unwrap(); /// first_partition.read_exact(&mut buf).unwrap(); /// assert_eq!(buf, [20]); /// second_partition.read_exact(&mut buf).unwrap(); /// assert_eq!(buf, [45]); /// /// second_partition.seek(SeekFrom::Start(5)).unwrap(); /// let mut second_clone = second_partition.clone(); /// let handle = thread::spawn(move || { /// second_clone.seek(SeekFrom::Current(2)).unwrap(); /// let mut buf = [0]; /// second_clone.read_exact(&mut buf).unwrap(); /// buf[0] /// }); /// /// second_partition.seek(SeekFrom::End(-1)).unwrap(); /// second_partition.read_exact(&mut buf).unwrap(); /// /// assert_eq!(handle.join().unwrap(), 47); /// assert_eq!(buf[0], 69); /// /// first_partition.seek(SeekFrom::Start(2)).unwrap(); /// { /// let mut locked = first_partition.lock().unwrap(); /// let mut buffer = [0; 2]; /// locked.read_exact(&mut buffer); /// assert_eq!(&buffer, &[12, 13]); /// } /// ``` #[derive(Debug, Clone)] pub struct PartitionMutex<T: Read + Seek> { file: Arc<Mutex<T>>, start: u64, pointer: u64, end: u64, } impl<T: Read + Seek> PartitionMutex<T> { /// Create new ``PartitionMutex``, with the specified input file, start and lenght. This will lock the input Mutex. /// /// This will check that the file is big enought at creation, and the cursor will be located at the beggining of the file. pub fn new(file: Arc<Mutex<T>>, start: u64, lenght: u64) -> io::Result<PartitionMutex<T>> { let end = start.checked_add(lenght).ok_or_else(|| { io::Error::new( io::ErrorKind::InvalidInput, ERROR_MESSAGE_START_LENGHT_OVERFLOW, ) })?; { let mut file = match file.lock() { Ok(value) => value, Err(_) => { return Err(io::Error::new( io::ErrorKind::Other, "the file mutex is poisoned", )) } }; check_seek_end_valid(&mut *file, start, end)?; } let mut result = PartitionMutex { file, start, pointer: start, end, }; result.seek(SeekFrom::Start(0))?; Ok(result) } /// Lock this [`PartitionMutex`], in a similar fashion to [`Mutex::lock`]. /// If the same thread lock this [`PartitionMutex`], or any other structure that use the same file, without first checking it is free, it might panic or softlock. /// Note that the seek/read implementation of [`PartitionMutex`] will lock the file for the duration of those function execution. /// you can use scope for this. pub fn lock(&mut self) -> Result<PartitionMutexLock<'_, T>, LockPartitionError> { let mut file_locked = self .file .lock() .map_err(|_| LockPartitionError::PoisonError)?; file_locked.seek(SeekFrom::Start(self.pointer))?; Ok(PartitionMutexLock { file: file_locked, pointer: &mut self.pointer, start: &mut self.start, end: &mut self.end, }) } } impl<T: Read + Seek> Read for PartitionMutex<T> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { let mut file = match self.file.lock() { Ok(value) => value, Err(_) => { return Err(io::Error::new( io::ErrorKind::Other, "the file mutex is poisoned", )) } }; let (new_pointer_pos, result) = partition_read(buf, &mut *file, self.start, self.end, self.pointer, false); self.pointer = new_pointer_pos; result } } impl<T: Read + Seek> Seek for PartitionMutex<T> { fn seek(&mut self, target: SeekFrom) -> io::Result<u64> { let mut file = match self.file.lock() { Ok(value) => value, Err(_) => { return Err(io::Error::new( io::ErrorKind::Other, "the file mutex is poisoned", )) } }; let (new_pointer_pos, result) = partition_seek(&mut *file, self.start, self.end, self.pointer, target); self.pointer = new_pointer_pos; result } } impl<T: Read + Seek> Write for PartitionMutex<T> { /// Do not use this write function. It will always fail. It is just here because some library require this to have the ``Write`` trait to make this work with this (rust_vfs) fn write(&mut self, _: &[u8]) -> io::Result<usize> { Err(io::Error::from(io::ErrorKind::PermissionDenied)) } /// Always suceed. It is useless to call it fn flush(&mut self) -> io::Result<()> { Ok(()) } } /// A locked [`PartitionMutex`]. See the documentation of [`PartitionMutex::lock`] for usage precaution. pub struct PartitionMutexLock<'a, T: Read + Seek> { file: MutexGuard<'a, T>, // NOTE: we assume the file is seeked at the right position start: &'a mut u64, pointer: &'a mut u64, end: &'a mut u64, } impl<'a, T: Read + Seek> Read for PartitionMutexLock<'a, T> { fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { let (new_pointer_pos, result) = partition_read( buf, &mut *self.file, *self.start, *self.end, *self.pointer, true, ); *self.pointer = new_pointer_pos; result } } impl<'a, T: Read + Seek> Seek for PartitionMutexLock<'a, T> { fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> { let (new_pointer_pos, result) = partition_seek(&mut *self.file, *self.start, *self.end, *self.pointer, pos); *self.pointer = new_pointer_pos; result } } impl<'a, T: Read + Seek> Write for PartitionMutexLock<'a, T> { /// Do not use this write function. It will always fail. It is just here because some library require this to have the ``Write`` trait to make this work with this (rust_vfs) fn write(&mut self, _: &[u8]) -> io::Result<usize> { Err(io::Error::from(io::ErrorKind::PermissionDenied)) } /// Always suceed. It is useless to call it fn flush(&mut self) -> io::Result<()> { Ok(()) } } /// Clone a part of a file into a Vec pub fn clone_into_vec<T: Read + Seek>( file: &mut T, start: u64, length: u64, ) -> Result<Vec<u8>, io::Error> { let mut buffer = [0]; file.seek(SeekFrom::Start(start))?; let mut output_buffer = Vec::new(); for _ in 0..length { file.read_exact(&mut buffer)?; output_buffer.push(buffer[0]); } Ok(output_buffer) } /// Clone a part of a file pub fn partition_clone<T: Read + Seek>( file: &mut T, start: u64, length: u64, ) -> Result<Cursor<Vec<u8>>, io::Error> { Ok(Cursor::new(clone_into_vec(file, start, length)?)) }