Project One - Brigham Young University



FAT File Management System

CS 345 - Project Six

Purpose

File management is a central component to most applications. With the exception of perhaps real-time applications, the input and output of an application is via a file. Files have a life outside of any individual application and provide a medium for later access by the user or by other programs. Typically, an operating system implements a file management system with a set of utility programs that run as privileged applications.

For this project, you are to write seven MS-DOS® FAT-12 file manager functions that will enable your operating system to define/delete, open/close, and read/write/seek DOS files.

Project Six is designed to help the student:

• understand the organization of a file management system.

• understand file allocation tables (FAT).

• understand FAT directory structures and cluster chains.

• understand a boot sector.

• understand file system buffering, basic file I/O, file descriptors, and file pointers.

• understand file access modes and learn how to modify file allocation tables (FAT), directory structures and cluster chains.

[pic]

Project Requirements

Correctly implement the following seven MS-DOS® FAT-12 file manager functions:

a. int fmsDefineFile(char* filename, int attribute)

▪ If attribute equals 0x10, then a new sub-directory file, named directoryName, is created in the current directory. One cluster is allocated to the file with new directory entries “.” and “..”.

▪ Else, a new file, named filename, is created in the current directory with file attribute equal to attribute. No clusters are allocated to that file.

▪ Return 0 for success; otherwise, return the error number.

b. int fmsDeleteFile(char* fileName)

▪ This function deletes the file/directory file named fileName from the current directory.

▪ The file name in the directory entry should be marked with a 0xe5 as the first character and the chained clusters in FAT 1 reallocated (set to 0).

▪ A directory file must not have any valid file or directory entries.

▪ Return 0 for success; otherwise, return the error number.

c. int fmsCloseFile(int fileDescriptor)

▪ The fmsCloseFile function closes the open file specified by fileDescriptor.

▪ The fileDescriptor was returned by fmsOpenFile and is an index into the open file table.

▪ Return a 0 for success; otherwise return error number.

d. int fmsOpenFile(char* fileName, int rwMode)

▪ This function opens the file named fileName for access as specified by rwMode.

▪ The open mode rwMode is defined as follows:

0 - Read access only.

The file pointer is initialized to the beginning of the file.

Writing to this file is not allowed.

1 - Write access only.

The file pointer is initialized to the beginning of the file.

Reading from this file is not allowed.

2 - Append access.

The file pointer is moved to the end of the file.

Reading from this file is not allowed.

3 - Read/Write access.

The file pointer is initialized to the beginning of the file.

Both read and writing to the file is allowed.

▪ A maximum of 32 files may be open at any one time.

▪ Return an error if the file does not exist, already open, or too many files are open.

▪ If successful, return a file descriptor that is used in calling subsequent file handling functions; otherwise, return the error number.

e. int fmsReadFile(int fileDescriptor, char* buffer, int nBytes)

▪ This function attempts to read nBytes bytes from the open file specified by fileDescriptor into memory pointed to by buffer.

▪ The fileDescriptor was returned by fmsOpenFile.

▪ After each read, the file pointer is advanced by the number of bytes read.

▪ Return the number of bytes successfully read (nBytes or bytes to end of file), or an error number.

▪ Return an error number for disc errors, invalid operation, or end of file (no bytes to read).

f. int fmsSeekFile(int fileDescriptor, int index)

▪ This function changes the current file pointer of the open file specified by fileDescriptor to the new file position specified by index.

▪ The fileDescriptor was returned by fmsOpenFile.

▪ The file position may not be positioned beyond the end of the file.

▪ Allow seek only on files opened for read or read/write access.

▪ Return the new position in the file if successful; otherwise, return the error number.

g. int fmsWriteFile(int fileDescriptor, char* buffer, int nBytes)

▪ This function writes nBytes bytes to the open file specified by fileDescriptor from memory pointed to by buffer.

▪ The fileDescriptor was returned by fmsOpenFile.

▪ Writing is always "overwriting" not "inserting" in the file and always writes forward from the current file pointer position.

▪ Return the number of bytes successfully written; otherwise, return the error number.

[pic]

Grading and Pass-off

 

Your FMS assignment is to be demonstrated in person to a TA and is outlined as follows:

1) Do the following:

