Advertisement
madegoff

get_index_parent

Jul 12th, 2023 (edited)
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.06 KB | None | 0 0
  1. int get_index_parent_from_path(file_system *fs, char *path){
  2.  
  3.     //printf("ANFANG GET INDEX PARENT\n");
  4.     //printf("PATH = %s\n", path);
  5.  
  6.     int index_current = fs->root_node; //fangen beim wurzel an
  7.     //printf("INDEX CURRENT = %d\n", index_current);
  8.     char* path_temp = strdup(path);
  9.  
  10.     char* token = NULL;
  11.     char* last_token = (char *)strrchr(path_temp, '/')+1;
  12.     //printf("last_token = %s\n", last_token);
  13.     token = strtok(path_temp, "/");
  14.     //printf("token = %s\n", token);
  15.     if(last_token == token) {
  16.             //printf("gleich, return 0\n");
  17.             return 0;
  18.     }
  19.     //bei pfad /root/home/folder/...
  20.  
  21.     while (token!=last_token){ //durchlaufen path
  22.  
  23.         printf("TOCKEN = %s\n", token);
  24.  
  25.         int flag = 0; //zeigt, ob wir das kind von current_node gefunden haben
  26.  
  27.         for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
  28.  
  29.             printf("i = %d\n", i);
  30.  
  31.             int block_num = fs->inodes[index_current].direct_blocks[i]; //schauen in direct_blocks von root
  32.  
  33.             printf("BLOCK_NUM = %d\n", block_num);
  34.                 printf("index_current = %d\n", index_current);
  35.  
  36.             if (block_num != -1){ //if block exist
  37.                 if(fs->inodes[block_num].n_type == directory){ //schauen nur auf verzeichnisse
  38.  
  39.                     if (strcmp(fs->inodes[block_num].name, token) == 0){ // wenn inodes name = home
  40.  
  41.                         printf("CHILD GEFUNDEN\n");
  42.                         index_current = block_num;
  43.                         flag = 1;
  44.                         token = strtok(NULL, "/");
  45.                         break; //for schleife beenden
  46.                     }
  47.                 }
  48.             }
  49.             else if(i>=DIRECT_BLOCKS_COUNT-1) flag = 0;
  50.         }
  51.         printf("WEITER GEHTS\n");
  52.  
  53.         if(flag != 1){ //es wurde in current directory kein kind gefunden
  54.             index_current = -1;
  55.             break; // etwas schief, raus aus while schleife
  56.         }
  57.     }
  58.     printf("index_current = %d\n", index_current);
  59.     free(path_temp);
  60.     return index_current;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement