/* * Gets the contents of a given directory path and returns the list of file * names to the requestor. * * req: TLV_TYPE_DIRECTORY_PATH - The directory that should be listed */ DWORD request_fs_ls(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); LPCSTR directory = packet_get_tlv_value_string(packet, TLV_TYPE_DIRECTORY_PATH); DWORD result; if (!directory) { result = ERROR_INVALID_PARAMETER; } else { result = fs_ls(directory, request_fs_ls_cb, response); } return packet_transmit_response(result, remote, response); }
/* * Gets the contents of a given directory path and returns the list of file * names to the requestor. * * req: TLV_TYPE_DIRECTORY_PATH - The directory that should be listed */ DWORD request_fs_ls(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); LPCSTR directory = packet_get_tlv_value_string(packet, TLV_TYPE_DIRECTORY_PATH); DWORD result; if (!directory) { result = ERROR_INVALID_PARAMETER; } else { result = fs_ls(directory, request_fs_ls_cb, response); } packet_add_tlv_uint(response, TLV_TYPE_RESULT, result); return PACKET_TRANSMIT(remote, response, NULL); }
/* pre: takes in int 'argc' and char** 'argv' command line arguments which * include: * -f <file_list> * -d <dir_list> * -s <disk size> * -b <block size> * post: runs the filesystem simulation program * return: 0 on sucessful exit, something else on error */ int main(int argc, char** argv) { int n; char* line; char** v; /* parse args and setup global environment */ parse_args(argc, argv); /* read input files and initialize filesystem */ init(); /* main command reading loop */ line = (char*)malloc(CMD_LEN * sizeof(char)); bzero((void*)line, CMD_LEN * sizeof(char)); while (1) { /* prompt */ printf("%s ", PROMPT); fflush(stdout); /* fd 0 is stdin */ if ((n = read(0, line, CMD_LEN)) == -1) { perror(argv[0]); fs_exit(); exit(-1); } else if (n == 0) { fs_exit(); break; } else { /* overwrite newline with NULL-byte */ line[n - 1] = '\0'; #ifdef DEBUG printf("[DEBUG]\tRead line: <%s>\n", line); fflush(stdout); #endif /* use str2vect to break 'line' into a list of whitespace separated * strings */ v = str2vect(line); /* check for commands and execute the proper function */ if (!strcmp(v[0], "append")) { if (v[1] != NULL && v[2] != NULL) fs_append(v[1], atoi(v[2])); else { printf("usage: append name size\n"); fflush(stdout); } } else if (!strcmp(v[0], "cd")) { if (v[1] != NULL) fs_cd(v[1]); else { printf("usage: cd directory\n"); fflush(stdout); } } else if (!strcmp(v[0], "cd..")) /* just in case this is the intended command */ { fs_cd(".."); } else if (!strcmp(v[0], "create")) { if (v[1] != NULL) fs_create(v[1]); else { printf("usage: create name\n"); fflush(stdout); } } else if (!strcmp(v[0], "delete")) { if (v[1] != NULL) fs_delete(v[1]); else { printf("usage: delete name\n"); fflush(stdout); } } else if (!strcmp(v[0], "dir")) { fs_dir(); } else if (!strcmp(v[0], "exit")) { /* free the vector immediately and break to exit */ free_vect(v); break; } else if (!strcmp(v[0], "ls")) { fs_ls(); } else if (!strcmp(v[0], "mkdir")) { if (v[1] != NULL) fs_mkdir(v[1]); else { printf("usage: mkdir directory\n"); fflush(stdout); } } else if (!strcmp(v[0], "prdisk")) { fs_prdisk(); } else if (!strcmp(v[0], "prfiles")) { fs_prfiles(); } else if (!strcmp(v[0], "remove")) { if (v[1] != NULL && v[2] != NULL) fs_remove(v[1], atoi(v[2])); else { printf("usage: remove name size\n"); fflush(stdout); } } else { printf("%s: command not found: %s\n", argv[0], v[0]); fflush(stdout); } /* free the vector to avoid memory leaks */ free_vect(v); } } free(line); fs_exit(); return 0; }
int process_request(const char *line, FILE *fp, FileSystem *fs) { char command[4096]; #ifdef DEBUG fprintf(stderr, "process request `%s`\n", line); #endif sscanf(line, "%s", command); #ifdef DEBUG fprintf(stderr, "command is `%s`\n", command); #endif if (0 == strcmp("f", command)) { fs_format(fs); return RESULT_DONE; } else if (0 == strcmp("mk", command)) { char f[4096]; char parent_path[4096]; sscanf(line + 2, "%s", f); #ifdef DEBUG fprintf(stderr, "> mk `%s`\n", f); #endif if (fs_exists(fs, f)) { #ifdef DEBUG fprintf(stderr, "> `%s` already exists\n", f); #endif return RESULT_NO; } else { #ifdef DEBUG fprintf(stderr, "> going to create `%s`\n", f); #endif } fs_split_path(f, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_create(fs, f)) { #ifdef DEBUG fprintf(stderr, "> failed to create `%s`\n", f); #endif return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "> parent path `%s` is not a directory\n", parent_path); #endif return RESULT_NO; } else if (0 == strcmp("mkdir", command)) { char d[4096]; char parent_path[4096]; sscanf(line + 5, "%s", d); if (fs_exists(fs, d)) { return RESULT_NO; } fs_split_path(d, parent_path, NULL); if (fs_isdir(fs, parent_path)) { if (fs_mkdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("rm", command)) { char f[4096]; sscanf(line + 2, "%s", f); if (fs_isfile(fs, f)) { if (fs_unlink(fs, f)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("cd", command)) { char path[4096]; sscanf(line + 2, "%s", path); if (fs_isdir(fs, path)) { if (fs_chdir(fs, path)) { return RESULT_NO; } return RESULT_YES; } #ifdef DEBUG fprintf(stderr, "`%s` is not a directory\n", path); #endif return RESULT_NO; } else if (0 == strcmp("rmdir", command)) { char d[4096]; sscanf(line + 5, "%s", d); if (fs_isdir(fs, d)) { if (fs_rmdir(fs, d)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("ls", command)) { fs_ls(fs, fp); return RESULT_ELSE; } else if (0 == strcmp("cat", command)) { char f[4096]; sscanf(line + 3, "%s", f); if (fs_isfile(fs, f)) { fs_cat(fs, f, fp); return RESULT_ELSE; } return RESULT_NO; } else if (0 == strcmp("w", command)) { char f[4096]; int l; char data[4096]; sscanf(line + 1, "%s %d %[^\n]", f, &l, data); if (fs_isfile(fs, f)) { if (fs_write(fs, f, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("i", command)) { char f[4096]; int pos; int l; char data[4096]; sscanf(line + 1, "%s %d %d %[^\n]", f, &pos, &l, data); if (fs_isfile(fs, f)) { if (fs_insert(fs, f, pos, l, data)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("d", command)) { char f[4096]; int pos; int l; sscanf(line + 1, "%s %d %d", f, &pos, &l); if (fs_isfile(fs, f)) { if (fs_delete(fs, f, pos, l)) { return RESULT_NO; } return RESULT_YES; } return RESULT_NO; } else if (0 == strcmp("e", command)) { return RESULT_EXIT; } return RESULT_ELSE; }
int main() { int i; int j; int k; heavyLine(); printf("filesystem self test\n"); heavyLine(); printf("\n"); fs_initialize(); // import programs fs_import("./programs.cpu/prog1out.cpu", "prog1"); fs_import("./programs.cpu/prog2out.cpu", "prog2"); // list the files fs_ls(); // remove file with id of 2 fs_removeFile(2); printf("\n"); // list the files fs_ls(); // invalid data int invalidData[3] = {FS_NULL, FS_NULL, FS_NULL}; // add invalid data int pStart[1] = {0}; int pSize[1] = {3}; fs_addFile("invalid data", 3, 1, pStart, pSize, invalidData); // list the files fs_ls(); fs_import("./programs.cpu/prog3out.cpu", "prog2"); // list the files fs_ls(); // remove all files fs_removeAllFiles(); // list the files fs_ls(); fs_import("./programs.cpu/prog1out.cpu", "prog1"); fs_import("./programs.cpu/prog2out.cpu", "prog2"); fs_import("./programs.cpu/prog3out.cpu", "prog3"); fs_import("./programs.cpu/prog4out.cpu", "prog4"); fs_ls(); fs_removeFile(6); heavyLine(); fs_dumpAllData(); fs_ls(); fs_copy(5, "progA"); fs_ls(); // testing paging INode* node; node = fs_getNode(4); int* page; page = calloc(PAGE_SIZE, sizeof(int)); for (i = 0; i < node->processes; i++) { heavyLine(); printf("process: %d \n", i); for (j = 0; j < (node->processSize[i] / PAGE_SIZE); j++) { page=fs_getPage(4, i, j * PAGE_SIZE, PAGE_SIZE); for (k = 0; k < PAGE_SIZE; k++) printf("%4d ", page[k]); printf("\n"); } } heavyLine(); //Since I cannot implementation of certain things at this point, we either need to have a memory leak //or do the following when finished with a set of inodes free(node->name); free(node); // fs_close(); return 0; }