Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a File System in C/C++ in Linux to implement the following commands: Create, Open, Close, Delete, Read, Write, Seek . The disk used has

Write a File System in C/C++ in Linux to implement the following commands: Create, Open, Close, Delete, Read, Write, Seek.

The disk used has 100 blocks or sectors from 0-99, each contains 512 bytes of data. You don't need to worry about the placement of the blocks on the disk, so all access to the disk should be done through 2 procedures (need it written) DREAD and DWRITE. You don't have to do disk reads and writes, the entire disk can be simulated using a 50K block of memory.

Directory Structure

The file directory in this system will be hierarchically ordered, the root directory always begins in block 0 which has the following structure:

1 Block

2 BACK FIXED BIN(31), /* ALWAYS ZERO IN THIS BLOCK */

2 FRWD FIXED BIN(31), /* BLOCK NUMBER OF SECOND DIRECTORY BLOCK, OR ZERO */

2 FREE FIXED BIN(31), /* BLOCK NUMBER OF FIRST UNUSED BLOCK */

2 FILLER CHARACTER(4), /* UNUSED */

2 DIR(31), /* DIRECTORY ENTRIES */

3 TYPE CHARACTER(1), /* 'F' = FREE, 'D' = DIRECTORY, 'U' = USER DATA */

3 NAME CHARACTER(9), /* FILE NAME, LEFT-JUSTIFIED, BLANK FILLED */

3 LINK FIXED BIN(31), /* BLOCK NUMBER OF FIRST BLOCK OF FILE */

3 SIZE FIXED BIN(15); /* NUMBER OF BYTES USED IN THE LAST BLOCK OF THE FILE */

The remaining blocks in the root file directory, if any, will be linked to blcok zero using the FRWD entry. These blocks, and blocks in the subordinate directories will have exactly the same structure as block zero, except that the FREE entry will be unused. This entry in block zero is used to point to the first unused block on the disk. The unused blocks should form a linked list which must be initially created when the file system starts execution.

The TYPE entry of each directory entry determines what type of file is referenced. 'F' is used to indicate that an entry in the file directory is unused. 'D' indicates that the entry points to another (subordinate) directory, while 'U' indicates that the entry points to a user data file. File names are given as strings of up to 9 alphabetic characters separated by virgules, or slashes.

The first file name would be located in the root directory as a user data file. The second file name would be found first in the root directory as a subordinate directory called SUB, then in the SUB directory as a user data file called SAMPLE. The third file name would involve two subordinate file directories, the first called SUB1 located in the root directory, the second called SUB2 located in the SUB1 directory, and the last called SAMPLE located in the SUB2 directory as a user data file. Each block in a data file, except the last, is considered full. The number of data bytes present in the last block of a data file is indicated by the SIZE field of the directory entry. Note that the directory blocks always have exactly 31 entries, with unused entries marked by a type field of 'F'.

Data File Format:

Data files also form linked lists. The structure of a data file block is:

DECLARE 1 DATA_BLOCK,

2 BACK FIXED BIN(31), /* BLOCK NUMBER OF PREVIOUS BLOCK */

2 FRWD FIXED BIN(31), /* BLOCK NUMBER OF SUCCESSOR BLOCK */

2 USER_DATA CHAR(504) /* USER DATA BYTES */

Commands:

CREATE type name

"type" is either U or D, and indicates whether a User data file or a Directory file is to be created. "name" is a full file name in the form described above. Neither of these items is enclosed in quotes. The CREATE command should cause a new directory entry to be created for the type of file specified. If CREATE is given for an existing file, no error should occur, but the file should be deleted and then reCREATEd. CREATE also leaves the file in the same state as an OPEN in the output mode.

OPEN mode name

"mode" is either I, O, or U indicating the file named "name" is to be opened in the Input, Output, or Update mode. Input mode means that only READ and SEEK commands are permitted while Output mode means only WRITE commands are permitted. Update mode allows READ, WRITE, and SEEK commands. Associated with each open file is a pointer to the next byte to be read or written. Opening a file for input or update places the pointer at the first byte of the file, while opening a file for output places the pointer at the byte immediately after the last byte of the file.

CLOSE

command causes the last OPENed or CREATEd file to be closed.. No filename is given.

DELETE name

This command deletes the named file.

READ

This command may only be used between an OPEN (in input or update mode) and the corresponding CLOSE. If possible, "n" bytes of datea should be read and displayed. If fewer than "n" bytes remain before the end of the file, then those bytes should be read and displayed with a message indicating that the end of file was reached.

WRITE n 'data'

This command causes the first "n" data bytes from 'data' (actually enclosed in single quotes in the command line) to be written to the file. If fewer than "n" bytes are given, then append sufficient blanks to 'data' to make "n" bytes. If it is impossible to write "n" bytes (because the disk is full) then an appropriate message should be issued, but the command should be otherwise treated as if the largest possible value of "n" was specified. (That is, the remaining available disk space should be filled.)

SEEK base offset

"base" is either -1, 0, or +1 indicating the beginning of the file, the current position in the file, or the end of the file. "offset" is a signed integer indicating the number of bytes from the "base" that the file pointer should be moved. For example, "SEEK -1 0" is a rewind, "SEEK +1 0" is equivalent to a position to end of file, and "SEEK 0 -5" positions the file pointer backward by five bytes.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

25.0 m C B A 52.0 m 65.0 m

Answered: 1 week ago

Question

How wide are Salary Structure Ranges?

Answered: 1 week ago