• Implement the seven MS-DOS® FAT-12 file manager functions to define/delete, open/close, and read/write/seek DOS files.

• Use an open file table, I/O buffering, and file descriptors to accommodate up to 32 open files at a time.

• Use the following shell commands to demonstrate your file manager: (Please ask your instructor/TA for further clarification of arguments.)

o cd Change directory

o chkdsk Check disk

o copy , Copy file

o define Define file

o delete Delete file

o dir {} Display files in current directory

o fat ,, Display FAT structure

o final Test file manager

o mkdir Create directory

o mount Initialize FAT-12 RAM disk from file

o run Execute LC-3 program

o type Display file

2) Demonstrate that your file management system is functioning correctly by:

a. Being able to traverse the directory structure on a disk and list selected files.

b. Being able to display file contents using the type command.

c. Being able to copy files using the copy command.

d. Being able to create and delete files as well as directories using the define, mkdir, and delete

commands.

e. Being able to open, seek, read/write, and append files using the LC-3 decoder programs

(decode1.hex,…, decode9.hex) from your RAM disk.

f. Being about to pass a disk validity check using the chkdsk command.

g. Being about to pass all 6 file management stress tests using the final command.

3) There are 20 points possible for Project 6. Points will be awarded as follows:

• 4 points – Successfully define (define) and delete (delete) files/directories.

• 4 points – Successfully copy files using the copy command (open/read/write/close).

• 4 points – Successfully execute (run) the LC-3 decoder programs (decode1.hex,…, decode9.hex) from RAM disk 4. (The decoder programs are in the disk4:\byu\cs345\projects\p6 directory.)

• 8 points – Successfully validate your implementation with the chkdsk command and pass all the file management stress tests (final, p6). (Tests must be passed in consecutive order.)

• -2 points penalty for each school day late.

• -20 points penalty for any invalid reference to the RAM disk.

In addition, after completing the above requirements, the following bonus points may be awarded:

• +2 points bonus for early pass-off (at least one day before due date.)

• +2 points bonus for implementing directory support (cd and dir only) for long file names.

• +2 points bonus for implementing an undelete command.

• +2 points bonus for implementing a rename command.

• +1 point bonus for deleting multiple files using a file mask.

• +5 points bonus for implementing your file management functions as background kernel tasks that suspend the calling process until I/O operations complete.

NOTE: Bonus points may be received anytime during the semester (regardless of any late penalties) as long as the project requirements have been completed.

[pic]

[pic]

Project Guidelines

The following are important guidelines for programming the FAT FMS assignment:

1) RAM Disk Image: A FAT-12 disk image is loaded into a RAM disk (memory array) using the shell mount command. The RAM disk is divided into 2849 sectors, each being 512 bytes in length. The file allocation tables (FAT1 and FAT2) are held in separate memory arrays. All memory accesses to the RAM disk array must be made using the following read/write sector functions:

a. int fmsReadSector(void* buffer, int sectorNumber)

b. int fmsWriteSector(void* buffer, int sectorNumber)

2) RAM Disk Files and Directories: A FAT-12 file system specifies how files are named, accessed, and stored in your RAM disk image. Your program will maintain a “current directory” variable (cDir in the task control block) which points to the start cluster of the current working directory. (cDir == 0 refers to the root directory.) DOS hierarchal directories are navigated using the cd and dir shell commands. Short file and directory names are at most 8 characters long, with an optional 1-3 character extension (filename is separated from the extensions by a dot [.]).

3) FAT Directory and Cluster Chains: Project Six can be divided into two parts. The first part deals with disk directories and file navigation. You will need to implement the following function API’s: (Do NOT change these API’s as the test suit uses these functions.)

a. int fmsDefineFile(char* filename, int attribute)

b. int fmsDeleteFile(char* fileName)

4) C File Management Functions: The second part of Project Six deals with basic file I/O operations. You will need to implement the following C functions in your File Management System: (Again, do NOT change the API’s.):

c. int fmsCloseFile(int fileDescriptor)

d. int fmsOpenFile(char* fileName, int rwMode)

e. int fmsReadFile(int fileDescriptor, char* buffer, int nBytes)

f. int fmsSeekFile(int fileDescriptor, int index)

g. int fmsWriteFile(int fileDescriptor, char* buffer, int nBytes)

