Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.25 KB | None | 0 0
  1. int cfs_create_file_root(char f_name[MAX_NAME_LEN], int file_idx_ptrs[], unsigned char content[]){
  2.     int free_block = disk_free_block();
  3.     int free_idx_pos = cfs_rbf_get_available_index();
  4.     unsigned long content_size = strlen(content);
  5.     unsigned long name_size = strlen(f_name);
  6.  
  7.     if(name_size < MAX_NAME_LEN){
  8.         f_name[MAX_NAME_LEN] = '\0';
  9.     }
  10.  
  11.     int blocks_to_use;
  12.    
  13.     if(content_size > (BLOCK_SIZE - MAX_NAME_LEN)){
  14.         double ceil_param = (content_size - (BLOCK_SIZE - MAX_NAME_LEN))/(double)BLOCK_SIZE;
  15.         blocks_to_use = (int)(ceil(ceil_param)) + 1;
  16.     }
  17.     else{
  18.         blocks_to_use = 1;
  19.     }
  20.    
  21.     int index_block[BLOCK_SIZE/INT_SIZE];
  22.     cfs_any_initialize_array(index_block, BLOCK_SIZE/INT_SIZE);
  23.  
  24.     file_idx_ptrs[free_idx_pos] = free_block;
  25.     disk_update_bitmap(free_block);
  26.     cfs_to_file();
  27.     int next_free_block;
  28.     for(int index = 0; index < blocks_to_use; index++){
  29.         next_free_block = disk_free_block();
  30.         index_block[index] = next_free_block;
  31.         disk_update_bitmap(next_free_block);
  32.     }
  33.     i_disk_write_block(index_block, free_block);
  34.     //Writing first block
  35.     int first_block = index_block[0];
  36.  
  37.     struct file_start fs;
  38.     strncpy(fs.file_name, f_name, MAX_NAME_LEN);
  39.     strncpy(fs.content, content, BLOCK_SIZE - MAX_NAME_LEN);
  40.  
  41.     f_disk_write_block(fs, first_block);
  42.  
  43.     if(content_size > (BLOCK_SIZE - MAX_NAME_LEN)){
  44.         int next_block;
  45.        
  46.         int index = BLOCK_SIZE - MAX_NAME_LEN;
  47.         int counter = 0;
  48.         int next_counter = 1;
  49.         unsigned char next_content[BLOCK_SIZE];
  50.         if(content_size < blocks_to_use*BLOCK_SIZE){
  51.             content_size = blocks_to_use*BLOCK_SIZE - MAX_NAME_LEN;
  52.         }
  53.         while(index <= content_size){
  54.             if(counter == BLOCK_SIZE){
  55.                 next_content[BLOCK_SIZE] = '\0';
  56.                 next_block = index_block[next_counter];
  57.                 disk_write_block(next_content, next_block);
  58.                 next_counter++;
  59.                 counter = 0;
  60.             }
  61.             else{
  62.                 next_content[counter] = content[index];
  63.                 index++;
  64.                 counter++;
  65.             }
  66.         }
  67.     }
  68.     return content_size;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement