void test_rename(char *workdir) { char file1[1024], file2[1024], link[1024], subdir[1024], newdir[1024]; struct stat sb1, sb2; int fd; printf("testing rename\n"); sprintf(file1, "%s/file1", workdir); sprintf(file2, "%s/file2", workdir); /* test stat first.. since we need it to test rename */ my_stat(file1, &sb1); /* Not much to look at, really.. how about making sure that the modification time of the file is not in the future. allow ten seconds of slack */ if (sb1.st_mtime > time(NULL) + 10) { printf("Modification time of %s is in the future!\n", file1); exit(1); } my_rename(file1, file2); /* try repeat */ if (!rename(file1, file2)) { printf("repeat rename(%s,%s) worked.. but it shouldn't have\n", file1, file2); exit(1); } /* inode number of file2 should be the same as it was for file1 */ verify_inum(file2, &sb1); /* rename back */ my_rename(file2, file1); verify_inum(file1, &sb1); /* Make a subdir, which will be renamed */ sprintf(subdir, "%s/rename_this_dir", workdir); my_mkdir(subdir); my_stat(subdir, &sb2); /* Test moving a file into a subdir */ sprintf(file2, "%s/file1", subdir); my_rename(file1, file2); verify_inum(file2, &sb1); /* Add in a hard link to spice things up */ sprintf(link, "%s/link", subdir); my_link(file2, link); sprintf(newdir, "%s/newdirname", workdir); my_rename(subdir, newdir); verify_inum(newdir, &sb2); sprintf(file2, "%s/file1", newdir); verify_inum(file2, &sb1); sprintf(link, "%s/newdirname/link", workdir); verify_inum(link, &sb1); /* Test moving up in the tree */ my_rename(file2,file1); verify_inum(file1, &sb1); my_unlink(link); my_rmdir(newdir); }
void test_my_write() { printf("\ntest_my_write:\n"); if (my_mkdir("/foo") < 0) { printf("Failed at making directory /foo\n"); exit(1); } if (my_mkdir("/foo/bar") < 0) { printf("Failed at making directory /foo/bar\n"); exit(1); } int fd; if ((fd = my_creat("/foo/bar/test.txt")) < 0) { printf("Failed at creating file /foo/bar/test.txt\n"); exit(1); } int block_num = get_associated_block_num(fd); printf("Created file at block number %d\n", block_num); char string[101] = "abcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghijabcdefghij\0"; if (DEBUG) { printf("Writing string of length 100\n"); } if (my_write(fd, string, 100) < 0) { printf("Failed to write string to file /foo/bar/test.txt\n"); exit(1); } char buffer[1024]; if (read_block(block_num, buffer) < 0) { printf("Error reading block #%d\n", block_num); exit(1); } short bytesAllocated; memcpy(&bytesAllocated, buffer + 1 + sizeof(int), sizeof(bytesAllocated)); if (DEBUG) { printf("bytes allocated: %d\n", bytesAllocated); } char * buffer_ptr = buffer + 8; char data[101]; strncpy(data, buffer_ptr, 100); data[100] = '\0'; if (strcmp(data, string) != 0) { printf("Error in reading back out data\n"); exit(1); } if (DEBUG) { printf("Closing file %d\n", fd); } if (my_close(fd) < 0) { printf("Failed to close file %d\n", fd); exit(1); } if ((fd = my_open("/foo/bar/test.txt")) < 0) { printf("Failed to open file /foo/bar/test.txt\n"); exit(1); } if (DEBUG) { printf("Opened file %d\n", fd); } char string2[50] = "jihgfedcbajihgfedcbajihgfedcbajihgfedcbajihgfedcba"; if (DEBUG) { printf("Writing string of length 50\n"); } strncpy(string, string2, 50); if (my_write(fd, string2, 50) < 0) { printf("Failed to write string2 to /foo/bar/test.txt\n"); exit(1); } block_num = get_associated_block_num(fd); if (read_block(block_num, buffer) < 0) { printf("Error reading block #%d\n", block_num); exit(1); } memcpy(&bytesAllocated, buffer + 1 + sizeof(int), sizeof(bytesAllocated)); if (DEBUG) { printf("bytes allocated: %d\n", bytesAllocated); } buffer_ptr = buffer + HEADER_SIZE; memset(data, '\0', strlen(data)); strncpy(data, buffer_ptr, 101); if (strcmp(data, string) != 0) { printf("Error in reading back out data\n"); exit(1); } if (DEBUG) { printf("Closing file %d\n", fd); } if (my_close(fd) < 0) { printf("Failed to close file %d\n", fd); exit(1); } if ((fd = my_open("/foo/bar/test.txt")) < 0) { printf("Failed to open file /foo/bar/test.txt\n"); exit(1); } if (DEBUG) { printf("Opened file %d\n", fd); } block_num = get_associated_block_num(fd); char alphabet[26] = "abcdefghijlkmnopqrstuvwxyz"; char longString[1501]; int i; for (i = 0; i < 1500; i++) { longString[i] = alphabet[(i % 26)]; } longString[1500] = '\0'; if (DEBUG) { printf("Writing string of length 1500\n"); } if (my_write(fd, longString, 1500) < 0) { printf("Failed to write string2 to /foo/bar/test.txt\n"); exit(1); } if (read_block(block_num, buffer) < 0) { printf("Error reading block #%d\n", block_num); exit(1); } memcpy(&bytesAllocated, buffer + 1 + sizeof(int), sizeof(bytesAllocated)); if (DEBUG) { printf("bytes allocated in first block excluding header: %d\n", (bytesAllocated - HEADER_SIZE)); } if (bytesAllocated != 1024) { printf("Error allocated %d bytes\n", bytesAllocated); } int next_block; memcpy(&next_block, buffer + 1, sizeof(next_block)); if (next_block == 0) { printf("Didn't properly create a new block.\n"); exit(1); } else { printf("Created new file block at block %d, when writing\n", next_block); } char new_buffer[1024]; if (read_block(next_block, new_buffer) < 0) { printf("Error reading block #%d\n", next_block); exit(1); } short newBytesAllocated; memcpy(&newBytesAllocated, new_buffer + 1 + sizeof(int), sizeof(newBytesAllocated)); if (DEBUG) { printf("Number of bytes allocated in second block excluding header: %d\n", (newBytesAllocated - HEADER_SIZE)); printf("Total number of bytes allocated from string of length 1500: %d\n", (bytesAllocated + newBytesAllocated - (2 * HEADER_SIZE))); } char longData[1501]; buffer_ptr = buffer + HEADER_SIZE; strncpy(longData, buffer_ptr, (bytesAllocated - HEADER_SIZE)); char * longData_ptr = longData + (bytesAllocated - HEADER_SIZE); char * new_buffer_ptr = new_buffer + HEADER_SIZE; strncpy(longData_ptr, new_buffer_ptr, (newBytesAllocated - HEADER_SIZE)); longData[1500] = '\0'; if (strcmp(longString, longData) != 0) { printf("Error in writing data\n"); exit(1); } char longerString[1601]; for (i = 0; i < 1600; i++) { if (i < 1500) { longerString[i] = longString[i]; } else { longerString[i] = string[(i - 1500)]; } } longerString[1600] = '\0'; if (DEBUG) { printf("Appending string of length 100 to open file %d\n", fd); } if (my_write(fd, string, 100) < 0) { printf("Failed to write string2 to /foo/bar/test.txt\n"); exit(1); } if (read_block(block_num, buffer) < 0) { printf("Error reading block #%d\n", block_num); exit(1); } buffer_ptr = buffer + 1; memcpy(&next_block, buffer_ptr, sizeof(next_block)); if (read_block(next_block, buffer) < 0) { printf("Error reading block #%d\n", block_num); exit(1); } buffer_ptr = buffer + 1 + sizeof(next_block); memcpy(&bytesAllocated, buffer_ptr, sizeof(bytesAllocated)); if (bytesAllocated != (newBytesAllocated + 100)) { printf("Error not enough bytes allocated\n"); exit(1); } if (DEBUG) { printf("bytes allocated in block appended too excluding header: %d\n", (bytesAllocated - HEADER_SIZE)); } buffer_ptr = buffer + newBytesAllocated; memset(data, '\0', 100); strncpy(data, buffer_ptr, 100); data[100] = '\0'; if (strcmp(data, string) != 0) { printf("Error in writing to already written to file that wasn't closed.\n"); exit(1); } if (DEBUG) { printf("Recursively removing directory /foo and all it's contents\n"); } if (my_rmdir("/foo") < 0) { printf("Failed at removing directory /foo\n"); exit(1); } block_num = get_associated_block_num(fd); if (block_num != 0) { printf("Error file still seen as open\n"); exit(1); } printf("Recursively deleted directory /foo\n"); printf("test_my_write: PASSED\n"); }
void dir_test(){ struct inode* root_inode; struct inode *rip; char string[DIRSIZ]; int number; int * inode_num = & number; int r; root_inode = get_inode(1); if(root_inode == NIL_INODE){ printf("root_inode is a null inode\n"); return; } /* printf("root_inode size is %d\n", root_inode->i_size); */ /* delete_dir(root_inode, 0); */ /* empty_inode_space(root_inode); */ /* put_inode(root_inode); */ /* return; */ /* my_strcpy(string, "usr"); */ /* if( r = search_dir(root_inode, string, inode_num, LOOK_UP) != OK){ */ /* printf("LOOK_UP a usr error: %s\n", strerror(r)); */ /* put_inode(root_inode); */ /* return; */ /* } */ /* printf("this time i node size is %d\n", root_inode->i_size); */ /* put_inode(root_inode); */ /* return; */ my_strcpy(string, ".."); *inode_num = root_inode->i_num; if (r = search_dir(root_inode, string, inode_num, ENTER) != OK){ printf("enter a item error: %s\n", strerror(r)); put_inode(root_inode); return; } my_strcpy(string, "."); *inode_num = root_inode->i_num; if (r = search_dir(root_inode, string, inode_num, ENTER) != OK){ printf("enter a item error: %s\n", strerror(r)); put_inode(root_inode); return; } root_inode->i_dirt = DIRTY; put_inode(root_inode); show_file_list(); /* return; */ printf("\n"); my_strcpy(string, "."); root_inode = get_inode(1); *inode_num = 0; if (r = search_dir(root_inode, string, inode_num, LOOK_UP) != OK){ printf("find a item error: %s\n", strerror(r)); put_inode(root_inode); return; } if(*inode_num != root_inode->i_num){ printf("can't find . in the root directory!!!\n"); put_inode(root_inode); return; } put_inode(root_inode); my_mkdir("./usr"); my_mkdir("src"); my_mkdir("../root"); show_file_list(); printf("\n"); root_inode = last_dir("./../usr", string); if(root_inode == NIL_INODE){ printf("last_dir error: can't find root dir\n"); return; } if(root_inode->i_num != 1){ printf("this time last dir is not root_inode, it's i_num is %d\n", root_inode->i_num); put_inode(root_inode); return; } printf("string remain is %s\n", string); show_file_list(); printf("\n"); rip = advance(root_inode, string); if(rip == NIL_INODE){ printf("advance error: can't find usr dir\n"); return; } show_file_list(); printf("\n"); put_inode(rip); put_inode(root_inode); if(my_rmdir("/src") != OK){ printf("can't remove dir src\n"); return; } show_file_list(); }