5) Validation: Your completed file management system must be able to pass a disk validation program (chkdsk) and a final stress test (final). The DOS function chkdsk analyzes a FAT-12 disk and reports any problems. (See description below.) The final program has six steps that will test various implementations of your file manager and must be executed in order. These tests include the creation and deletion of a large number of files and directories, opening and closing a maximum number of files, random access within files using the read, seek, and write primitives, and testing the various modes of opening files including read-only, write, read/write, and append.

[pic]

Directory structure

A DOS directory entry is a 32-byte struct that contains the file name, extension, time and date of last update, size of the file (in bytes), and the cluster number of the cluster of the file. File names follow the 8.3 naming standard. Time, date, startCluster, and fileSize are in little-endian format. The struct must be byte aligned.

#pragma pack(push,1) /* byte align in memory (no padding) */

typedef struct

{ unsigned char Name[8]; /* file name */

unsigned char Extension[3]; /* extension */

unsigned char Attributes; /* holds the attributes code */

unsigned char Reserved[10]; /* reserved */

unsigned short Time; /* time of last write */

unsigned short Date; /* date of last write */

unsigned short startCluster; /* pointer to the first cluster of the file. */

unsigned long fileSize; /* file size in bytes */

} DirEntry;

#pragma pack(pop) /* end of strict alignment */

[pic]

File/Directory Deletion

DOS does not immediately erase a file or directory when it is deleted. In fact, it does nothing to the clusters that contain the information (this is why it is sometimes possible to un-delete something). However, DOS does zero out the file/directory's cluster chain from the FAT table and places a special character (0xe5) in the first byte of the directory entry signaling that this entry has been deleted. You will need to do the same when a file or directory is deleted. Start with the cluster indicated in the directory entry, traverse the cluster chain, and then set each FAT entry to zero including the EOC entry.

The special code 0xe5 in the first byte of a directory entry, indicates that this directory entry is free, and may be overwritten with a new entry in the future. Place this value in the first byte of the directory entry to effectively delete it (it will be the first character of the file/directory name field). When reading directory entries, ignore all entries that begin with 0xe5.

When making any change to the FAT 1 table, copy the FAT 1 table to the FAT 2 table, EXCEPT when deleting a file.

[pic]

[pic]

Validity (chkdsk) Command

The DOS function chkdsk analyzes a FAT-12 disk and reports any problems. You should use chkdsk to validate your file manager implementation. Here are some checks and error messages that the chkdsk program will look for:

• lost chains – Allocated sectors not belonging to any directory or file.

• cross-linked chains – two entries in the FAT table are "pointing" to the same cluster.

• invalid directory entry

o invalid name – directory entries padded with zeros instead of spaces or lowercase.

o invalid extended attribute handle – "reserved" field of directory entry not zeroed out.

o invalid attribute – combination of attribute bits not recognizable.

o invalid date – date in future (or before 2000).

o invalid file size – file size impossible or file size 0 with non-zero start cluster.

o invalid file start cluster – start cluster out of bounds.

o zombie long name directory entry – long short names differ.

• invalid directory

o dot/dotdot invalid – dot or dotdot entries invalid or missing.

o invalid format – directory entries after “last” entry.

• improperly formatted disk

o invalid boot sector – corrupted data or constants.

o invalid fat tables – fat tables have not been or cannot be reconciled.

[pic]

[pic]

FMS Errors

In order to use the LC-3 file programs, we must standardize on error numbers coming from the File Management System. As such, please use the following error definitions in your implementation:

|# |Error Description | |# |Error Description |

|-50 |"Invalid File Name" | |-67 |"End-Of-Directory" |

|-51 |"Invalid File Type" | |-68 |"Directory Not Found" |

|-52 |"Invalid File Descriptor" | |-69 |"Can Not Delete" |

|-53 |"Invalid Sector Number" | |-70 |"Too Many Files Open" |

|-54 |"Invalid FAT Chain" | |-71 |"Not Enough Contiguous Space" |

|-55 |"Invalid Directory" | |-72 |"Disk Not Mounted" |

|-60 |"File Already Defined" | |-80 |"File Seek Error" |

|-61 |"File Not Defined" | |-81 |"File Locked"" |

|-62 |"File Already Open" | |-82 |"File Delete Protected" |

|-63 |"File Not Open" | |-83 |"File Write Protected" |

