1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
//! Virtual filesystem path
//!
//! The virtual file system abstraction generalizes over file systems and allow using
//! different VirtualFileSystem implementations (i.e. an in memory implementation for unit tests)
use crate::async_vfs::AsyncFileSystem;
use crate::error::{VfsError, VfsErrorKind};
use crate::path::PathLike;
use crate::path::VfsFileType;
use crate::{VfsMetadata, VfsResult};
use async_recursion::async_recursion;
use async_std::io::{Read, ReadExt, Seek, Write};
use async_std::sync::Arc;
use async_std::task::{Context, Poll};
use futures::{future::BoxFuture, FutureExt, Stream, StreamExt};
use std::pin::Pin;
/// Trait combining Seek and Read, return value for opening files
pub trait SeekAndRead: Seek + Read {}
impl<T> SeekAndRead for T where T: Seek + Read {}
#[derive(Debug)]
struct AsyncVFS {
fs: Box<dyn AsyncFileSystem>,
}
/// A virtual filesystem path, identifying a single file or directory in this virtual filesystem
#[derive(Clone, Debug)]
pub struct AsyncVfsPath {
path: String,
fs: Arc<AsyncVFS>,
}
impl PathLike for AsyncVfsPath {
fn get_path(&self) -> String {
self.path.clone()
}
}
impl PartialEq for AsyncVfsPath {
fn eq(&self, other: &Self) -> bool {
self.path == other.path && Arc::ptr_eq(&self.fs, &other.fs)
}
}
impl Eq for AsyncVfsPath {}
impl AsyncVfsPath {
/// Creates a root path for the given filesystem
///
/// ```
/// # use vfs::async_vfs::{AsyncPhysicalFS, AsyncVfsPath};
/// let path = AsyncVfsPath::new(AsyncPhysicalFS::new("."));
/// ````
pub fn new<T: AsyncFileSystem>(filesystem: T) -> Self {
AsyncVfsPath {
path: "".to_string(),
fs: Arc::new(AsyncVFS {
fs: Box::new(filesystem),
}),
}
}
/// Returns the string representation of this path
///
/// ```
/// # use vfs::async_vfs::{AsyncPhysicalFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// let path = AsyncVfsPath::new(AsyncPhysicalFS::new("."));
///
/// assert_eq!(path.as_str(), "");
/// assert_eq!(path.join("foo.txt")?.as_str(), "/foo.txt");
/// # Ok::<(), VfsError>(())
/// ````
pub fn as_str(&self) -> &str {
&self.path
}
/// Appends a path segment to this path, returning the result
///
/// ```
/// # use vfs::async_vfs::{AsyncPhysicalFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// let path = AsyncVfsPath::new(AsyncPhysicalFS::new("."));
///
/// assert_eq!(path.join("foo.txt")?.as_str(), "/foo.txt");
/// assert_eq!(path.join("foo/bar.txt")?.as_str(), "/foo/bar.txt");
///
/// let foo = path.join("foo")?;
///
/// assert_eq!(path.join("foo/bar.txt")?, foo.join("bar.txt")?);
/// assert_eq!(path, foo.join("..")?);
/// # Ok::<(), VfsError>(())
/// ```
pub fn join(&self, path: impl AsRef<str>) -> VfsResult<Self> {
let new_path = self.join_internal(&self.path, path.as_ref())?;
Ok(Self {
path: new_path,
fs: self.fs.clone(),
})
}
/// Returns the root path of this filesystem
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType};
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo/bar")?;
///
/// assert_eq!(directory.root(), path);
/// # Ok::<(), VfsError>(())
/// ```
pub fn root(&self) -> AsyncVfsPath {
AsyncVfsPath {
path: "".to_string(),
fs: self.fs.clone(),
}
}
/// Returns true if this is the root path
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType};
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// assert!(path.is_root());
/// let path = path.join("foo/bar")?;
/// assert!(! path.is_root());
/// # Ok::<(), VfsError>(())
/// ```
pub fn is_root(&self) -> bool {
self.path.is_empty()
}
/// Creates the directory at this path
///
/// Note that the parent directory must exist, while the given path must not exist.
///
/// Returns VfsErrorKind::FileExists if a file already exists at the given path
/// Returns VfsErrorKind::DirectoryExists if a directory already exists at the given path
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
///
/// directory.create_dir().await?;
///
/// assert!(directory.exists().await?);
/// assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn create_dir(&self) -> VfsResult<()> {
self.get_parent("create directory").await?;
self.fs.fs.create_dir(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not create directory")
})
}
/// Creates the directory at this path, also creating parent directories as necessary
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo/bar")?;
///
/// directory.create_dir_all().await?;
///
/// assert!(directory.exists().await?);
/// assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
/// let parent = path.join("foo")?;
/// assert!(parent.exists().await?);
/// assert_eq!(parent.metadata().await?.file_type, VfsFileType::Directory);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn create_dir_all(&self) -> VfsResult<()> {
let mut pos = 1;
let path = &self.path;
if path.is_empty() {
// root exists always
return Ok(());
}
loop {
// Iterate over path segments
let end = path[pos..]
.find('/')
.map(|it| it + pos)
.unwrap_or_else(|| path.len());
let directory = &path[..end];
if let Err(error) = self.fs.fs.create_dir(directory).await {
match error.kind() {
VfsErrorKind::DirectoryExists => {}
_ => {
return Err(error.with_path(directory).with_context(|| {
format!("Could not create directories at '{}'", path)
}))
}
}
}
if end == path.len() {
break;
}
pos = end + 1;
}
Ok(())
}
/// Iterates over all entries of this directory path
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use futures::stream::Collect;
/// use futures::stream::StreamExt;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// path.join("foo")?.create_dir().await?;
/// path.join("bar")?.create_dir().await?;
///
/// let mut directories: Vec<_> = path.read_dir().await?.collect().await;
///
/// directories.sort_by_key(|path| path.as_str().to_string());
/// assert_eq!(directories, vec![path.join("bar")?, path.join("foo")?]);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn read_dir(&self) -> VfsResult<Box<dyn Unpin + Stream<Item = AsyncVfsPath> + Send>> {
let parent = self.path.clone();
let fs = self.fs.clone();
Ok(Box::new(
self.fs
.fs
.read_dir(&self.path)
.await
.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not read directory")
})?
.map(move |path| {
println!("{:?}", path);
AsyncVfsPath {
path: format!("{}/{}", parent, path),
fs: fs.clone(),
}
}),
))
}
/// Creates a file at this path for writing, overwriting any existing file
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use async_std::io:: {ReadExt, WriteExt};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo.txt")?;
///
/// write!(file.create_file().await?, "Hello, world!").await?;
///
/// let mut result = String::new();
/// file.open_file().await?.read_to_string(&mut result).await?;
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn create_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>> {
self.get_parent("create file").await?;
self.fs.fs.create_file(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not create file")
})
}
/// Opens the file at this path for reading
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use async_std::io:: {ReadExt, WriteExt};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo.txt")?;
/// write!(file.create_file().await?, "Hello, world!").await?;
/// let mut result = String::new();
///
/// file.open_file().await?.read_to_string(&mut result).await?;
///
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn open_file(&self) -> VfsResult<Box<dyn SeekAndRead + Send + Unpin>> {
self.fs.fs.open_file(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not open file")
})
}
/// Checks whether parent is a directory
async fn get_parent(&self, action: &str) -> VfsResult<()> {
let parent = self.parent();
if !parent.exists().await? {
return Err(VfsError::from(VfsErrorKind::Other(format!(
"Could not {}, parent directory does not exist",
action
)))
.with_path(&self.path));
}
let metadata = parent.metadata().await?;
if metadata.file_type != VfsFileType::Directory {
return Err(VfsError::from(VfsErrorKind::Other(format!(
"Could not {}, parent path is not a directory",
action
)))
.with_path(&self.path));
}
Ok(())
}
/// Opens the file at this path for appending
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use async_std::io:: {ReadExt, WriteExt};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo.txt")?;
/// write!(file.create_file().await?, "Hello, ").await?;
/// write!(file.append_file().await?, "world!").await?;
/// let mut result = String::new();
/// file.open_file().await?.read_to_string(&mut result).await?;
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn append_file(&self) -> VfsResult<Box<dyn Write + Send + Unpin>> {
self.fs.fs.append_file(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not open file for appending")
})
}
/// Removes the file at this path
///
/// ```
/// use async_std::io:: {ReadExt, WriteExt};
/// # use vfs::async_vfs::{AsyncMemoryFS , AsyncVfsPath};
/// # use vfs::VfsError;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo.txt")?;
/// write!(file.create_file().await?, "Hello, ").await?;
/// assert!(file.exists().await?);
///
/// file.remove_file().await?;
///
/// assert!(!file.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn remove_file(&self) -> VfsResult<()> {
self.fs.fs.remove_file(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not remove file")
})
}
/// Removes the directory at this path
///
/// The directory must be empty.
///
/// ```
/// # tokio_test::block_on(async {
/// use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// use vfs::VfsError;
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
/// directory.create_dir().await;
/// assert!(directory.exists().await?);
///
/// directory.remove_dir().await?;
///
/// assert!(!directory.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn remove_dir(&self) -> VfsResult<()> {
self.fs.fs.remove_dir(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not remove directory")
})
}
/// Ensures that the directory at this path is removed, recursively deleting all contents if necessary
///
/// Returns successfully if directory does not exist
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
/// directory.join("bar")?.create_dir_all().await?;
/// assert!(directory.exists().await?);
///
/// directory.remove_dir_all().await?;
///
/// assert!(!directory.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
#[async_recursion]
pub async fn remove_dir_all(&self) -> VfsResult<()> {
if !self.exists().await? {
return Ok(());
}
let mut path_stream = self.read_dir().await?;
while let Some(child) = path_stream.next().await {
let metadata = child.metadata().await?;
match metadata.file_type {
VfsFileType::File => child.remove_file().await?,
VfsFileType::Directory => child.remove_dir_all().await?,
}
}
self.remove_dir().await?;
Ok(())
}
/// Returns the file metadata for the file at this path
///
/// ```
/// use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// use vfs::{VfsError, VfsFileType, VfsMetadata};
/// use async_std::io::WriteExt;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
/// directory.create_dir().await?;
///
/// assert_eq!(directory.metadata().await?.len, 0);
/// assert_eq!(directory.metadata().await?.file_type, VfsFileType::Directory);
///
/// let file = path.join("bar.txt")?;
/// write!(file.create_file().await?, "Hello, world!").await?;
///
/// assert_eq!(file.metadata().await?.len, 13);
/// assert_eq!(file.metadata().await?.file_type, VfsFileType::File);
/// # Ok::<(), VfsError>(())
/// # });
pub async fn metadata(&self) -> VfsResult<VfsMetadata> {
self.fs.fs.metadata(&self.path).await.map_err(|err| {
err.with_path(&self.path)
.with_context(|| "Could not get metadata")
})
}
/// Returns `true` if the path exists and is pointing at a regular file, otherwise returns `false`.
///
/// Note that this call may fail if the file's existence cannot be determined or the metadata can not be retrieved
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
/// directory.create_dir().await?;
/// let file = path.join("foo.txt")?;
/// file.create_file().await?;
///
/// assert!(!directory.is_file().await?);
/// assert!(file.is_file().await?);
/// # Ok::<(), VfsError>(())
/// # });
pub async fn is_file(&self) -> VfsResult<bool> {
if !self.exists().await? {
return Ok(false);
}
let metadata = self.metadata().await?;
Ok(metadata.file_type == VfsFileType::File)
}
/// Returns `true` if the path exists and is pointing at a directory, otherwise returns `false`.
///
/// Note that this call may fail if the directory's existence cannot be determined or the metadata can not be retrieved
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
/// directory.create_dir().await?;
/// let file = path.join("foo.txt")?;
/// file.create_file().await?;
///
/// assert!(directory.is_dir().await?);
/// assert!(!file.is_dir().await?);
/// # Ok::<(), VfsError>(())
/// # });
pub async fn is_dir(&self) -> VfsResult<bool> {
if !self.exists().await? {
return Ok(false);
}
let metadata = self.metadata().await?;
Ok(metadata.file_type == VfsFileType::Directory)
}
/// Returns true if a file or directory exists at this path, false otherwise
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let directory = path.join("foo")?;
///
/// assert!(!directory.exists().await?);
///
/// directory.create_dir().await?;
///
/// assert!(directory.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
pub async fn exists(&self) -> VfsResult<bool> {
self.fs.fs.exists(&self.path).await
}
/// Returns the filename portion of this path
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo/bar.txt")?;
///
/// assert_eq!(&file.filename(), "bar.txt");
///
/// # Ok::<(), VfsError>(())
pub fn filename(&self) -> String {
self.filename_internal()
}
/// Returns the extension portion of this path
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
///
/// assert_eq!(path.join("foo/bar.txt")?.extension(), Some("txt".to_string()));
/// assert_eq!(path.join("foo/bar.txt.zip")?.extension(), Some("zip".to_string()));
/// assert_eq!(path.join("foo/bar")?.extension(), None);
///
/// # Ok::<(), VfsError>(())
pub fn extension(&self) -> Option<String> {
self.extension_internal()
}
/// Returns the parent path of this portion of this path
///
/// Root will return itself.
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsFileType, VfsMetadata};
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
///
/// assert_eq!(path.parent(), path.root());
/// assert_eq!(path.join("foo/bar")?.parent(), path.join("foo")?);
/// assert_eq!(path.join("foo")?.parent(), path);
///
/// # Ok::<(), VfsError>(())
pub fn parent(&self) -> Self {
let parent_path = self.parent_internal(&self.path);
Self {
path: parent_path,
fs: self.fs.clone(),
}
}
/// Recursively iterates over all the directories and files at this path
///
/// Directories are visited before their children
///
/// Note that the iterator items can contain errors, usually when directories are removed during the iteration.
/// The returned paths may also point to non-existant files if there is concurrent removal.
///
/// Also note that loops in the file system hierarchy may cause this iterator to never terminate.
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::{VfsError, VfsResult};
/// use futures::stream::StreamExt;
/// # tokio_test::block_on(async {
/// let root = AsyncVfsPath::new(AsyncMemoryFS::new());
/// root.join("foo/bar")?.create_dir_all().await?;
/// root.join("fizz/buzz")?.create_dir_all().await?;
/// root.join("foo/bar/baz")?.create_file().await?;
///
/// let mut directories = root.walk_dir().await?.map(|res| res.unwrap()).collect::<Vec<_>>().await;
///
/// directories.sort_by_key(|path| path.as_str().to_string());
/// let expected = vec!["fizz", "fizz/buzz", "foo", "foo/bar", "foo/bar/baz"].iter().map(|path| root.join(path)).collect::<VfsResult<Vec<_>>>()?;
/// assert_eq!(directories, expected);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn walk_dir(&self) -> VfsResult<WalkDirIterator> {
Ok(WalkDirIterator {
inner: self.read_dir().await?,
todo: vec![],
prev_result: None,
metadata_fut: None,
read_dir_fut: None,
})
}
/// Reads a complete file to a string
///
/// Returns an error if the file does not exist or is not valid UTF-8
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use async_std::io::{ReadExt, WriteExt};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let file = path.join("foo.txt")?;
/// write!(file.create_file().await?, "Hello, world!").await?;
///
/// let result = file.read_to_string().await?;
///
/// assert_eq!(&result, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn read_to_string(&self) -> VfsResult<String> {
let metadata = self.metadata().await?;
if metadata.file_type != VfsFileType::File {
return Err(
VfsError::from(VfsErrorKind::Other("Path is a directory".into()))
.with_path(&self.path)
.with_context(|| "Could not read path"),
);
}
let mut result = String::with_capacity(metadata.len as usize);
self.open_file()
.await?
.read_to_string(&mut result)
.await
.map_err(|source| {
VfsError::from(source)
.with_path(&self.path)
.with_context(|| "Could not read path")
})?;
Ok(result)
}
/// Copies a file to a new destination
///
/// The destination must not exist, but its parent directory must
///
/// ```
/// use async_std::io::{ReadExt, WriteExt};
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let src = path.join("foo.txt")?;
/// write!(src.create_file().await?, "Hello, world!").await?;
/// let dest = path.join("bar.txt")?;
///
/// src.copy_file(&dest).await?;
///
/// assert_eq!(dest.read_to_string().await?, "Hello, world!");
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn copy_file(&self, destination: &AsyncVfsPath) -> VfsResult<()> {
async {
if destination.exists().await? {
return Err(VfsError::from(VfsErrorKind::Other(
"Destination exists already".into(),
))
.with_path(&self.path));
}
if Arc::ptr_eq(&self.fs, &destination.fs) {
let result = self.fs.fs.copy_file(&self.path, &destination.path).await;
match result {
Err(err) => match err.kind() {
VfsErrorKind::NotSupported => {
// continue
}
_ => return Err(err),
},
other => return other,
}
}
let mut src = self.open_file().await?;
let mut dest = destination.create_file().await?;
async_std::io::copy(&mut src, &mut dest)
.await
.map_err(|source| {
VfsError::from(source)
.with_path(&self.path)
.with_context(|| "Could not read path")
})?;
Ok(())
}
.await
.map_err(|err| {
err.with_path(&self.path).with_context(|| {
format!(
"Could not copy '{}' to '{}'",
self.as_str(),
destination.as_str()
)
})
})?;
Ok(())
}
/// Moves or renames a file to a new destination
///
/// The destination must not exist, but its parent directory must
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// use async_std::io::{ReadExt, WriteExt};
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let src = path.join("foo.txt")?;
/// write!(src.create_file().await?, "Hello, world!").await?;
/// let dest = path.join("bar.txt")?;
///
/// src.move_file(&dest).await?;
///
/// assert_eq!(dest.read_to_string().await?, "Hello, world!");
/// assert!(!src.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn move_file(&self, destination: &AsyncVfsPath) -> VfsResult<()> {
async {
if destination.exists().await? {
return Err(VfsError::from(VfsErrorKind::Other(
"Destination exists already".into(),
))
.with_path(&destination.path));
}
if Arc::ptr_eq(&self.fs, &destination.fs) {
let result = self.fs.fs.move_file(&self.path, &destination.path);
match result.await {
Err(err) => match err.kind() {
VfsErrorKind::NotSupported => {
// continue
}
_ => return Err(err),
},
other => return other,
}
}
let mut src = self.open_file().await?;
let mut dest = destination.create_file().await?;
async_std::io::copy(&mut src, &mut dest)
.await
.map_err(|source| {
VfsError::from(source)
.with_path(&self.path)
.with_context(|| "Could not read path")
})?;
self.remove_file().await?;
Ok(())
}
.await
.map_err(|err| {
err.with_path(&self.path).with_context(|| {
format!(
"Could not move '{}' to '{}'",
self.as_str(),
destination.as_str()
)
})
})?;
Ok(())
}
/// Copies a directory to a new destination, recursively
///
/// The destination must not exist, but the parent directory must
///
/// Returns the number of files copied
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let src = path.join("foo")?;
/// src.join("dir")?.create_dir_all().await?;
/// let dest = path.join("bar.txt")?;
///
/// src.copy_dir(&dest).await?;
///
/// assert!(dest.join("dir")?.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn copy_dir(&self, destination: &AsyncVfsPath) -> VfsResult<u64> {
let files_copied = async {
let mut files_copied = 0u64;
if destination.exists().await? {
return Err(VfsError::from(VfsErrorKind::Other(
"Destination exists already".into(),
))
.with_path(&destination.path));
}
destination.create_dir().await?;
let prefix = self.path.as_str();
let prefix_len = prefix.len();
let mut path_stream = self.walk_dir().await?;
while let Some(file) = path_stream.next().await {
let src_path: AsyncVfsPath = file?;
let dest_path = destination.join(&src_path.as_str()[prefix_len + 1..])?;
match src_path.metadata().await?.file_type {
VfsFileType::Directory => dest_path.create_dir().await?,
VfsFileType::File => src_path.copy_file(&dest_path).await?,
}
files_copied += 1;
}
Ok(files_copied)
}
.await
.map_err(|err| {
err.with_path(&self.path).with_context(|| {
format!(
"Could not copy directory '{}' to '{}'",
self.as_str(),
destination.as_str()
)
})
})?;
Ok(files_copied)
}
/// Moves a directory to a new destination, including subdirectories and files
///
/// The destination must not exist, but its parent directory must
///
/// ```
/// # use vfs::async_vfs::{AsyncMemoryFS, AsyncVfsPath};
/// # use vfs::VfsError;
/// # tokio_test::block_on(async {
/// let path = AsyncVfsPath::new(AsyncMemoryFS::new());
/// let src = path.join("foo")?;
/// src.join("dir")?.create_dir_all().await?;
/// let dest = path.join("bar.txt")?;
///
/// src.move_dir(&dest).await?;
///
/// assert!(dest.join("dir")?.exists().await?);
/// assert!(!src.join("dir")?.exists().await?);
/// # Ok::<(), VfsError>(())
/// # });
/// ```
pub async fn move_dir(&self, destination: &AsyncVfsPath) -> VfsResult<()> {
async {
if destination.exists().await? {
return Err(VfsError::from(VfsErrorKind::Other(
"Destination exists already".into(),
))
.with_path(&destination.path));
}
if Arc::ptr_eq(&self.fs, &destination.fs) {
let result = self.fs.fs.move_dir(&self.path, &destination.path).await;
match result {
Err(err) => match err.kind() {
VfsErrorKind::NotSupported => {
// continue
}
_ => return Err(err),
},
other => return other,
}
}
destination.create_dir().await?;
let prefix = self.path.as_str();
let prefix_len = prefix.len();
let mut path_stream = self.walk_dir().await?;
while let Some(file) = path_stream.next().await {
let src_path: AsyncVfsPath = file?;
let dest_path = destination.join(&src_path.as_str()[prefix_len + 1..])?;
match src_path.metadata().await?.file_type {
VfsFileType::Directory => dest_path.create_dir().await?,
VfsFileType::File => src_path.copy_file(&dest_path).await?,
}
}
self.remove_dir_all().await?;
Ok(())
}
.await
.map_err(|err| {
err.with_path(&self.path).with_context(|| {
format!(
"Could not move directory '{}' to '{}'",
self.as_str(),
destination.as_str()
)
})
})?;
Ok(())
}
}
/// An iterator for recursively walking a file hierarchy
pub struct WalkDirIterator {
/// the path iterator of the current directory
inner: Box<dyn Stream<Item = AsyncVfsPath> + Send + Unpin>,
/// stack of subdirectories still to walk
todo: Vec<AsyncVfsPath>,
/// used to store the previous yield of the todo stream,
/// which would otherwise get dropped if path.metadata() is pending
prev_result: Option<AsyncVfsPath>,
// Used to store futures when poll_next returns pending
// this ensures a new future is not spawned on each poll.
read_dir_fut: Option<
BoxFuture<'static, Result<Box<(dyn Stream<Item = AsyncVfsPath> + Send + Unpin)>, VfsError>>,
>,
metadata_fut: Option<BoxFuture<'static, Result<VfsMetadata, VfsError>>>,
}
impl std::fmt::Debug for WalkDirIterator {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("WalkDirIterator")?;
self.todo.fmt(f)
}
}
impl Stream for WalkDirIterator {
type Item = VfsResult<AsyncVfsPath>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
// Check if we have a previously stored result from last call
// that we could not utilize due to pending path.metadata() call
let result = if this.prev_result.is_none() {
loop {
match this.inner.poll_next_unpin(cx) {
Poll::Pending => return Poll::Pending,
Poll::Ready(Some(path)) => break Ok(path),
Poll::Ready(None) => {
let directory = if this.todo.is_empty() {
return Poll::Ready(None);
} else {
this.todo[this.todo.len() - 1].clone()
};
let mut read_dir_fut = if this.read_dir_fut.is_some() {
this.read_dir_fut.take().unwrap()
} else {
Box::pin(async move { directory.read_dir().await })
};
match read_dir_fut.poll_unpin(cx) {
Poll::Pending => {
this.read_dir_fut = Some(read_dir_fut);
return Poll::Pending;
}
Poll::Ready(Err(err)) => {
let _ = this.todo.pop();
break Err(err);
}
Poll::Ready(Ok(iterator)) => {
let _ = this.todo.pop();
this.inner = iterator;
}
}
}
}
}
} else {
Ok(this.prev_result.take().unwrap())
};
if let Ok(path) = &result {
let mut metadata_fut = if this.metadata_fut.is_some() {
this.metadata_fut.take().unwrap()
} else {
let path_clone = path.clone();
Box::pin(async move { path_clone.metadata().await })
};
match metadata_fut.poll_unpin(cx) {
Poll::Pending => {
this.prev_result = Some(path.clone());
this.metadata_fut = Some(metadata_fut);
return Poll::Pending;
}
Poll::Ready(Ok(meta)) => {
if meta.file_type == VfsFileType::Directory {
this.todo.push(path.clone());
}
}
Poll::Ready(Err(err)) => return Poll::Ready(Some(Err(err))),
}
}
Poll::Ready(Some(result))
}
}