├── .cliffignore ├── .github └── workflows │ ├── gen-changelog.yml │ └── test.yml ├── .gitignore ├── .vscode └── settings.json ├── CHANGELOG.md ├── Cargo.toml ├── LICENSE ├── README.md ├── cliff.toml ├── imgs ├── fat12.img ├── fat12.img.check ├── fat16.img ├── fat16.img.check ├── fat32.img ├── fat32.img.check ├── minfs.img └── minfs.img.check ├── src ├── error.rs ├── fs.rs ├── io.rs ├── lib.rs └── path.rs └── tests ├── bee movie script.txt ├── checksums.rs └── img_commons └── mod.rs /.cliffignore: -------------------------------------------------------------------------------- 1 | # remove Cargo.toml from workspace 2 | 3ab8735c80d6b1304afb7f09b74289fd0818bf8e 3 | 4 | # create README 5 | 9c49238f25ec6c97f79fa2dd7816691514b6eec7 6 | 7 | # create CHANGELOG and git-cliff config file 8 | ceabaf891782f99cb935748f8f7bcc18627b872d 9 | 10 | # upload FAT .img files 11 | 69bb0a8e3c34891401f1c746ca44fb902a7b0997 12 | 13 | # update .gitignore 14 | 21c7d6ba6e1336239efb875f9f157cd98f2066c6 15 | -------------------------------------------------------------------------------- /.github/workflows/gen-changelog.yml: -------------------------------------------------------------------------------- 1 | name: Update CHANGELOG.md weekly 2 | on: 3 | workflow_dispatch: 4 | schedule: 5 | - cron: "0 0 * * 0" 6 | 7 | jobs: 8 | changelog: 9 | name: Generate changelog 10 | runs-on: ubuntu-latest 11 | steps: 12 | - name: Checkout 13 | uses: actions/checkout@v4 14 | with: 15 | fetch-depth: 0 16 | 17 | - name: Generate a changelog 18 | uses: orhun/git-cliff-action@v3 19 | with: 20 | config: cliff.toml 21 | args: --verbose 22 | env: 23 | OUTPUT: CHANGELOG.md 24 | GITHUB_REPO: ${{ github.repository }} 25 | GITHUB_BRANCH: ${{ github.ref_name }} 26 | 27 | - name: Prepare commit 28 | run: | 29 | git config user.name 'github-actions[bot]' 30 | git config user.email 'github-actions[bot]@users.noreply.github.com' 31 | set +e 32 | git add CHANGELOG.md 33 | git commit -m "ci(changelog): Update changelog" 34 | 35 | - name: Push commit 36 | # if there is nothing to commit, `git commit` returns 1 37 | if: success() 38 | run: | 39 | git push https://${{ secrets.GITHUB_TOKEN }}@github.com/${GITHUB_REPOSITORY}.git ${GITHUB_BRANCH} 40 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Cargo Build & Test 2 | 3 | on: 4 | push: 5 | pull_request: 6 | types: [opened, reopened] 7 | workflow_dispatch: 8 | 9 | env: 10 | CARGO_TERM_COLOR: always 11 | RUST_LOG: trace 12 | 13 | jobs: 14 | test_all_features: 15 | name: Run tests - All features enabled 16 | runs-on: ubuntu-latest 17 | strategy: 18 | matrix: 19 | toolchain: 20 | - stable 21 | - beta 22 | - nightly 23 | steps: 24 | - uses: actions/checkout@v4 25 | - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} 26 | - run: cargo build --verbose --all-features 27 | - run: timeout 1m cargo test --verbose --all-features -- --nocapture 28 | test_no_default_features: 29 | name: Run tests - No features enabled 30 | runs-on: ubuntu-latest 31 | strategy: 32 | matrix: 33 | toolchain: 34 | - stable 35 | - beta 36 | - nightly 37 | steps: 38 | - uses: actions/checkout@v4 39 | - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} 40 | - run: cargo build --verbose --no-default-features --all-targets 41 | - run: timeout 1m cargo test --verbose --no-default-features --all-targets -- --nocapture 42 | 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Generated by Cargo 2 | # will have compiled files and executables 3 | debug/ 4 | target/ 5 | 6 | # Remove Cargo.lock from gitignore if creating an executable, leave it for libraries 7 | # More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html 8 | Cargo.lock 9 | 10 | # These are backup files generated by rustfmt 11 | **/*.rs.bk 12 | 13 | # MSVC Windows builds of rustc generate these, which store debugging information 14 | *.pdb 15 | 16 | # RustRover 17 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 18 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 19 | # and can be added to the global gitignore or merged into this file. For a more nuclear 20 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 21 | #.idea/ 22 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "[rust]": { 3 | "editor.defaultFormatter": "rust-lang.rust-analyzer", 4 | "editor.formatOnSave": true 5 | } 6 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. 4 | 5 | This changelog is automatically updated weekly by a cron job 6 | 7 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 8 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). 9 | 10 | ## [Unreleased] 11 | 12 | ### Changed 13 | 14 | - Make 2 consts public & document them ([983350d](983350de7aa3248cc2c1ce0d00fd404e8d777da5)) 15 | - Implement basic logging ([c849a6b](c849a6b256ef4848be0b09fe805b0ec262371b5f)) 16 | 17 | ### Fixed 18 | 19 | - Fix potential bug when File read stops at the end of a sector ([6eb9226](6eb922610fa5b1c76c61f7e20ff56039e310991b)) 20 | 21 | ## [0.1.0-alpha.1] - 2024-08-04 22 | 23 | ### Added 24 | 25 | - Add basic filesystem functionality ([a565da4](a565da4af6e11571bd2e2cd6f1072085630f9c63)) 26 | - Implement checksum validation for LFNs ([236db1b](236db1b97af7c4f8a4555263d6477f2de918e33d)) 27 | - Implement sector caching ([7a5a618](7a5a618218ba8a03076ce92332c77865ce2f9c72)) 28 | - Add basic documentation ([22af530](22af530e21555da1141d99184974894d62ad4d26)) 29 | - FAT12 support!!! ([6460079](646007928cacac6dd8112e0d8896fcd708673d23)) 30 | - Create new InternalFSError enum ([88a99a3](88a99a32281726c27fb027bf425b102741473c2c)) 31 | - Add missing docs & fix already-existing ones ([d1da5b0](d1da5b0ffaa4335b51da9ff390c2cdc1c6e6b328)) 32 | 33 | ### Changed 34 | 35 | - Use "time" crate for date & time handling ([b934c7b](b934c7b1db974cc07c730e1f508842918a3a9138)) 36 | - Pushing an absolute path replaces destination pathbuf ([278e60f](278e60f73977cdfd28fe263b7720508f98bd762d)) 37 | - IOError now have an IOErrorKind ([4ac6a95](4ac6a95424884e8a775a36590788a0897cfbba8d)) 38 | - In the Read trait, read_exact is now auto-implemented ([f9ca087](f9ca0873d58696c8244e9df6874d784922b1ab04)) 39 | - Correctly implement Read + Seek for File ([dd2823d](dd2823deff32a78a62f20bebc7c135ff42eb1502)) 40 | - Add a bunch of default implementations & make documentation more clear ([72cd1bd](72cd1bd6d38ebc20861bd078ab9115cf5545d4a0)) 41 | 42 | ### Fixed 43 | 44 | - Correctly handle forbidden/reserved filenames ([16b14d6](16b14d6ea4429c28d180cbf8eff0cc6ca7eb60b1)) 45 | - Due to a bug in the code, files larger than 1 cluster wouldn't be read properly ([3116e9d](3116e9d9d8bc53acdd7eab720a1a9f6bc74ebfd7)) 46 | - Calling Read on a File would sometimes "loop" the same cluster over and over again ([49a67d1](49a67d11b84a233b6f53d86715b2454198d39459)) 47 | - Fix potential endianess issue when transmuting an array ([54962a1](54962a1d13f746a5194234ae89f3c3c2194b168a)) 48 | 49 | [Unreleased]: https://github.com/Oakchris1955/simple-fatfs/compare/v0.1.0-alpha.1..HEAD 50 | 51 | [0.1.0-alpha.1]: https://github.com/Oakchris1955/simple-fatfs/tree/v0.1.0-alpha.1 52 | 53 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simple-fatfs" 3 | version = "0.1.0-alpha.1" 4 | edition = "2021" 5 | description = "A simple-to-use FAT filesystem library for Rust (mainly targeted at embedded systems)" 6 | license = "MIT" 7 | repository = "https://github.com/Oakchris1955/simple-fatfs" 8 | exclude = ["/.github", ".vscode", "/imgs", "**/*cliff*"] 9 | 10 | [dependencies] 11 | bincode = "1.3.3" 12 | bitfield-struct = "0.8.0" 13 | bitflags = { version = "2.6.0", features = ["serde"] } 14 | displaydoc = { version = "0.2.5", default-features = false } 15 | log = "0.4.22" 16 | serde = { version = "1.0.204", default-features = false, features = [ "alloc", "derive" ] } 17 | serde-big-array = "0.5.1" 18 | time = { version = "0.3.36", default-features = false, features = [ "alloc", "parsing", "macros" ]} 19 | 20 | [features] 21 | default = ["std"] 22 | std = ["displaydoc/std", "serde/std", "time/std"] 23 | 24 | [dev-dependencies] 25 | test-log = "0.2.16" 26 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Oakchris1955 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # simple-fatfs 2 | 3 | [![CI Status](https://github.com/Oakchris1955/simple-fatfs/actions/workflows/test.yml/badge.svg)](https://github.com/Oakchris1955/simple-fatfs/actions/workflows/test.yml) 4 | ![GitHub License](https://img.shields.io/github/license/Oakchris1955/simple-fatfs?color=blue) 5 | [![Crates.io Version](https://img.shields.io/crates/v/simple-fatfs)](https://crates.io/crates/simple-fatfs) 6 | [![docs.rs](https://docs.rs/simple-fatfs/badge.svg)](https://docs.rs/simple-fatfs) 7 | ![Crates.io MSRV](https://img.shields.io/crates/msrv/simple-fatfs) 8 | 9 | A simple-to-use filesystem driver for the File Allocation Table (FAT) 10 | 11 | ## Motive 12 | 13 | Apart from [rafalh's rust-fatfs] library, there aren't actually any other FAT filesystem drivers in [crates.io]. All the other libraries either support only FAT16/32, aren't being actively developed or are just bindings to some C library. 14 | 15 | Another thing I found somewhat frustrating about [rafalh's rust-fatfs] (which ultimately led to my decision of creating this project) is the fact that his library isn't suitable for embedded Rust, since it requires implementing [some weird kind of buffered Read/Write](https://github.com/rafalh/rust-fatfs/issues/94), while it is also worth mentioning that the [crates.io] version of his library is somewhat outdated (there have been 144 [additional commits](https://github.com/rafalh/rust-fatfs/compare/v0.3.6...master) as of the time I'm writing this). 16 | 17 | ## Intent 18 | 19 | A fully-working FAT driver that covers the following criteria: 20 | 21 | - An easy-to-use public API for developers 22 | - Avoids unnecessary/overbloated dependencies (I am talking about [leftpad](https://www.npmjs.com/package/left-pad)-like dependencies) 23 | - `#[no_std]` support 24 | - Auto-`impl`s for already-existing `std` APIs (like the `Read`, `Write` & `Seek` traits) 25 | - FAT12/16/32/ExFAT support 26 | - VFAT/LFN (long filename) support 27 | 28 | ## TODO 29 | 30 | - [x] FAT12 support (just handle entries between 2 sectors) 31 | - [x] Distinguish between dirs and files in paths (this must also be verified by the filesystem, just like in the `std`) 32 | - [ ] Check whether system endianness matters (FAT is little-endian) 33 | - [ ] Handle non-printable characters in names of files and directories 34 | - [ ] ExFAT support 35 | - [ ] when [feature(error_in_core)](https://github.com/rust-lang/rust/issues/103765) gets released to stable, bump MSRV & use the `core::error::Error` trait instead of our custom `error::Error` 36 | 37 | [crates.io]: https://crates.io 38 | [rafalh's rust-fatfs]: https://github.com/rafalh/rust-fatfs 39 | 40 | ## Acknowledgements 41 | 42 | This project adheres to [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/) (since commit `21c7d6b`, that is excluding the first two commits which don't actually contain any code). It also uses [git-cliff](https://github.com/orhun/git-cliff) to parse commit messages into a `CHANGELOG` 43 | 44 | ## License 45 | 46 | [MIT](LICENSE) 47 | -------------------------------------------------------------------------------- /cliff.toml: -------------------------------------------------------------------------------- 1 | # git-cliff ~ configuration file 2 | # https://git-cliff.org/docs/configuration 3 | 4 | [changelog] 5 | # template for the changelog footer 6 | header = """ 7 | # Changelog\n 8 | All notable changes to this project will be documented in this file. 9 | 10 | This changelog is automatically updated weekly by a cron job 11 | 12 | The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), 13 | and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).\n 14 | """ 15 | # template for the changelog body 16 | # https://keats.github.io/tera/docs/#introduction 17 | body = """ 18 | {% if version -%} 19 | ## [{{ version | trim_start_matches(pat="v") }}] - {{ timestamp | date(format="%Y-%m-%d") }} 20 | {% else -%} 21 | ## [Unreleased] 22 | {% endif -%} 23 | {% for group, commits in commits | filter(attribute="merge_commit", value=false) | group_by(attribute="group") %} 24 | ### {{ group | upper_first }} 25 | {% for commit in commits %} 26 | - {{ commit.message | upper_first }} \ 27 | ([{{ commit.id | truncate(length=7, end="") }}]({{ commit.id }}))\ 28 | {% endfor %} 29 | {% endfor %}\n 30 | """ 31 | # template for the changelog footer 32 | footer = """ 33 | {% for release in releases -%} 34 | {% if release.version -%} 35 | {% if release.previous.version -%} 36 | [{{ release.version | trim_start_matches(pat="v") }}]: \ 37 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ 38 | /compare/{{ release.previous.version }}..{{ release.version }} 39 | {% else %} 40 | [{{ release.version | trim_start_matches(pat="v") }}]: \ 41 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ 42 | /tree/{{ release.version }} 43 | {% endif -%} 44 | {% else -%} 45 | [Unreleased]: \ 46 | {% if release.previous.version -%} 47 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ 48 | /compare/{{ release.previous.version }}..HEAD 49 | {% else -%} 50 | https://github.com/{{ remote.github.owner }}/{{ remote.github.repo }}\ 51 | /tree/HEAD 52 | {% endif -%} 53 | {% endif -%} 54 | {% endfor %}\ 55 | 56 | """ 57 | # remove the leading and trailing whitespace from the templates 58 | trim = true 59 | 60 | [git] 61 | # parse the commits based on https://www.conventionalcommits.org 62 | conventional_commits = true 63 | # filter out the commits that are not conventional 64 | filter_unconventional = true 65 | # process each line of a commit as an individual commit 66 | split_commits = false 67 | # regex for parsing and grouping commits 68 | commit_parsers = [ 69 | { message = "^chore", skip = true }, 70 | { message = "^ci", skip = true }, 71 | { message = "^test", skip = true }, 72 | { message = "^refactor", skip = true }, 73 | { message = "^.*: add", group = "Added" }, 74 | { message = "^.*: create", group = "Added" }, 75 | { message = "^.*support", group = "Added" }, 76 | { message = "^.*: implement", group = "Added" }, 77 | { message = "^.*: remove", group = "Removed" }, 78 | { message = "^.*: delete", group = "Removed" }, 79 | { message = "^test", group = "Testing" }, 80 | { message = "^fix", group = "Fixed" }, 81 | { message = "^.*: fix", group = "Fixed" }, 82 | { message = "^.*", group = "Changed" }, 83 | ] 84 | # protect breaking changes from being skipped due to matching a skipping commit_parser 85 | protect_breaking_commits = false 86 | # filter out the commits that are not matched by commit parsers 87 | filter_commits = true 88 | # regex for matching git tags 89 | tag_pattern = "v[0-9].*" 90 | # regex for skipping tags 91 | skip_tags = "" 92 | # regex for ignoring tags 93 | ignore_tags = "" 94 | # sort the tags topologically 95 | topo_order = false 96 | # sort the commits inside sections by oldest/newest order 97 | sort_commits = "oldest" 98 | -------------------------------------------------------------------------------- /imgs/fat12.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oakchris1955/simple-fatfs/01dfd55e749a91da06d2c6f1cbe20fd74fc54677/imgs/fat12.img -------------------------------------------------------------------------------- /imgs/fat12.img.check: -------------------------------------------------------------------------------- 1 | ec293f4087abbd287e6024fc7918836de91ced075428f15688b9fb2462733990 *imgs/fat12.img -------------------------------------------------------------------------------- /imgs/fat16.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oakchris1955/simple-fatfs/01dfd55e749a91da06d2c6f1cbe20fd74fc54677/imgs/fat16.img -------------------------------------------------------------------------------- /imgs/fat16.img.check: -------------------------------------------------------------------------------- 1 | 4eddf671476c0ac64ca0ae3bf8837e07515082f89975ddd2ff43113fde521558 *imgs/fat16.img 2 | -------------------------------------------------------------------------------- /imgs/fat32.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oakchris1955/simple-fatfs/01dfd55e749a91da06d2c6f1cbe20fd74fc54677/imgs/fat32.img -------------------------------------------------------------------------------- /imgs/fat32.img.check: -------------------------------------------------------------------------------- 1 | 604b7fdf8131868241dc4f33817994838f0da4d9795d400c0120e460cf7897e1 *imgs/fat32.img -------------------------------------------------------------------------------- /imgs/minfs.img: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Oakchris1955/simple-fatfs/01dfd55e749a91da06d2c6f1cbe20fd74fc54677/imgs/minfs.img -------------------------------------------------------------------------------- /imgs/minfs.img.check: -------------------------------------------------------------------------------- 1 | b29d4eec202e503d5e68efccf1dfb6355ea35ca56b308f59a02a2c5730f92e4a *imgs/minfs.img 2 | -------------------------------------------------------------------------------- /src/error.rs: -------------------------------------------------------------------------------- 1 | #[cfg(not(feature = "std"))] 2 | use core::*; 3 | #[cfg(feature = "std")] 4 | use std::*; 5 | 6 | /// Base error type 7 | /// 8 | /// To be replaced with [`core::error`] when feature [`error_in_core`](https://github.com/rust-lang/rust/issues/103765) gets pushed to `stable` 9 | pub trait Error: fmt::Debug + fmt::Display {} 10 | 11 | #[cfg(feature = "std")] 12 | impl Error for std::io::Error {} 13 | 14 | /// Base IO error type 15 | pub trait IOError: Error { 16 | /// The type of the kind of this [`IOError`] 17 | type Kind: IOErrorKind; 18 | 19 | /// Construct a new [`IOError`] from an [`IOErrorKind`] and a `msg` 20 | fn new(kind: Self::Kind, msg: M) -> Self 21 | where 22 | M: fmt::Display; 23 | 24 | /// Get the kind of this [`IOError`] 25 | fn kind(&self) -> Self::Kind; 26 | } 27 | 28 | #[cfg(feature = "std")] 29 | impl IOError for std::io::Error { 30 | type Kind = std::io::ErrorKind; 31 | 32 | #[inline] 33 | fn new(kind: Self::Kind, msg: M) -> Self 34 | where 35 | M: fmt::Display, 36 | { 37 | std::io::Error::new(kind, msg.to_string()) 38 | } 39 | 40 | #[inline] 41 | fn kind(&self) -> Self::Kind { 42 | self.kind() 43 | } 44 | } 45 | 46 | /// The kind of an [`IOError`] 47 | pub trait IOErrorKind: PartialEq + Sized { 48 | /// Create a new `UnexpectedEOF` [`IOErrorKind`] 49 | fn new_unexpected_eof() -> Self; 50 | /// Create a new `Interrupted` [`IOErrorKind`] 51 | fn new_interrupted() -> Self; 52 | /// Create a new `InvalidData` [`IOErrorKind`] 53 | fn new_invalid_data() -> Self; 54 | 55 | #[inline] 56 | /// Check whether this [`IOErrorKind`] is of kind `UnexpectedEOF` 57 | fn is_unexpected_eof(&self) -> bool { 58 | self == &Self::new_unexpected_eof() 59 | } 60 | /// Check whether this [`IOErrorKind`] is of kind `Interrupted` 61 | #[inline] 62 | fn is_interrupted(&self) -> bool { 63 | self == &Self::new_interrupted() 64 | } 65 | /// Check whether this [`IOErrorKind`] is of kind `InvalidData` 66 | #[inline] 67 | fn is_invalid_data(&self) -> bool { 68 | self == &Self::new_invalid_data() 69 | } 70 | } 71 | 72 | #[cfg(feature = "std")] 73 | impl IOErrorKind for std::io::ErrorKind { 74 | #[inline] 75 | fn new_unexpected_eof() -> Self { 76 | std::io::ErrorKind::UnexpectedEof 77 | } 78 | #[inline] 79 | fn new_interrupted() -> Self { 80 | std::io::ErrorKind::Interrupted 81 | } 82 | #[inline] 83 | fn new_invalid_data() -> Self { 84 | std::io::ErrorKind::InvalidData 85 | } 86 | } 87 | 88 | /// An error type that denotes that there is something wrong 89 | /// with the filesystem's structure itself (perhaps the FS itself is malformed/corrupted) 90 | #[derive(Debug, Clone, Copy, displaydoc::Display)] 91 | pub enum InternalFSError { 92 | /// The storage medium isn't large enough to accompany a FAT filesystem 93 | StorageTooSmall, 94 | /// Invalid boot sector signature. Perhaps this isn't a FAT filesystem? 95 | InvalidBPBSig, 96 | /** 97 | Invalid FAT32 FSInfo signature. 98 | Perhaps the FSInfo structure or the FAT32 EBR's fat_info field is malformed? 99 | */ 100 | InvalidFSInfoSig, 101 | /// Encountered a malformed cluster chain 102 | MalformedClusterChain, 103 | } 104 | 105 | /// An error indicating that a filesystem-related operation has failed 106 | #[derive(Debug, displaydoc::Display)] 107 | pub enum FSError 108 | where 109 | I: IOError, 110 | { 111 | /// An internal FS error occured 112 | #[displaydoc("An internal FS error occured: {0}")] 113 | InternalFSError(InternalFSError), 114 | /** 115 | The [PathBuf](`crate::path::PathBuf`) provided is malformed. 116 | 117 | This is mostly an error variant used for internal testing. 118 | If you get this error, open an issue: 119 | */ 120 | MalformedPath, 121 | /** 122 | [`bincode`] errored out while (de)serializing 123 | 124 | This error variant should NEVER be raised. 125 | If you get this error, open an issue: 126 | */ 127 | BincodeError(bincode::Error), 128 | /// Expected a directory 129 | NotADirectory, 130 | /// Found a directory when we expected a file 131 | IsADirectory, 132 | /// A file or directory wasn't found 133 | NotFound, 134 | /// An IO error occured 135 | #[displaydoc("An IO error occured: {0}")] 136 | IOError(I), 137 | } 138 | 139 | impl From for FSError 140 | where 141 | I: IOError, 142 | { 143 | #[inline] 144 | fn from(value: I) -> Self { 145 | FSError::IOError(value) 146 | } 147 | } 148 | 149 | impl From for FSError 150 | where 151 | I: IOError, 152 | { 153 | #[inline] 154 | fn from(value: bincode::Error) -> Self { 155 | FSError::BincodeError(value) 156 | } 157 | } 158 | 159 | /// An alias for a [`Result`] with a [`FSError`] error type 160 | pub type FSResult = Result>; 161 | -------------------------------------------------------------------------------- /src/fs.rs: -------------------------------------------------------------------------------- 1 | #[cfg(not(feature = "std"))] 2 | use core::*; 3 | #[cfg(feature = "std")] 4 | use std::*; 5 | 6 | use ::alloc::{ 7 | borrow::ToOwned, 8 | format, 9 | string::{FromUtf16Error, String, ToString}, 10 | vec::*, 11 | }; 12 | 13 | use bitfield_struct::bitfield; 14 | use bitflags::bitflags; 15 | 16 | use bincode::Options as _; 17 | use serde::{Deserialize, Serialize}; 18 | use serde_big_array::BigArray; 19 | 20 | use ::time; 21 | use time::{Date, PrimitiveDateTime, Time}; 22 | 23 | use crate::{error::*, io::prelude::*, path::PathBuf}; 24 | 25 | /// The minimum size (in bytes) a sector is allowed to have 26 | pub const SECTOR_SIZE_MIN: usize = 512; 27 | /// The maximum size (in bytes) a sector is allowed to have 28 | pub const SECTOR_SIZE_MAX: usize = 4096; 29 | 30 | /// Place this in the BPB _jmpboot field to hang if a computer attempts to boot this partition 31 | /// The first two bytes jump to 0 on all bit modes and the third byte is just a NOP 32 | const INFINITE_LOOP: [u8; 3] = [0xEB, 0xFE, 0x90]; 33 | 34 | const BPBFAT_SIZE: usize = 36; 35 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 36 | struct BPBFAT { 37 | _jmpboot: [u8; 3], 38 | _oem_identifier: [u8; 8], 39 | bytes_per_sector: u16, 40 | sectors_per_cluster: u8, 41 | reserved_sector_count: u16, 42 | table_count: u8, 43 | root_entry_count: u16, 44 | // If this is 0, check `total_sectors_32` 45 | total_sectors_16: u16, 46 | _media_type: u8, 47 | table_size_16: u16, 48 | _sectors_per_track: u16, 49 | _head_side_count: u16, 50 | hidden_sector_count: u32, 51 | total_sectors_32: u32, 52 | } 53 | 54 | #[derive(Debug)] 55 | enum BootRecord { 56 | FAT(BootRecordFAT), 57 | ExFAT(BootRecordExFAT), 58 | } 59 | 60 | impl BootRecord { 61 | #[inline] 62 | /// The FAT type of this file system 63 | pub(crate) fn fat_type(&self) -> FATType { 64 | match self { 65 | BootRecord::FAT(boot_record_fat) => { 66 | let total_clusters = boot_record_fat.total_clusters(); 67 | if total_clusters < 4085 { 68 | FATType::FAT12 69 | } else if total_clusters < 65525 { 70 | FATType::FAT16 71 | } else { 72 | FATType::FAT32 73 | } 74 | } 75 | BootRecord::ExFAT(_boot_record_exfat) => { 76 | todo!("ExFAT not yet implemented"); 77 | FATType::ExFAT 78 | } 79 | } 80 | } 81 | } 82 | 83 | const BOOT_SIGNATURE: u8 = 0x29; 84 | const FAT_SIGNATURE: u16 = 0x55AA; 85 | 86 | #[derive(Debug, Clone, Copy)] 87 | struct BootRecordFAT { 88 | bpb: BPBFAT, 89 | ebr: EBR, 90 | } 91 | 92 | impl BootRecordFAT { 93 | #[inline] 94 | fn verify_signature(&self) -> bool { 95 | match self.fat_type() { 96 | FATType::FAT12 | FATType::FAT16 | FATType::FAT32 => match self.ebr { 97 | EBR::FAT12_16(ebr_fat12_16) => { 98 | ebr_fat12_16.boot_signature == BOOT_SIGNATURE 99 | && ebr_fat12_16.signature == FAT_SIGNATURE 100 | } 101 | EBR::FAT32(ebr_fat32, _) => { 102 | ebr_fat32.boot_signature == BOOT_SIGNATURE 103 | && ebr_fat32.signature == FAT_SIGNATURE 104 | } 105 | }, 106 | FATType::ExFAT => todo!("ExFAT not yet implemented"), 107 | } 108 | } 109 | 110 | #[inline] 111 | /// Total sectors in volume (including VBR)s 112 | pub(crate) fn total_sectors(&self) -> u32 { 113 | if self.bpb.total_sectors_16 == 0 { 114 | self.bpb.total_sectors_32 115 | } else { 116 | self.bpb.total_sectors_16 as u32 117 | } 118 | } 119 | 120 | #[inline] 121 | /// FAT size in sectors 122 | pub(crate) fn fat_sector_size(&self) -> u32 { 123 | match self.ebr { 124 | EBR::FAT12_16(_ebr_fat12_16) => self.bpb.table_size_16.into(), 125 | EBR::FAT32(ebr_fat32, _) => ebr_fat32.table_size_32, 126 | } 127 | } 128 | 129 | #[inline] 130 | /// The size of the root directory (unless we have FAT32, in which case the size will be 0) 131 | /// This calculation will round up 132 | pub(crate) fn root_dir_sectors(&self) -> u16 { 133 | // 32 is the size of a directory entry in bytes 134 | ((self.bpb.root_entry_count * 32) + (self.bpb.bytes_per_sector - 1)) 135 | / self.bpb.bytes_per_sector 136 | } 137 | 138 | #[inline] 139 | /// The first sector in the File Allocation Table 140 | pub(crate) fn first_fat_sector(&self) -> u16 { 141 | self.bpb.reserved_sector_count 142 | } 143 | 144 | #[inline] 145 | /// The first sector of the root directory 146 | pub(crate) fn first_root_dir_sector(&self) -> u16 { 147 | self.first_fat_sector() + self.bpb.table_count as u16 * self.fat_sector_size() as u16 148 | } 149 | 150 | #[inline] 151 | /// The first data sector (that is, the first sector in which directories and files may be stored) 152 | pub(crate) fn first_data_sector(&self) -> u16 { 153 | self.first_root_dir_sector() + self.root_dir_sectors() 154 | } 155 | 156 | #[inline] 157 | /// The total number of data sectors 158 | pub(crate) fn total_data_sectors(&self) -> u32 { 159 | self.total_sectors() - (self.bpb.table_count as u32 * self.fat_sector_size()) 160 | + self.root_dir_sectors() as u32 161 | } 162 | 163 | #[inline] 164 | /// The total number of clusters 165 | pub(crate) fn total_clusters(&self) -> u32 { 166 | self.total_data_sectors() / self.bpb.sectors_per_cluster as u32 167 | } 168 | 169 | #[inline] 170 | /// The FAT type of this file system 171 | pub(crate) fn fat_type(&self) -> FATType { 172 | if self.bpb.bytes_per_sector == 0 { 173 | todo!("ExFAT not yet implemented"); 174 | FATType::ExFAT 175 | } else { 176 | let total_clusters = self.total_clusters(); 177 | if total_clusters < 4085 { 178 | FATType::FAT12 179 | } else if total_clusters < 65525 { 180 | FATType::FAT16 181 | } else { 182 | FATType::FAT32 183 | } 184 | } 185 | } 186 | } 187 | 188 | #[derive(Debug, Clone, Copy)] 189 | // Everything here is naturally aligned (thank god) 190 | struct BootRecordExFAT { 191 | _dummy_jmp: [u8; 3], 192 | _oem_identifier: [u8; 8], 193 | _zeroed: [u8; 53], 194 | _partition_offset: u64, 195 | volume_len: u64, 196 | fat_offset: u32, 197 | fat_len: u32, 198 | cluster_heap_offset: u32, 199 | cluster_count: u32, 200 | root_dir_cluster: u32, 201 | partition_serial_num: u32, 202 | fs_revision: u16, 203 | flags: u16, 204 | sector_shift: u8, 205 | cluster_shift: u8, 206 | fat_count: u8, 207 | drive_select: u8, 208 | used_percentage: u8, 209 | _reserved: [u8; 7], 210 | } 211 | 212 | const EBR_SIZE: usize = 512 - BPBFAT_SIZE; 213 | #[derive(Clone, Copy)] 214 | enum EBR { 215 | FAT12_16(EBRFAT12_16), 216 | FAT32(EBRFAT32, FSInfoFAT32), 217 | } 218 | 219 | impl fmt::Debug for EBR { 220 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 221 | // TODO: find a good way of printing this 222 | write!(f, "FAT12-16/32 Extended boot record...") 223 | } 224 | } 225 | 226 | #[derive(Deserialize, Serialize, Clone, Copy)] 227 | struct EBRFAT12_16 { 228 | _drive_num: u8, 229 | _windows_nt_flags: u8, 230 | boot_signature: u8, 231 | volume_serial_num: u32, 232 | volume_label: [u8; 11], 233 | _system_identifier: [u8; 8], 234 | #[serde(with = "BigArray")] 235 | _boot_code: [u8; 448], 236 | signature: u16, 237 | } 238 | 239 | // FIXME: these might be the other way around 240 | #[derive(Deserialize, Serialize, Debug, Clone, Copy)] 241 | struct FATVersion { 242 | minor: u8, 243 | major: u8, 244 | } 245 | 246 | #[derive(Deserialize, Serialize, Clone, Copy)] 247 | struct EBRFAT32 { 248 | table_size_32: u32, 249 | _extended_flags: u16, 250 | fat_version: FATVersion, 251 | root_cluster: u32, 252 | fat_info: u16, 253 | backup_boot_sector: u16, 254 | _reserved: [u8; 12], 255 | _drive_num: u8, 256 | _windows_nt_flags: u8, 257 | boot_signature: u8, 258 | volume_serial_num: u32, 259 | volume_label: [u8; 11], 260 | _system_ident: [u8; 8], 261 | #[serde(with = "BigArray")] 262 | _boot_code: [u8; 420], 263 | signature: u16, 264 | } 265 | 266 | const FSINFO_LEAD_SIGNATURE: u32 = 0x41615252; 267 | const FSINFO_MID_SIGNATURE: u32 = 0x61417272; 268 | const FSINFO_TRAIL_SIGNAUTE: u32 = 0xAA550000; 269 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 270 | struct FSInfoFAT32 { 271 | lead_signature: u32, 272 | #[serde(with = "BigArray")] 273 | _reserved1: [u8; 480], 274 | mid_signature: u32, 275 | last_free_cluster: u32, 276 | cluster_width: u32, 277 | _reserved2: [u8; 12], 278 | trail_signature: u32, 279 | } 280 | 281 | impl FSInfoFAT32 { 282 | fn verify_signature(&self) -> bool { 283 | self.lead_signature == FSINFO_LEAD_SIGNATURE 284 | && self.mid_signature == FSINFO_MID_SIGNATURE 285 | && self.trail_signature == FSINFO_TRAIL_SIGNAUTE 286 | } 287 | } 288 | 289 | /// An enum representing different versions of the FAT filesystem 290 | #[derive(Debug, Clone, Copy, PartialEq)] 291 | // no need for enum variant documentation here 292 | #[allow(missing_docs)] 293 | pub enum FATType { 294 | FAT12, 295 | FAT16, 296 | FAT32, 297 | ExFAT, 298 | } 299 | 300 | impl FATType { 301 | #[inline] 302 | /// How many bits this [`FATType`] uses to address clusters in the disk 303 | pub fn bits_per_entry(&self) -> u8 { 304 | match self { 305 | FATType::FAT12 => 12, 306 | FATType::FAT16 => 16, 307 | FATType::FAT32 => 28, 308 | FATType::ExFAT => 32, 309 | } 310 | } 311 | } 312 | 313 | #[derive(Debug, Clone, PartialEq)] 314 | enum FATEntry { 315 | /// This cluster is free 316 | Free, 317 | /// This cluster is allocated and the next cluster is the contained value 318 | Allocated(u32), 319 | /// This cluster is reserved 320 | Reserved, 321 | /// This is a bad (defective) cluster 322 | Bad, 323 | /// This cluster is allocated and is the final cluster of the file 324 | EOF, 325 | } 326 | 327 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 328 | struct SFN { 329 | name: [u8; 8], 330 | ext: [u8; 3], 331 | } 332 | 333 | impl SFN { 334 | fn get_byte_slice(&self) -> [u8; 11] { 335 | let mut slice = [0; 11]; 336 | 337 | slice[..8].copy_from_slice(&self.name); 338 | slice[8..].copy_from_slice(&self.ext); 339 | 340 | slice 341 | } 342 | 343 | fn gen_checksum(&self) -> u8 { 344 | let mut sum = 0; 345 | 346 | for c in self.get_byte_slice() { 347 | sum = (if (sum & 1) != 0 { 0x80_u8 } else { 0_u8 }) 348 | .wrapping_add(sum >> 1) 349 | .wrapping_add(c) 350 | } 351 | 352 | log::debug!("SFN checksum: {:X}", sum); 353 | 354 | sum 355 | } 356 | } 357 | 358 | impl fmt::Display for SFN { 359 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 360 | // we begin by writing the name (even if it is padded with spaces, they will be trimmed, so we don't care) 361 | write!(f, "{}", String::from_utf8_lossy(&self.name).trim())?; 362 | 363 | // then, if the extension isn't empty (padded with zeroes), we write it too 364 | let ext = String::from_utf8_lossy(&self.ext).trim().to_owned(); 365 | if !ext.is_empty() { 366 | write!(f, ".{}", ext)?; 367 | }; 368 | 369 | Ok(()) 370 | } 371 | } 372 | 373 | bitflags! { 374 | /// A list of the various attributes specified for a file/directory 375 | /// 376 | /// To check whether a given [`Attributes`] struct contains a flag, use the [`contains()`](Attributes::contains()) method 377 | /// 378 | /// Generated using [bitflags](https://docs.rs/bitflags/2.6.0/bitflags/) 379 | #[derive(Deserialize, Serialize, Debug, Clone, Copy, PartialEq)] 380 | pub struct Attributes: u8 { 381 | /// This entry is read-only 382 | const READ_ONLY = 0x01; 383 | /// This entry is normally hidden 384 | const HIDDEN = 0x02; 385 | /// This entry is a system file 386 | const SYSTEM = 0x04; 387 | /// This entry represents the volume's ID. 388 | /// This is used internally and the library will never return such an entry 389 | const VOLUME_ID = 0x08; 390 | /// This entry is a directory. You should normally use a [`PathBuf`]s [`is_dir()`](PathBuf::is_dir) method instead 391 | const DIRECTORY = 0x10; 392 | /// This entry is marked to be archived. Used by archiving software for backing up files and directories 393 | const ARCHIVE = 0x20; 394 | 395 | /// This entry is part of a LFN (long filename). Used internally 396 | const LFN = Self::READ_ONLY.bits() | 397 | Self::HIDDEN.bits() | 398 | Self::SYSTEM.bits() | 399 | Self::VOLUME_ID.bits(); 400 | } 401 | } 402 | 403 | const START_YEAR: i32 = 1980; 404 | 405 | #[bitfield(u16)] 406 | #[derive(Serialize, Deserialize)] 407 | struct TimeAttribute { 408 | /// Multiply by 2 409 | #[bits(5)] 410 | seconds: u8, 411 | #[bits(6)] 412 | minutes: u8, 413 | #[bits(5)] 414 | hour: u8, 415 | } 416 | 417 | #[bitfield(u16)] 418 | #[derive(Serialize, Deserialize)] 419 | struct DateAttribute { 420 | #[bits(5)] 421 | day: u8, 422 | #[bits(4)] 423 | month: u8, 424 | #[bits(7)] 425 | year: u8, 426 | } 427 | 428 | impl TryFrom for Time { 429 | type Error = (); 430 | 431 | fn try_from(value: TimeAttribute) -> Result { 432 | time::parsing::Parsed::new() 433 | .with_hour_24(value.hour()) 434 | .and_then(|parsed| parsed.with_minute(value.minutes())) 435 | .and_then(|parsed| parsed.with_second(value.seconds() * 2)) 436 | .map(|parsed| parsed.try_into().ok()) 437 | .flatten() 438 | .ok_or(()) 439 | } 440 | } 441 | 442 | impl TryFrom for Date { 443 | type Error = (); 444 | 445 | fn try_from(value: DateAttribute) -> Result { 446 | time::parsing::Parsed::new() 447 | .with_year(i32::from(value.year()) + START_YEAR) 448 | .and_then(|parsed| parsed.with_month(value.month().try_into().ok()?)) 449 | .and_then(|parsed| parsed.with_day(num::NonZeroU8::new(value.day())?)) 450 | .map(|parsed| parsed.try_into().ok()) 451 | .flatten() 452 | .ok_or(()) 453 | } 454 | } 455 | 456 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 457 | struct EntryCreationTime { 458 | hundredths_of_second: u8, 459 | time: TimeAttribute, 460 | date: DateAttribute, 461 | } 462 | 463 | impl TryFrom for PrimitiveDateTime { 464 | type Error = (); 465 | 466 | fn try_from(value: EntryCreationTime) -> Result { 467 | let mut time: Time = value.time.try_into()?; 468 | 469 | let new_seconds = time.second() + value.hundredths_of_second / 100; 470 | let milliseconds = u16::from(value.hundredths_of_second) % 100 * 10; 471 | time = time 472 | .replace_second(new_seconds) 473 | .map_err(|_| ())? 474 | .replace_millisecond(milliseconds) 475 | .map_err(|_| ())?; 476 | 477 | let date: Date = value.date.try_into()?; 478 | 479 | Ok(PrimitiveDateTime::new(date, time)) 480 | } 481 | } 482 | 483 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 484 | struct EntryModificationTime { 485 | time: TimeAttribute, 486 | date: DateAttribute, 487 | } 488 | 489 | impl TryFrom for PrimitiveDateTime { 490 | type Error = (); 491 | 492 | fn try_from(value: EntryModificationTime) -> Result { 493 | Ok(PrimitiveDateTime::new( 494 | value.date.try_into()?, 495 | value.time.try_into()?, 496 | )) 497 | } 498 | } 499 | 500 | #[derive(Serialize, Deserialize, Debug, Clone, Copy)] 501 | struct FATDirEntry { 502 | sfn: SFN, 503 | attributes: Attributes, 504 | _reserved: [u8; 1], 505 | created: EntryCreationTime, 506 | accessed: DateAttribute, 507 | cluster_high: u16, 508 | modified: EntryModificationTime, 509 | cluster_low: u16, 510 | file_size: u32, 511 | } 512 | 513 | #[derive(Debug, Deserialize, Serialize)] 514 | struct LFNEntry { 515 | /// masked with 0x40 if this is the last entry 516 | order: u8, 517 | first_chars: [u8; 10], 518 | /// Always equals 0x0F 519 | _lfn_attribute: u8, 520 | /// Both OSDev and the FAT specification say this is always 0 521 | _long_entry_type: u8, 522 | /// If this doesn't match with the computed cksum, then the set of LFNs is considered corrupt 523 | /// 524 | /// A [`LFNEntry`] will be marked as corrupt even if it isn't, if the SFN is modifed by a legacy system, 525 | /// since the new SFN's signature and the one on this field won't (probably) match 526 | checksum: u8, 527 | mid_chars: [u8; 12], 528 | _zeroed: [u8; 2], 529 | last_chars: [u8; 4], 530 | } 531 | 532 | impl LFNEntry { 533 | fn get_byte_slice(&self) -> [u16; 13] { 534 | let mut slice = [0_u8; 13 * mem::size_of::()]; 535 | 536 | slice[..10].copy_from_slice(&self.first_chars); 537 | slice[10..22].copy_from_slice(&self.mid_chars); 538 | slice[22..].copy_from_slice(&self.last_chars); 539 | 540 | let mut out_slice = [0_u16; 13]; 541 | for (i, chunk) in slice.chunks(mem::size_of::()).enumerate() { 542 | out_slice[i] = u16::from_le_bytes(chunk.try_into().unwrap()); 543 | } 544 | 545 | out_slice 546 | } 547 | 548 | #[inline] 549 | fn verify_signature(&self) -> bool { 550 | self._long_entry_type == 0 && self._zeroed.iter().all(|v| *v == 0) 551 | } 552 | } 553 | 554 | /// A resolved file/directory entry (for internal usage only) 555 | #[derive(Debug)] 556 | struct RawProperties { 557 | name: String, 558 | is_dir: bool, 559 | attributes: Attributes, 560 | created: PrimitiveDateTime, 561 | modified: PrimitiveDateTime, 562 | accessed: Date, 563 | file_size: u32, 564 | data_cluster: u32, 565 | } 566 | 567 | /// A container for file/directory properties 568 | #[derive(Debug)] 569 | pub struct Properties { 570 | path: PathBuf, 571 | attributes: Attributes, 572 | created: PrimitiveDateTime, 573 | modified: PrimitiveDateTime, 574 | accessed: Date, 575 | file_size: u32, 576 | data_cluster: u32, 577 | } 578 | 579 | /// Getter methods 580 | impl Properties { 581 | #[inline] 582 | /// Get the corresponding [`PathBuf`] to this entry 583 | pub fn path(&self) -> &PathBuf { 584 | &self.path 585 | } 586 | 587 | #[inline] 588 | /// Get the corresponding [`Attributes`] to this entry 589 | pub fn attributes(&self) -> &Attributes { 590 | &self.attributes 591 | } 592 | 593 | #[inline] 594 | /// Find out when this entry was created (max resolution: 1ms) 595 | /// 596 | /// Returns a [`PrimitiveDateTime`] from the [`time`] crate 597 | pub fn creation_time(&self) -> &PrimitiveDateTime { 598 | &self.created 599 | } 600 | 601 | #[inline] 602 | /// Find out when this entry was last modified (max resolution: 2 secs) 603 | /// 604 | /// Returns a [`PrimitiveDateTime`] from the [`time`] crate 605 | pub fn modification_time(&self) -> &PrimitiveDateTime { 606 | &self.modified 607 | } 608 | 609 | #[inline] 610 | /// Find out when this entry was last accessed (max resolution: 1 day) 611 | /// 612 | /// Returns a [`Date`] from the [`time`] crate 613 | pub fn last_accessed_date(&self) -> &Date { 614 | &self.accessed 615 | } 616 | 617 | #[inline] 618 | /// Find out the size of this entry 619 | /// 620 | /// Always returns `0` for directories 621 | pub fn file_size(&self) -> u32 { 622 | self.file_size 623 | } 624 | } 625 | 626 | /// Serialization methods 627 | impl Properties { 628 | #[inline] 629 | fn from_raw(raw: RawProperties, path: PathBuf) -> Self { 630 | Properties { 631 | path, 632 | attributes: raw.attributes, 633 | created: raw.created, 634 | modified: raw.modified, 635 | accessed: raw.accessed, 636 | file_size: raw.file_size, 637 | data_cluster: raw.data_cluster, 638 | } 639 | } 640 | } 641 | 642 | /// A thin wrapper for [`Properties`] represing a directory entry 643 | #[derive(Debug)] 644 | pub struct DirEntry { 645 | entry: Properties, 646 | } 647 | 648 | impl ops::Deref for DirEntry { 649 | type Target = Properties; 650 | 651 | #[inline] 652 | fn deref(&self) -> &Self::Target { 653 | &self.entry 654 | } 655 | } 656 | 657 | /// A file within the FAT filesystem 658 | #[derive(Debug)] 659 | pub struct File<'a, S> 660 | where 661 | S: Read + Write + Seek, 662 | { 663 | fs: &'a mut FileSystem, 664 | entry: Properties, 665 | /// the byte offset of the R/W pointer 666 | offset: u64, 667 | current_cluster: u32, 668 | } 669 | 670 | impl ops::Deref for File<'_, S> 671 | where 672 | S: Read + Write + Seek, 673 | { 674 | type Target = Properties; 675 | 676 | fn deref(&self) -> &Self::Target { 677 | &self.entry 678 | } 679 | } 680 | 681 | impl IOBase for File<'_, S> 682 | where 683 | S: Read + Write + Seek, 684 | { 685 | type Error = S::Error; 686 | } 687 | 688 | /// Internal functions 689 | impl File<'_, S> 690 | where 691 | S: Read + Write + Seek, 692 | { 693 | /// Panics if the current cluser doesn't point to another clluster 694 | fn next_cluster(&mut self) -> Result<(), ::Error> { 695 | match self.fs.read_nth_FAT_entry(self.current_cluster)? { 696 | FATEntry::Allocated(next_cluster) => self.current_cluster = next_cluster, 697 | // when a `File` is created, `cluster_chain_is_healthy` is called, if it fails, that File is dropped 698 | _ => unreachable!(), 699 | }; 700 | 701 | Ok(()) 702 | } 703 | } 704 | 705 | impl Read for File<'_, S> 706 | where 707 | S: Read + Write + Seek, 708 | { 709 | fn read(&mut self, buf: &mut [u8]) -> Result { 710 | let mut bytes_read = 0; 711 | // this is the maximum amount of bytes that can be read 712 | let read_cap = cmp::min(buf.len(), self.file_size as usize - self.offset as usize); 713 | log::debug!("Byte read cap set to {}", read_cap); 714 | 715 | 'outer: loop { 716 | let sector_init_offset = u32::try_from(self.offset % self.fs.cluster_size()).unwrap() 717 | / self.fs.sector_size(); 718 | let first_sector_of_cluster = self 719 | .fs 720 | .data_cluster_to_partition_sector(self.current_cluster) 721 | + sector_init_offset; 722 | let last_sector_of_cluster = first_sector_of_cluster 723 | + self.fs.sectors_per_cluster() as u32 724 | - sector_init_offset 725 | - 1; 726 | log::debug!( 727 | "Reading cluster {} from sectors {} to {}", 728 | self.current_cluster, 729 | first_sector_of_cluster, 730 | last_sector_of_cluster 731 | ); 732 | 733 | for sector in first_sector_of_cluster..=last_sector_of_cluster { 734 | self.fs.read_nth_sector(sector.into())?; 735 | 736 | let start_index = self.offset as usize % self.fs.sector_size() as usize; 737 | let bytes_to_read = cmp::min( 738 | read_cap - bytes_read, 739 | self.fs.sector_size() as usize - start_index, 740 | ); 741 | log::debug!( 742 | "Gonna read {} bytes from sector {} starting at byte {}", 743 | bytes_to_read, 744 | sector, 745 | start_index 746 | ); 747 | 748 | buf[bytes_read..bytes_read + bytes_to_read].copy_from_slice( 749 | &self.fs.sector_buffer[start_index..start_index + bytes_to_read], 750 | ); 751 | 752 | bytes_read += bytes_to_read; 753 | self.offset += bytes_to_read as u64; 754 | 755 | // if we have read as many bytes as we want... 756 | if bytes_read >= read_cap { 757 | // ...but we must process get the next cluster for future uses, 758 | // we do that before breaking 759 | if self.offset % self.fs.cluster_size() == 0 760 | && self.offset < self.file_size.into() 761 | { 762 | self.next_cluster()?; 763 | } 764 | 765 | break 'outer; 766 | } 767 | } 768 | 769 | self.next_cluster()?; 770 | } 771 | 772 | Ok(bytes_read) 773 | } 774 | 775 | // the default `read_to_end` implementation isn't efficient enough, so we just do this 776 | fn read_to_end(&mut self, buf: &mut Vec) -> Result { 777 | let bytes_to_read = self.file_size as usize - self.offset as usize; 778 | let init_buf_len = buf.len(); 779 | 780 | // resize buffer to fit the file contents exactly 781 | buf.resize(init_buf_len + bytes_to_read, 0); 782 | 783 | // this is guaranteed not to raise an EOF (although other error kinds might be raised...) 784 | self.read_exact(&mut buf[init_buf_len..])?; 785 | 786 | Ok(bytes_to_read) 787 | } 788 | } 789 | 790 | impl Seek for File<'_, S> 791 | where 792 | S: Read + Write + Seek, 793 | { 794 | fn seek(&mut self, pos: SeekFrom) -> Result { 795 | let mut offset = match pos { 796 | SeekFrom::Start(offset) => offset, 797 | SeekFrom::Current(offset) => { 798 | let offset = self.offset as i64 + offset; 799 | offset.try_into().unwrap_or(u64::MIN) 800 | } 801 | SeekFrom::End(offset) => { 802 | let offset = self.file_size as i64 + offset; 803 | offset.try_into().unwrap_or(u64::MIN) 804 | } 805 | }; 806 | 807 | if offset > self.file_size.into() { 808 | log::debug!("Capping cursor offset to file_size"); 809 | offset = self.file_size.into(); 810 | } 811 | 812 | log::debug!( 813 | "Previous cursor offset is {}, new cursor offset is {}", 814 | self.offset, 815 | offset 816 | ); 817 | 818 | use cmp::Ordering; 819 | match offset.cmp(&self.offset) { 820 | Ordering::Less => { 821 | // here, we basically "rewind" back to the start of the file and then seek to where we want 822 | // this of course has performance issues, so TODO: find a solution that is both memory & time efficient 823 | // (perhaps we could follow a similar approach to elm-chan's FATFS, by using a cluster link map table, perhaps as an optional feature) 824 | self.offset = 0; 825 | self.current_cluster = self.data_cluster; 826 | self.seek(pos)?; 827 | } 828 | Ordering::Equal => (), 829 | Ordering::Greater => { 830 | for _ in (self.offset / self.fs.cluster_size()..offset / self.fs.cluster_size()) 831 | .step_by(self.fs.cluster_size() as usize) 832 | { 833 | self.next_cluster()?; 834 | } 835 | self.offset = offset; 836 | } 837 | } 838 | 839 | Ok(self.offset) 840 | } 841 | } 842 | 843 | impl File<'_, S> 844 | where 845 | S: Read + Write + Seek, 846 | { 847 | fn cluster_chain_is_healthy(&mut self) -> Result { 848 | let mut current_cluster = self.entry.data_cluster; 849 | let mut cluster_count = 0; 850 | 851 | loop { 852 | cluster_count += 1; 853 | 854 | if cluster_count * self.fs.cluster_size() >= self.file_size.into() { 855 | break; 856 | } 857 | 858 | match self.fs.read_nth_FAT_entry(current_cluster)? { 859 | FATEntry::Allocated(next_cluster) => current_cluster = next_cluster, 860 | _ => return Ok(false), 861 | }; 862 | } 863 | 864 | Ok(true) 865 | } 866 | } 867 | 868 | /// variation of https://stackoverflow.com/a/42067321/19247098 for processing LFNs 869 | pub(crate) fn string_from_lfn(utf16_src: &[u16]) -> Result { 870 | let nul_range_end = utf16_src 871 | .iter() 872 | .position(|c| *c == 0x0000) 873 | .unwrap_or(utf16_src.len()); // default to length if no `\0` present 874 | 875 | String::from_utf16(&utf16_src[0..nul_range_end]) 876 | } 877 | 878 | trait OffsetConversions { 879 | fn sector_size(&self) -> u32; 880 | fn cluster_size(&self) -> u64; 881 | fn first_data_sector(&self) -> u32; 882 | 883 | #[inline] 884 | fn cluster_to_sector(&self, cluster: u64) -> u32 { 885 | (cluster * self.cluster_size() / self.sector_size() as u64) 886 | .try_into() 887 | .unwrap() 888 | } 889 | 890 | #[inline] 891 | fn sectors_per_cluster(&self) -> u64 { 892 | self.cluster_size() / self.sector_size() as u64 893 | } 894 | 895 | // this function assumes that the first sector is also the first sector of the partition 896 | #[inline] 897 | fn sector_to_partition_offset(&self, sector: u32) -> u32 { 898 | sector * self.sector_size() 899 | } 900 | 901 | // these three functions assume that the first sector (or cluster) is the first sector (or cluster) of the data area 902 | #[inline] 903 | fn data_cluster_to_partition_sector(&self, cluster: u32) -> u32 { 904 | self.cluster_to_sector((cluster - 2).into()) + self.first_data_sector() 905 | } 906 | } 907 | 908 | /// Some generic properties common across all FAT versions, like a sector's size, are cached here 909 | #[derive(Debug)] 910 | struct FSProperties { 911 | sector_size: u32, 912 | cluster_size: u64, 913 | total_clusters: u32, 914 | /// sector offset of the FAT 915 | fat_offset: u32, 916 | first_data_sector: u32, 917 | } 918 | 919 | #[inline] 920 | // an easy way to universally use the same bincode (de)serialization options 921 | fn bincode_config() -> impl bincode::Options + Copy { 922 | // also check https://docs.rs/bincode/1.3.3/bincode/config/index.html#options-struct-vs-bincode-functions 923 | bincode::DefaultOptions::new() 924 | .with_fixint_encoding() 925 | .allow_trailing_bytes() 926 | .with_little_endian() 927 | } 928 | 929 | /// An API to process a FAT filesystem 930 | #[derive(Debug)] 931 | pub struct FileSystem 932 | where 933 | S: Read + Write + Seek, 934 | { 935 | /// Any struct that implements the [`Read`], [`Write`] & [`Seek`] traits 936 | storage: S, 937 | 938 | /// The length of this will be the sector size of the FS for all FAT types except FAT12, in that case, it will be double that value 939 | sector_buffer: Vec, 940 | stored_sector: u64, 941 | 942 | boot_record: BootRecord, 943 | // since `self.fat_type()` calls like 5 nested functions, we keep this cached and expose it as a public field 944 | fat_type: FATType, 945 | props: FSProperties, 946 | } 947 | 948 | impl OffsetConversions for FileSystem 949 | where 950 | S: Read + Write + Seek, 951 | { 952 | #[inline] 953 | fn sector_size(&self) -> u32 { 954 | self.props.sector_size 955 | } 956 | 957 | #[inline] 958 | fn cluster_size(&self) -> u64 { 959 | self.props.cluster_size 960 | } 961 | 962 | #[inline] 963 | fn first_data_sector(&self) -> u32 { 964 | self.props.first_data_sector 965 | } 966 | } 967 | 968 | /// Getter functions 969 | impl FileSystem 970 | where 971 | S: Read + Write + Seek, 972 | { 973 | /// What is the [`FATType`] of the filesystem 974 | pub fn fat_type(&self) -> FATType { 975 | self.fat_type 976 | } 977 | } 978 | 979 | /// Public functions 980 | impl FileSystem 981 | where 982 | S: Read + Write + Seek, 983 | { 984 | /// Create a [`FileSystem`] from a storage object that implements [`Read`], [`Write`] & [`Seek`] 985 | /// 986 | /// Fails if the storage is way too small to support a FAT filesystem. 987 | /// For most use cases, that shouldn't be an issue, you can just call [`.unwrap()`](Result::unwrap) 988 | pub fn from_storage(mut storage: S) -> FSResult { 989 | // Begin by reading the boot record 990 | // We don't know the sector size yet, so we just go with the biggest possible one for now 991 | let mut buffer = [0u8; SECTOR_SIZE_MAX]; 992 | 993 | let bytes_read = storage.read(&mut buffer)?; 994 | let mut stored_sector = 0; 995 | 996 | if bytes_read < 512 { 997 | return Err(FSError::InternalFSError(InternalFSError::StorageTooSmall)); 998 | } 999 | 1000 | let bpb: BPBFAT = bincode_config().deserialize(&buffer[..BPBFAT_SIZE])?; 1001 | 1002 | let ebr = if bpb.table_size_16 == 0 { 1003 | let ebr_fat32 = bincode_config() 1004 | .deserialize::(&buffer[BPBFAT_SIZE..BPBFAT_SIZE + EBR_SIZE])?; 1005 | 1006 | storage.seek(SeekFrom::Start( 1007 | ebr_fat32.fat_info as u64 * bpb.bytes_per_sector as u64, 1008 | ))?; 1009 | stored_sector = ebr_fat32.fat_info.into(); 1010 | storage.read_exact(&mut buffer[..bpb.bytes_per_sector as usize])?; 1011 | let fsinfo = bincode_config() 1012 | .deserialize::(&buffer[..bpb.bytes_per_sector as usize])?; 1013 | 1014 | if !fsinfo.verify_signature() { 1015 | log::error!("FAT32 FSInfo has invalid signature(s)"); 1016 | return Err(FSError::InternalFSError(InternalFSError::InvalidFSInfoSig)); 1017 | } 1018 | 1019 | EBR::FAT32(ebr_fat32, fsinfo) 1020 | } else { 1021 | EBR::FAT12_16( 1022 | bincode_config() 1023 | .deserialize::(&buffer[BPBFAT_SIZE..BPBFAT_SIZE + EBR_SIZE])?, 1024 | ) 1025 | }; 1026 | 1027 | // TODO: see how we will handle this for exfat 1028 | let boot_record = BootRecord::FAT(BootRecordFAT { bpb, ebr }); 1029 | 1030 | // verify boot record signature 1031 | let fat_type = boot_record.fat_type(); 1032 | log::info!("The FAT type of the filesystem is {:?}", fat_type); 1033 | 1034 | match boot_record { 1035 | BootRecord::FAT(boot_record_fat) => { 1036 | if boot_record_fat.verify_signature() { 1037 | log::error!("FAT boot record has invalid signature(s)"); 1038 | return Err(FSError::InternalFSError(InternalFSError::InvalidBPBSig)); 1039 | } 1040 | } 1041 | BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT not yet implemented"), 1042 | }; 1043 | 1044 | let sector_size: u32 = match boot_record { 1045 | BootRecord::FAT(boot_record_fat) => boot_record_fat.bpb.bytes_per_sector.into(), 1046 | BootRecord::ExFAT(boot_record_exfat) => 1 << boot_record_exfat.sector_shift, 1047 | }; 1048 | let cluster_size: u64 = match boot_record { 1049 | BootRecord::FAT(boot_record_fat) => { 1050 | (boot_record_fat.bpb.sectors_per_cluster as u32 * sector_size).into() 1051 | } 1052 | BootRecord::ExFAT(boot_record_exfat) => { 1053 | 1 << (boot_record_exfat.sector_shift + boot_record_exfat.cluster_shift) 1054 | } 1055 | }; 1056 | 1057 | let first_data_sector = match boot_record { 1058 | BootRecord::FAT(boot_record_fat) => boot_record_fat.first_data_sector().into(), 1059 | BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"), 1060 | }; 1061 | 1062 | let fat_offset = match boot_record { 1063 | BootRecord::FAT(boot_record_fat) => boot_record_fat.first_fat_sector().into(), 1064 | BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"), 1065 | }; 1066 | 1067 | let total_clusters = match boot_record { 1068 | BootRecord::FAT(boot_record_fat) => boot_record_fat.total_clusters(), 1069 | BootRecord::ExFAT(_boot_record_exfat) => todo!("ExFAT is not yet implemented"), 1070 | }; 1071 | 1072 | let props = FSProperties { 1073 | sector_size, 1074 | cluster_size, 1075 | fat_offset, 1076 | total_clusters, 1077 | first_data_sector, 1078 | }; 1079 | 1080 | Ok(Self { 1081 | storage, 1082 | sector_buffer: buffer[..sector_size as usize].to_vec(), 1083 | stored_sector, 1084 | boot_record, 1085 | fat_type, 1086 | props, 1087 | }) 1088 | } 1089 | 1090 | /// Read all the entries of a directory ([`PathBuf`]) into [`Vec`] 1091 | /// 1092 | /// Fails if `path` doesn't represent a directory, or if that directory doesn't exist 1093 | pub fn read_dir(&mut self, path: PathBuf) -> FSResult, S::Error> { 1094 | if path.is_malformed() { 1095 | return Err(FSError::MalformedPath); 1096 | } 1097 | if !path.is_dir() { 1098 | log::error!("Not a directory"); 1099 | return Err(FSError::NotADirectory); 1100 | } 1101 | 1102 | let mut entries = self.process_root_dir()?; 1103 | 1104 | for dir_name in path.clone().into_iter() { 1105 | let dir_cluster = match entries.iter().find(|entry| { 1106 | entry.name == dir_name && entry.attributes.contains(Attributes::DIRECTORY) 1107 | }) { 1108 | Some(entry) => entry.data_cluster, 1109 | None => { 1110 | log::error!("Directory {} not found", path); 1111 | return Err(FSError::NotFound); 1112 | } 1113 | }; 1114 | 1115 | entries = unsafe { self.process_normal_dir(dir_cluster)? }; 1116 | } 1117 | 1118 | // if we haven't returned by now, that means that the entries vector 1119 | // contains what we want, let's map it to DirEntries and return 1120 | Ok(entries 1121 | .into_iter() 1122 | .map(|rawentry| { 1123 | let mut entry_path = path.clone(); 1124 | 1125 | entry_path.push(format!( 1126 | "{}{}", 1127 | rawentry.name, 1128 | if rawentry.is_dir { "/" } else { "" } 1129 | )); 1130 | DirEntry { 1131 | entry: Properties::from_raw(rawentry, entry_path), 1132 | } 1133 | }) 1134 | .collect()) 1135 | } 1136 | 1137 | /// Get a corresponding [`File`] object from a [`PathBuf`] 1138 | /// 1139 | /// Borrows `&mut self` until that [`File`] object is dropped, effectively locking `self` until that file closed 1140 | /// 1141 | /// Fails if `path` doesn't represent a file, or if that file doesn't exist 1142 | pub fn get_file(&mut self, path: PathBuf) -> FSResult, S::Error> { 1143 | if path.is_malformed() { 1144 | return Err(FSError::MalformedPath); 1145 | } 1146 | 1147 | if let Some(file_name) = path.file_name() { 1148 | let parent_dir = self.read_dir(path.parent())?; 1149 | match parent_dir.into_iter().find(|direntry| { 1150 | direntry 1151 | .path() 1152 | .file_name() 1153 | .is_some_and(|entry_name| entry_name == file_name) 1154 | }) { 1155 | Some(direntry) => { 1156 | let mut file = File { 1157 | fs: self, 1158 | offset: 0, 1159 | current_cluster: direntry.entry.data_cluster, 1160 | entry: direntry.entry, 1161 | }; 1162 | 1163 | if file.cluster_chain_is_healthy()? { 1164 | Ok(file) 1165 | } else { 1166 | log::error!("The cluster chain of a file is malformed"); 1167 | Err(FSError::InternalFSError( 1168 | InternalFSError::MalformedClusterChain, 1169 | )) 1170 | } 1171 | } 1172 | None => { 1173 | log::error!("File {} not found", path); 1174 | Err(FSError::NotFound) 1175 | } 1176 | } 1177 | } else { 1178 | log::error!("Is a directory (not a file)"); 1179 | Err(FSError::IsADirectory) 1180 | } 1181 | } 1182 | } 1183 | 1184 | /// Internal low-level functions 1185 | impl FileSystem 1186 | where 1187 | S: Read + Write + Seek, 1188 | { 1189 | /// Unsafe because the sector number must point to an area with directory entries 1190 | /// 1191 | /// Also the sector number starts from the beginning of the partition 1192 | unsafe fn process_entry_sector( 1193 | &mut self, 1194 | sector: u32, 1195 | ) -> FSResult, S::Error> { 1196 | let mut entries = Vec::new(); 1197 | let mut lfn_buf: Vec = Vec::new(); 1198 | let mut lfn_checksum: Option = None; 1199 | 1200 | 'outer: loop { 1201 | for chunk in self.read_nth_sector(sector.into())?.chunks(32) { 1202 | match chunk[0] { 1203 | // nothing else to read 1204 | 0 => break 'outer, 1205 | // unused entry 1206 | 0xE5 => continue, 1207 | _ => (), 1208 | }; 1209 | 1210 | let Ok(entry) = bincode_config().deserialize::(&chunk) else { 1211 | continue; 1212 | }; 1213 | 1214 | if entry.attributes.contains(Attributes::LFN) { 1215 | // TODO: perhaps there is a way to utilize the `order` field? 1216 | let Ok(lfn_entry) = bincode_config().deserialize::(&chunk) else { 1217 | continue; 1218 | }; 1219 | 1220 | // If the signature verification fails, consider this entry corrupted 1221 | if !lfn_entry.verify_signature() { 1222 | continue; 1223 | } 1224 | 1225 | match lfn_checksum { 1226 | Some(checksum) => { 1227 | if checksum != lfn_entry.checksum { 1228 | lfn_checksum = None; 1229 | lfn_buf.clear(); 1230 | continue; 1231 | } 1232 | } 1233 | None => lfn_checksum = Some(lfn_entry.checksum), 1234 | } 1235 | 1236 | let char_arr = lfn_entry.get_byte_slice().to_vec(); 1237 | if let Ok(temp_str) = string_from_lfn(&char_arr) { 1238 | lfn_buf.push(temp_str); 1239 | } 1240 | 1241 | continue; 1242 | } 1243 | 1244 | let filename = if !lfn_buf.is_empty() 1245 | && lfn_checksum.is_some_and(|checksum| checksum == entry.sfn.gen_checksum()) 1246 | { 1247 | // for efficiency reasons, we store the LFN string sequences as we read them 1248 | let parsed_str: String = lfn_buf.iter().cloned().rev().collect(); 1249 | lfn_buf.clear(); 1250 | lfn_checksum = None; 1251 | parsed_str 1252 | } else { 1253 | entry.sfn.to_string() 1254 | }; 1255 | 1256 | if let (Ok(created), Ok(modified), Ok(accessed)) = ( 1257 | entry.created.try_into(), 1258 | entry.modified.try_into(), 1259 | entry.accessed.try_into(), 1260 | ) { 1261 | entries.push(RawProperties { 1262 | name: filename, 1263 | is_dir: entry.attributes.contains(Attributes::DIRECTORY), 1264 | attributes: entry.attributes, 1265 | created, 1266 | modified, 1267 | accessed, 1268 | file_size: entry.file_size, 1269 | data_cluster: ((entry.cluster_high as u32) << 16) 1270 | + entry.cluster_low as u32, 1271 | }) 1272 | } 1273 | } 1274 | } 1275 | 1276 | Ok(entries) 1277 | } 1278 | 1279 | fn process_root_dir(&mut self) -> FSResult, S::Error> { 1280 | match self.boot_record { 1281 | BootRecord::FAT(boot_record_fat) => match boot_record_fat.ebr { 1282 | EBR::FAT12_16(_ebr_fat12_16) => { 1283 | let mut entries = Vec::new(); 1284 | 1285 | let root_dir_sector = boot_record_fat.first_root_dir_sector(); 1286 | let sector_count = boot_record_fat.root_dir_sectors(); 1287 | 1288 | for sector in root_dir_sector..(root_dir_sector + sector_count) { 1289 | let mut new_entries = unsafe { self.process_entry_sector(sector.into())? }; 1290 | entries.append(&mut new_entries); 1291 | } 1292 | 1293 | Ok(entries) 1294 | } 1295 | EBR::FAT32(ebr_fat32, _) => { 1296 | let cluster = ebr_fat32.root_cluster; 1297 | unsafe { self.process_normal_dir(cluster) } 1298 | } 1299 | }, 1300 | BootRecord::ExFAT(_boot_record_exfat) => todo!(), 1301 | } 1302 | } 1303 | 1304 | /// Unsafe for the same reason as [`process_entry_sector`] 1305 | unsafe fn process_normal_dir( 1306 | &mut self, 1307 | mut data_cluster: u32, 1308 | ) -> FSResult, S::Error> { 1309 | let mut entries = Vec::new(); 1310 | 1311 | loop { 1312 | // FAT specification, section 6.7 1313 | let first_sector_of_cluster = self.data_cluster_to_partition_sector(data_cluster); 1314 | for sector in first_sector_of_cluster 1315 | ..(first_sector_of_cluster + self.sectors_per_cluster() as u32) 1316 | { 1317 | let mut new_entries = unsafe { self.process_entry_sector(sector.into())? }; 1318 | entries.append(&mut new_entries); 1319 | } 1320 | 1321 | // Read corresponding FAT entry 1322 | let current_fat_entry = self.read_nth_FAT_entry(data_cluster)?; 1323 | 1324 | match current_fat_entry { 1325 | // we are done here, break the loop 1326 | FATEntry::EOF => break, 1327 | // this cluster chain goes on, follow it 1328 | FATEntry::Allocated(next_cluster) => data_cluster = next_cluster, 1329 | // any other case (whether a bad, reserved or free cluster) is invalid, consider this cluster chain malformed 1330 | _ => { 1331 | log::error!("Cluster chain of directory is malformed"); 1332 | return Err(FSError::InternalFSError( 1333 | InternalFSError::MalformedClusterChain, 1334 | )); 1335 | } 1336 | } 1337 | } 1338 | 1339 | Ok(entries) 1340 | } 1341 | 1342 | /// Read the nth sector from the partition's beginning and store it in [`self.sector_buffer`](Self::sector_buffer) 1343 | /// 1344 | /// This function also returns an immutable reference to [`self.sector_buffer`](Self::sector_buffer) 1345 | fn read_nth_sector(&mut self, n: u64) -> Result<&Vec, S::Error> { 1346 | // nothing to do if the sector we wanna read is already cached 1347 | if n != self.stored_sector { 1348 | self.storage.seek(SeekFrom::Start( 1349 | self.sector_to_partition_offset(n as u32).into(), 1350 | ))?; 1351 | self.storage.read_exact(&mut self.sector_buffer)?; 1352 | self.stored_sector = n; 1353 | } 1354 | 1355 | Ok(&self.sector_buffer) 1356 | } 1357 | 1358 | #[allow(non_snake_case)] 1359 | fn read_nth_FAT_entry(&mut self, n: u32) -> Result { 1360 | // the size of an entry rounded up to bytes 1361 | let entry_size = self.fat_type.bits_per_entry().next_power_of_two() as u32 / 8; 1362 | let fat_offset: u32 = n * self.fat_type.bits_per_entry() as u32 / 8; 1363 | let fat_sector_offset = self.props.fat_offset + fat_offset / self.props.sector_size; 1364 | let entry_offset: usize = (fat_offset % self.props.sector_size) as usize; 1365 | 1366 | self.read_nth_sector(fat_sector_offset.into())?; 1367 | 1368 | let mut value_bytes = [0_u8; 4]; 1369 | let bytes_to_read: usize = cmp::min( 1370 | entry_offset + entry_size as usize, 1371 | self.sector_size() as usize, 1372 | ) - entry_offset; 1373 | value_bytes[..bytes_to_read] 1374 | .copy_from_slice(&self.sector_buffer[entry_offset..entry_offset + bytes_to_read]); // this shouldn't panic 1375 | 1376 | // in FAT12, FAT entries may be split between two different sectors 1377 | if self.fat_type == FATType::FAT12 && (bytes_to_read as u32) < entry_size { 1378 | self.read_nth_sector((fat_sector_offset + 1).into())?; 1379 | 1380 | value_bytes[bytes_to_read..entry_size as usize] 1381 | .copy_from_slice(&self.sector_buffer[..(entry_size as usize - bytes_to_read)]); 1382 | } 1383 | 1384 | let mut value = u32::from_le_bytes(value_bytes); 1385 | match self.fat_type { 1386 | // FAT12 entries are split between different bytes 1387 | FATType::FAT12 => { 1388 | if n & 1 != 0 { 1389 | value >>= 4 1390 | } else { 1391 | value &= 0xFFF 1392 | } 1393 | } 1394 | // ignore the high 4 bits if this is FAT32 1395 | FATType::FAT32 => value &= 0x0FFFFFFF, 1396 | _ => (), 1397 | } 1398 | 1399 | /* 1400 | // pad unused bytes with 1s 1401 | let padding: u32 = u32::MAX.to_be() << self.fat_type.bits_per_entry(); 1402 | value |= padding.to_le(); 1403 | */ 1404 | 1405 | // TODO: perhaps byte padding can replace some redundant code here? 1406 | Ok(match self.fat_type { 1407 | FATType::FAT12 => match value { 1408 | 0x000 => FATEntry::Free, 1409 | 0xFF7 => FATEntry::Bad, 1410 | 0xFF8..=0xFFE | 0xFFF => FATEntry::EOF, 1411 | _ => { 1412 | if (0x002..(self.props.total_clusters + 1)).contains(&value.into()) { 1413 | FATEntry::Allocated(value.into()) 1414 | } else { 1415 | FATEntry::Reserved 1416 | } 1417 | } 1418 | }, 1419 | FATType::FAT16 => match value { 1420 | 0x0000 => FATEntry::Free, 1421 | 0xFFF7 => FATEntry::Bad, 1422 | 0xFFF8..=0xFFFE | 0xFFFF => FATEntry::EOF, 1423 | _ => { 1424 | if (0x0002..(self.props.total_clusters + 1)).contains(&value.into()) { 1425 | FATEntry::Allocated(value.into()) 1426 | } else { 1427 | FATEntry::Reserved 1428 | } 1429 | } 1430 | }, 1431 | FATType::FAT32 => match value { 1432 | 0x00000000 => FATEntry::Free, 1433 | 0xFFFFFFF7 => FATEntry::Bad, 1434 | 0xFFFFFFF8..=0xFFFFFFFE | 0xFFFFFFFF => FATEntry::EOF, 1435 | _ => { 1436 | if (0x00000002..(self.props.total_clusters + 1)).contains(&value.into()) { 1437 | FATEntry::Allocated(value.into()) 1438 | } else { 1439 | FATEntry::Reserved 1440 | } 1441 | } 1442 | }, 1443 | FATType::ExFAT => todo!("ExFAT not yet implemented"), 1444 | }) 1445 | } 1446 | } 1447 | 1448 | #[cfg(all(test, feature = "std"))] 1449 | mod tests { 1450 | use super::*; 1451 | use test_log::test; 1452 | use time::macros::*; 1453 | 1454 | static MINFS: &[u8] = include_bytes!("../imgs/minfs.img"); 1455 | static FAT12: &[u8] = include_bytes!("../imgs/fat12.img"); 1456 | static FAT16: &[u8] = include_bytes!("../imgs/fat16.img"); 1457 | static FAT32: &[u8] = include_bytes!("../imgs/fat32.img"); 1458 | 1459 | #[test] 1460 | #[allow(non_snake_case)] 1461 | fn check_FAT_offset() { 1462 | use std::io::Cursor; 1463 | 1464 | let mut storage = Cursor::new(FAT16.to_owned()); 1465 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1466 | 1467 | // we manually read the first and second entry of the FAT table 1468 | fs.read_nth_sector(fs.props.fat_offset.into()).unwrap(); 1469 | 1470 | let first_entry = u16::from_le_bytes(fs.sector_buffer[0..2].try_into().unwrap()); 1471 | let media_type = if let BootRecord::FAT(boot_record_fat) = fs.boot_record { 1472 | boot_record_fat.bpb._media_type 1473 | } else { 1474 | unreachable!("this should be a FAT16 filesystem") 1475 | }; 1476 | assert_eq!(u16::MAX << 8 | media_type as u16, first_entry); 1477 | 1478 | let second_entry = u16::from_le_bytes(fs.sector_buffer[2..4].try_into().unwrap()); 1479 | assert_eq!(u16::MAX, second_entry); 1480 | } 1481 | 1482 | #[test] 1483 | fn read_file_in_root_dir() { 1484 | use std::io::Cursor; 1485 | 1486 | let mut storage = Cursor::new(FAT16.to_owned()); 1487 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1488 | 1489 | let mut file = fs.get_file(PathBuf::from("/root.txt")).unwrap(); 1490 | 1491 | let mut file_string = String::new(); 1492 | file.read_to_string(&mut file_string).unwrap(); 1493 | const EXPECTED_STR: &str = "I am in the filesystem's root!!!\n\n"; 1494 | assert_eq!(file_string, EXPECTED_STR); 1495 | } 1496 | 1497 | static BEE_MOVIE_SCRIPT: &str = include_str!("../tests/bee movie script.txt"); 1498 | fn assert_file_is_bee_movie_script(file: &mut File<'_, S>) 1499 | where 1500 | S: Read + Write + Seek, 1501 | { 1502 | let mut file_string = String::new(); 1503 | let bytes_read = file.read_to_string(&mut file_string).unwrap(); 1504 | 1505 | let expected_filesize = BEE_MOVIE_SCRIPT.len(); 1506 | assert_eq!(bytes_read, expected_filesize); 1507 | 1508 | assert_eq!(file_string, BEE_MOVIE_SCRIPT); 1509 | } 1510 | 1511 | #[test] 1512 | fn read_huge_file() { 1513 | use std::io::Cursor; 1514 | 1515 | let mut storage = Cursor::new(FAT16.to_owned()); 1516 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1517 | 1518 | let mut file = fs.get_file(PathBuf::from("/bee movie script.txt")).unwrap(); 1519 | assert_file_is_bee_movie_script(&mut file); 1520 | } 1521 | 1522 | #[test] 1523 | fn seek_n_read() { 1524 | // this uses the famous "I'd like to interject for a moment" copypasta as a test file 1525 | // you can find it online by just searching this term 1526 | 1527 | use std::io::Cursor; 1528 | 1529 | let mut storage = Cursor::new(FAT16.to_owned()); 1530 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1531 | 1532 | let mut file = fs 1533 | .get_file(PathBuf::from("/GNU ⁄ Linux copypasta.txt")) 1534 | .unwrap(); 1535 | let mut file_bytes = [0_u8; 4096]; 1536 | 1537 | // we first perform a forward seek... 1538 | const EXPECTED_STR1: &str = "Linux is the kernel"; 1539 | file.seek(SeekFrom::Start(792)).unwrap(); 1540 | let bytes_read = file.read(&mut file_bytes[..EXPECTED_STR1.len()]).unwrap(); 1541 | assert_eq!( 1542 | String::from_utf8_lossy(&file_bytes[..bytes_read]), 1543 | EXPECTED_STR1 1544 | ); 1545 | 1546 | // ...then a backward one 1547 | const EXPECTED_STR2: &str = "What you're referring to as Linux, is in fact, GNU/Linux"; 1548 | file.seek(SeekFrom::Start(39)).unwrap(); 1549 | let bytes_read = file.read(&mut file_bytes[..EXPECTED_STR2.len()]).unwrap(); 1550 | assert_eq!( 1551 | String::from_utf8_lossy(&file_bytes[..bytes_read]), 1552 | EXPECTED_STR2 1553 | ); 1554 | } 1555 | 1556 | #[test] 1557 | fn read_file_in_subdir() { 1558 | use std::io::Cursor; 1559 | 1560 | let mut storage = Cursor::new(FAT16.to_owned()); 1561 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1562 | 1563 | let mut file = fs.get_file(PathBuf::from("/rootdir/example.txt")).unwrap(); 1564 | 1565 | let mut file_string = String::new(); 1566 | file.read_to_string(&mut file_string).unwrap(); 1567 | const EXPECTED_STR: &str = "I am not in the root directory :(\n\n"; 1568 | assert_eq!(file_string, EXPECTED_STR); 1569 | } 1570 | 1571 | #[test] 1572 | fn check_file_timestamps() { 1573 | use std::io::Cursor; 1574 | 1575 | let mut storage = Cursor::new(FAT16.to_owned()); 1576 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1577 | 1578 | let file = fs.get_file(PathBuf::from("/rootdir/example.txt")).unwrap(); 1579 | 1580 | assert_eq!(datetime!(2024-07-11 13:02:38.15), file.entry.created); 1581 | assert_eq!(datetime!(2024-07-11 13:02:38.0), file.entry.modified); 1582 | assert_eq!(date!(2024 - 07 - 11), file.entry.accessed); 1583 | } 1584 | 1585 | #[test] 1586 | fn read_file_fat12() { 1587 | use std::io::Cursor; 1588 | 1589 | let mut storage = Cursor::new(FAT12.to_owned()); 1590 | let mut fs = FileSystem::from_storage(&mut storage).unwrap(); 1591 | 1592 | let mut file = fs.get_file(PathBuf::from("/foo/bar.txt")).unwrap(); 1593 | let mut file_string = String::new(); 1594 | file.read_to_string(&mut file_string).unwrap(); 1595 | const EXPECTED_STR: &str = "Hello, World!\n"; 1596 | assert_eq!(file_string, EXPECTED_STR); 1597 | 1598 | // please not that the FAT12 image has been modified so that 1599 | // one FAT entry of the file we are reading is split between different sectors 1600 | // this way, we also test for this case 1601 | let mut file = fs 1602 | .get_file(PathBuf::from("/test/bee movie script.txt")) 1603 | .unwrap(); 1604 | assert_file_is_bee_movie_script(&mut file); 1605 | } 1606 | 1607 | #[test] 1608 | fn assert_img_fat_type() { 1609 | static TEST_CASES: &[(&[u8], FATType)] = &[ 1610 | (MINFS, FATType::FAT12), 1611 | (FAT12, FATType::FAT12), 1612 | (FAT16, FATType::FAT16), 1613 | (FAT32, FATType::FAT32), 1614 | ]; 1615 | 1616 | for case in TEST_CASES { 1617 | use std::io::Cursor; 1618 | 1619 | let mut storage = Cursor::new(case.0.to_owned()); 1620 | let fs = FileSystem::from_storage(&mut storage).unwrap(); 1621 | 1622 | assert_eq!(fs.fat_type, case.1) 1623 | } 1624 | } 1625 | } 1626 | -------------------------------------------------------------------------------- /src/io.rs: -------------------------------------------------------------------------------- 1 | //! This module contains all the IO-related objects of the crate 2 | //! 3 | //! If you want to implement IO functionality in a `std` environment, 4 | //! it is advised to implement the relevant [`std::io`] traits, which will 5 | //! auto-`impl` the corresponding IO traits here. In a `no-std` environment, 6 | //! implement the traits directly 7 | //! 8 | //! # Traits 9 | //! 10 | //! - [`IOBase`] provides a common error type for [`Read`], [`Write`] & [`Seek`] 11 | //! - [`Read`] allows for reading bytes from a source. 12 | //! - [`Write`] allows for writing bytes to a sink. 13 | //! - [`Seek`] provides a cursor which can be moved within a stream of bytes 14 | 15 | #[cfg(not(feature = "std"))] 16 | use core::*; 17 | #[cfg(feature = "std")] 18 | use std::*; 19 | 20 | use ::alloc::{string::String, vec::Vec}; 21 | 22 | use crate::error::{IOError, IOErrorKind}; 23 | 24 | /// With `use prelude::*`, all IO-related traits are automatically imported 25 | pub mod prelude { 26 | pub use super::{IOBase, Read, Seek, SeekFrom, Write}; 27 | } 28 | 29 | // Some things here were borrowed from the Rust Standard Library and the source code of the `ciborium-io` crate 30 | 31 | /// All other IO traits ([`Read`], [`Write`] and [`Seek`]) are supertraits of this 32 | /// 33 | /// Used to define a shared error type for these traits 34 | pub trait IOBase { 35 | /// The error type 36 | type Error: IOError; 37 | } 38 | 39 | /// A simplified version of [`std::io::Read`] for use within a `no_std` context 40 | pub trait Read: IOBase { 41 | /// Pull some bytes from this source into the specified buffer, 42 | /// returning how many bytes were read. 43 | /// 44 | /// If the return value of this method is [`Ok(n)`], then implementations must 45 | /// guarantee that `0 <= n <= buf.len()`. A nonzero `n` value indicates 46 | /// that the buffer `buf` has been filled in with `n` bytes of data from this 47 | /// source. If `n` is `0`, then it can indicate one of two scenarios: 48 | /// 49 | /// 1. This reader has reached its "end of file" and will likely no longer 50 | /// be able to produce bytes. Note that this does not mean that the 51 | /// reader will *always* no longer be able to produce bytes. 52 | /// 2. The buffer specified was 0 bytes in length. 53 | /// 54 | /// It is not an error if the returned value `n` is smaller than the buffer size, 55 | /// even when the reader is not at the end of the stream yet. 56 | /// This may happen for example because fewer bytes are actually available right now 57 | /// (e. g. being close to end-of-file) or because read() was interrupted by a signal, 58 | /// although for the later, an [`IOErrorKind`] of kind `Interrupted` should preferably be raised 59 | /// 60 | fn read(&mut self, buf: &mut [u8]) -> Result; 61 | 62 | /// Read all bytes until EOF in this source, placing them into `buf`. 63 | /// 64 | /// All bytes read from this source will be appended to the specified buffer 65 | /// `buf`. This function will continuously call [`read()`] to append more data to 66 | /// `buf` until [`read()`] returns either [`Ok(0)`] or an [`IOErrorKind`] of 67 | /// non-`Interrupted` kind. 68 | /// 69 | /// [`read()`]: Read::read 70 | fn read_to_end(&mut self, buf: &mut Vec) -> Result { 71 | let mut bytes_read = 0; 72 | 73 | const PROBE_SIZE: usize = 32; 74 | let mut probe = [0_u8; PROBE_SIZE]; 75 | 76 | loop { 77 | match self.read(&mut probe) { 78 | Ok(0) => break, 79 | Ok(n) => { 80 | buf.extend_from_slice(&probe[..n]); 81 | bytes_read += n; 82 | } 83 | Err(ref e) if e.kind().is_interrupted() => continue, 84 | Err(e) => return Err(e), 85 | } 86 | } 87 | 88 | Ok(bytes_read) 89 | } 90 | 91 | /// Read all bytes until EOF in this source, appending them to `string`. 92 | /// 93 | /// If successful, this function returns the number of bytes which were read and appended to buf. 94 | /// 95 | /// # Errors 96 | /// 97 | /// If the data in this stream is *not* valid UTF-8 then an error is returned and `string` is unchanged. 98 | /// 99 | /// See [`read_to_end`](Read::read_to_end) for other error semantics. 100 | fn read_to_string(&mut self, string: &mut String) -> Result { 101 | let mut buf = Vec::new(); 102 | let bytes_read = self.read_to_end(&mut buf)?; 103 | 104 | string.push_str(str::from_utf8(&buf).map_err(|_| { 105 | IOError::new( 106 | ::Kind::new_invalid_data(), 107 | "found invalid utf-8", 108 | ) 109 | })?); 110 | 111 | Ok(bytes_read) 112 | } 113 | 114 | /// Read the exact number of bytes required to fill `buf`. 115 | /// 116 | /// This function reads as many bytes as necessary to completely fill the 117 | /// specified buffer `buf`. 118 | /// 119 | /// *Implementations* of this method can make no assumptions about the contents of `buf` when 120 | /// this function is called. It is recommended that implementations only write data to `buf` 121 | /// instead of reading its contents. The documentation on [`read`](Read::read) has a more detailed 122 | /// explanation of this subject. 123 | fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), Self::Error> { 124 | while !buf.is_empty() { 125 | match self.read(buf) { 126 | Ok(0) => break, 127 | Ok(n) => buf = &mut buf[n..], 128 | Err(ref e) if e.kind().is_interrupted() => {} 129 | Err(e) => return Err(e), 130 | } 131 | } 132 | if !buf.is_empty() { 133 | Err(Self::Error::new( 134 | <::Kind as IOErrorKind>::new_unexpected_eof(), 135 | "failed to fill whole buffer", 136 | )) 137 | } else { 138 | Ok(()) 139 | } 140 | } 141 | } 142 | 143 | /// A simplified version of [`std::io::Write`] for use within a `no_std` context 144 | pub trait Write: IOBase { 145 | /// Write a buffer into this writer, returning how many bytes were written. 146 | /// 147 | /// This function will attempt to write the entire contents of `buf`, but 148 | /// the entire write might not succeed, or the write may also generate an 149 | /// error. Typically, a call to `write` represents one attempt to write to 150 | /// any wrapped object. 151 | /// 152 | /// If this method consumed `n > 0` bytes of `buf` it must return [`Ok(n)`]. 153 | /// If the return value is `Ok(n)` then `n` must satisfy `n <= buf.len()`. 154 | /// A return value of `Ok(0)` typically means that the underlying object is 155 | /// no longer able to accept bytes and will likely not be able to in the 156 | /// future as well, or that the buffer provided is empty. 157 | /// 158 | /// # Errors 159 | /// 160 | /// Each call to `write` may generate an I/O error indicating that the 161 | /// operation could not be completed. If an error is returned then no bytes 162 | /// in the buffer were written to this writer. 163 | /// 164 | /// It is **not** considered an error if the entire buffer could not be 165 | /// written to this writer. 166 | /// 167 | /// An error of the `Interrupted` [`IOErrorKind`] is non-fatal and the 168 | /// write operation should be retried if there is nothing else to do. 169 | fn write(&mut self, buf: &[u8]) -> Result; 170 | 171 | /// Attempts to write an entire buffer into this writer. 172 | /// 173 | /// This method will continuously call [`write`] until there is no more data 174 | /// to be written or an error of non-`Interrupted` [`IOErrorKind`] is 175 | /// returned. This method will not return until the entire buffer has been 176 | /// successfully written or such an error occurs. The first error that is 177 | /// not of `Interrupted` [`IOErrorKind`] generated from this method will be 178 | /// returned. 179 | /// 180 | /// If the buffer contains no data, this will never call [`write`]. 181 | /// 182 | /// # Errors 183 | /// 184 | /// This function will return the first error of 185 | /// non-`Interrupted` [`IOErrorKind`] that [`write`] returns. 186 | /// 187 | /// [`write`]: Write::write 188 | fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Self::Error> { 189 | while !buf.is_empty() { 190 | match self.write(buf) { 191 | Ok(0) => { 192 | return Err(IOError::new( 193 | IOErrorKind::new_unexpected_eof(), 194 | "writer returned EOF before all data could be written", 195 | )); 196 | } 197 | Ok(n) => buf = &buf[n..], 198 | Err(ref e) if e.kind().is_interrupted() => {} 199 | Err(e) => return Err(e), 200 | } 201 | } 202 | Ok(()) 203 | } 204 | 205 | /// Flush this output stream, ensuring that all intermediately buffered 206 | /// contents reach their destination. 207 | /// 208 | /// # Errors 209 | /// 210 | /// It is considered an error if not all bytes could be written due to 211 | /// I/O errors or EOF being reached. 212 | fn flush(&mut self) -> Result<(), Self::Error>; 213 | } 214 | 215 | /// A copy of [`std::io::SeekFrom`] for use within a `no_std` context 216 | #[derive(Debug, Clone, Copy)] 217 | pub enum SeekFrom { 218 | /// Sets the offset to the provided number of bytes. 219 | Start(u64), 220 | 221 | /// Sets the offset to the size of this object plus the specified number of 222 | /// bytes. 223 | /// 224 | /// It is possible to seek beyond the end of an object, but it's an error to 225 | /// seek before byte 0. 226 | End(i64), 227 | 228 | /// Sets the offset to the current position plus the specified number of 229 | /// bytes. 230 | /// 231 | /// It is possible to seek beyond the end of an object, but it's an error to 232 | /// seek before byte 0. 233 | Current(i64), 234 | } 235 | 236 | #[cfg(feature = "std")] 237 | impl From for std::io::SeekFrom { 238 | fn from(value: SeekFrom) -> Self { 239 | match value { 240 | SeekFrom::Start(offset) => std::io::SeekFrom::Start(offset), 241 | SeekFrom::Current(offset) => std::io::SeekFrom::Current(offset), 242 | SeekFrom::End(offset) => std::io::SeekFrom::End(offset), 243 | } 244 | } 245 | } 246 | 247 | #[cfg(feature = "std")] 248 | impl From for SeekFrom { 249 | fn from(value: std::io::SeekFrom) -> Self { 250 | match value { 251 | std::io::SeekFrom::Start(offset) => SeekFrom::Start(offset), 252 | std::io::SeekFrom::Current(offset) => SeekFrom::Current(offset), 253 | std::io::SeekFrom::End(offset) => SeekFrom::End(offset), 254 | } 255 | } 256 | } 257 | 258 | /// A simplified version of [`std::io::Seek`] for use within a `no_std` context 259 | pub trait Seek: IOBase { 260 | /// Seek to an offset, in bytes, in a stream. 261 | /// 262 | /// A seek beyond the end of a stream **IS NOT ALLOWED**. 263 | /// 264 | /// If the seek operation completed successfully, this method returns the new position from the start of the stream. That position can be used later with `SeekFrom::Start`. 265 | /// 266 | /// # Errors 267 | /// Seeking can fail, for example because it might involve flushing a buffer. 268 | /// 269 | /// Seeking to a negative offset is considered an error. 270 | /// Seeking beyond the end of the stream should also be considered an error. 271 | fn seek(&mut self, pos: SeekFrom) -> Result; 272 | 273 | /// Rewind to the beginning of a stream. 274 | /// 275 | /// This is a convenience method, equivalent to `seek(SeekFrom::Start(0))`. 276 | /// 277 | /// # Errors 278 | /// Rewinding can fail, for example because it might involve flushing a buffer. 279 | fn rewind(&mut self) -> Result<(), Self::Error> { 280 | self.seek(SeekFrom::Start(0))?; 281 | 282 | Ok(()) 283 | } 284 | 285 | /// Returns the current seek position from the start of the stream. 286 | /// 287 | /// This is equivalent to `self.seek(SeekFrom::Current(0))`. 288 | fn stream_position(&mut self) -> Result { 289 | self.seek(SeekFrom::Current(0)) 290 | } 291 | } 292 | 293 | #[cfg(feature = "std")] 294 | impl IOBase for T 295 | where 296 | T: std::io::Read + std::io::Write + std::io::Seek, 297 | { 298 | type Error = std::io::Error; 299 | } 300 | 301 | #[cfg(feature = "std")] 302 | impl Read for T 303 | where 304 | T: std::io::Read + IOBase, 305 | { 306 | #[inline] 307 | fn read(&mut self, buf: &mut [u8]) -> Result { 308 | self.read(buf) 309 | } 310 | 311 | #[inline] 312 | fn read_to_end(&mut self, buf: &mut Vec) -> Result { 313 | self.read_to_end(buf) 314 | } 315 | 316 | #[inline] 317 | fn read_to_string(&mut self, string: &mut String) -> Result { 318 | self.read_to_string(string) 319 | } 320 | 321 | #[inline] 322 | fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Self::Error> { 323 | self.read_exact(buf) 324 | } 325 | } 326 | 327 | #[cfg(feature = "std")] 328 | impl Write for T 329 | where 330 | T: std::io::Write + IOBase, 331 | { 332 | #[inline] 333 | fn write(&mut self, buf: &[u8]) -> Result { 334 | self.write(buf) 335 | } 336 | 337 | #[inline] 338 | fn write_all(&mut self, buf: &[u8]) -> Result<(), Self::Error> { 339 | self.write_all(buf) 340 | } 341 | 342 | #[inline] 343 | fn flush(&mut self) -> Result<(), Self::Error> { 344 | self.flush() 345 | } 346 | } 347 | 348 | #[cfg(feature = "std")] 349 | impl Seek for T 350 | where 351 | T: std::io::Seek + IOBase, 352 | { 353 | #[inline] 354 | fn seek(&mut self, pos: SeekFrom) -> Result { 355 | self.seek(pos.into()) 356 | } 357 | 358 | #[inline] 359 | fn rewind(&mut self) -> Result<(), Self::Error> { 360 | self.rewind() 361 | } 362 | 363 | #[inline] 364 | fn stream_position(&mut self) -> Result { 365 | self.stream_position() 366 | } 367 | } 368 | 369 | // https://github.com/rust-lang/rust/issues/31844 open for 8 years, not yet stabilized (as of 2024) 370 | #[cfg(not(feature = "std"))] 371 | impl IOBase for &mut T 372 | where 373 | T: IOBase, 374 | { 375 | type Error = T::Error; 376 | } 377 | 378 | #[cfg(not(feature = "std"))] 379 | impl Read for &mut R { 380 | #[inline] 381 | fn read(&mut self, buf: &mut [u8]) -> Result { 382 | (**self).read(buf) 383 | } 384 | 385 | #[inline] 386 | fn read_to_end(&mut self, buf: &mut Vec) -> Result { 387 | (**self).read_to_end(buf) 388 | } 389 | 390 | #[inline] 391 | fn read_to_string(&mut self, buf: &mut String) -> Result { 392 | (**self).read_to_string(buf) 393 | } 394 | 395 | #[inline] 396 | fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), R::Error> { 397 | (**self).read_exact(buf) 398 | } 399 | } 400 | 401 | #[cfg(not(feature = "std"))] 402 | impl Write for &mut W { 403 | #[inline] 404 | fn write(&mut self, buf: &[u8]) -> Result { 405 | (**self).write(buf) 406 | } 407 | 408 | #[inline] 409 | fn write_all(&mut self, buf: &[u8]) -> Result<(), W::Error> { 410 | (**self).write_all(buf) 411 | } 412 | 413 | #[inline] 414 | fn flush(&mut self) -> Result<(), W::Error> { 415 | (**self).flush() 416 | } 417 | } 418 | 419 | #[cfg(not(feature = "std"))] 420 | impl Seek for &mut S { 421 | #[inline] 422 | fn seek(&mut self, pos: SeekFrom) -> Result { 423 | (**self).seek(pos.into()) 424 | } 425 | 426 | #[inline] 427 | fn rewind(&mut self) -> Result<(), S::Error> { 428 | (**self).rewind() 429 | } 430 | 431 | #[inline] 432 | fn stream_position(&mut self) -> Result { 433 | (**self).stream_position() 434 | } 435 | } 436 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! # simple-fatfs 2 | //! 3 | //! An easy-to-use FAT filesystem library designed for usage in embedded systems 4 | //! 5 | //! ## Features 6 | //! 7 | //! - `no_std` support 8 | //! - FAT12/16/32 support 9 | //! - VFAT/LFN (long filenames) support 10 | //! - Auto-`impl`s for [`std::io`] traits and structs 11 | //! - Easy-to-implement [`io`] traits 12 | //! 13 | //! # Examples 14 | //! ``` 15 | //! # // this test fails on a no_std environment, don't run it in such a case 16 | //! extern crate simple_fatfs; 17 | //! use simple_fatfs::*; 18 | //! use simple_fatfs::io::prelude::*; 19 | //! 20 | //! const FAT_IMG: &[u8] = include_bytes!("../imgs/fat12.img"); 21 | //! 22 | //! fn main() { 23 | //! let mut cursor = std::io::Cursor::new(FAT_IMG.to_owned()); 24 | //! 25 | //! // We can either pass by value of by (mutable) reference 26 | //! let mut fs = FileSystem::from_storage(&mut cursor).unwrap(); 27 | //! 28 | //! // Let's see what entries are in the root directory 29 | //! for entry in fs.read_dir(PathBuf::from("/")).unwrap() { 30 | //! if entry.path().is_dir() { 31 | //! println!("Directory: {}", entry.path()) 32 | //! } else if entry.path().is_file() { 33 | //! println!("File: {}", entry.path()) 34 | //! } else { 35 | //! unreachable!() 36 | //! } 37 | //! } 38 | //! 39 | //! // the image we currently use has a file named "root.txt" 40 | //! // in the root directory. Let's read it 41 | //! 42 | //! // please keep in mind that opening a `File` borrows 43 | //! // the parent `FileSystem` until that `File` is dropped 44 | //! let mut file = fs.get_file(PathBuf::from("/root.txt")).unwrap(); 45 | //! let mut string = String::new(); 46 | //! file.read_to_string(&mut string); 47 | //! println!("root.txt contents:\n{}", string); 48 | //! } 49 | //! ``` 50 | 51 | #![cfg_attr(not(feature = "std"), no_std)] 52 | // Even inside unsafe functions, we must acknowlegde the usage of unsafe code 53 | #![deny(deprecated)] 54 | #![deny(elided_lifetimes_in_paths)] 55 | #![deny(macro_use_extern_crate)] 56 | #![deny(missing_copy_implementations)] 57 | #![deny(missing_debug_implementations)] 58 | #![deny(missing_docs)] 59 | #![deny(non_ascii_idents)] 60 | #![deny(trivial_numeric_casts)] 61 | #![deny(single_use_lifetimes)] 62 | #![deny(unsafe_op_in_unsafe_fn)] 63 | #![deny(unused_import_braces)] 64 | #![deny(unused_lifetimes)] 65 | 66 | extern crate alloc; 67 | 68 | mod error; 69 | mod fs; 70 | pub mod io; 71 | mod path; 72 | 73 | pub use error::*; 74 | pub use fs::*; 75 | pub use path::*; 76 | -------------------------------------------------------------------------------- /src/path.rs: -------------------------------------------------------------------------------- 1 | //! Note: Paths here are Unix-like when converted to [`String`]s (the root directory is `/`, and the directory separator is `/`), 2 | //! but DOS-like paths are also accepted and converted to Unix-like when pushed 3 | //! 4 | //! It is possible to push forbidden characters to a [`PathBuf`], doing so however will make most functions 5 | //! return a [`MalformedPath`](crate::error::FSError::MalformedPath) error 6 | //! 7 | 8 | #[cfg(not(feature = "std"))] 9 | use core::*; 10 | #[cfg(feature = "std")] 11 | use std::*; 12 | 13 | #[cfg(not(feature = "std"))] 14 | use ::alloc::{ 15 | borrow::ToOwned, 16 | collections::vec_deque::VecDeque, 17 | string::{String, ToString}, 18 | vec::Vec, 19 | }; 20 | 21 | #[cfg(feature = "std")] 22 | use std::collections::VecDeque; 23 | 24 | // A (incomplete) list of all the forbidden filename/directory name characters 25 | // See https://learn.microsoft.com/en-us/windows/win32/fileio/naming-a-file?redirectedfrom=MSDN 26 | 27 | /// A list of all the characters that are forbidden to exist in a filename 28 | pub const FORBIDDEN_CHARS: &[char] = &['<', '>', ':', '"', '/', '\\', ',', '?', '*']; 29 | /// A list of all filenames that are reserved in Windows (and should probably also not be used by FAT) 30 | pub const RESERVED_FILENAMES: &[&str] = &[ 31 | "CON", "PRN", "AUX", "NUL", "COM0", "COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", 32 | "COM8", "COM9", "COM¹", "COM²", "COM³", "LPT0", "LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", 33 | "LPT7", "LPT8", "LPT9", "LPT¹", "LPT²", "LPT³", 34 | ]; 35 | 36 | /// Check whether a [`PathBuf`] is forbidden for use in filenames or directory names 37 | fn is_forbidden(pathbuf: &PathBuf) -> bool { 38 | for subpath in pathbuf.clone() { 39 | if subpath 40 | .chars() 41 | .any(|c| c.is_control() || FORBIDDEN_CHARS.contains(&c)) 42 | { 43 | return true; 44 | } 45 | } 46 | 47 | if let Some(file_name) = pathbuf.file_name() { 48 | if RESERVED_FILENAMES 49 | .iter() 50 | // remove extension 51 | .map(|file_name| file_name.split_once(".").map(|s| s.0).unwrap_or(file_name)) 52 | .any(|reserved| file_name == reserved) 53 | { 54 | return true; 55 | } 56 | } 57 | 58 | false 59 | } 60 | 61 | // TODO: pushing an absolute path should replace a pathbuf 62 | 63 | /// Represents an owned, mutable path 64 | #[derive(Debug, Clone)] 65 | pub struct PathBuf { 66 | inner: VecDeque, 67 | } 68 | 69 | impl PathBuf { 70 | /// Create a new, empty [`PathBuf`] pointing to the root directory ("/") 71 | pub fn new() -> Self { 72 | log::debug!("New PathBuf created"); 73 | Self::default() 74 | } 75 | 76 | /// Extends `self` with `subpath` 77 | /// 78 | /// Doesn't replace the current path if the `path` is absolute 79 | pub fn push