|-64 |"File Directory Full" | |-84 |"Read Only File" |

|-65 |"File Space Full" | |-85 |"Illegal Access" |

|-66 |"End-Of-File" | |-1 |"Undefined Error |

[pic]

Implementation Strategy

1. Read and comprehend the MS-DOS FAT File System help document as well as Stallings, Chapter 12.

2. Comprehend these lab specs. Discuss questions with classmates, the TA’s and/or the professor. Make sure you understand all the requirements!

3. Design your file management system. Break the problem down into manageable parts.

4. A suggested implementation order might be:

a. Implement fmsOpenFile, fmsReadFile, and fmsCloseFile. Verify your implementation using the type command.

b. Implement fmsWriteFile. Verify your implementation using the copy command.

c. Implement fmsDefineFile, fmsDeleteFile.

d. Implement fmsSeekFile and test with LC-3 decoder programs.

e. Valid your completed FMS with the chkdsk and final programs.

[pic]

Sample Output

***NOTE: This sample may or may not reflect your results and should not be used as validation of your FMS implementation.

CS345 F2009

0>>mount

Mount Disk "c:/lcc/projects/disk4"

System: IBM 3.3

Bytes/Sector: 512

Sectors/Cluster: 1

Reserved sectors: 1

FAT tables: 2

Max root dir entries: 224

FAT-12 sectors: 2880

FAT sectors: 9

Sectors/track: 18

Heads/volume: 2

FAT-32 sectors: 0

c:/lcc/projects/disk4:\>>final all

Running Test 1...

Define 128 directories...

Define 64 files...

Running Test 2...

Open 64 files...

Now is the time for all good men to come to the aid of their country.

Running Test 3...

Now is the time for all good men to come to the aid of their country.

Running Test 4...

Now is the time for all good men to come to the aid of their country.

Running Test 5...

fmsMakeDirectory("TESTDIR")

fmsChangeDir("TESTDIR")

Define 64 files...

fmsChangeDir("..")

fmsDeleteFile("TESTDIR") Can Not Delete... Good!

fmsChangeDir("TESTDIR")

Delete 64 files...

fmsChangeDir("..")

fmsDeleteFile("TESTDIR")

Running Test 6...

Delete 64 files...

Delete 128 directories...

Slot Name Ext Atr Size Strt Curr cDir cPID Mode Flag Indx

Name:ext time date cluster size

BIGDIR ----D- 11:31:40 03/31/2004 3 0

BYU ----D- 11:34:54 03/31/2004 171 0

JOKES ----D- 11:37:06 03/31/2004 800 0

LONGFI~1 ----D- 11:37:14 03/31/2004 875 0

PERSONAL ----D- 11:37:18 03/31/2004 937 0

TEMP ----D- 11:37:36 03/31/2004 1355 0

H2O.C -----A 19:00:02 02/12/2004 1380 3425

MAKE.TXT -----A 16:26:58 02/27/2004 1387 18584

CONGRATULATIONS! YOU PASS!

c:/lcc/projects/disk4:\>>cd byu

c:/lcc/projects/disk4:\byu>>cd cs345

c:/lcc/projects/disk4:\byu\cs345>>cd projects

c:/lcc/projects/disk4:\byu\cs345\projects>>cd p6

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>run decode1.hex

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>

Load "decode1.hex" from FAT

Memory loaded! PC=0x3000

Process #1 Halted at 0x313e

Exit Task 1

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>dir

Name:ext time date cluster size

. ----D- 11:35:10 03/31/2004 224 0

.. ----D- 11:35:10 03/31/2004 222 0

DECODE1.HEX -----A 11:00:58 03/31/2004 225 3630

DECODE2.HEX -----A 11:02:10 03/31/2004 233 3630

DECODE3.HEX -----A 11:02:56 03/31/2004 241 3630

DECODE4.HEX -----A 11:03:44 03/31/2004 249 3630

DECODE5.HEX -----A 11:04:14 03/31/2004 257 3630

DECODE6.HEX -----A 11:04:36 03/31/2004 265 3630

DECODE7.HEX -----A 11:05:02 03/31/2004 273 3630

DECODE8.HEX -----A 11:05:26 03/31/2004 281 3630

DECODE9.HEX -----A 11:05:48 03/31/2004 289 3630

