-
Notifications
You must be signed in to change notification settings - Fork 76
/
open_files.rs
145 lines (118 loc) · 4.61 KB
/
open_files.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! File opening related tests
use embedded_sdmmc::{Error, Mode, VolumeIdx, VolumeManager};
mod utils;
#[test]
fn open_files() {
let time_source = utils::make_time_source();
let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap();
let volume_mgr: VolumeManager<utils::RamDisk<Vec<u8>>, utils::TestTimeSource, 4, 2, 1> =
VolumeManager::new_with_limits(disk, time_source, 0xAA00_0000);
let volume = volume_mgr
.open_raw_volume(VolumeIdx(0))
.expect("open volume");
let root_dir = volume_mgr.open_root_dir(volume).expect("open root dir");
// Open with string
let f = volume_mgr
.open_file_in_dir(root_dir, "README.TXT", Mode::ReadWriteTruncate)
.expect("open file");
assert!(matches!(
volume_mgr.open_file_in_dir(root_dir, "README.TXT", Mode::ReadOnly),
Err(Error::FileAlreadyOpen)
));
volume_mgr.close_file(f).expect("close file");
// Open with SFN
let dir_entry = volume_mgr
.find_directory_entry(root_dir, "README.TXT")
.expect("find file");
let f = volume_mgr
.open_file_in_dir(root_dir, &dir_entry.name, Mode::ReadWriteCreateOrAppend)
.expect("open file with dir entry");
assert!(matches!(
volume_mgr.open_file_in_dir(root_dir, &dir_entry.name, Mode::ReadOnly),
Err(Error::FileAlreadyOpen)
));
// Can still spot duplicates even if name given the other way around
assert!(matches!(
volume_mgr.open_file_in_dir(root_dir, "README.TXT", Mode::ReadOnly),
Err(Error::FileAlreadyOpen)
));
let f2 = volume_mgr
.open_file_in_dir(root_dir, "64MB.DAT", Mode::ReadWriteTruncate)
.expect("open file");
// Hit file limit
assert!(matches!(
volume_mgr.open_file_in_dir(root_dir, "EMPTY.DAT", Mode::ReadOnly),
Err(Error::TooManyOpenFiles)
));
volume_mgr.close_file(f).expect("close file");
volume_mgr.close_file(f2).expect("close file");
// File not found
assert!(matches!(
volume_mgr.open_file_in_dir(root_dir, "README.TXS", Mode::ReadOnly),
Err(Error::NotFound)
));
// Create a new file
let f3 = volume_mgr
.open_file_in_dir(root_dir, "NEWFILE.DAT", Mode::ReadWriteCreate)
.expect("open file");
volume_mgr.write(f3, b"12345").expect("write to file");
volume_mgr.write(f3, b"67890").expect("write to file");
volume_mgr.close_file(f3).expect("close file");
// Open our new file
let f3 = volume_mgr
.open_file_in_dir(root_dir, "NEWFILE.DAT", Mode::ReadOnly)
.expect("open file");
// Should have 10 bytes in it
assert_eq!(volume_mgr.file_length(f3).expect("file length"), 10);
volume_mgr.close_file(f3).expect("close file");
volume_mgr.close_dir(root_dir).expect("close dir");
volume_mgr.close_volume(volume).expect("close volume");
}
#[test]
fn open_non_raw() {
let time_source = utils::make_time_source();
let disk = utils::make_block_device(utils::DISK_SOURCE).unwrap();
let volume_mgr: VolumeManager<utils::RamDisk<Vec<u8>>, utils::TestTimeSource, 4, 2, 1> =
VolumeManager::new_with_limits(disk, time_source, 0xAA00_0000);
let volume = volume_mgr.open_volume(VolumeIdx(0)).expect("open volume");
let root_dir = volume.open_root_dir().expect("open root dir");
let f = root_dir
.open_file_in_dir("README.TXT", Mode::ReadOnly)
.expect("open file");
let mut buffer = [0u8; 512];
let len = f.read(&mut buffer).expect("read from file");
// See directory listing in utils.rs, to see that README.TXT is 258 bytes long
assert_eq!(len, 258);
assert_eq!(f.length(), 258);
f.seek_from_current(0).unwrap();
assert!(f.is_eof());
assert_eq!(f.offset(), 258);
assert!(matches!(f.seek_from_current(1), Err(Error::InvalidOffset)));
f.seek_from_current(-258).unwrap();
assert!(!f.is_eof());
assert_eq!(f.offset(), 0);
f.seek_from_current(10).unwrap();
assert!(!f.is_eof());
assert_eq!(f.offset(), 10);
f.seek_from_end(0).unwrap();
assert!(f.is_eof());
assert_eq!(f.offset(), 258);
assert!(matches!(
f.seek_from_current(-259),
Err(Error::InvalidOffset)
));
f.seek_from_start(25).unwrap();
assert!(!f.is_eof());
assert_eq!(f.offset(), 25);
drop(f);
let Err(Error::FileAlreadyExists) =
root_dir.open_file_in_dir("README.TXT", Mode::ReadWriteCreate)
else {
panic!("Expected to file to exist");
};
}
// ****************************************************************************
//
// End Of File
//
// ****************************************************************************