/* * Copyright (C) 2002. University of California, Irvine * All rights reserved. * * File Name : * fs_user.c * * Description : * This file contains the implementation of the user interface to * perform some basic operations on the filesystem */ #ident "fs_user.c" /* Module Header File Inclusion */ #include "fs_internal.h" /* Global Variable Declarations */ void *ptr; UCHAR blk_ptr[128]; /* Static Variable Declarations */ //UCHAR help_menu[] = "Welcome to FileSystem!\n"; // chenli char help_menu[] = "Welcome to FileSystem!\n"; /* ============== H E L P M E N U ============= Available Commands and their Semantics 1) createFS - Creates a File System Synopsis: createFS Example: createFS disk1 128 1024 Explanation: Creates a filesystem consisting on 'disk1' with a block size equal to 128 bytes and size of the disk equal to 1024 blocks 2) addDisk - Adds a new disk to the File System Synopsis: addDisk Example: addDisk disk2 128 1024 3) reorganizeDisk - Defragments the disk Synopsis: reorganizeDisk Example: reorganizeDisk disk1 4) createFile - Creats a file with the specified parameters Synopsis: createFile Example: createFile fileA WRITE|APPEND 200 disk1 200 disk1 5) extendFile - Extends a file by an extent with size either equal to the specified one or DEFAULT in which case the size specified during the creation of file is used Synopsis: extendFile Example: extendFile fileA 100 6) deleteFile - Deletes the specified file and also updates the associated catalogue information in the system disk Synopsis: deleteFile Example: deleteFile fileA 7) openFile - Opens the specified file and returns a pointer which can be later used to perform any of the read, write, append or close operations on the file Synopsis: openFile Example: openFile fileA RD 8) readPage - Reads a page from a file corresponding to a block number, dumps the data into a memory buffer and returns a pointer to that buffer. The file is identified by its file pointer that was returned during the openFile operation Synopsis: readPage Example: readPage fileA 34 blk_ptr 9) writePage - Writes a page from a memory buffer to a file at the block specified by the block number. The file is identified by its file pointer that was returned during the openFile operation Synopsis: writePage Example: writePage fileA 34 blk_ptr 10) appendPage - Appends a page from the memory buffer to the end of the file. If the current file extent gets exhausted in the process then it shall automatically allocate a new extent based on the size mentioned during the creation of the file Synopsis: appendPage Example: appendPage fileA blk_ptr 11) closeFile - Closes the file specified by the file pointer and updates the catalogue information on the system disk if there have been changes in the same Synopsis: closeFile Example: closeFile fileA 12) destroyFS - Destroys the filesystem and all the files stored on it. It also updates the catalogue information on the system disk Synopsis: destroyFS Example: destroyFS disk1 To get the help menu again please type the command 'help' at the command prompt. To exit please type 'exit' \n"; */ /* Functions */ //FS_RESULT parse_command(UCHAR *cmd, INT16 length) // chenli FS_RESULT parse_command(char *cmd, INT16 length) { //UCHAR *command, *option[8]; // chenli char *command, *option[8]; UCHAR opt_cnt, cmd_len; UINT32 param[4]; opt_cnt = 0; command = strtok(cmd, " "); cmd_len = strlen(command); while((option[opt_cnt++] = strtok(NULL, " ")) != NULL); if (!strncmp(command, "help", cmd_len)) { printf(help_menu); } else if (!strncmp(command, "createFS", cmd_len)) { /* options: Disk name, disk size, block size, extents allocation policy */ param[0] = strtoul(option[1], NULL, 10); /* size */ param[1] = strtoul(option[2], NULL, 10); /* num blocks */ if (strncmp(option[3], "FF", 2) == 0) /* allocation policy */ { param[2] = FF; } else if (strncmp(option[3], "BF", 2) == 0) /* allocation policy */ { param[2] = BF; } else if (strncmp(option[3], "WF", 2) == 0) /* allocation policy */ { param[2] = WF; } else { printf("Invalid Option %s\n", option[3]); return FS_FAILURE; } fs_create_filesystem(option[0], param[0], param[1], param[2]); } else if (!strncmp(command, "addDisk", cmd_len)) { /* options: Disk name, disk size, block size */ param[0] = strtoul(option[1], NULL, 10); /* Disk Size */ param[1] = strtoul(option[2], NULL, 10); /* Block Size */ fs_add_disk(option[0], param[0], param[1]); } else if (!strncmp(command, "reorganizeDisk", cmd_len)) { /* options: Disk name */ fs_reorganize_disk(option[0]); } else if (!strncmp(command, "destroyFS", cmd_len)) { /* options: Disk name */ fs_destroy_filesystem(option[0]); } else if (!strncmp(command, "createFile", cmd_len)) { /* options: File name, pSize(O), pDisk name(O), sSize(O), sDisk name(O), File permissions(O) */ if (strncmp(option[1], "WR", 2) == 0) /* Mode */ { param[0] = WRITE; } else if (strncmp(option[1], "AP", 2) == 0) /* Mode */ { param[0] = APPEND; } else { printf("Invalid option: %s\n", option[1]); return FS_FAILURE; } if (strncmp(option[2], "DF", 2) == 0) /* Primary Extent Size */ { param[1] = DEFAULT; } else { param[1] = strtoul(option[2], NULL, 10); } if (strncmp(option[4], "DF", 2) == 0) /* Secondary Extent Size */ { param[2] = DEFAULT; } else { param[2] = strtoul(option[4], NULL, 10); } fs_create_file(option[0], param[0], param[1], option[3], param[2], option[5]); } else if (!strncmp(command, "extendFile", cmd_len)) { /* options: File name, sSize(O) */ param[0] = strtoul(option[1], NULL, 10); /* Extent size */ return fs_extend_file(option[0], param[0]); } else if (!strncmp(command, "deleteFile", cmd_len)) { /* options: File name */ fs_delete_file(option[0]); } else if (!strncmp(command, "openFile", cmd_len)) { /* options: File name, Mode */ if (strncmp(option[1], "RD", 2) == 0) { param[0] = READ; } else if (strncmp(option[1], "WR", 2) == 0) { param[0] = WRITE; } else if (strncmp(option[1], "AP", 2) == 0) { param[0] = APPEND; } ptr = fs_open_file(option[0], param[0]); } else if (!strncmp(command, "fillFile", cmd_len)) { /* options: File name */ fill_file(ptr); } else if (!strncmp(command, "fillBlock", cmd_len)) { /* options: File name */ fill_block(blk_ptr); } else if (!strncmp(command, "readPage", cmd_len)) { /* options: File descriptor, Block number, Mem ptr */ param[0] = strtoul(option[1], NULL, 10); /* Block Number */ fs_read_filepage(ptr, param[0], blk_ptr); printf("Contents of block pointer -->\n%s\n", blk_ptr); } else if (!strncmp(command, "writePage", cmd_len)) { /* options: File name, Block number, Mem ptr */ param[0] = strtoul(option[1], NULL, 10); /* Block Number */ fs_write_filepage(ptr, param[0], blk_ptr); } else if (!strncmp(command, "appendPage", cmd_len)) { /* options: File name, Mem ptr */ fs_append_filepage(&ptr, blk_ptr); // ALEX } else if (!strncmp(command, "closeFile", cmd_len)) { /* options: File name */ fs_close_file(ptr); } else { FS_ERR("Invalid Command\n"); return FS_FAILURE; } return FS_SUCCESS; } int main(void) { UINT16 count; //UCHAR command[32]; char command[32]; FS_RESULT fsres; printf(help_menu); while (strncmp(command, "exit", 4)) { printf(">> "); count = -1; memset(command, '\0', 32); while ((command[++count] = getchar()) != '\n'); command[count] = '\0'; if ((command[0] != '\0') && (strncmp(command, "exit", 4))) { fsres = parse_command(command, count); printf("%d\n", fsres); } } return 0; }