DICT.DAT -----A 13:45:30 03/22/2004 297 4600

SECRET1.DAT -----A 13:45:30 03/22/2004 306 156

SECRET2.DAT -----A 13:45:30 03/22/2004 307 966

SECRET3.DAT -----A 13:45:30 03/22/2004 309 300

SECRET4.DAT -----A 13:45:30 03/22/2004 310 1146

SECRET5.DAT -----A 13:45:30 03/22/2004 314 660

SECRET6.DAT -----A 13:45:30 03/22/2004 316 1494

SECRET7.DAT -----A 13:45:30 03/22/2004 319 492

SECRET8.DAT -----A 13:45:30 03/22/2004 320 438

SECRET9.DAT -----A 13:45:30 03/22/2004 321 282

MAP.HEX -----A 13:11:24 03/20/2004 1563 1782

CALCUL~1.HEX -----A 14:38:18 01/27/2004 1567 6174

CRAWLER.HEX -----A 17:18:00 02/02/2004 1580 1980

MEMTEST.HEX -----A 22:09:32 02/09/2004 1584 2640

MESSAGE1.TXT -----A 15:14:44 10/01/2008 2 61

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>type message1.txt

Men do not fail,

but often rather,

they simply quit trying...

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>final all

Running Test 1...

Define 128 directories...

Define 64 files...

Running Test 2...

Open 64 files...

Now is the time for all good men to come to the aid of their country.

Running Test 3...

Now is the time for all good men to come to the aid of their country.

Running Test 4...

Now is the time for all good men to come to the aid of their country.

Running Test 5...

fmsMakeDirectory("TESTDIR")

fmsChangeDir("TESTDIR")

Define 64 files...

fmsChangeDir("..")

fmsDeleteFile("TESTDIR") Can Not Delete... Good!

fmsChangeDir("TESTDIR")

Delete 64 files...

fmsChangeDir("..")

fmsDeleteFile("TESTDIR")

Running Test 6...

Delete 64 files...

Delete 128 directories...

Slot Name Ext Atr Size Strt Curr cDir cPID Mode Flag Indx

Name:ext time date cluster size

. ----D- 11:35:10 03/31/2004 224 0

.. ----D- 11:35:10 03/31/2004 222 0

DECODE1.HEX -----A 11:00:58 03/31/2004 225 3630

DECODE2.HEX -----A 11:02:10 03/31/2004 233 3630

DECODE3.HEX -----A 11:02:56 03/31/2004 241 3630

DECODE4.HEX -----A 11:03:44 03/31/2004 249 3630

DECODE5.HEX -----A 11:04:14 03/31/2004 257 3630

DECODE6.HEX -----A 11:04:36 03/31/2004 265 3630

DECODE7.HEX -----A 11:05:02 03/31/2004 273 3630

DECODE8.HEX -----A 11:05:26 03/31/2004 281 3630

DECODE9.HEX -----A 11:05:48 03/31/2004 289 3630

DICT.DAT -----A 13:45:30 03/22/2004 297 4600

SECRET1.DAT -----A 13:45:30 03/22/2004 306 156

SECRET2.DAT -----A 13:45:30 03/22/2004 307 966

SECRET3.DAT -----A 13:45:30 03/22/2004 309 300

SECRET4.DAT -----A 13:45:30 03/22/2004 310 1146

SECRET5.DAT -----A 13:45:30 03/22/2004 314 660

SECRET6.DAT -----A 13:45:30 03/22/2004 316 1494

SECRET7.DAT -----A 13:45:30 03/22/2004 319 492

SECRET8.DAT -----A 13:45:30 03/22/2004 320 438

SECRET9.DAT -----A 13:45:30 03/22/2004 321 282

MAP.HEX -----A 13:11:24 03/20/2004 1563 1782

CALCUL~1.HEX -----A 14:38:18 01/27/2004 1567 6174

CRAWLER.HEX -----A 17:18:00 02/02/2004 1580 1980

MEMTEST.HEX -----A 22:09:32 02/09/2004 1584 2640

MESSAGE1.TXT -----A 15:14:44 10/01/2008 2 61

CONGRATULATIONS! YOU PASS!

c:/lcc/projects/disk4:\byu\cs345\projects\p6>>

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download