bool test_file_directory_read_3 (Test *test)
{
	char *path;
	Directory *directory;
	List *directories;
	List *files;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/1d_1f"));
        /*
                d stage/1d_1f
                f f1
                d stage/1d_1f/d1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 1);
	CATCH (directory->files->count != 1);
	directories = directory->directories;
	files = directory->files;
	CATCH (!directory_read (directory));
	CATCH (directories != directory->directories);
	CATCH (files != directory->files);
	directory_close (directory);
	PASS ();
}
bool test_file_directory_find (Test *test)
{
	char *path;
	Directory *directory;
	Directory *found_directory;
	File *found_file;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/find"));
        /*
                d stage/find
                f f1
                d stage/find/d1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 1);
	CATCH (directory->files->count != 1);
	CATCH (!(found_directory = directory_find_directory (directory, "d1")));
	CATCH (!string_equals (found_directory->name, "d1"));
	CATCH (directory_find_directory (directory, "d2"));
	CATCH (!(found_file = directory_find_file (directory, "f1")));
	CATCH (!string_equals (found_file->name, "f1"));
	CATCH (directory_find_file (directory, "f2"));
	directory_close (directory);
	PASS ();
}
bool test_file_directory_read_4 (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/9d_9f"));
        /*
                d stage/9d_9f/d8
                d stage/9d_9f/d7
                d stage/9d_9f/d1
                d stage/9d_9f/d4
                d stage/9d_9f/d9
                d stage/9d_9f/d2
                d stage/9d_9f/d5
                d stage/9d_9f/d3
                d stage/9d_9f/d6
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	CATCH (directory->directories->count != 9);
	CATCH (directory->files->count != 9);
	directory_close (directory);
	PASS ();
}
bool test_file_directory_read_invalid_argument (Test *test)
{
	TITLE ();
	CATCH (directory_read (NULL));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	PASS ();
}
bool test_file_directory_read_6 (Test *test)
{
	char *path;
	Directory *d3_1f;
	Directory *d1;
	Directory *d2;
	Directory *d3;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/d3_1f"));
        /*
                d stage/d3_1f
                d stage/d3_1f/d1
                f f1
                d stage/d3_1f/d1/d2
                d stage/d3_1f/d1/d2/d3
                f f1
         */
	CATCH (!(d3_1f = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (d3_1f));
	CATCH (!d3_1f->directories);
	CATCH (d3_1f->directories->count != 1);
	CATCH (!(d1 = list_first (d3_1f->directories)->data));
	CATCH (!directory_read (d1));
	CATCH (d1->files->count != 1);
	CATCH (d1->directories->count != 1);
	CATCH (!(d2 = list_first (d1->directories)->data));
	CATCH (!directory_read (d2));
	CATCH (d2->directories->count != 1);
	CATCH (!(d3 = list_first (d2->directories)->data));
	CATCH (!directory_read (d3));
	CATCH (d3->files->count != 1);
	directory_close (d3_1f);
	PASS ();
}
bool test_file_directory_read_1 (Test *test)
{
	char *path;
	Directory *directory;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/empty"));
        /* d stage/empty */
	CATCH (!(directory = directory_open (path)));
	CATCH (!directory_read (directory));
	CATCH (!directory->directories);
	CATCH (!directory->files);
	directory_close (directory);
	string_destroy (path);
	PASS ();
}
bool test_file_close (Test *test)
{
	Directory *directory;
	File *file;
	char *path;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/open"));
        /*
                d stage/open
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f1")));
	file_close (file);
	directory_close (directory);
	PASS ();
}
bool test_file_readline_f2 (Test *test)
{
	Directory *directory;
	File *file;
	char *path;
	char *line;
	size_t bytes_read;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/readline"));
        /*
                d stage/readline
                f f2
                f f3 \
                         \
                        0 \
                        AB \
                        012 \
                        ABCD \
                        01234 \
                        ABCD \
                        012 \
                        AB \
                        0
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f2")));
	CATCH (!file_open (file));
	CATCH (!(line = string_create_with_size (1)));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	CATCH (memory_size (line) != 1);
	directory_close (directory);
	string_destroy (line);
	PASS ();
}
bool test_file_open_fail (Test *test)
{
	Directory *directory;
	File *file;
	char *path;

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/open_fail"));
        /* stage does not exist */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	CATCH (!directory_read (directory));
	CATCH (!(file = directory_find_file (directory, "f1")));
	CATCH (file_open (file));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorSystemCall);
	error_reset ();
	CATCH (file_open (NULL));
	CATCH (error_count () != 1);
	CATCH (error_at (0).error != ErrorInvalidArgument);
	directory_close (directory);
	PASS ();
}
Beispiel #10
0
int main(int argc, char *argv[]){
    FILE *file;
    if (argc < 2) {
        /* read input */
        /* give error message and exit */
        fprintf(stderr, "Must pass an argument!\n");
        exit(1);
    }
    file = fopen(argv[1],"rb"); // open file
    //FILE *file = fopen("fat_volume.dat","rb");
    if (file==NULL) {
        printf("No such a file");
        exit(0);
    }
    printf("File found   ");
    fseek(file, 0L, SEEK_END);
    long buff_size = ftell(file);
    
    fseek(file, 0L, SEEK_SET);
    char *buf = malloc(buff_size+1);// create a buffer
    
    fread(buf,buff_size,1,file);
    
    long boot_off = 0;
    char* sec_buf = malloc(512);
    sec_read(file, boot_off,sec_buf); // read boot and store it in boot_sect_t
    boot_sect_t* boot = boot_read(sec_buf);
    free(sec_buf);
    ssize = (boot->ssize[1]<<8|boot->ssize[0]); //its sector size.
    printf("Sector Size: %i\n",ssize);
    csize = boot->csize;
    printf("cluster size in sectors: %i\n",boot->csize);
    int reserved = (boot->reserved[1]<<8|boot->reserved[0]);//the number of reserved sectors on the disk.
    printf("The number of reserved sctor: %i\n",reserved);
    printf("The number of FAT copies: %i\n",boot->numfat);
    int numroot = (boot->numroot[1] <<8|boot->numroot[0] ); //the number of entries in its root directory
    printf("The number of entries in its root directory: %i\n",numroot);
    int sectors16 = (boot->sectors16[1]<<8|boot->sectors16[0]);//Total number of sectors in the filesystem
                                                               //printf("The total number of sectors in filesystem: %i\n",sectors16);
    int sectperfat16 = (boot->sectperfat16[1]<<8|boot->sectperfat16[0]);
    printf("The number of sectors in FAT: %i\n",sectperfat16);
    int sectpertrack = (boot->sectpertrack[1]<<8|boot->sectpertrack[0]);
    int heads = (boot->heads[1]<<8|boot->heads[0]);
    int num_hidden_sec = (boot->prevsect[1]<<8|boot->prevsect[0]); //Number of hidden sectors
    printf("The number of hidden sectors on the disk: %i\n",num_hidden_sec);
    sec_num_first_FAT = 0+reserved;
    printf("The sector number of the first copy of FAT: %i\n",sec_num_first_FAT);
    sec_num_first_root = reserved+(boot->numfat*sectperfat16);
    printf("The sector number of the first sector of root dictory: %i\n",sec_num_first_root);
    sec_num_first_data = ((numroot*32)/ssize)+sec_num_first_root;
    printf("The sector number of the first sector of data: %i\n",sec_num_first_data);
    // printf("Data from file:\n%u",buf[sec_num_first_data] );
    /***************************************************************************/
    //end of boot sec
    long cur_buf_pos = (sec_num_first_root)*ssize;
    //printf("%c\n",buf[cur_buf_pos]);
    directory_read(file,cur_buf_pos, 0);
    
    
    fclose(file); // close file
                  //free(buf);
    
}
Beispiel #11
0
void directory_read(FILE *file, long cur_buf_pos, int count_3){
    // printf("current cur_buf_pos%d\n",cur_buf_pos);
    int DIRECTORY_SIZE  = 32;
    int i;
    char* buf_ = malloc(512);
    sec_read(file, cur_buf_pos,buf_);
    int curpos = cur_buf_pos;
        // read 512 bytes each time
        int j;
        for (j = 0; j<16; j++) {
            cur_buf_pos = j * 32;
            // if first byte of directory is 0 means end of file
            if(buf_[cur_buf_pos] == 0){
                free(buf_);
                return;
            }
            root_directory* rd = malloc(sizeof(root_directory));
            rd->file_name[0] = buf_[cur_buf_pos];
            rd->file_name[1] = buf_[cur_buf_pos+1];
            rd->file_name[2] = buf_[cur_buf_pos+2];
            rd->file_name[3] = buf_[cur_buf_pos+3];
            rd->file_name[4] = buf_[cur_buf_pos+4];
            rd->file_name[5] = buf_[cur_buf_pos+5];
            rd->file_name[6] = buf_[cur_buf_pos+6];
            rd->file_name[7] = buf_[cur_buf_pos+7];
            rd->ext[0] = buf_[cur_buf_pos+8];
            rd->ext[1] = buf_[cur_buf_pos+9];
            rd->ext[2] = buf_[cur_buf_pos+10];
            rd->attri = buf_[cur_buf_pos+11];
            rd->start_cluster [0] = buf_[cur_buf_pos+26];
            rd->start_cluster [1] = buf_[cur_buf_pos+27];
            rd->file_size[0] = buf_[cur_buf_pos+28];
            rd->file_size[1] = buf_[cur_buf_pos+29];
            rd->file_size[2] = buf_[cur_buf_pos+30];
            rd->file_size[3] = buf_[cur_buf_pos+31];
            //cur_buf_pos = cur_buf_pos + DIRECTORY_SIZE;
            char first = rd->file_name[0];
            int start_cluster;
            if(first!=229){
                char * name_buf;
                start_cluster = print_directory(rd, name_buf);
            }
            else{
                  break;
            }
            uint8_t attri = rd->attri;
            free(rd);
            
            if ((attri & 0x10)&&(start_cluster>0)){
                // if has subdirectory, go read FAT
                int* clusters = FAT_read(start_cluster, file);//FAT_read return an array of clusters
                                                              //int count_2 = 0;
                count_3 = 0;
                    for (count_3; count_3<10; count_3++) {
                        // condition: if a valid cluster number and not subdirectory 
                        if ((clusters[count_3] >= 2)&&(clusters[count_3]<4095)&&first!='.'){
                        long  new_pos = ((clusters[count_3] - 2) * csize )*ssize + (sec_num_first_data * ssize);
                            directory_read(file, new_pos,count_3+1);
                        }else{
                        }
                    }
            
            }else{
                
            }

        }
    // }
    
}
Beispiel #12
0
bool test_file_readline_f3 (Test *test)
{
	Directory *directory;
	File *file;
	char *path;
	char *line;
	size_t bytes_read;
	char *check[] = { 
		"",
		"0",
		"AB",
		"012",
		"ABCD",
		"01234",
		"ABCD",
		"012",
		"AB",
		"0",
	};

	TITLE ();
	CATCH (!(path = directory_current_path ()));
	CATCH (!string_append (&path, "/stage/readline"));
        /*
                d stage/readline
                f f2
                f f3 \
                         \
                        0 \
                        AB \
                        012 \
                        ABCD \
                        01234 \
                        ABCD \
                        012 \
                        AB \
                        0
                f f1
         */
	CATCH (!(directory = directory_open (path)));
	string_destroy (path);
	directory_read (directory);
	CATCH (!(file = directory_find_file (directory, "f3")));
	CATCH (!file_open (file));
	CATCH (!(line = string_create_with_size (5)));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 1);
	CATCH (!string_equals (line, check[0]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 2);
	CATCH (!string_equals (line, check[1]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 3);
	CATCH (!string_equals (line, check[2]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 4);
	CATCH (!string_equals (line, check[3]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, check[4]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, "0123"));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 5);
	CATCH (!string_equals (line, check[6]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 4);
	CATCH (!string_equals (line, check[7]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 3);
	CATCH (!string_equals (line, check[8]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 2);
	CATCH (!string_equals (line, check[9]));
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	CATCH (!file_readline (file, line, &bytes_read));
	CATCH (bytes_read != 0);
	directory_close (directory);
	string_destroy (line);
	PASS ();
}
bool compile_project_prepare (CompileProject *project)
{
	ListNode *node;
	Directory *sub_directory;
	Compile *compile;
	Compile *sub_compile;
	TreeIterator *iterator;

	if (!project) {
                error (InvalidArgument);
		return false;
	}
	if (!directory_read (project->directory)) {
		compile_print ("Failed to read directory: %s\n", project->directory->name);
                error_code (FunctionCall, 1);
		return false;
	}
	for (node = list_first (project->directory->directories); node; node = list_next (node)) {
		if (!(sub_directory = node->data)) {
                        error_code (InvalidOperation, 1);
			return false;
		}
		if (!string_begins_with (sub_directory->name, "lib.") &&
		    !string_begins_with (sub_directory->name, "app.") &&
                    !string_begins_with (sub_directory->name, "plugin.")) {
			continue;
		}
		if (!(compile = compile_create (project->directory, sub_directory))) {
                        error_code (FunctionCall, 2);
			return false;
		}
		if (!list_append (project->nodes, compile)) {
			compile_destroy (compile);
                        error_code (FunctionCall, 3);
			return false;
		}
		if (!topological_add_vertex (project->topological, (Object *)compile)) {
                        error_code (FunctionCall, 4);
			return false;
		}
		if (!tree_insert (project->directory_to_compile, (Object *)sub_directory, compile)) {
                        error_code (FunctionCall, 5);
			return false;
		}
	}
	for (node = list_first (project->directory->directories); node; node = list_next (node)) {
		if (!(sub_directory = node->data)) {
                        error_code (InvalidOperation, 3);
			return false;
		}
                if (!string_begins_with (sub_directory->name, "lib.") &&
		    !string_begins_with (sub_directory->name, "app.") &&
                    !string_begins_with (sub_directory->name, "plugin.")) {
			continue;
		}
		if (!(compile = tree_search (project->directory_to_compile, 
                                             (Object *)sub_directory))) {
                        error_code (InvalidOperation, 5);
			return false;
		}
		if (!compile_prepare (compile)) {
                        error_code (FunctionCall, 6);
			return false;
		}
		if (!(iterator = tree_iterator_create (compile->libraries))) {
                        error_code (FunctionCall, 7);
			return false;
		}
		while (tree_iterator_next (iterator)) {
			if (string_equals (sub_directory->name, 
                                           ((Directory *)iterator->key)->name)) {
				continue;
			}
			if (!(sub_compile = tree_search (project->directory_to_compile, 
                                                         iterator->key))) {
				tree_iterator_destroy (iterator);
                                error_code (InvalidOperation, 6);
				return false;
			}
			if (!topological_set_edge (project->topological, 
                                                   (Object *)compile, 
                                                   (Object *)sub_compile)) {
				tree_iterator_destroy (iterator);
				error_code (FunctionCall, 8);
				return false;
			}
		}
		tree_iterator_destroy (iterator);
	}
	if (!(project->sorted = topological_sort (project->topological))) {
		compile_print ("Topological sort of project directories failed.\n");
                error_code (InvalidOperation, 7);        
		return false;
	}
	for (node = list_first (project->sorted); node; node = list_next (node)) {
		if (!recursively_flatten_libraries (project, node->data)) {
			return false;
		}
		if (!sort_libraries (project, node->data)) {
			return false;
		}
	}
	for (node = list_last (project->sorted); node; node = list_previous (node)) {
		if (!compile_actions (node->data, project->directory->path)) {
                        error_code (FunctionCall, 9);
			return false;
		}
	}
	return true;
}