Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int cfs_create_file_root(char f_name[MAX_NAME_LEN], int file_idx_ptrs[], unsigned char content[]){
- int free_block = disk_free_block();
- int free_idx_pos = cfs_rbf_get_available_index();
- unsigned long content_size = strlen(content);
- unsigned long name_size = strlen(f_name);
- if(name_size < MAX_NAME_LEN){
- f_name[MAX_NAME_LEN] = '\0';
- }
- int blocks_to_use;
- if(content_size > (BLOCK_SIZE - MAX_NAME_LEN)){
- double ceil_param = (content_size - (BLOCK_SIZE - MAX_NAME_LEN))/(double)BLOCK_SIZE;
- blocks_to_use = (int)(ceil(ceil_param)) + 1;
- }
- else{
- blocks_to_use = 1;
- }
- int index_block[BLOCK_SIZE/INT_SIZE];
- cfs_any_initialize_array(index_block, BLOCK_SIZE/INT_SIZE);
- file_idx_ptrs[free_idx_pos] = free_block;
- disk_update_bitmap(free_block);
- cfs_to_file();
- int next_free_block;
- for(int index = 0; index < blocks_to_use; index++){
- next_free_block = disk_free_block();
- index_block[index] = next_free_block;
- disk_update_bitmap(next_free_block);
- }
- i_disk_write_block(index_block, free_block);
- //Writing first block
- int first_block = index_block[0];
- struct file_start fs;
- strncpy(fs.file_name, f_name, MAX_NAME_LEN);
- strncpy(fs.content, content, BLOCK_SIZE - MAX_NAME_LEN);
- f_disk_write_block(fs, first_block);
- if(content_size > (BLOCK_SIZE - MAX_NAME_LEN)){
- int next_block;
- int index = BLOCK_SIZE - MAX_NAME_LEN;
- int counter = 0;
- int next_counter = 1;
- unsigned char next_content[BLOCK_SIZE];
- if(content_size < blocks_to_use*BLOCK_SIZE){
- content_size = blocks_to_use*BLOCK_SIZE - MAX_NAME_LEN;
- }
- while(index <= content_size){
- if(counter == BLOCK_SIZE){
- next_content[BLOCK_SIZE] = '\0';
- next_block = index_block[next_counter];
- disk_write_block(next_content, next_block);
- next_counter++;
- counter = 0;
- }
- else{
- next_content[counter] = content[index];
- index++;
- counter++;
- }
- }
- }
- return content_size;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement