Skip to content

Commit ea53bc9

Browse files
authored
Merge pull request #67 from jbeaurivage/fix-close-file-dir
2 parents 778ed6c + 0a51284 commit ea53bc9

File tree

6 files changed

+201
-77
lines changed

6 files changed

+201
-77
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ version = "0.5.0"
1414
byteorder = {version = "1", default-features = false}
1515
defmt = {version = "0.3", optional = true}
1616
embedded-hal = "0.2.3"
17+
heapless = "0.7"
1718
log = {version = "0.4", default-features = false, optional = true}
1819

1920
[dev-dependencies]

src/filesystem/directory.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use core::convert::TryFrom;
22

33
use crate::blockdevice::BlockIdx;
44
use crate::fat::{FatType, OnDiskDirEntry};
5-
use crate::filesystem::{Attributes, Cluster, ShortFileName, Timestamp};
5+
use crate::filesystem::{Attributes, Cluster, SearchId, ShortFileName, Timestamp};
66

77
/// Represents a directory entry, which tells you about
88
/// other files and directories.
@@ -33,6 +33,8 @@ pub struct DirEntry {
3333
pub struct Directory {
3434
/// The starting point of the directory listing.
3535
pub(crate) cluster: Cluster,
36+
/// Search ID for this directory.
37+
pub(crate) search_id: SearchId,
3638
}
3739

3840
impl DirEntry {

src/filesystem/files.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::filesystem::{Cluster, DirEntry};
1+
use crate::filesystem::{Cluster, DirEntry, SearchId};
22

33
/// Represents an open file on disk.
44
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
@@ -16,6 +16,8 @@ pub struct File {
1616
pub(crate) mode: Mode,
1717
/// DirEntry of this file
1818
pub(crate) entry: DirEntry,
19+
/// Search ID for this file
20+
pub(crate) search_id: SearchId,
1921
}
2022

2123
/// Errors related to file operations

src/filesystem/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@ mod cluster;
1111
mod directory;
1212
mod filename;
1313
mod files;
14+
mod search_id;
1415
mod timestamp;
1516

1617
pub use self::attributes::Attributes;
1718
pub use self::cluster::Cluster;
1819
pub use self::directory::{DirEntry, Directory};
1920
pub use self::filename::{FilenameError, ShortFileName};
2021
pub use self::files::{File, FileError, Mode};
22+
pub use self::search_id::{IdGenerator, SearchId};
2123
pub use self::timestamp::{TimeSource, Timestamp};

src/filesystem/search_id.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use core::num::Wrapping;
2+
3+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
4+
#[cfg_attr(feature = "defmt-log", derive(defmt::Format))]
5+
/// Unique ID used to search for files and directories in the open File/Directory lists
6+
pub struct SearchId(pub(crate) u32);
7+
8+
/// ID generator intented to be used in a static context.
9+
///
10+
/// This object will always return a different ID.
11+
pub struct IdGenerator {
12+
next_id: Wrapping<u32>,
13+
}
14+
15+
impl IdGenerator {
16+
/// Create a new [`IdGenerator`].
17+
pub const fn new() -> Self {
18+
Self {
19+
next_id: Wrapping(0),
20+
}
21+
}
22+
23+
/// Generate a new, unique [`SearchId`].
24+
pub fn get(&mut self) -> SearchId {
25+
let id = self.next_id;
26+
self.next_id += 1;
27+
SearchId(id.0)
28+
}
29+
}

0 commit comments

Comments
 (0)