1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
use alloc::collections::BTreeMap;
use alloc::vec;
use core::convert::TryInto;
use core::mem;
use volatile::ReadOnly;
use super::{DiskInodeType, EasyFileSystem};
use alloc::string::String;
use crate::block_cache::{Cache, CacheManager};
use crate::dir_iter::*;
use crate::layout::{
FATDirEnt, FATDiskInodeType, FATLongDirEnt, FATShortDirEnt, LONG_DIR_ENT_NAME_CAPACITY,
};
use crate::DataBlock;
use alloc::sync::Arc;
use alloc::vec::Vec;
use spin::{Mutex, MutexGuard};
pub struct FileContent<T: CacheManager> {
pub size: u32,
pub clus_list: Vec<u32>,
pub file_cache_mgr: T,
pub hint: u32,
}
pub enum OpenTabCmd<T: CacheManager, F: CacheManager> {
InsertFile(Arc<Inode<T, F>>),
GetFileByInode(u64),
DropFileByInode(u64),
ChInode(u64, u64),
}
#[allow(unused)]
impl<T: CacheManager, F: CacheManager> OpenTabCmd<T, F> {
fn is_inode(&self) -> bool {
matches!(self, Self::GetFileByInode(..))
}
fn is_file(&self) -> bool {
matches!(self, Self::InsertFile(..))
}
fn as_file(&self) -> Option<&Inode<T, F>> {
if let Self::InsertFile(v) = self {
Some(v)
} else {
None
}
}
fn as_inode(&self) -> Option<&u64> {
if let Self::GetFileByInode(v) = self {
Some(v)
} else {
None
}
}
}
pub struct InodeTime {
create_time: u64,
access_time: u64,
modify_time: u64,
}
impl InodeTime {
pub fn set_create_time(&mut self, create_time: u64) {
self.create_time = create_time;
}
pub fn create_time(&self) -> &u64 {
&self.create_time
}
pub fn set_access_time(&mut self, access_time: u64) {
self.access_time = access_time;
}
pub fn access_time(&self) -> &u64 {
&self.access_time
}
pub fn set_modify_time(&mut self, modify_time: u64) {
self.modify_time = modify_time;
}
pub fn modify_time(&self) -> &u64 {
&self.modify_time
}
}
pub struct Inode<T: CacheManager, F: CacheManager> {
pub file_content: Mutex<FileContent<T>>,
pub file_type: ReadOnly<DiskInodeType>,
pub parent_dir: Option<(Arc<Self>, u32)>,
pub fs: Arc<EasyFileSystem<T, F>>,
pub time: Mutex<InodeTime>,
}
impl<T: CacheManager, F: CacheManager> Drop for Inode<T, F> {
fn drop(&mut self) {
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn new(
fst_clus: u32,
file_type: DiskInodeType,
size: Option<u32>,
parent_dir: Option<(Arc<Self>, u32)>,
fs: Arc<EasyFileSystem<T, F>>,
) -> Arc<Self> {
let file_cache_mgr = T::new();
let clus_list = match fst_clus {
0 => Vec::new(),
_ => fs.fat.get_all_clus_num(fst_clus, &fs.block_device),
};
let size = size.unwrap_or_else(|| clus_list.len() as u32 * fs.clus_size());
let hint = 0;
let file_content = Mutex::new(FileContent {
size,
clus_list,
file_cache_mgr,
hint,
});
let time = InodeTime {
create_time: 0,
access_time: 0,
modify_time: 0,
};
Arc::new(Inode {
file_content,
file_type: ReadOnly::new(file_type),
parent_dir,
fs,
time: Mutex::new(time),
})
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn get_first_clus(&self) -> Option<u32> {
let lock = self.file_content.lock();
let clus_list = &lock.clus_list;
if !clus_list.is_empty() {
Some(clus_list[0])
} else {
None
}
}
#[inline(always)]
pub fn get_inode_num(&self) -> Option<u32> {
self.get_first_clus()
.map(|clus| self.fs.first_sector_of_cluster(clus))
}
pub fn get_neighboring_sec(&self, inner_cache_id: usize) -> Vec<usize> {
Vec::new()
}
#[inline(always)]
pub fn is_dir(&self) -> bool {
self.file_type.read() == DiskInodeType::Directory
}
#[inline(always)]
pub fn is_file(&self) -> bool {
self.file_type.read() == DiskInodeType::File
}
pub fn get_file_size(&self) -> u32 {
self.file_content.lock().size
}
pub fn get_file_clus(&self) -> u32 {
self.total_clus(self.get_file_size())
}
fn total_clus(&self, size: u32) -> u32 {
size.div_ceil(self.fs.clus_size())
}
#[inline(always)]
fn get_block_id(&self, lock: &MutexGuard<FileContent<T>>, blk: u32) -> Option<u32> {
let idx = blk as usize / self.fs.sec_per_clus as usize;
let clus_list = &lock.clus_list;
if idx >= clus_list.len() {
return None;
}
let base = self.fs.first_sector_of_cluster(clus_list[idx]);
let offset = blk % self.fs.sec_per_clus as u32;
Some(base + offset)
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
fn alloc_clus(&self, lock: &mut MutexGuard<FileContent<T>>, alloc_num: usize) {
let clus_list = &mut lock.clus_list;
let mut new_clus_list = self.fs.fat.alloc_mult(
&self.fs.block_device,
alloc_num,
clus_list.last().map(|clus| *clus),
);
clus_list.append(&mut new_clus_list);
}
fn dealloc_clus(&self, lock: &mut MutexGuard<FileContent<T>>, dealloc_num: usize) {
let clus_list = &mut lock.clus_list;
let dealloc_num = dealloc_num.min(clus_list.len());
for _ in 0..dealloc_num {
self.fs
.fat
.dealloc(&self.fs.block_device, clus_list.pop().unwrap())
}
}
pub fn modify_size(&self, lock: &mut MutexGuard<FileContent<T>>, diff: isize) {
let mut dir_ent = FATDirEnt::empty();
let mut may_par_lock: Option<MutexGuard<FileContent<T>>> = None;
if let Some((par_inode, offset)) = &self.parent_dir {
let mut lock = par_inode.file_content.lock();
par_inode.read_at_block_cache(&mut lock, *offset as usize, dir_ent.as_bytes_mut());
may_par_lock = Some(lock);
}
if diff.saturating_add(lock.size as isize) <= 0 {
return;
}
let old_size = lock.size;
let new_size = (lock.size as isize + diff) as u32;
let old_clus_num = self.total_clus(old_size) as usize;
let new_clus_num = self.total_clus(new_size) as usize;
if diff > 0 {
self.alloc_clus(lock, new_clus_num - old_clus_num);
lock.size = new_size;
if old_size == 0 {
dir_ent.set_fst_clus(lock.clus_list[0]);
}
} else {
lock.size = new_size;
self.dealloc_clus(lock, old_clus_num - new_clus_num);
if new_size == 0 {
dir_ent.set_fst_clus(0);
}
}
dir_ent.set_size(new_size);
if let Some((par_inode, offset)) = &self.parent_dir {
par_inode.write_at_block_cache(
&mut may_par_lock.unwrap(),
*offset as usize,
dir_ent.as_bytes_mut(),
);
}
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn open_tab(file: OpenTabCmd<T, F>) -> Option<Arc<Self>> {
static mut ORG_LI: usize = 0;
static mut V: Vec<(u64, u64)> = Vec::new();
unsafe {
if ORG_LI == 0 {
ORG_LI = &mut (Mutex::new(BTreeMap::new())) as *mut Mutex<BTreeMap<u64, Arc<Self>>>
as usize;
}
macro_rules! tree {
() => {
*(ORG_LI as *mut Mutex<BTreeMap<u64, Arc<Self>>>)
};
}
match file {
OpenTabCmd::InsertFile(f) => {
let ino = f.get_inode_num();
let arc = tree!().lock().insert(ino.unwrap_or(0) as u64, f.clone());
return None;
}
OpenTabCmd::GetFileByInode(i) => {
let lock = tree!().lock();
lock.get(&i).map(|i| i.clone()).or_else(|| {
if let Some((_, next)) = V.iter().find(|(pre, _)| *pre == i) {
lock.get(next).map(|i| i.clone())
} else {
None
}
})
}
OpenTabCmd::DropFileByInode(i) => {
let mut lock = tree!().lock();
if let Some((_, next)) = V.iter().find(|&(pre, _)| *pre == i) {
lock.remove(next);
}
lock.remove(&i)
}
OpenTabCmd::ChInode(pre, next) => {
let mut lock = tree!().lock();
V.push((pre, next));
let arc = lock.remove(&pre).unwrap();
lock.insert(next, arc);
return None;
}
}
}
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn read_at_block_cache(
&self,
lock: &mut MutexGuard<FileContent<T>>,
offset: usize,
buf: &mut [u8],
) -> usize {
let mut start = offset;
let size = lock.size as usize;
let end = (offset + buf.len()).min(size);
if start >= end {
return 0;
}
let mut start_cache = start / T::CACHE_SZ;
let mut read_size = 0;
loop {
let mut end_current_block = (start / T::CACHE_SZ + 1) * T::CACHE_SZ;
end_current_block = end_current_block.min(end);
let block_read_size = end_current_block - start;
let dst = &mut buf[read_size..read_size + block_read_size];
let block_id = self.get_block_id(lock, start_cache as u32).unwrap() as usize;
lock.file_cache_mgr
.get_block_cache(
block_id,
start_cache,
|| -> Vec<usize> { self.get_neighboring_sec(start_cache) },
Arc::clone(&self.fs.block_device),
)
.lock()
.read(0, |data_block: &DataBlock| {
let src =
&data_block[start % T::CACHE_SZ..start % T::CACHE_SZ + block_read_size];
dst.copy_from_slice(src);
});
read_size += block_read_size;
if end_current_block == end {
break;
}
start_cache += 1;
start = end_current_block;
}
read_size
}
pub fn write_at_block_cache(
&self,
lock: &mut MutexGuard<FileContent<T>>,
offset: usize,
buf: &[u8],
) -> usize {
let mut start = offset;
let old_size = lock.size as usize;
let diff_len = buf.len() as isize + offset as isize - old_size as isize;
log::info!("[wr_at_blk_cache] diff{},size{}", diff_len, old_size);
if diff_len > 0 as isize {
log::info!("[wr_at_blk_cache] trying to modify size...");
log::info!("[wr_at_blk_cache] diff{},size{}", diff_len, old_size);
self.modify_size(lock, diff_len);
log::info!("[wr_at_blk_cache] \"file.size\":{}", lock.size);
}
let end = (offset + buf.len()).min(lock.size as usize);
assert!(start <= end);
let mut start_cache = start / T::CACHE_SZ;
let mut write_size = 0;
log::info!(
"[wr_at_blk_cache] st,end,st_ch,wr_sz, buf.len:{:?}",
(start, end, start_cache, write_size, buf.len())
);
loop {
let mut end_current_block = (start / T::CACHE_SZ + 1) * T::CACHE_SZ;
end_current_block = end_current_block.min(end);
let block_write_size = end_current_block - start;
let block_id = self.get_block_id(lock, start_cache as u32).unwrap() as usize;
lock.file_cache_mgr
.get_block_cache(
block_id,
start_cache,
|| -> Vec<usize> { self.get_neighboring_sec(start_cache) },
Arc::clone(&self.fs.block_device),
)
.lock()
.modify(0, |data_block: &mut DataBlock| {
let src = &buf[write_size..write_size + block_write_size];
let dst = &mut data_block
[start % T::CACHE_SZ..start % T::CACHE_SZ + block_write_size];
dst.copy_from_slice(src);
});
write_size += block_write_size;
log::info!(
"[wr_at_blk_cache] end_cur_blk blk_wr_sz,blk_id,wr_sz:{:?}",
(end_current_block, block_write_size, block_id, write_size)
);
if end_current_block == end {
break;
}
start_cache += 1;
start = end_current_block;
}
write_size
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
fn dir_iter<'a>(
&'a self,
lock: MutexGuard<'a, FileContent<T>>,
offset: Option<u32>,
mode: DirIterMode,
forward: bool,
) -> DirIter<'a, T, F> {
if self.file_type.read() != DiskInodeType::Directory {
panic!("this isn't a directory")
}
let inode = self;
DirIter {
lock,
offset,
mode,
forward,
inode,
}
}
fn expand_dir_size(&self, lock: &mut MutexGuard<FileContent<T>>) -> core::fmt::Result {
let size = self.fs.clus_size();
self.modify_size(lock, size as isize);
let buf = vec![0u8; size as usize];
self.write_at_block_cache(lock, (lock.size - size) as usize, buf.as_ref());
Ok(())
}
fn alloc_dir_ent<'a>(
parent_dir: &'a Arc<Self>,
lock: MutexGuard<'a, FileContent<T>>,
alloc_num: usize,
) -> Result<(u32, MutexGuard<'a, FileContent<T>>), ()> {
log::info!("[alloc_dir_ent]alloc no:{:?}", alloc_num);
let offset = lock.hint;
let mut iter = parent_dir.dir_iter(lock, None, DirIterMode::Enum, FORWARD);
iter.set_iter_offset(offset);
let mut found_free_dir_ent = 0;
loop {
let dir_ent = iter.next();
if dir_ent.is_none() {
if parent_dir.expand_dir_size(&mut iter.lock).is_err() {
return Err(());
}
continue;
}
let dir_ent = dir_ent.unwrap();
if dir_ent.unused() {
found_free_dir_ent += 1;
log::info!("[alloc_dir_ent]{:?}", iter.get_offset());
if found_free_dir_ent >= alloc_num {
let offset = iter.get_offset().unwrap();
let lock = iter.lock;
return Ok((offset, lock));
}
} else {
log::info!("[alloc_dir_ent]NO! That was wrong.");
found_free_dir_ent = 0;
}
}
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
fn delete_self_dir_ent(&self) {
let (inode, offset) = self.parent_dir.as_ref().unwrap();
let lock = inode.file_content.lock();
let mut iter = inode.dir_iter(lock, Some(*offset), DirIterMode::UsedIter, BACKWARD);
iter.write_to_current_ent(&FATDirEnt::unused_not_last_entry());
{
let dir_ent = iter.next();
if dir_ent.is_none() {
return;
}
let dir_ent = dir_ent.unwrap();
if !dir_ent.is_long() {
return;
}
}
loop {
let dir_ent = iter.current_clone();
if dir_ent.is_none() {
panic!("illegal long dir_ent");
}
let dir_ent = dir_ent.unwrap();
if !dir_ent.is_long() {
panic!("illegal long dir_ent");
}
iter.write_to_current_ent(&FATDirEnt::unused_not_last_entry());
iter.next();
if dir_ent.is_last_long_dir_ent() {
return;
}
}
}
pub fn delete_from_disk(trash: Arc<Self>) -> Result<(), ()> {
if trash.is_dir() {
let v = trash.ls().unwrap();
if v.len() > 2 {
return Err(());
}
for (name, _) in v {
if ![".", ".."].contains(&name.as_str()) {
return Err(());
}
}
}
let mut lock = trash.file_content.lock();
lock.size = 0;
let clus_list = mem::take(&mut lock.clus_list);
trash.fs.fat.mult_dealloc(&trash.fs.block_device, clus_list);
trash.delete_self_dir_ent();
return Ok(());
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn create(
parent_dir: &Arc<Self>,
name: String,
file_type: DiskInodeType,
) -> Result<Arc<Inode<T, F>>, ()> {
if parent_dir.is_file()
|| name.len() >= 256
|| parent_dir
.ls()
.unwrap()
.iter()
.find(|(existed_name, _)| existed_name.to_uppercase() == name.to_uppercase())
.is_some()
{
Err(())
} else {
let mut short_name_slice: [u8; 11] = [' ' as u8; 11];
if Self::gen_short_name_slice(&parent_dir, &name, &mut short_name_slice).is_err() {
return Err(());
}
let lock = parent_dir.file_content.lock();
let long_ent_num = name.len().div_ceil(LONG_DIR_ENT_NAME_CAPACITY);
let short_ent_num = 1;
let short_ent_offset =
Self::alloc_dir_ent(&parent_dir, lock, long_ent_num + short_ent_num);
if short_ent_offset.is_err() {
return Err(());
}
let (short_ent_offset, lock) = short_ent_offset.unwrap();
let fst_clus = if file_type == DiskInodeType::Directory {
let fst_clus = parent_dir
.fs
.fat
.alloc_one(&parent_dir.fs.block_device, None);
if fst_clus.is_none() {
return Err(());
}
fst_clus.unwrap()
} else {
0
};
log::error!("short_name_slice: {:?}", short_name_slice);
let short_ent = FATShortDirEnt::from_name(short_name_slice, fst_clus, file_type);
let mut long_ents = Vec::<FATLongDirEnt>::new();
for i in 1..=long_ent_num {
long_ents.push(FATLongDirEnt::from_name_slice(
i == long_ent_num,
i,
Self::get_long_name_slice(&name, i),
))
}
Self::write_back_dir_ent(&parent_dir, short_ent_offset, lock, short_ent, long_ents);
let current_dir = Inode::from_ent(&parent_dir, &short_ent, short_ent_offset);
if file_type == DiskInodeType::Directory {
let lock = current_dir.file_content.lock();
Self::fill_empty_dir(&parent_dir, ¤t_dir, lock, fst_clus);
}
Ok(current_dir)
}
}
fn get_long_name_slice(
name: &String,
long_ent_num: usize,
) -> [u16; LONG_DIR_ENT_NAME_CAPACITY] {
let mut v: Vec<u16> = name.encode_utf16().collect();
assert!(long_ent_num >= 1);
let long_ent_num = long_ent_num - 1;
assert!(long_ent_num * LONG_DIR_ENT_NAME_CAPACITY < v.len());
while v.len() < (long_ent_num + 1) * LONG_DIR_ENT_NAME_CAPACITY {
v.push(0);
}
let start = long_ent_num * LONG_DIR_ENT_NAME_CAPACITY;
let end = (long_ent_num + 1) * LONG_DIR_ENT_NAME_CAPACITY;
v[start..end].try_into().expect("should be able to cast")
}
fn gen_short_name_slice(
parent_dir: &Arc<Self>,
name: &String,
short_name_slice: &mut [u8; 11],
) -> Result<(), ()> {
let short_name = FATDirEnt::gen_short_name_prefix(name.clone());
if short_name.len() == 0 || short_name.find(' ').unwrap_or(8) == 0 {
return Err(());
}
short_name_slice.copy_from_slice(&short_name.as_bytes()[0..11]);
let lock = parent_dir.file_content.lock();
let iter = parent_dir.dir_iter(lock, None, DirIterMode::ShortIter, FORWARD);
FATDirEnt::gen_short_name_numtail(iter.collect(), short_name_slice);
Ok(())
}
pub fn from_ent(parent_dir: &Arc<Self>, ent: &FATShortDirEnt, offset: u32) -> Arc<Self> {
Self::new(
ent.get_first_clus(),
if ent.is_dir() {
DiskInodeType::Directory
} else {
DiskInodeType::File
},
if ent.is_file() {
Some(ent.file_size)
} else {
None
},
Some((parent_dir.clone(), offset)),
parent_dir.fs.clone(),
)
}
fn write_back_dir_ent(
parent_dir: &Arc<Self>,
short_ent_offset: u32,
lock: MutexGuard<FileContent<T>>,
short_ent: FATShortDirEnt,
long_ents: Vec<FATLongDirEnt>,
) {
let mut iter =
parent_dir.dir_iter(lock, Some(short_ent_offset), DirIterMode::Enum, BACKWARD);
iter.write_to_current_ent(&FATDirEnt {
short_entry: short_ent,
});
log::info!("[wr_back_dir_ent]{:?}", iter.current_clone());
for long_ent in long_ents {
iter.next();
let n = iter.current_clone();
log::info!("[wr_back_dir_ent]{:?}", iter.get_offset());
assert!(n.is_some() && n.unwrap().unused());
iter.write_to_current_ent(&FATDirEnt {
long_entry: long_ent,
});
}
}
fn fill_empty_dir(
parent_dir: &Arc<Inode<T, F>>,
current_dir: &Arc<Inode<T, F>>,
mut current_lock: MutexGuard<FileContent<T>>,
fst_clus: u32,
) {
let buf = vec![0; current_lock.size as usize];
current_dir.write_at_block_cache(&mut current_lock, 0, buf.as_ref());
let mut iter = current_dir.dir_iter(current_lock, None, DirIterMode::Enum, FORWARD);
let mut short_name: [u8; 11] = [' ' as u8; 11];
iter.next();
short_name[0] = '.' as u8;
iter.write_to_current_ent(&FATDirEnt {
short_entry: FATShortDirEnt::from_name(
short_name,
fst_clus as u32,
DiskInodeType::Directory,
),
});
iter.next();
short_name[1] = '.' as u8;
iter.write_to_current_ent(&FATDirEnt {
short_entry: FATShortDirEnt::from_name(
short_name,
parent_dir.get_file_clus(),
DiskInodeType::Directory,
),
});
iter.next();
iter.write_to_current_ent(&FATDirEnt::unused_and_last_entry());
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
#[inline(always)]
pub fn ls(&self) -> Result<Vec<(String, FATShortDirEnt)>, ()> {
if !self.is_dir() {
return Err(());
}
let lock = self.file_content.lock();
Ok(self
.dir_iter(lock, None, DirIterMode::UsedIter, FORWARD)
.walk()
.collect())
}
pub fn find_local(
&self,
req_name: String,
) -> Result<Option<(String, FATShortDirEnt, u32)>, ()> {
if !self.is_dir() {
return Err(());
}
let lock = self.file_content.lock();
let mut walker = self
.dir_iter(lock, None, DirIterMode::UsedIter, FORWARD)
.walk();
match walker.find(|(name, _)| name.as_str() == req_name.as_str()) {
Some((name, short_ent)) => {
log::trace!("[easy-fs: find_local] Query name: {} found", req_name);
Ok(Some((name, short_ent, walker.iter.get_offset().unwrap())))
}
None => {
log::trace!("[easy-fs: find_local] Query name: {} not found", req_name);
Ok(None)
}
}
}
}
impl<T: CacheManager, F: CacheManager> Inode<T, F> {
pub fn stat(&self) -> (i64, i64, i64, i64, u64) {
let time = self.time.lock();
(
self.get_file_size() as i64,
time.access_time as i64,
time.modify_time as i64,
time.create_time as i64,
self.get_inode_num().unwrap_or(0) as u64,
)
}
pub fn dirent_info(
&self,
offset: u32,
length: usize,
) -> Result<Vec<(String, usize, u64, FATDiskInodeType)>, ()> {
if !self.is_dir() {
return Err(());
}
let lock = self.file_content.lock();
let size = lock.size;
let mut walker = self
.dir_iter(lock, None, DirIterMode::UsedIter, FORWARD)
.walk();
walker.iter.set_iter_offset(offset);
let mut v = Vec::with_capacity(length);
let (mut last_name, mut last_short_ent) = match walker.next() {
Some(tuple) => tuple,
None => return Ok(v),
};
for _ in 0..length {
let next_dirent_offset =
walker.iter.get_offset().unwrap() as usize + core::mem::size_of::<FATDirEnt>();
let (name, short_ent) = match walker.next() {
Some(tuple) => tuple,
None => {
v.push((
last_name,
size as usize,
last_short_ent.get_first_clus() as u64,
last_short_ent.attr,
));
return Ok(v);
}
};
log::trace!(
"{}, offset: {}",
last_name,
walker.iter.get_offset().unwrap() as usize
);
v.push((
last_name,
next_dirent_offset,
last_short_ent.get_first_clus() as u64,
last_short_ent.attr,
));
last_name = name;
last_short_ent = short_ent;
}
Ok(v)
}
}