1 of 13

Lab 11

Project 3 Requests

2 of 13

The FAT

Cluster

Value {Cluster, EOC, 0}

2

EOC

3

0

4

0

5

0

6

0

7

0

8

0

9

0

10

0

...

3 of 13

How to get value of cluster N=4?

Formula from the whitepaper:

FATOffset = 4*N;

ThisFATSecNum = BPB_ResvdSecCnt + (FATOffset / BPB_BytsPerSec);

ThisFATEntOffset = REM(FATOffset / BPB_BytsPerSec);

Translated to functions of N

ThisFATSecNum(N) = BPB_ResvdSecCnt + ((4*N) / BPB_BytsPerSec);

ThisFATEntOffset(N) = REM((4*N) / BPB_BytsPerSec);

4 of 13

The FAT

Cluster

Value

2

EOC

3

0

4

0

5

0

6

0

7

0

8

0

9

0

10

0

...

ReadFAT(4)

int sector = ThisFATSecNum(4);

5 of 13

The FAT

Cluster

Value

2

EOC

3

0

4

0

5

0

6

0

7

0

8

0

9

0

10

0

...

ReadFAT(4)

int sector = ThisFATSecNum(4);

int offset = sector*BytsPerSec;

6 of 13

The FAT

Cluster

Value

2

EOC

3

0

4

0

5

0

6

0

7

0

8

0

9

0

10

0

...

ReadFAT(4)

int sector = ThisFATSecNum(4);

int offset = sector*BytsPerSec;

offset += ThisFATEntOffset(4);

7 of 13

The FAT

Cluster

Value

2

EOC

3

0

4

0

5

0

6

0

7

0

8

0

9

0

10

0

...

ReadFAT(4)

int sector = ThisFATSecNum(4);

int offset = sector*BytsPerSec;

offset += ThisFATEntOffset(4);

fseek(...,offset, SEEK_SET);

uint32_t someint;

int value = fread(&someint, 4, 1, ...);

return value;

8 of 13

The FAT

Cluster

Value

2

EOC

3

0

4

12345

5

0

6

0

7

0

8

0

9

0

10

0

...

WriteFAT(4, Val = 12345)

int sector = ThisFATSecNum(4);

int offset = sector*BytsPerSec;

offset += ThisFATEntOffset(4);

fseek(...,offset, SEEK_SET);

fwrite(&val, 4, 1, ...)

9 of 13

What's the point?

  • Every file has a cluster associated with it in its directory entry
  • When creating a file, how do you know which cluster's aren't used
    • Solution: If ReadFAT(N) = 0, cluster N is free.
  • Every cluster has M sectors (See BPB)
  • Every sector has N bytes (See BPB)
  • Max size of a file is M*N bytes?
    • Obviously not, so what happens when the cluster is full
  • Start writing to another empty cluster!

10 of 13

What's the point

  • Start writing to another empty cluster!
    • ... So if each directory entry only has one cluster associated with it, when reading the file, how d you know where the second cluster is?
      • or third...or fourth...etc.
  • When you pick a new (empty) cluster to continue a file in, record it in the fat

11 of 13

FAT Examples

  • If a file's first cluster is 147, and you need more space
    • pick the first empty cluster you see
      • N s.t. ReadFAT(N) == 0
      • Assume you get 27
    • Continue writing in 27...repeat until you write everything needed
  • If you just wrote to cluster 97, and you do not need more space, WriteFAT(97,EOC)
  • If you just deleted a file whose first cluster is C
    • Set DirEntry.Name[0] = EMPTY_ENTRY (or 0)
    • RecursiveDelete(C):
      • int value = ReadFAT(C);
      • WriteFAT(C, 0);
      • if (value != EOC) RecursiveDelete(C);

12 of 13

Next Week...

Forget about all of this for your holiday break

13 of 13

Questions/Requests?