static int read_file(test_input_t *t, test_file_t *test_file) { int size, bytes; int data_offset; /* offset in char *data */ int file_offset; /* offset in a file */ int offset = test_file->offset; int written_bytes = test_file->written_bytes; if (t->read_size.max - t->read_size.min > 0) { size = t->read_size.min + (rand() % (t->read_size.max - t->read_size.min)); } else { size = t->read_size.min; } file_offset = (written_bytes - size > 0) ? written_bytes - size : 0; data_offset = (offset + file_offset) % DATA_SIZE; bytes = sfs_read(t->sfs, test_file->id, file_offset, size, t->buffer); if (bytes != size && bytes != (written_bytes - file_offset)) { error("sfs_read returned %d. file id[%d], read_size[%d]", bytes, test_file->id, size); error("written_bytes[%d] - file_offset[%d] = [%d]", written_bytes, file_offset, written_bytes-file_offset); /* error("sfs->super_block->file_count[%d],file_total_byte[%d]", t->sfs->super_block->file_count, t->sfs->super_block->file_total_byte); */ return FAIL; } if (memcmp(t->buffer, t->data + data_offset, bytes) != 0) { error("memcmp returned non-zero value. "); error("sfs_read returned %d. file id[%d], read_size[%d]", bytes, test_file->id, size); error("written_bytes[%d] - file_offset[%d] = [%d]", written_bytes, file_offset, written_bytes-file_offset); t->buffer[bytes] = '\0'; error("read data[%s]", t->buffer); memcpy(t->buffer, t->data+data_offset, bytes); t->buffer[bytes] = '\0'; error("written data[%s]", t->buffer); /* error("sfs->super_block->file_count[%d],file_total_byte[%d]", sfs->super_block->file_count, sfs->super_block->file_total_byte); */ memset(t->buffer, 0x00, DATA_SIZE); bytes = sfs_read(t->sfs, test_file->id, 0, MAX_FILE_SIZE, t->buffer); t->buffer[bytes] = '\0'; error("full file data[%s]", t->buffer); return FAIL; } return SUCCESS; }
int ext_read_block(const char *filename, int block_id, void *buf) { char *blk_path = (char *)malloc(sizeof(char) * strlen(filename) + 7); int fd = 0; get_block_file_path(blk_path, filename, block_id); fd = sfs_open("/", blk_path); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); return 0; }
void browse_sfs(int sfs_handle) { int done; char cmd[256], *fn; int ret, perm; done = 0; printf("Welcome to SFS Browsing Shell v3.0\n\n"); printf("Block size : %d bytes\n", sb.block_size); printf("Partition size : %d blocks\n", sb.partition_size); printf("File entry size: %d bytes\n", sb.entry_size); printf("Entry tbl size : %d blocks\n", sb.entry_table_size); printf("Entry count : %d\n", sb.entry_count); printf("\n"); init_browsing(sfs_handle); while (!done) { printf(" $> "); ret = scanf("%[^\n]", cmd); if (ret < 0) { done = 1; printf("\n"); continue; } else { getchar(); if (ret == 0) continue; } if (strcmp(cmd, "?") == 0) { usage(); continue; } else if (strcmp(cmd, "quit") == 0) { done = 1; continue; } else if (strcmp(cmd, "list") == 0) { sfs_list(sfs_handle); continue; } else if (strncmp(cmd, "create", 6) == 0) { if (cmd[6] == ' ') { fn = cmd + 7; while (*fn == ' ') fn++; if (*fn != '\0') { sfs_create(sfs_handle, fn); continue; } } } else if (strncmp(cmd, "remove", 6) == 0) { if (cmd[6] == ' ') { fn = cmd + 7; while (*fn == ' ') fn++; if (*fn != '\0') { sfs_remove(sfs_handle, fn); continue; } } } else if (strncmp(cmd, "chperm", 6) == 0) { if (cmd[6] == ' ') { perm = cmd[7] - '0'; if ((0 <= perm) && (perm <= 7) && (cmd[8] == ' ')) { fn = cmd + 9; while (*fn == ' ') fn++; if (*fn != '\0') { sfs_chperm(sfs_handle, fn, perm); continue; } } } } else if (strncmp(cmd, "read", 4) == 0) { if (cmd[4] == ' ') { fn = cmd + 5; while (*fn == ' ') fn++; if (*fn != '\0') { sfs_read(sfs_handle, fn); continue; } } } else if (strncmp(cmd, "write", 5) == 0) { if (cmd[5] == ' ') { fn = cmd + 6; while (*fn == ' ') fn++; if (*fn != '\0') { sfs_write(sfs_handle, fn); continue; } } } printf("Unknown/Incorrect command: %s\n", cmd); usage(); } shut_browsing(sfs_handle); }
main() { int i; int retval; /* used to hold return values of file system calls */ /* do forever: 1) print a list of available commands 2) read a command 3) read arguments for the command 4) perform the requested operation 5) display the results of the operation */ while(1) { /* print a list of available commands */ printf("\n"); printf("o: open a file\n"); printf("r: read from a file\n"); printf("w: write to a file\n"); printf("R: read from a directory\n"); printf("c: close a file\n"); printf("m: create (make) a new file\n"); printf("d: delete a file\n"); printf("s: get the size of a file\n"); printf("t: get the type of a file\n"); printf("i: initialize the file system\n"); printf("q: quit - exit this program\n"); /* read in the next command */ printf("\nCommand? "); if (gets(command_buffer) == NULL) break; /* determine which command was requested */ switch(command_buffer[0]) { case 'o': /* Open a file */ printf("Enter full path name of file to open: "); scanf(INPUT_BUF_FORMAT,data_buffer_1); retval = sfs_open(data_buffer_1); if (retval >= 0) { printf("Open succeeded. File Descriptor number is %d\n",retval); } else { printf("Error. Return value was %d\n",retval); } break; case 'r': /* Read from a file */ printf("Enter file descriptor number: "); scanf("%d",&p1); printf("Enter read start location: "); scanf("%d",&p2); printf("Enter number of bytes to read: "); scanf("%d",&p3); retval = sfs_read(p1,p2,p3,io_buffer); if (retval > 0) { printf("Read succeeded.\n"); printf("The following data was read (only printable ASCII will display)\n"); for(i=0;i<p3;i++) { putchar(io_buffer[i]); } printf("\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 'w': /* Write to a file */ printf("Enter file descriptor number: "); scanf("%d",&p1); printf("Enter write start location: "); scanf("%d",&p2); printf("Enter number of bytes to write: "); scanf("%d",&p3); printf("This program allows only non-white-space, printable ASCII characters to be written to a file.\n"); printf("Enter %d characters to be written: ",p3); scanf(IO_BUF_FORMAT,io_buffer); retval = sfs_write(p1,p2,p3,io_buffer); if (retval > 0) { printf("Write succeeded.\n"); printf("Wrote %s to the disk\n",io_buffer); } else { printf("Error. Return value was %d\n",retval); } break; case 'R': /* Read from a directory */ printf("Enter file descriptor number: "); scanf("%d",&p1); retval = sfs_readdir(p1,io_buffer); if (retval > 0) { printf("sfs_readdir succeeded.\n"); printf("Directory entry is: %s\n",io_buffer); } else if (retval == 0) { printf("sfs_readdir succeeded.\n"); printf("No more entries in this directory\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 'c': /* Close a file */ printf("Enter file descriptor number: "); scanf("%d",&p1); retval = sfs_close(p1); if (retval > 0) { printf("sfs_close succeeded.\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 'm': /* Create a new file */ printf("Enter full path name of new file: "); scanf(INPUT_BUF_FORMAT,data_buffer_1); printf("Enter 0 for regular file, 1 for directory: "); scanf("%d",&p1); retval = sfs_create(data_buffer_1,p1); if (retval > 0) { printf("sfs_create succeeded.\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 'd': /* Delete a file */ printf("Enter full path name of file to delete: "); scanf(INPUT_BUF_FORMAT,data_buffer_1); retval = sfs_delete(data_buffer_1); if (retval > 0) { printf("sfs_delete succeeded.\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 's': /* Get the size of a file */ printf("Enter full path name of file: "); scanf(INPUT_BUF_FORMAT,data_buffer_1); retval = sfs_getsize(data_buffer_1); if (retval >= 0) { printf("sfs_getsize succeeded.\n"); printf("size = %d\n",retval); } else { printf("Error. Return value was %d\n",retval); } break; case 't': /* Get the type of a file */ printf("Enter full path name of file: "); scanf(INPUT_BUF_FORMAT,data_buffer_1); retval = sfs_gettype(data_buffer_1); if (retval >= 0) { printf("sfs_gettype succeeded.\n"); if (retval == 0) { printf("file type is REGULAR\n"); } else if (retval == 1) { printf("file type is DIRECTORY\n"); } else { printf("file has unknown type %d\n",retval); } } else { printf("Error. Return value was %d\n",retval); } break; case 'i': /* Initialize the file system */ printf("Enter 1 to erase disk while initializing, 0 otherwise: "); scanf("%d",&p1); retval = sfs_initialize(p1); if (retval > 0) { printf("sfs_initialize succeeded.\n"); } else { printf("Error. Return value was %d\n",retval); } break; case 'q': /* Quit this program */ break; default: printf("Unknown command: %s\n",command_buffer); break; } if (command_buffer[0] == 'q') break; /* cleanup the newline that remains after reading command parameter(s) */ gets(command_buffer); } }
int test_case_1(char **argv, int op_type) { if (send_file_request(argv, "local_file", 1) == -1) { return 1; } sleep(5); int ret = 0; int fd; FILE *local_fp = fopen("local_file", "rb"); char *buf = (char *) malloc(sizeof(char) * DFS_BLOCK_SIZE); char *buf_local = (char *) malloc(sizeof(char) * DFS_BLOCK_SIZE); sfs_reloadfs("d1/local_fs"); fd = sfs_open("/", "local_file_blk_0"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; if (ret == 0) { fseek(local_fp, 2 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_2"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } if (ret == 0) { fseek(local_fp, 4 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_4"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } if (ret == 0) { fseek(local_fp, 6 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_6"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } sfs_close_storage(); sfs_reloadfs("d2/local_fs"); fd = sfs_open("/", "local_file_blk_1"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fseek(local_fp, 1 * DFS_BLOCK_SIZE, SEEK_SET); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; if (ret == 0) { fseek(local_fp, 3 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_3"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } if (ret == 0) { fseek(local_fp, 5 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_5"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } if (ret == 0) { fseek(local_fp, 7 * DFS_BLOCK_SIZE, SEEK_SET); fd = sfs_open("/", "local_file_blk_7"); sfs_read(fd, buf, DFS_BLOCK_SIZE); sfs_close(fd); fread(buf_local, 1, DFS_BLOCK_SIZE, local_fp); if (memcmp(buf_local, buf, DFS_BLOCK_SIZE) != 0) ret = 1; } sfs_close_storage(); fclose(local_fp); free(buf); free(buf_local); return ret; }
int main(int argc, char *argv[]) { if ( argc == 2 ) { port = strtol(argv[1], &endptr, 0); if ( *endptr ) { fprintf(stderr, "Invalid port number.\n"); exit(EXIT_FAILURE); } } else if ( argc < 2 ) { port = ecPort; } else { fprintf(stderr, "Invalid arguments\n"); exit(EXIT_FAILURE); } fileSystemInit(); connection_setup(); if ( (connS = accept(listS, NULL, NULL) ) < 0 ) { fprintf(stderr, "Error!!\n"); exit(EXIT_FAILURE); } header = (char *)malloc(2 * sizeof(int)); elreadmos = (char *)malloc(10000); myLStable = (char *)malloc(100); while ( 1 ) { recieveing(connS, recEI, 2 * sizeof(int)); memcpy(&recvReq, recEI, sizeof(int)); memcpy(&recvSize, recEI +sizeof(int), sizeof(int)); switch (recvReq) { case 1: printf("root directory\n"); LStableAdd = sfs_ls(); strcpy(myLStable, LStableAdd); size = strlen(myLStable); DataSend = (char *)malloc(size); memcpy(header, &lsCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); memcpy(DataSend, myLStable, size); Sending(connS, header, (2 * sizeof(int))); Sending(connS, DataSend, size); free(DataSend); break; case 2: printf("open file\n"); recData = (char *)malloc(recvSize); recieveing(connS, recData, recvSize); memcpy(openReq, recData, recvSize); openReq[recvSize] = 0; remID = sfs_open(openReq); DataSend = (char *)malloc(sizeof(int)); size = sizeof(int); memcpy(header, &opCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); memcpy(DataSend, &remID, size); Sending(connS, header, (2 * sizeof(int))); Sending(connS, DataSend, size); free(recData); free(DataSend); break; case 3: printf("close file\n"); recData = (char *)malloc(recvSize); recieveing(connS, recData, recvSize); memcpy(&elremote, recData, sizeof(int)); sfs_close(elremote); memcpy(header, &closeCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); Sending(connS, header, (2 * sizeof(int))); free(recData); break; case 4: printf("write file\n"); recData = (char *)malloc(recvSize); elWritBuf = (char *)malloc(recvSize); recieveing(connS, recData, recvSize - 8); memcpy(elWritBuf, recData, recvSize -8); elWritBuf[recvSize - 8] = 0; recieveing(connS, recData, sizeof(int)); memcpy(&remID, recData, sizeof(int)); recieveing(connS, recData, sizeof(int)); memcpy(&my_len, recData, sizeof(int)); sfs_write(remID, elWritBuf, my_len); size = 0; memcpy(header, &writCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); Sending(connS, header, (2 * sizeof(int))); free(recData); free(elWritBuf); break; case 5: printf("read file\n"); recData = (char *)malloc(recvSize); recieveing(connS, recData, sizeof(int)); memcpy(&remID, recData, sizeof(int)); recieveing(connS, recData, sizeof(int)); memcpy(&my_len, recData, sizeof(int)); sfs_read(remID, elreadmos, my_len); size = strlen(elreadmos); memcpy(header, &readCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); Sending(connS, header, (2 * sizeof(int))); DataSend = (char *)malloc(size); memcpy(DataSend, elreadmos, size); DataSend[size] = 0; puts(DataSend); Sending(connS, DataSend, size); break; case 6: printf("remove file\n"); recData = (char *)malloc(recvSize); recieveing(connS, recData, recvSize); memcpy(remReq, recData, recvSize); remReq[recvSize] = 0; printf("file to remove = %s\n",remReq); if (sfs_remove(remReq) == -1){ size = strlen("Error: file doesnot exist\n"); DataSend = (char *)malloc(size); strcpy(DataSend, "Error: file doesnt exist\n"); } else{ size = strlen("file removed successfully\n"); DataSend = (char *)malloc(size); strcpy(DataSend, "file remove successfull\n"); } memcpy(header, &remCom, sizeof(int)); memcpy(header+sizeof(int), &size, sizeof(int)); Sending(connS, header, (2 * sizeof(int))); Sending(connS, DataSend, size); free(recData); free(DataSend); break; default: printf("Invalid input\n"); break; } } }
/* The main testing program */ int main(int argc, char **argv) { int i, j, k; int chunksize; char *buffer; int fds[MAX_FD]; char *names[MAX_FD]; int filesize[MAX_FD]; int error_count = 0; int tmp; mksfs(1); /* Initialize the file system. */ /* First we open five files and attempt to write data to them. */ for (i = 0; i < 5; i++) { names[i] = rand_name(); fds[i] = sfs_open(names[i]); if (fds[i] < 0) { fprintf(stderr, "ERROR: creating first test file %s\n", names[i]); error_count++; } tmp = sfs_open(names[i]); if (tmp >= 0 && tmp != fds[i]) { fprintf(stderr, "ERROR: file %s was opened twice\n", names[i]); error_count++; } filesize[i] = (rand() % (MAX_BYTES-MIN_BYTES)) + MIN_BYTES; } for (i = 0; i < 5; i++) { for (j = i + 1; j < 2; j++) { if (fds[i] == fds[j]) { fprintf(stderr, "Warning: the file descriptors probably shouldn't be the same?\n"); } } } printf("Five files created with zero length:\n"); sfs_ls(); printf("\n"); for (i = 0; i < 5; i++) { for (j = 0; j < filesize[i]; j += chunksize) { if ((filesize[i] - j) < 10) { chunksize = filesize[i] - j; } else { chunksize = (rand() % (filesize[i] - j)) + 1; } if ((buffer = malloc(chunksize)) == NULL) { fprintf(stderr, "ABORT: Out of memory!\n"); exit(-1); } for (k = 0; k < chunksize; k++) { buffer[k] = (char) (j+k); } sfs_write(fds[i], buffer, chunksize); free(buffer); } } for (i = 0; i < 5; i++) sfs_close(fds[i]); sfs_ls(); for (i = 0; i < 5; i++) fds[i] = sfs_open(names[i]); printf("Reopened the files again.. the read/write pointers should be set to front\n"); for (i = 0; i < 5; i++) { for (j = 0; j < filesize[i]; j += chunksize) { if ((filesize[i] - j) < 10) { chunksize = filesize[i] - j; } else { chunksize = (rand() % (filesize[i] - j)) + 1; } if ((buffer = malloc(chunksize)) == NULL) { fprintf(stderr, "ABORT: Out of memory!\n"); exit(-1); } sfs_read(fds[i], buffer, chunksize); puts(buffer); for (k = 0; k < chunksize; k++) { if (buffer[k] != (char)(j+k)) { fprintf(stderr, "ERROR: data error at offset %d in file %s (%x,%x)\n", j+k, names[i], buffer[k], (char)(j+k)); error_count++; break; } } free(buffer); } } fprintf(stderr, "Test program exiting with %d errors\n", error_count); return (error_count); }