(&mut self, subpath: P) 80 | where 81 | P: ToString, 82 | { 83 | let subpath = subpath.to_string(); 84 | 85 | // if this is an absolute path, clear `self` 86 | if ['\\', '/'].contains(&subpath.chars().next().unwrap_or_default()) { 87 | self.clear() 88 | } 89 | 90 | let mut split_subpath: VecDeque<&str> = 91 | subpath.split(|c| ['\\', '/'].contains(&c)).collect(); 92 | 93 | // remove trailing slash in the beginning of split_subpath.. 94 | if split_subpath.front().map(|s| s.is_empty()).unwrap_or(false) { 95 | split_subpath.pop_front(); 96 | }; 97 | // ...as well in the beginning of the inner deque 98 | if self.inner.back().map(|s| s.is_empty()).unwrap_or(false) { 99 | self.inner.pop_back(); 100 | }; 101 | 102 | for p in split_subpath { 103 | match p { 104 | "." => continue, 105 | ".." => { 106 | self.inner.pop_back(); 107 | continue; 108 | } 109 | _ => self.inner.push_back(p.to_string()), 110 | } 111 | } 112 | 113 | log::debug!("Pushed {} into PathBuf", subpath); 114 | } 115 | 116 | /// If `self` is a file, returns `Ok(file_name)`, otherwise `None` 117 | pub fn file_name(&self) -> Option { 118 | if let Some(file_name) = self.inner.back() { 119 | if !file_name.is_empty() { 120 | return Some(file_name.to_owned()); 121 | } 122 | } 123 | 124 | None 125 | } 126 | 127 | /// Returns the parent directory of the current [`PathBuf`] 128 | pub fn parent(&self) -> PathBuf { 129 | if self.inner.len() > 1 { 130 | let mut pathbuf = self.clone(); 131 | pathbuf.inner.back_mut().unwrap().clear(); 132 | pathbuf 133 | } else { 134 | PathBuf::new() 135 | } 136 | } 137 | 138 | /// Whether or not `self` represents a directory 139 | pub fn is_dir(&self) -> bool { 140 | match self.inner.len() { 141 | // root directory 142 | 0 => true, 143 | n => self.inner[n - 1].is_empty(), 144 | } 145 | } 146 | 147 | /// Whether or not `self` represents a file 148 | pub fn is_file(&self) -> bool { 149 | !self.is_dir() 150 | } 151 | 152 | /// Resets `self` 153 | pub fn clear(&mut self) { 154 | *self = Self::new(); 155 | } 156 | 157 | /// Checks whether `self` is malformed (as if someone pushed a string with many consecutive slashes) 158 | pub(crate) fn is_malformed(&self) -> bool { 159 | is_forbidden(&self) 160 | } 161 | } 162 | 163 | impl From for PathBuf { 164 | fn from(value: String) -> Self { 165 | let mut pathbuf = PathBuf::new(); 166 | pathbuf.push(value); 167 | 168 | pathbuf 169 | } 170 | } 171 | 172 | impl From<&str> for PathBuf { 173 | fn from(value: &str) -> Self { 174 | Self::from(value.to_string()) 175 | } 176 | } 177 | 178 | impl fmt::Display for PathBuf { 179 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 180 | write!( 181 | f, 182 | "/{}", 183 | self.inner.iter().cloned().collect::>().join("/") 184 | ) 185 | } 186 | } 187 | 188 | impl Default for PathBuf { 189 | fn default() -> Self { 190 | Self { 191 | inner: VecDeque::new(), 192 | } 193 | } 194 | } 195 | 196 | impl iter::Iterator for PathBuf { 197 | type Item = String; 198 | 199 | fn next(&mut self) -> Option { 200 | // for this to work correctly, the PathBuf mustn't be malformed 201 | self.inner.pop_front().filter(|s| !s.is_empty()) 202 | } 203 | } 204 | 205 | #[cfg(all(test, feature = "std"))] 206 | mod tests { 207 | use super::*; 208 | use test_log::test; 209 | 210 | #[test] 211 | fn empty_pathbuf_tostring() { 212 | let pathbuf = PathBuf::new(); 213 | 214 | assert_eq!(pathbuf.to_string(), "/"); 215 | assert!(!pathbuf.is_malformed()); 216 | } 217 | 218 | #[test] 219 | fn catch_invalid_path() { 220 | #[cfg(not(feature = "std"))] 221 | use ::alloc::format; 222 | 223 | let mut pathbuf = PathBuf::new(); 224 | 225 | // ignore path separators 226 | for filename in RESERVED_FILENAMES { 227 | pathbuf.push(format!("/{filename}")); 228 | 229 | assert!( 230 | pathbuf.is_malformed(), 231 | "unable to detect invalid filename {}", 232 | pathbuf 233 | ); 234 | 235 | pathbuf.clear(); 236 | } 237 | } 238 | 239 | #[test] 240 | fn catch_non_control_forbidden_chars() { 241 | #[cfg(not(feature = "std"))] 242 | use ::alloc::format; 243 | 244 | let mut pathbuf = PathBuf::new(); 245 | 246 | // ignore path separators 247 | const PATH_SEPARATORS: &[char] = &['/', '\\']; 248 | for c in FORBIDDEN_CHARS 249 | .iter() 250 | .filter(|c| !PATH_SEPARATORS.contains(c)) 251 | { 252 | pathbuf.push(format!("/{c}")); 253 | 254 | assert!( 255 | pathbuf.is_malformed(), 256 | "unable to detect character {} (hex: {:#02x}) as forbidden {:?}", 257 | c, 258 | (*c as usize), 259 | pathbuf 260 | ); 261 | 262 | pathbuf.clear(); 263 | } 264 | } 265 | 266 | #[test] 267 | fn push_to_pathbuf() { 268 | let mut pathbuf = PathBuf::new(); 269 | 270 | pathbuf.push("foo"); 271 | pathbuf.push("bar/test"); 272 | pathbuf.push("bar2\\test2"); 273 | pathbuf.push("ignored\\../."); 274 | pathbuf.push("fintest1"); 275 | pathbuf.push("fintest2/"); 276 | pathbuf.push("last"); 277 | 278 | assert_eq!( 279 | pathbuf.to_string(), 280 | "/foo/bar/test/bar2/test2/fintest1/fintest2/last" 281 | ) 282 | } 283 | 284 | #[test] 285 | fn push_absolute_path() { 286 | let mut pathbuf = PathBuf::from("/foo/bar.txt"); 287 | 288 | pathbuf.push("\\test"); 289 | 290 | assert_eq!(pathbuf.to_string(), "/test") 291 | } 292 | } 293 | -------------------------------------------------------------------------------- /tests/bee movie script.txt: -------------------------------------------------------------------------------- 1 | According to all known laws of aviation, there is no way a bee should be able to fly. 2 | Its wings are too small to get its fat little body off the ground. 3 | The bee, of course, flies anyway because bees don't care what humans think is impossible. 4 | Yellow, black. Yellow, black. Yellow, black. Yellow, black. 5 | Ooh, black and yellow! 6 | Let's shake it up a little. 7 | Barry! Breakfast is ready! 8 | Coming! 9 | Hang on a second. 10 | Hello? 11 | Barry? 12 | Adam? 13 | Can you believe this is happening? 14 | I can't. 15 | I'll pick you up. 16 | Looking sharp. 17 | Use the stairs, Your father paid good money for those. 18 | Sorry. I'm excited. 19 | Here's the graduate. 20 | We're very proud of you, son. 21 | A perfect report card, all B's. 22 | Very proud. 23 | Ma! I got a thing going here. 24 | You got lint on your fuzz. 25 | Ow! That's me! 26 | Wave to us! We'll be in row 118,000. 27 | Bye! 28 | Barry, I told you, stop flying in the house! 29 | Hey, Adam. 30 | Hey, Barry. 31 | Is that fuzz gel? 32 | A little. Special day, graduation. 33 | Never thought I'd make it. 34 | Three days grade school, three days high school. 35 | Those were awkward. 36 | Three days college. I'm glad I took a day and hitchhiked around The Hive. 37 | You did come back different. 38 | Hi, Barry. Artie, growing a mustache? Looks good. 39 | Hear about Frankie? 40 | Yeah. 41 | You going to the funeral? 42 | No, I'm not going. 43 | Everybody knows, sting someone, you die. 44 | Don't waste it on a squirrel. 45 | Such a hothead. 46 | I guess he could have just gotten out of the way. 47 | I love this incorporating an amusement park into our day. 48 | That's why we don't need vacations. 49 | Boy, quite a bit of pomp under the circumstances. 50 | Well, Adam, today we are men. 51 | We are! 52 | Bee-men. 53 | Amen! 54 | Hallelujah! 55 | Students, faculty, distinguished bees, 56 | please welcome Dean Buzzwell. 57 | Welcome, New Hive City graduating class of 9:15. 58 | That concludes our ceremonies And begins your career at Honex Industries! 59 | Will we pick our job today? 60 | I heard it's just orientation. 61 | Heads up! Here we go. 62 | Keep your hands and antennas inside the tram at all times. 63 | Wonder what it'll be like? 64 | A little scary. 65 | Welcome to Honex, a division of Honesco and a part of the Hexagon Group. 66 | This is it! 67 | Wow. 68 | Wow. 69 | We know that you, as a bee, have worked your whole life to get to the point where you can work for your whole life. 70 | Honey begins when our valiant Pollen Jocks bring the nectar to The Hive. 71 | Our top-secret formula is automatically color-corrected, scent-adjusted and bubble-contoured into this soothing sweet syrup with its distinctive golden glow you know as... Honey! 72 | That girl was hot. 73 | She's my cousin! 74 | She is? 75 | Yes, we're all cousins. 76 | Right. You're right. 77 | At Honex, we constantly strive to improve every aspect of bee existence. 78 | These bees are stress-testing a new helmet technology. 79 | What do you think he makes? 80 | Not enough. 81 | Here we have our latest advancement, the Krelman. 82 | What does that do? 83 | Catches that little strand of honey that hangs after you pour it. 84 | Saves us millions. 85 | Can anyone work on the Krelman? 86 | Of course. Most bee jobs are small ones. 87 | But bees know that every small job, if it's done well, means a lot. 88 | But choose carefully because you'll stay in the job you pick for the rest of your life. 89 | The same job the rest of your life? I didn't know that. 90 | What's the difference? 91 | You'll be happy to know that bees, as a species, haven't had one day off in 27 million years. 92 | So you'll just work us to death? 93 | We'll sure try. 94 | Wow! That blew my mind! 95 | "What's the difference?" 96 | How can you say that? 97 | One job forever? 98 | That's an insane choice to have to make. 99 | I'm relieved. Now we only have to make one decision in life. 100 | But, Adam, how could they never have told us that? 101 | Why would you question anything? We're bees. 102 | We're the most perfectly functioning society on Earth. 103 | You ever think maybe things work a little too well here? 104 | Like what? Give me one example. 105 | I don't know. But you know what I'm talking about. 106 | Please clear the gate. Royal Nectar Force on approach. 107 | Wait a second. Check it out. 108 | Hey, those are Pollen Jocks! 109 | Wow. 110 | I've never seen them this close. 111 | They know what it's like outside The Hive. 112 | Yeah, but some don't come back. 113 | Hey, Jocks! 114 | Hi, Jocks! 115 | You guys did great! 116 | You're monsters! 117 | You're sky freaks! I love it! I love it! 118 | I wonder where they were. 119 | I don't know. 120 | Their day's not planned. 121 | Outside The Hive, flying who knows where, doing who knows what. 122 | You can't just decide to be a Pollen Jock. You have to be bred for that. 123 | Right. 124 | Look. That's more pollen than you and I will see in a lifetime. 125 | It's just a status symbol. 126 | Bees make too much of it. 127 | Perhaps. Unless you're wearing it and the ladies see you wearing it. 128 | Those ladies? 129 | Aren't they our cousins too? 130 | Distant. Distant. 131 | Look at these two. 132 | Couple of Hive Harrys. 133 | Let's have fun with them. 134 | It must be dangerous being a Pollen Jock. 135 | Yeah. Once a bear pinned me against a mushroom! 136 | He had a paw on my throat, and with the other, he was slapping me! 137 | Oh, my! 138 | I never thought I'd knock him out. 139 | What were you doing during this? 140 | Trying to alert the authorities. 141 | I can autograph that. 142 | A little gusty out there today, wasn't it, comrades? 143 | Yeah. Gusty. 144 | We're hitting a sunflower patch six miles from here tomorrow. 145 | Six miles, huh? 146 | Barry! 147 | A puddle jump for us, but maybe you're not up for it. 148 | Maybe I am. 149 | You are not! 150 | We're going 0900 at J-Gate. 151 | What do you think, buzzy-boy? 152 | Are you bee enough? 153 | I might be. It all depends on what 0900 means. 154 | Hey, Honex! 155 | Dad, you surprised me. 156 | You decide what you're interested in? 157 | Well, there's a lot of choices. 158 | But you only get one. 159 | Do you ever get bored doing the same job every day? 160 | Son, let me tell you about stirring. 161 | You grab that stick, and you just move it around, and you stir it around. 162 | You get yourself into a rhythm. 163 | It's a beautiful thing. 164 | You know, Dad, the more I think about it, 165 | maybe the honey field just isn't right for me. 166 | You were thinking of what, making balloon animals? 167 | That's a bad job for a guy with a stinger. 168 | Janet, your son's not sure he wants to go into honey! 169 | Barry, you are so funny sometimes. 170 | I'm not trying to be funny. 171 | You're not funny! You're going into honey. Our son, the stirrer! 172 | You're gonna be a stirrer? 173 | No one's listening to me! 174 | Wait till you see the sticks I have. 175 | I could say anything right now. 176 | I'm gonna get an ant tattoo! 177 | Let's open some honey and celebrate! 178 | Maybe I'll pierce my thorax. Shave my antennae. Shack up with a grasshopper. Get a gold tooth and call everybody "dawg"! 179 | I'm so proud. 180 | We're starting work today! 181 | Today's the day. 182 | Come on! All the good jobs will be gone. 183 | Yeah, right. 184 | Pollen counting, stunt bee, pouring, stirrer, front desk, hair removal... 185 | Is it still available? 186 | Hang on. Two left! 187 | One of them's yours! Congratulations! 188 | Step to the side. 189 | What'd you get? 190 | Picking crud out. Stellar! 191 | Wow! 192 | Couple of newbies? 193 | Yes, sir! Our first day! We are ready! 194 | Make your choice. 195 | You want to go first? 196 | No, you go. 197 | Oh, my. What's available? 198 | Restroom attendant's open, not for the reason you think. 199 | Any chance of getting the Krelman? 200 | Sure, you're on. 201 | I'm sorry, the Krelman just closed out. 202 | Wax monkey's always open. 203 | The Krelman opened up again. 204 | What happened? 205 | A bee died. Makes an opening. See? He's dead. Another dead one. 206 | Deady. Deadified. Two more dead. 207 | Dead from the neck up. Dead from the neck down. That's life! 208 | Oh, this is so hard! 209 | Heating, cooling, stunt bee, pourer, stirrer, humming, inspector number seven, lint coordinator, stripe supervisor, mite wrangler. 210 | Barry, what do you think I should... Barry? 211 | Barry! 212 | All right, we've got the sunflower patch in quadrant nine... 213 | What happened to you? 214 | Where are you? 215 | I'm going out. 216 | Out? Out where? 217 | Out there. 218 | Oh, no! 219 | I have to, before I go to work for the rest of my life. 220 | You're gonna die! You're crazy! Hello? 221 | Another call coming in. 222 | If anyone's feeling brave, there's a Korean deli on 83rd that gets their roses today. 223 | Hey, guys. 224 | Look at that. 225 | Isn't that the kid we saw yesterday? 226 | Hold it, son, flight deck's restricted. 227 | It's OK, Lou. We're gonna take him up. 228 | Really? Feeling lucky, are you? 229 | Sign here, here. Just initial that. 230 | Thank you. 231 | OK. 232 | You got a rain advisory today, and as you all know, bees cannot fly in rain. 233 | So be careful. As always, watch your brooms, hockey sticks, dogs, birds, bears and bats. 234 | Also, I got a couple of reports of root beer being poured on us. 235 | Murphy's in a home because of it, babbling like a cicada! 236 | That's awful. 237 | And a reminder for you rookies, bee law number one, absolutely no talking to humans! 238 | All right, launch positions! 239 | Buzz, buzz, buzz, buzz! Buzz, buzz, buzz, buzz! Buzz, buzz, buzz, buzz! 240 | Black and yellow! 241 | Hello! 242 | You ready for this, hot shot? 243 | Yeah. Yeah, bring it on. 244 | Wind, check. 245 | Antennae, check. 246 | Nectar pack, check. 247 | Wings, check. 248 | Stinger, check. 249 | Scared out of my shorts, check. 250 | OK, ladies, 251 | let's move it out! 252 | Pound those petunias, you striped stem-suckers! 253 | All of you, drain those flowers! 254 | Wow! I'm out! 255 | I can't believe I'm out! 256 | So blue. 257 | I feel so fast and free! 258 | Box kite! 259 | Wow! 260 | Flowers! 261 | This is Blue Leader, We have roses visual. 262 | Bring it around 30 degrees and hold. 263 | Roses! 264 | 30 degrees, roger. Bringing it around. 265 | Stand to the side, kid. 266 | It's got a bit of a kick. 267 | That is one nectar collector! 268 | Ever see pollination up close? 269 | No, sir. 270 | I pick up some pollen here, sprinkle it over here. Maybe a dash over there, a pinch on that one. 271 | See that? It's a little bit of magic. 272 | That's amazing. Why do we do that? 273 | That's pollen power. More pollen, more flowers, more nectar, more honey for us. 274 | Cool. 275 | I'm picking up a lot of bright yellow, Could be daisies, Don't we need those? 276 | Copy that visual. 277 | Wait. One of these flowers seems to be on the move. 278 | Say again? You're reporting a moving flower? 279 | Affirmative. 280 | That was on the line! 281 | This is the coolest. What is it? 282 | I don't know, but I'm loving this color. 283 | It smells good. 284 | Not like a flower, but I like it. 285 | Yeah, fuzzy. 286 | Chemical-y. 287 | Careful, guys. It's a little grabby. 288 | My sweet lord of bees! 289 | Candy-brain, get off there! 290 | Problem! 291 | Guys! 292 | This could be bad. 293 | Affirmative. 294 | Very close. 295 | Gonna hurt. 296 | Mama's little boy. 297 | You are way out of position, rookie! 298 | Coming in at you like a missile! 299 | Help me! 300 | I don't think these are flowers. 301 | Should we tell him? 302 | I think he knows. 303 | What is this?! 304 | Match point! 305 | You can start packing up, honey, because you're about to eat it! 306 | Yowser! 307 | Gross. 308 | There's a bee in the car! 309 | Do something! 310 | I'm driving! 311 | Hi, bee. 312 | He's back here! 313 | He's going to sting me! 314 | Nobody move. If you don't move, he won't sting you. Freeze! 315 | He blinked! 316 | Spray him, Granny! 317 | What are you doing?! 318 | Wow... the tension level out here is unbelievable. 319 | I gotta get home. 320 | Can't fly in rain. Can't fly in rain. Can't fly in rain. 321 | Mayday! Mayday! Bee going down! 322 | Ken, could you close the window please? 323 | Ken, could you close the window please? 324 | Check out my new resume. I made it into a fold-out brochure. You see? Folds out. 325 | Oh, no. More humans. I don't need this. 326 | What was that? 327 | Maybe this time. This time. This time. This time! This time! This... Drapes! 328 | That is diabolical. 329 | It's fantastic. It's got all my special skills, even my top-ten favorite movies. 330 | What's number one? Star Wars? 331 | Nah, I don't go for that... kind of stuff. 332 | No wonder we shouldn't talk to them. They're out of their minds. 333 | When I leave a job interview, they're flabbergasted, can't believe what I say. 334 | There's the sun. Maybe that's a way out. 335 | I don't remember the sun having a big 75 on it. 336 | I predicted global warming. I could feel it getting hotter. At first I thought it was just me. 337 | Wait! Stop! Bee! 338 | Stand back. These are winter boots. 339 | Wait! 340 | Don't kill him! 341 | You know I'm allergic to them! This thing could kill me! 342 | Why does his life have less value than yours? 343 | Why does his life have any less value than mine? Is that your statement? 344 | I'm just saying all life has value. You don't know what he's capable of feeling. 345 | My brochure! 346 | There you go, little guy. 347 | I'm not scared of him.It's an allergic thing. 348 | Put that on your resume brochure. 349 | My whole face could puff up. 350 | Make it one of your special skills. 351 | Knocking someone out is also a special skill. 352 | Right. Bye, Vanessa. Thanks. 353 | Vanessa, next week? Yogurt night? 354 | Sure, Ken. You know, whatever. 355 | You could put carob chips on there. 356 | Bye. 357 | Supposed to be less calories. 358 | Bye. 359 | I gotta say something. She saved my life. I gotta say something. 360 | All right, here it goes. 361 | Nah. 362 | What would I say? 363 | I could really get in trouble. It's a bee law. You're not supposed to talk to a human. 364 | I can't believe I'm doing this. I've got to. 365 | Oh, I can't do it. Come on! 366 | No. Yes. No. Do it. I can't. 367 | How should I start it? "You like jazz?" No, that's no good. 368 | Here she comes! Speak, you fool! 369 | Hi! 370 | I'm sorry. You're talking. 371 | Yes, I know. 372 | You're talking! 373 | I'm so sorry. 374 | No, it's OK. It's fine. 375 | I know I'm dreaming. But I don't recall going to bed. 376 | Well, I'm sure this is very disconcerting. 377 | This is a bit of a surprise to me. I mean, you're a bee! 378 | I am. And I'm not supposed to be doing this, but they were all trying to kill me. 379 | And if it wasn't for you... I had to thank you. It's just how I was raised. 380 | That was a little weird. I'm talking with a bee. 381 | Yeah. 382 | I'm talking to a bee. And the bee is talking to me! 383 | I just want to say I'm grateful. 384 | I'll leave now. 385 | Wait! How did you learn to do that? 386 | What? 387 | The talking thing. 388 | Same way you did, I guess. "Mama, Dada, honey." You pick it up. 389 | That's very funny. 390 | Yeah. 391 | Bees are funny. If we didn't laugh, we'd cry with what we have to deal with. 392 | Anyway... Can I... get you something? 393 | Like what? 394 | I don't know. I mean... I don't know. Coffee? 395 | I don't want to put you out. 396 | It's no trouble. It takes two minutes. 397 | It's just coffee. 398 | I hate to impose. 399 | Don't be ridiculous! 400 | Actually, I would love a cup. 401 | Hey, you want rum cake? 402 | I shouldn't. 403 | Have some. 404 | No, I can't. 405 | Come on! 406 | I'm trying to lose a couple micrograms. 407 | Where? 408 | These stripes don't help. 409 | You look great! 410 | I don't know if you know anything about fashion. 411 | Are you all right? 412 | No. 413 | He's making the tie in the cab as they're flying up Madison. 414 | He finally gets there. 415 | He runs up the steps into the church. 416 | The wedding is on. 417 | And he says, "Watermelon? 418 | I thought you said Guatemalan. 419 | Why would I marry a watermelon?" 420 | Is that a bee joke? 421 | That's the kind of stuff we do. 422 | Yeah, different. 423 | So, what are you gonna do, Barry? 424 | About work? I don't know. 425 | I want to do my part for The Hive, but I can't do it the way they want. 426 | I know how you feel. 427 | You do? 428 | Sure. 429 | My parents wanted me to be a lawyer or a doctor, but I wanted to be a florist. 430 | Really? 431 | My only interest is flowers. 432 | Our new queen was just elected with that same campaign slogan. 433 | Anyway, if you look... There's my hive right there. See it? 434 | You're in Sheep Meadow! 435 | Yes! I'm right off the Turtle Pond! 436 | No way! I know that area. I lost a toe ring there once. 437 | Why do girls put rings on their toes? 438 | Why not? 439 | It's like putting a hat on your knee. 440 | Maybe I'll try that. 441 | You all right, ma'am? 442 | Oh, yeah. Fine. 443 | Just having two cups of coffee! 444 | Anyway, this has been great. 445 | Thanks for the coffee. 446 | Yeah, it's no trouble. 447 | Sorry I couldn't finish it. If I did, I'd be up the rest of my life. 448 | Are you...? 449 | Can I take a piece of this with me? 450 | Sure! Here, have a crumb. 451 | Thanks! 452 | Yeah. 453 | All right. Well, then... I guess I'll see you around. Or not. 454 | OK, Barry. 455 | And thank you so much again... for before. 456 | Oh, that? That was nothing. 457 | Well, not nothing, but... Anyway... 458 | This can't possibly work. 459 | He's all set to go. 460 | We may as well try it. 461 | OK, Dave, pull the chute. 462 | Sounds amazing. 463 | It was amazing! 464 | It was the scariest, happiest moment of my life. 465 | Humans! I can't believe you were with humans! 466 | Giant, scary humans! 467 | What were they like? 468 | Huge and crazy. They talk crazy. 469 | They eat crazy giant things. 470 | They drive crazy. 471 | Do they try and kill you, like on TV? 472 | Some of them. But some of them don't. 473 | How'd you get back? 474 | Poodle. 475 | You did it, and I'm glad. You saw whatever you wanted to see. 476 | You had your "experience." Now you can pick out yourjob and be normal. 477 | Well... 478 | Well? 479 | Well, I met someone. 480 | You did? Was she Bee-ish? 481 | A wasp?! Your parents will kill you! 482 | No, no, no, not a wasp. 483 | Spider? 484 | I'm not attracted to spiders. 485 | I know it's the hottest thing, with the eight legs and all. I can't get by that face. 486 | So who is she? 487 | She's... human. 488 | No, no. That's a bee law. You wouldn't break a bee law. 489 | Her name's Vanessa. 490 | Oh, boy. 491 | She's so nice. And she's a florist! 492 | Oh, no! You're dating a human florist! 493 | We're not dating. 494 | You're flying outside The Hive, talking to humans that attack our homes with power washers and M-80s! One-eighth a stick of dynamite! 495 | She saved my life! And she understands me. 496 | This is over! 497 | Eat this. 498 | This is not over! What was that? 499 | They call it a crumb. 500 | It was so stingin' stripey! 501 | And that's not what they eat. 502 | That's what falls off what they eat! 503 | You know what a Cinnabon is? 504 | No. 505 | It's bread and cinnamon and frosting. They heat it up... 506 | Sit down! 507 | ...really hot! 508 | Listen to me! 509 | We are not them! We're us. 510 | There's us and there's them! 511 | Yes, but who can deny the heart that is yearning? 512 | There's no yearning. Stop yearning. Listen to me! 513 | You have got to start thinking bee, my friend. Thinking bee! 514 | Thinking bee. 515 | Thinking bee. 516 | Thinking bee! Thinking bee! Thinking bee! Thinking bee! 517 | There he is. He's in the pool. 518 | You know what your problem is, Barry? 519 | I gotta start thinking bee? 520 | How much longer will this go on? 521 | It's been three days! Why aren't you working? 522 | I've got a lot of big life decisions to think about. 523 | What life? You have no life! 524 | You have no job. You're barely a bee! 525 | Would it kill you to make a little honey? 526 | Barry, come out. Your father's talking to you. 527 | Martin, would you talk to him? 528 | Barry, I'm talking to you! 529 | You coming? 530 | Got everything? 531 | All set! 532 | Go ahead. I'll catch up. 533 | Don't be too long. 534 | Watch this! 535 | Vanessa! 536 | We're still here. 537 | I told you not to yell at him. 538 | He doesn't respond to yelling! 539 | Then why yell at me? 540 | Because you don't listen! 541 | I'm not listening to this. 542 | Sorry, I've gotta go. 543 | Where are you going? 544 | I'm meeting a friend. 545 | A girl? Is this why you can't decide? 546 | Bye. 547 | I just hope she's Bee-ish. 548 | They have a huge parade of flowers every year in Pasadena? 549 | To be in the Tournament of Roses, that's every florist's dream! 550 | Up on a float, surrounded by flowers, crowds cheering. 551 | A tournament. Do the roses compete in athletic events? 552 | No. All right, I've got one. 553 | How come you don't fly everywhere? 554 | It's exhausting. Why don't you run everywhere? It's faster. 555 | Yeah, OK, I see, I see. 556 | All right, your turn. 557 | TiVo. You can just freeze live TV? That's insane! 558 | You don't have that? 559 | We have Hivo, but it's a disease. It's a horrible, horrible disease. 560 | Oh, my. 561 | Dumb bees! 562 | You must want to sting all those jerks. 563 | We try not to sting. It's usually fatal for us. 564 | So you have to watch your temper. 565 | Very carefully. 566 | You kick a wall, take a walk, write an angry letter and throw it out. Work through it like any emotion: Anger, jealousy, lust. 567 | Oh, my goodness! Are you OK? 568 | Yeah. 569 | What is wrong with you?! 570 | It's a bug. 571 | He's not bothering anybody. 572 | Get out of here, you creep! 573 | What was that? A Pic 'N' Save circular? 574 | Yeah, it was. How did you know? 575 | It felt like about 10 pages. Seventy-five is pretty much our limit. 576 | You've really got that down to a science. 577 | I lost a cousin to Italian Vogue. 578 | I'll bet. 579 | What in the name of Mighty Hercules is this? 580 | How did this get here? cute Bee, Golden Blossom, Ray Liotta Private Select? 581 | Is he that actor? 582 | I never heard of him. 583 | Why is this here? 584 | For people. We eat it. 585 | You don't have enough food of your own? 586 | Well, yes. 587 | How do you get it? 588 | Bees make it. 589 | I know who makes it! And it's hard to make it! 590 | There's heating, cooling, stirring. You need a whole Krelman thing! 591 | It's organic. 592 | It's our-ganic! 593 | It's just honey, Barry. 594 | Just what?! 595 | Bees don't know about this! This is stealing! A lot of stealing! 596 | You've taken our homes, schools,hospitals! This is all we have! 597 | And it's on sale?! I'm getting to the bottom of this. 598 | I'm getting to the bottom of all of this! 599 | Hey, Hector. You almost done? 600 | Almost. 601 | He is here. I sense it. 602 | Well, I guess I'll go home now and just leave this nice honey out, with no one around. 603 | You're busted, box boy! 604 | I knew I heard something. 605 | So you can talk! 606 | I can talk. And now you'll start talking! 607 | Where you getting the sweet stuff? Who's your supplier? 608 | I don't understand. 609 | I thought we were friends. 610 | The last thing we want to do is upset bees! 611 | You're too late! It's ours now! 612 | You, sir, have crossed the wrong sword! 613 | You, sir, will be lunch for my iguana, Ignacio! 614 | Where is the honey coming from? Tell me where! 615 | Honey Farms! It comes from Honey Farms! 616 | Crazy person! 617 | What horrible thing has happened here? 618 | These faces, they never knew what hit them. And now 619 | they're on the road to nowhere! 620 | Just keep still. 621 | What? You're not dead? 622 | Do I look dead? They will wipe anything that moves. Where you headed? 623 | To Honey Farms. I am onto something huge here. 624 | I'm going to Alaska. Moose blood, crazy stuff. Blows your head off! 625 | I'm going to Tacoma. 626 | And you? 627 | He really is dead. 628 | All right. 629 | Uh-oh! 630 | What is that?! 631 | Oh, no! 632 | A wiper! Triple blade! 633 | Triple blade? 634 | Jump on! It's your only chance, bee! 635 | Why does everything have 636 | to be so doggone clean?! 637 | How much do you people need to see?! 638 | Open your eyes! 639 | Stick your head out the window! 640 | From NPR News in Washington, 641 | I'm Carl Kasell. 642 | But don't kill no more bugs! 643 | Bee! 644 | Moose blood guy!! 645 | You hear something? 646 | Like what? 647 | Like tiny screaming. 648 | Turn off the radio. 649 | Whassup, bee boy? 650 | Hey, Blood. 651 | Just a row of honey jars, as far as the eye could see. 652 | Wow! 653 | I assume wherever this truck goes is where they're getting it. I mean, that honey's ours. 654 | Bees hang tight. We're all jammed in. 655 | It's a close community. 656 | Not us, man. We on our own. Every mosquito on his own. 657 | What if you get in trouble? 658 | You a mosquito, you in trouble. Nobody likes us. They just smack. See a mosquito, smack, smack! 659 | At least you're out in the world. You must meet girls. 660 | Mosquito girls try to trade up, get with a moth, dragonfly. Mosquito girl don't want no mosquito. 661 | You got to be kidding me! 662 | Mooseblood's about to leave the building! So long, bee! 663 | Hey, guys! 664 | Mooseblood! 665 | I knew I'd catch y'all down here. 666 | Did you bring your crazy straw? 667 | We throw it in jars, slap a label on it, and it's pretty much pure profit. 668 | What is this place? 669 | A bee's got a brain the size of a pinhead. 670 | They are pinheads! 671 | Pinhead. 672 | Check out the new smoker. 673 | Oh, sweet. That's the one you want. The Thomas 3000! 674 | Smoker? 675 | Ninety puffs a minute, semi-automatic. Twice the nicotine, all the tar. A couple breaths of this knocks them right out. 676 | They make the honey, and we make the money. 677 | "They make the honey, and we make the money"? 678 | Oh, my! 679 | What's going on? Are you OK? 680 | Yeah. It doesn't last too long. 681 | Do you know you're in a fake hive with fake walls? 682 | Our queen was moved here. We had no choice. 683 | This is your queen? That's a man in women's clothes! That's a drag queen! 684 | What is this? 685 | Oh, no! 686 | There's hundreds of them! 687 | Bee honey. 688 | Our honey is being brazenly stolen on a massive scale! 689 | This is worse than anything bears have done! I intend to do something. 690 | Oh, Barry, stop. 691 | Who told you humans are taking our honey? That's a rumor. 692 | Do these look like rumors? 693 | That's a conspiracy theory. These are obviously doctored photos. How did you get mixed up in this? 694 | He's been talking to humans. 695 | What? Talking to humans?! 696 | He has a human girlfriend. And they make out! 697 | Make out? Barry! 698 | We do not. 699 | You wish you could. 700 | Whose side are you on? 701 | The bees! 702 | I dated a cricket once in San Antonio. Those crazy legs kept me up all night. 703 | Barry, this is what you want to do with your life? 704 | I want to do it for all our lives. Nobody works harder than bees! 705 | Dad, I remember you coming home so overworked 706 | your hands were still stirring. You couldn't stop. 707 | I remember that. 708 | What right do they have to our honey? 709 | We live on two cups a year. They put it in lip balm for no reason whatsoever! 710 | Even if it's true, what can one bee do? 711 | Sting them where it really hurts. 712 | In the face! The eye! 713 | That would hurt. 714 | No. 715 | Up the nose? That's a killer. 716 | There's only one place you can sting the humans, one place where it matters. 717 | Hive at Five, The Hive's only full-hour action news source. 718 | No more bee beards! 719 | With Bob Bumble at the anchor desk. Weather with Storm Stinger. Sports with Buzz Larvi. And Jeanette Chung. 720 | Good evening. I'm Bob Bumble. 721 | And I'm Jeanette Ohung. 722 | A tri-county bee, Barry Benson, intends to sue the human race for stealing our honey, packaging it and profiting from it illegally! 723 | Tomorrow night on Bee Larry King, we'll have three former queens here in our studio, discussing their new book, classy Ladies, out this week on Hexagon. 724 | Tonight we're talking to Barry Benson. 725 | Did you ever think, "I'm a kid from The Hive. I can't do this"? 726 | Bees have never been afraid to change the world. 727 | What about Bee Oolumbus? Bee Gandhi? Bejesus? 728 | Where I'm from, we'd never sue humans. 729 | We were thinking of stickball or candy stores. 730 | How old are you? 731 | The bee community is supporting you in this case, which will be the trial of the bee century. 732 | You know, they have a Larry King in the human world too. 733 | It's a common name. Next week... 734 | He looks like you and has a show and suspenders and colored dots... 735 | Next week... 736 | Glasses, quotes on the bottom from the guest even though you just heard 'em. 737 | Bear Week next week! They're scary, hairy and here live. 738 | Always leans forward, pointy shoulders, squinty eyes, very Jewish. 739 | In tennis, you attack at the point of weakness! 740 | It was my grandmother, Ken. She's 81. 741 | Honey, her backhand's a joke! 742 | I'm not gonna take advantage of that? 743 | Quiet, please. 744 | Actual work going on here. 745 | Is that that same bee? 746 | Yes, it is! 747 | I'm helping him sue the human race. 748 | Hello. 749 | Hello, bee. 750 | This is Ken. 751 | Yeah, I remember you. Timberland, size ten and a half. Vibram sole, I believe. 752 | Why does he talk again? 753 | Listen, you better go 'cause we're really busy working. 754 | But it's our yogurt night! 755 | Bye-bye. 756 | Why is yogurt night so difficult?! 757 | You poor thing. You two have been at this for hours! 758 | Yes, and Adam here has been a huge help. 759 | Frosting... 760 | How many sugars? 761 | Just one. I try not to use the competition. 762 | So why are you helping me? 763 | Bees have good qualities. And it takes my mind off the shop. Instead of flowers, people are giving balloon bouquets now. 764 | Those are great, if you're three. 765 | And artificial flowers. 766 | Oh, those just get me psychotic! 767 | Yeah, me too. 768 | Bent stingers, pointless pollination. 769 | Bees must hate those fake things! 770 | Nothing worse than a daffodil that's had work done. 771 | Maybe this could make up for it a little bit. 772 | This lawsuit's a pretty big deal. 773 | I guess. 774 | You sure you want to go through with it? 775 | Am I sure? When I'm done with the humans, they won't be able to say, "Honey, I'm home," without paying a royalty! 776 | It's an incredible scene here in downtown Manhattan, where the world anxiously waits, because for the first time in history, we will hear for ourselves if a honeybee can actually speak. 777 | What have we gotten into here, Barry? 778 | It's pretty big, isn't it? 779 | I can't believe how many humans don't work during the day. 780 | You think billion-dollar multinational food companies have good lawyers? 781 | Everybody needs to stay behind the barricade. 782 | What's the matter? 783 | I don't know, I just got a chill. 784 | Well, if it isn't the bee team. 785 | You boys work on this? 786 | All rise! The Honorable Judge Bumbleton presiding. 787 | All right. Case number 4475, 788 | Superior Court of New York, 789 | Barry Bee Benson v. the Honey Industry is now in session. 790 | Mr. Montgomery, you're representing the five food companies collectively? 791 | A privilege. 792 | Mr. Benson... you're representing all the bees of the world? 793 | I'm kidding. Yes, Your Honor, we're ready to proceed. 794 | Mr. Montgomery, your opening statement, please. 795 | Ladies and gentlemen of the jury, my grandmother was a simple woman. Born on a farm, she believed it was man's divine right to benefit from the bounty of nature God put before us. 796 | If we lived in the topsy-turvy world Mr. Benson imagines, just think of what would it mean. 797 | I would have to negotiate with the silkworm for the elastic in my britches! 798 | Talking bee! 799 | How do we know this isn't some sort of holographic motion-picture-capture Hollywood wizardry? 800 | They could be using laser beams! Robotics! Ventriloquism! Cloning! For all we know, he could be on steroids! 801 | Mr. Benson? 802 | Ladies and gentlemen, there's no trickery here. I'm just an ordinary bee. Honey's pretty important to me. It's important to all bees. We invented it! We make it. And we protect it with our lives. 803 | Unfortunately, there are some people in this room who think they can take it from us 'cause we're the little guys! 804 | I'm hoping that, after this is all over, you'll see how, by taking our honey, you not only take everything we have but everything we are! 805 | I wish he'd dress like that all the time. So nice! 806 | Call your first witness. 807 | So, Mr. Klauss Vanderhayden of Honey Farms, big company you have. 808 | I suppose so. 809 | I see you also own Honeyburton and Honron! 810 | Yes, they provide beekeepers for our farms. 811 | Beekeeper. I find that to be a very disturbing term. 812 | I don't imagine you employ any bee-free-ers, do you? 813 | No. 814 | I couldn't hear you. 815 | No. 816 | No. Because you don't free bees. You keep bees. Not only that, it seems you thought a bear would be an appropriate image for a jar of honey. 817 | They're very lovable creatures. Yogi Bear, Fozzie Bear, Build-A-Bear. 818 | You mean like this? 819 | Bears kill bees! 820 | How'd you like his head crashing through your living room?! Biting into your couch! Spitting out your throw pillows! OK, that's enough. Take him away. 821 | So, Mr. Sting, thank you for being here. Your name intrigues me. Where have I heard it before? 822 | I was with a band called The Police. 823 | But you've never been a police officer, have you? 824 | No, I haven't. 825 | No, you haven't. And so here we have yet another example of bee culture casually stolen by a human for nothing more than a prance-about stage name. 826 | Oh, please. 827 | Have you ever been stung, Mr. Sting? Because I'm feeling a little stung, Sting. Or should I say... Mr. Gordon M. Sumner! 828 | That's not his real name?! You idiots! 829 | Mr. Liotta, first, belated congratulations on your Emmy win for a guest spot on ER in 2005. 830 | Thank you. Thank you. 831 | I see from your resume that you're devilishly handsome with a churning inner turmoil that's ready to blow. 832 | I enjoy what I do. Is that a crime? 833 | Not yet it isn't. But is this what it's come to for you? Exploiting tiny, helpless bees so you don't have to rehearse your part and learn your lines, sir? 834 | Watch it, Benson! I could blow right now! 835 | This isn't a goodfella. 836 | This is a badfella! 837 | Why doesn't someone just step on this creep, and we can all go home?! 838 | Order in this court! 839 | You're all thinking it! 840 | Order! Order, I say! 841 | Say it! 842 | Mr. Liotta, please sit down! 843 | I think it was awfully nice of that bear to pitch in like that. I think the jury's on our side. 844 | Are we doing everything right, legally? 845 | I'm a florist. 846 | Right. Well, here's to a great team. 847 | To a great team! 848 | Well, hello. 849 | Ken! 850 | Hello. 851 | I didn't think you were coming. 852 | No, I was just late I tried to call, but... the battery. 853 | I didn't want all this to go to waste, 854 | so I called Barry. Luckily, he was free. 855 | Oh, that was lucky. 856 | There's a little left. I could heat it up. 857 | Yeah, heat it up, sure, whatever. 858 | So I hear you're quite a tennis player. I'm not much for the game myself. The ball's a little grabby. 859 | That's where I usually sit. Right... there. 860 | Ken, Barry was looking at your resume, and he agreed with me that eating with chopsticks isn't really a special skill. 861 | You think I don't see what you're doing? 862 | I know how hard it is to find the right job. We have that in common. 863 | Do we? 864 | Bees have 100 percent employment, but we do jobs like taking the crud out. 865 | That's just what I was thinking about doing. 866 | Ken, I let Barry borrow your razor for his fuzz. I hope that was all right. 867 | I'm going to drain the old stinger. 868 | Yeah, you do that. 869 | Look at that. 870 | You know, I've just about had it with your little Mind Games. 871 | What's that? 872 | Italian Vogue. 873 | Mamma mia, that's a lot of pages. 874 | A lot of ads. 875 | Remember what Van said, why is your life more valuable than mine? 876 | Funny, I just can't seem to recall that! I think something stinks in here! 877 | I love the smell of flowers. 878 | How do you like the smell of flames?! 879 | Not as much. 880 | Water bug! Not taking sides! 881 | Ken, I'm wearing a Chapstick hat! 882 | This is pathetic! 883 | I've got issues! 884 | Well, well, well, a royal flush! 885 | You're bluffing. 886 | Am I? 887 | Surf's up, dude! 888 | Poo water! 889 | That bowl is gnarly. Except for those dirty yellow rings! 890 | Kenneth! What are you doing?! 891 | You know, I don't even like honey! I don't eat it! 892 | We need to talk! He's just a little bee! 893 | And he happens to be the nicest bee I've met in a long time! 894 | Long time? What are you talking about?! Are there other bugs in your life? 895 | No, but there are other things bugging me in life. And you're one of them! 896 | Fine! Talking bees, no yogurt night... 897 | My nerves are fried from riding on this emotional roller coaster! 898 | Goodbye, Ken. 899 | And for your information, I prefer sugar-free, artificial sweeteners made by man! 900 | I'm sorry about all that. 901 | I know it's got an aftertaste! I like it! 902 | I always felt there was some kind of barrier between Ken and me. I couldn't overcome it. 903 | Oh, well. 904 | Are you OK for the trial? 905 | I believe Mr. Montgomery is about out of ideas. 906 | We would like to call Mr. Barry Benson Bee to the stand. 907 | Good idea! You can really see why he's considered one of the best lawyers... 908 | Yeah. 909 | Layton, you've gotta weave some magic with this jury, or it's gonna be all over. 910 | Don't worry. The only thing I have to do to turn this jury around is to remind them of what they don't like about bees. 911 | You got the tweezers? 912 | Are you allergic? 913 | Only to losing, son. Only to losing. 914 | Mr. Benson Bee, I'll ask you what I think we'd all like to know. 915 | What exactly is your relationship to that woman? 916 | We're friends. 917 | Good friends? 918 | Yes. 919 | How good? Do you live together? 920 | Wait a minute... Are you her little... bedbug? 921 | I've seen a bee documentary or two. From what I understand, doesn't your queen give birth to all the bee children? 922 | Yeah, but... 923 | So those aren't your real parents! 924 | Oh, Barry... 925 | Yes, they are! 926 | Hold me back! 927 | You're an illegitimate bee, aren't you, Benson? 928 | He's denouncing bees! 929 | Don't y'all date your cousins? 930 | Objection! 931 | I'm going to pincushion this guy! 932 | Adam, don't! It's what he wants! 933 | Oh, I'm hit!! Oh, lordy, I am hit! 934 | Order! Order! 935 | The venom! The venom is coursing through my veins! I have been felled by a winged beast of destruction! You see? You can't treat them like equals! They're striped savages! Stinging's the only thing they know! It's their way! 936 | Adam, stay with me. 937 | I can't feel my legs. 938 | What Angel of Mercy will come forward to suck the poison from my heaving buttocks? 939 | I will have order in this court. Order! Order, please! 940 | The case of the honeybees versus the human race took a pointed Turn Against the bees yesterday when one of their legal team stung Layton T. Montgomery. 941 | Hey, buddy. 942 | Hey. 943 | Is there much pain? 944 | Yeah. 945 | I... I blew the whole case, didn't I? 946 | It doesn't matter. What matters is 947 | you're alive. You could have died. 948 | I'd be better off dead. Look at me. 949 | They got it from the cafeteria downstairs, in a tuna sandwich. Look, there's a little celery still on it. 950 | What was it like to sting someone? 951 | I can't explain it. It was all... All adrenaline and then...and then ecstasy! 952 | All right. 953 | You think it was all a trap? 954 | Of course. I'm sorry. I flew us right into this. 955 | What were we thinking? Look at us. We're just a couple of bugs in this world. 956 | What will the humans do to us if they win? 957 | I don't know. 958 | I hear they put the roaches in motels. That doesn't sound so bad. 959 | Adam, they check in, but they don't check out! 960 | Oh, my. 961 | Could you get a nurse to close that window? 962 | Why? 963 | The smoke. 964 | Bees don't smoke. 965 | Right. Bees don't smoke. 966 | Bees don't smoke! 967 | But some bees are smoking. 968 | That's it! That's our case! 969 | It is? It's not over? 970 | Get dressed. I've gotta go somewhere. 971 | Get back to the court and stall. Stall any way you can. 972 | And assuming you've done step correctly, you're ready for the tub. 973 | Mr. Flayman. 974 | Yes? Yes, Your Honor! 975 | Where is the rest of your team? 976 | Well, Your Honor, it's interesting. Bees are trained to fly haphazardly, and as a result, we don't make very good time. 977 | I actually heard a funny story about... 978 | Your Honor, haven't these ridiculous bugs taken up enough of this court's valuable time? How much longer will we allow these absurd shenanigans to go on? 979 | They have presented no compelling evidence to support their charges against my clients, who run legitimate businesses. 980 | I move for a complete dismissal of this entire case! 981 | Mr. Flayman, I'm afraid I'm going to have to consider Mr. Montgomery's motion. 982 | But you can't! We have a terrific case. 983 | Where is your proof? 984 | Where is the evidence? 985 | Show me the smoking gun! 986 | Hold it, Your Honor! 987 | You want a smoking gun? Here is your smoking gun. 988 | What is that? 989 | It's a bee smoker! 990 | What, this? This harmless little contraption? This couldn't hurt a fly, let alone a bee. 991 | Look at what has happened to bees who have never been asked, "Smoking or non?" Is this what nature intended for us? To be forcibly addicted to smoke machines and man-made wooden slat work camps? 992 | Living out our lives as honey slaves to the white man? 993 | What are we gonna do? 994 | He's playing the species card. 995 | Ladies and gentlemen, please, free these bees! 996 | Free the bees! Free the bees! Free the bees! Free the bees! Free the bees! 997 | The court finds in favor of the bees! 998 | Vanessa, we won! 999 | I knew you could do it! High-five! 1000 | Sorry. 1001 | I'm OK! You know what this means? 1002 | All the honey will finally belong to the bees. 1003 | Now we won't have to work so hard all the time. 1004 | This is an unholy perversion of the balance of nature, Benson. 1005 | You'll regret this. 1006 | Barry, how much honey is out there? 1007 | All right. One at a time. 1008 | Barry, who are you wearing? 1009 | My sweater is Ralph Lauren, and I have no pants. 1010 | What if Montgomery's right? 1011 | What do you mean? 1012 | We've been living the bee way a long time, 27 million years. 1013 | Congratulations on your victory. What will you demand as a settlement? 1014 | First, we'll demand a complete shutdown of all bee work camps. 1015 | Then we want back the honey that was ours to begin with, every last drop. 1016 | We demand an end to the glorification of the bear as anything more than a filthy, smelly, bad-breath stink machine. 1017 | We're all aware of what they do in the woods. 1018 | Wait for my signal. Take him out. 1019 | He'll have nauseous for a few hours, then he'll be fine. 1020 | And we will no longer tolerate bee-negative nicknames... 1021 | But it's just a prance-about stage name! 1022 | ...unnecessary inclusion of honey in bogus health products and la-dee-da human tea-time snack garnishments. 1023 | Can't breathe. 1024 | Bring it in, boys! 1025 | Hold it right there! Good. 1026 | Tap it. 1027 | Mr. Buzzwell, we just passed three cups and there's gallons more coming! 1028 | I think we need to shut down! 1029 | Shut down? We've never shut down. 1030 | Shut down honey production! 1031 | Stop making honey! 1032 | Turn your key, sir! 1033 | What do we do now? 1034 | Cannonball! 1035 | We're shutting honey production! 1036 | Mission abort. 1037 | Aborting pollination and nectar detail. 1038 | Returning to base. 1039 | Adam, you wouldn't believe how much honey was out there. 1040 | Oh, yeah? 1041 | What's going on? Where is everybody? 1042 | Are they out celebrating? 1043 | They're home. 1044 | They don't know what to do. Laying out, sleeping in. 1045 | I heard your Uncle Carl was on his way to San Antonio with a cricket. 1046 | At least we got our honey back. 1047 | Sometimes I think, so what if humans liked our honey? Who wouldn't? 1048 | It's the greatest thing in the world! I was excited to be part of making it. 1049 | This was my new desk. This was my new job. I wanted to do it really well. And now... 1050 | Now I can't. 1051 | I don't understand why they're not happy. 1052 | I thought their lives would be better! 1053 | They're doing nothing. It's amazing. 1054 | Honey really changes people. 1055 | You don't have any idea what's going on, do you? 1056 | What did you want to show me? 1057 | This. 1058 | What happened here? 1059 | That is not the half of it. 1060 | Oh, no. Oh, my. 1061 | They're all wilting. 1062 | Doesn't look very good, does it? 1063 | No. 1064 | And whose fault do you think that is? 1065 | You know, I'm gonna guess bees. 1066 | Bees? 1067 | Specifically, me. 1068 | I didn't think bees not needing to make honey would affect all these things. 1069 | It's not just flowers. Fruits, vegetables, they all need bees. 1070 | That's our whole SAT test right there. 1071 | Take away produce, that affects the entire animal kingdom. 1072 | And then, of course... 1073 | The human species? 1074 | So if there's no more pollination, it could all just go south here, couldn't it? 1075 | I know this is also partly my fault. 1076 | How about a suicide pact? 1077 | How do we do it? 1078 | I'll sting you, you step on me. 1079 | That just kills you twice. 1080 | Right, right. 1081 | Listen, Barry... sorry, but I gotta get going. 1082 | I had to open my mouth and talk. 1083 | Vanessa? 1084 | Vanessa? Why are you leaving? 1085 | Where are you going? 1086 | To the final Tournament of Roses parade in Pasadena. 1087 | They've moved it to this weekend because all the flowers are dying. 1088 | It's the Last Chance I'll ever have to see it. 1089 | Vanessa, I just wanna say I'm sorry. 1090 | I never meant it to turn out like this. 1091 | I know. Me neither. 1092 | Tournament of Roses. 1093 | Roses can't do sports. 1094 | Wait a minute. Roses. Roses? 1095 | Roses! 1096 | Vanessa! 1097 | Roses?! 1098 | Barry? 1099 | Roses are flowers! 1100 | Yes, they are. 1101 | Flowers, bees, pollen! 1102 | I know. 1103 | That's why this is the last parade. 1104 | Maybe not. 1105 | Could you ask him to slow down? 1106 | Could you slow down? 1107 | Barry! 1108 | OK, I made a huge mistake. 1109 | This is a total disaster, all my fault. 1110 | Yes, it kind of is. 1111 | I've ruined the planet. I wanted to help you with the flower shop. I've made it worse. 1112 | Actually, it's completely closed down. 1113 | I thought maybe you were remodeling. 1114 | But I have another idea, and it's greater than my previous ideas combined. 1115 | I don't want to hear it! 1116 | All right, they have the roses, the roses have the pollen. 1117 | I know every bee, plant and flower bud in this park. 1118 | All we gotta do is get what they've got back here with what we've got. 1119 | Bees. 1120 | Park. 1121 | Pollen! 1122 | Flowers. 1123 | Repollination! 1124 | Across the nation! 1125 | Tournament of Roses, Pasadena, California. 1126 | They've got nothing but flowers, floats and cotton candy. 1127 | Security will be tight. 1128 | I have an idea. 1129 | Vanessa Bloome, FTD. 1130 | Official floral business. It's real. 1131 | Sorry, ma'am. Nice brooch. 1132 | Thank you. It was a gift. 1133 | Once inside, we just pick the right float. 1134 | How about The Princess and the Pea? 1135 | I could be the princess, and you could be the pea! 1136 | Yes, I got it. 1137 | Where should I sit? 1138 | What are you? 1139 | I believe I'm the pea. 1140 | The pea? 1141 | It goes under the mattresses. 1142 | Not in this fairy tale, sweetheart. 1143 | I'm getting the marshal. 1144 | You do that! This whole parade is a fiasco! 1145 | Let's see what this baby'll do. 1146 | Hey, what are you doing?! 1147 | Then all we do is blend in with traffic... without arousing suspicion. 1148 | Once at the airport, there's no stopping us. 1149 | Stop! Security. 1150 | You and your insect pack your float? 1151 | Yes. 1152 | Has it been in your possession the entire time? 1153 | Would you remove your shoes? 1154 | Remove your stinger. 1155 | It's part of me. 1156 | I know. Just having some fun. 1157 | Enjoy your flight. 1158 | Then if we're lucky, we'll have just enough pollen to do the job. 1159 | Can you believe how lucky we are? We have just enough pollen to do the job! 1160 | I think this is gonna work. 1161 | It's got to work. 1162 | Attention, passengers, this is Captain Scott. We have a bit of bad weather in New York. It looks like we'll experience a couple hours delay. 1163 | Barry, these are cut flowers with no water. They'll never make it. 1164 | I gotta get up there and talk to them. 1165 | Be careful. 1166 | Can I get help with the Sky Mall magazine? I'd like to order the talking inflatable nose and ear hair trimmer. 1167 | Captain, I'm in a real situation. 1168 | What'd you say, Hal? 1169 | Nothing. 1170 | Bee! 1171 | Don't freak out! My entire species... 1172 | What are you doing? 1173 | Wait a minute! I'm an attorney! 1174 | Who's an attorney? 1175 | Don't move. 1176 | Oh, Barry. 1177 | Good afternoon, passengers. This is your captain. Would a Miss Vanessa Bloome in 24B please report to the cockpit? And please hurry! 1178 | What happened here? 1179 | There was a DustBuster, a toupee, a life raft exploded. 1180 | One's bald, one's in a boat, they're both unconscious! 1181 | Is that another bee joke? 1182 | No! 1183 | No one's flying the plane! 1184 | This is JFK control tower, Flight 356. What's your status? 1185 | This is Vanessa Bloome. I'm a florist from New York. 1186 | Where's the pilot? 1187 | He's unconscious, and so is the copilot. 1188 | Not good. Does anyone onboard have flight experience? 1189 | As a matter of fact, there is. 1190 | Who's that? 1191 | Barry Benson. 1192 | From the honey trial?! Oh, great. 1193 | Vanessa, this is nothing more than a big metal bee. 1194 | It's got giant wings, huge engines. 1195 | I can't fly a plane. 1196 | Why not? Isn't John Travolta a pilot? 1197 | Yes. 1198 | How hard could it be? 1199 | Wait, Barry! 1200 | We're headed into some lightning. 1201 | This is Bob Bumble. We have some late-breaking news from JFK Airport, where a suspenseful scene is developing. 1202 | Barry Benson, fresh from his legal victory... 1203 | That's Barry! 1204 | ...is attempting to land a plane, loaded with people, flowers and an incapacitated flight crew. 1205 | Flowers?! 1206 | We have a storm in the area and two individuals at the controls with absolutely no flight experience. 1207 | Just a minute. There's a bee on that plane. 1208 | I'm quite familiar with Mr. Benson and his no-account compadres. 1209 | They've done enough damage. 1210 | But isn't he your only hope? 1211 | Technically, a bee shouldn't be able to fly at all. 1212 | Their wings are too small... Haven't we heard this a million times? 1213 | "The surface area of the wings and body mass make no sense." 1214 | Get this on the air! 1215 | Got it. 1216 | Stand by. 1217 | We're going live. 1218 | The way we work may be a mystery to you. Making honey takes a lot of bees doing a lot of small jobs. 1219 | But let me tell you about a small job. If you do it well, it makes a big difference. 1220 | More than we realized. To us, to everyone. 1221 | That's why I want to get bees back to working together. That's the bee way! We're not made of Jell-O. 1222 | We get behind a fellow. 1223 | Black and yellow! 1224 | Hello! 1225 | Left, right, down, hover. 1226 | Hover? 1227 | Forget hover. 1228 | This isn't so hard. 1229 | Beep-beep! Beep-beep! 1230 | Barry, what happened?! 1231 | Wait, I think we were on autopilot the whole time. 1232 | That may have been helping me. 1233 | And now we're not! 1234 | So it turns out I cannot fly a plane. 1235 | All of you, let's get behind this fellow! Move it out! 1236 | Move out! 1237 | Our only chance is if I do what I'd do, you copy me with the wings of the plane! 1238 | Don't have to yell. 1239 | I'm not yelling! We're in a lot of trouble. 1240 | It's very hard to concentrate with that panicky tone in your voice! 1241 | It's not a tone. I'm panicking! 1242 | I can't do this! 1243 | Vanessa, pull yourself together. You have to snap out of it! 1244 | You snap out of it. 1245 | You snap out of it. 1246 | You snap out of it! 1247 | You snap out of it! 1248 | You snap out of it! 1249 | You snap out of it! 1250 | You snap out of it! 1251 | You snap out of it! 1252 | Hold it! 1253 | Why? Come on, it's my turn. 1254 | How is the plane flying? 1255 | I don't know. 1256 | Hello? 1257 | Benson, got any flowers for a happy occasion in there? 1258 | The Pollen Jocks! 1259 | They do get behind a fellow. 1260 | Black and yellow. 1261 | Hello. 1262 | All right, let's drop this tin can on the blacktop. 1263 | Where? I can't see anything. Can you? 1264 | No, nothing. It's all cloudy. 1265 | Come on. You got to think bee, Barry. 1266 | Thinking bee. 1267 | Thinking bee. 1268 | Thinking bee! 1269 | Thinking bee! Thinking bee! 1270 | Wait a minute. I think I'm feeling something. 1271 | What? 1272 | I don't know. It's strong, pulling me. 1273 | Like a 27-million-year-old instinct. 1274 | Bring the nose down. 1275 | Thinking bee! 1276 | Thinking bee! Thinking bee! 1277 | What in the world is on the tarmac? 1278 | Get some lights on that! 1279 | Thinking bee! 1280 | Thinking bee! Thinking bee! 1281 | Vanessa, aim for the flower. 1282 | OK. 1283 | Cut the engines. We're going in on bee power. Ready, boys? 1284 | Affirmative! 1285 | Good. Good. Easy, now. That's it. 1286 | Land on that flower! 1287 | Ready? Full reverse! 1288 | Spin it around! 1289 | Not that flower! The other one! 1290 | Which one? 1291 | That flower. 1292 | I'm aiming at the flower! 1293 | That's a fat guy in a flowered shirt. 1294 | I mean the giant pulsating flower made of millions of bees! 1295 | Pull forward. Nose down. Tail up. 1296 | Rotate around it. 1297 | This is insane, Barry! 1298 | This's the only way I know how to fly. 1299 | Am I koo-koo-kachoo, or is this plane flying in an insect-like pattern? 1300 | Get your nose in there. Don't be afraid. Smell it. Full reverse! 1301 | Just drop it. Be a part of it. 1302 | Aim for the center! 1303 | Now drop it in! Drop it in, woman! 1304 | Come on, already. 1305 | Barry, we did it! You taught me how to fly! 1306 | Yes. No high-five! 1307 | Right. 1308 | Barry, it worked! 1309 | Did you see the giant flower? 1310 | What giant flower? Where? Of course 1311 | I saw the flower! That was genius! 1312 | Thank you. 1313 | But we're not done yet. 1314 | Listen, everyone! 1315 | This runway is covered with the last pollen from the last flowers available anywhere on Earth. 1316 | That means this is our Last Chance. We're the only ones who make honey, pollinate flowers and dress like this. 1317 | If we're gonna survive as a species, this is our moment! What do you say? 1318 | Are we going to be bees, or just Museum of Natural History keychains? 1319 | We're bees! 1320 | Keychain! 1321 | Then follow me! Except Keychain. 1322 | Hold on, Barry. Here. You've earned this. 1323 | Yeah! 1324 | I'm a Pollen Jock! And it's a perfect fit. All I gotta do are the sleeves. 1325 | Oh, yeah. 1326 | That's our Barry. 1327 | Mom! The bees are back! 1328 | If anybody needs to make a call, now's the time. I got a feeling we'll be working late tonight! 1329 | Here's your change. Have a great afternoon! Can I help who's next? 1330 | Would you like some honey with that? 1331 | It is bee-approved. Don't forget these. 1332 | Milk, cream, cheese, it's all me. And I don't see a nickel! 1333 | Sometimes I just feel like a piece of meat! 1334 | I had no idea. 1335 | Barry, I'm sorry. 1336 | Have you got a moment? 1337 | Would you excuse me? 1338 | My mosquito associate will help you. 1339 | Sorry I'm late. 1340 | He's a lawyer too? 1341 | I was already a blood-sucking parasite. All I needed was a briefcase. 1342 | Have a great afternoon! 1343 | Barry, I just got this huge tulip order, and I can't get them anywhere. 1344 | No problem, Vannie. Just leave it to me. 1345 | You're a lifesaver, Barry. Can I help who's next? 1346 | All right, scramble, jocks! It's time to fly. 1347 | Thank you, Barry! 1348 | That bee is living my life! 1349 | Let it go, Kenny. 1350 | When will this nightmare end?! 1351 | Let it all go. 1352 | Beautiful day to fly. 1353 | Sure is. 1354 | Between you and me, 1355 | I was dying to get out of that office. 1356 | You have got to start thinking bee, my friend. 1357 | Thinking bee! 1358 | Me? 1359 | Hold it. Let's just stop for a second. Hold it. 1360 | I'm sorry. I'm sorry, everyone. Can we stop here? 1361 | I'm not making a major life decision during a production number! 1362 | All right. Take ten, everybody. Wrap it up, guys. 1363 | I had virtually no rehearsal for that. 1364 | -------------------------------------------------------------------------------- /tests/checksums.rs: -------------------------------------------------------------------------------- 1 | mod img_commons; 2 | 3 | use std::fs; 4 | use std::path::PathBuf; 5 | use std::process::Command; 6 | 7 | #[test] 8 | fn verify_checksums() { 9 | let mut fails: Vec = Vec::new(); 10 | let mut total_checks = 0_usize; 11 | 12 | for file in fs::read_dir(img_commons::get_imgs_path()) 13 | .unwrap() 14 | .map(|f| f.unwrap().path()) 15 | .filter(|p| p.extension().unwrap() == "check") 16 | { 17 | total_checks += 1; 18 | 19 | if !sha256sum(&file).success() { 20 | fails.push(file) 21 | } 22 | } 23 | 24 | if !fails.is_empty() { 25 | panic!( 26 | "A total of {} out of {} checksum checks failed:\n{:?}", 27 | fails.len(), 28 | total_checks, 29 | fails 30 | ) 31 | } 32 | } 33 | 34 | fn sha256sum(file: &std::path::PathBuf) -> std::process::ExitStatus { 35 | Command::new("sha256sum") 36 | .args(["--status", "-c", file.as_os_str().to_str().unwrap()]) 37 | .status() 38 | // if the sha256sum utility can't be run (not installed), we assume that it returned a success 39 | .unwrap_or_default() 40 | } 41 | -------------------------------------------------------------------------------- /tests/img_commons/mod.rs: -------------------------------------------------------------------------------- 1 | use std::env; 2 | use std::path::PathBuf; 3 | 4 | pub const IMGS_SUBDIR: &str = "imgs/"; 5 | 6 | /// get the pathbuf to the subdir where the /img files are located 7 | pub fn get_imgs_path() -> PathBuf { 8 | let mut path_buf = env::current_dir().unwrap(); 9 | path_buf.push(IMGS_SUBDIR); 10 | path_buf 11 | } 12 | 13 | #[test] 14 | fn img_dir_exists() { 15 | assert!(get_imgs_path().is_dir()); 16 | } 17 | --------------------------------------------------------------------------------