Advertisement
madegoff

sysprog2ha_nicht_fertig2

Jul 10th, 2023 (edited)
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.01 KB | None | 0 0
  1. #include "../lib/operations.h"
  2. #include <stddef.h>
  3. #include <stdint.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7.  
  8. int get_index_parent_from_path(file_system *fs, char *path){
  9.  
  10.     printf("ANFANG GET INDEX PARENT\n");
  11.     printf("PATH = %s", path);
  12.  
  13.     int index_current = fs->root_node; //fangen beim wurzel an
  14.     printf("INDEX CURRENT = %d\n", index_current);
  15.     char* path_temp = strdup(path);
  16.  
  17.     char* token = NULL;
  18.     char* last_token = (char *)strrchr(path_temp, '/')+1;
  19.     token = strtok(path_temp, "/");
  20.     if(last_token == token) return 0;
  21.  
  22.     //bei pfad /root/home/folder/...
  23.  
  24.     while (token!=last_token){ //durchlaufen path
  25.  
  26.         printf("TOCKEN = %s\n", token);
  27.  
  28.         int flag = 0; //zeigt, ob wir das kind von current_node gefunden haben
  29.  
  30.         for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
  31.  
  32.             printf("i = %d\n", i);
  33.  
  34.             int block_num = fs->inodes[index_current].direct_blocks[i]; //schauen in direct_blocks von root
  35.  
  36.             printf("BLOCK_NUM = %d\n", block_num);
  37.                 printf("index_current = %d\n", index_current);
  38.  
  39.             if (block_num != -1){ //if block exist
  40.                 if(fs->inodes[block_num].n_type == directory){ //schauen nur auf verzeichnisse
  41.  
  42.                     if (strcmp(fs->inodes[block_num].name, token) == 0){ // wenn inodes name = home
  43.  
  44.                         printf("CHILD GEFUNDEN\n");
  45.                         index_current = block_num;
  46.                         flag = 1;
  47.                         token = strtok(NULL, "/");
  48.                         break; //for schleife beenden
  49.                     }
  50.                 }
  51.             }
  52.             else if(i>=DIRECT_BLOCKS_COUNT-1) flag = 0;
  53.         }
  54.         printf("WEITER GEHTS\n");
  55.  
  56.         if(flag != 1){ //es wurde in current directory kein kind gefunden
  57.             index_current = -1;
  58.             break; // etwas schief, raus aus while schleife
  59.         }
  60.     }
  61.     printf("index_current = %d\n", index_current);
  62.     free(path_temp);
  63.     return index_current;
  64. }
  65.  
  66. int set_kid(file_system* fs, int index_parent, int index_kind){
  67.  
  68.     //size of array direct_blocks
  69.         for (int j = 0; j<DIRECT_BLOCKS_COUNT; j++){
  70.                 if (fs->inodes[index_parent].direct_blocks[j] == -1){ //no block
  71.                         fs->inodes[index_parent].direct_blocks[j] = index_kind;
  72.                         printf("......\n");
  73.                         return 0;
  74.                 }
  75.             }
  76.         return -1;
  77. }
  78.  
  79. int fs_mkdir(file_system *fs, char *path)
  80. {
  81.     if (fs == NULL || path == NULL) return -1;
  82.  
  83.     if (path[0] != '/') return -1; //wenn path kein absoluter pfad ist
  84.  
  85.     int index_parent = get_index_parent_from_path(fs,path);
  86.     if (index_parent == -1) return -1; //kein parent gefunden
  87.  
  88.     int index = find_free_inode(fs);
  89.  
  90.     if(index==-1) return -1; //es gibt keinen freien inode
  91.  
  92.     if(set_kid(fs, index_parent, index)) return -1;
  93.     fs->inodes[index].n_type = directory; //type = directory
  94.     fs->inodes[index].size = 0;
  95.     strncpy(fs->inodes[index].name, strrchr(path, '/')+1, NAME_MAX_LENGTH);
  96.  
  97.     //printf("root index = %d\n", fs->root_node);
  98.     //printf("index parent = %d\n", fs->root_node);
  99.  
  100.     fs->inodes[index].parent = index_parent;
  101.  
  102.     return 0; //wenn diese stelle erreicht alles gut
  103. }
  104.  
  105. int
  106. fs_mkfile(file_system *fs, char *path_and_name)
  107. {
  108.     if (fs == NULL || path_and_name == NULL){
  109.         return -1;
  110.     }
  111.  
  112.     if (path_and_name[0] != '/') return -1; //wenn path kein absoluter pfad ist
  113.  
  114.     int index_parent = get_index_parent_from_path(fs,path_and_name);
  115.     if (index_parent == -1) return -1; //kein parent gefunden
  116.  
  117.     int index = find_free_inode(fs);
  118.  
  119.     if(index==-1) return -1; //es gibt keinen freien inode
  120.  
  121.     if(set_kid(fs, index_parent, index)) return -1;
  122.     fs->inodes[index].n_type = reg_file; //type = directory
  123.     fs->inodes[index].size = 0;
  124.     strncpy(fs->inodes[index].name, strrchr(path_and_name, '/')+1, NAME_MAX_LENGTH);
  125.  
  126.     //printf("root index = %d\n", fs->root_node);
  127.     //printf("index parent = %d\n", fs->root_node);
  128.  
  129.     fs->inodes[index].parent = index_parent;
  130.  
  131.     return 0; //wenn diese stelle erreicht alles gut
  132. }
  133.  
  134. char *
  135. fs_list(file_system *fs, char *path)
  136. {
  137.     if(fs == NULL || path == NULL) return NULL;
  138.  
  139.     int index_current = fs->root_node; //fangen beim wurzel an
  140.     printf("INDEX CURRENT = %d\n", index_current);
  141.     char* path_temp = strdup(path);
  142.     char* token = strtok(path_temp, "/");
  143.  
  144.     //bei pfad /root/home/folder/...
  145.  
  146.     while (token!=NULL){ //durchlaufen path
  147.  
  148.         printf("TOCKEN = %s\n", token);
  149.  
  150.         int flag = 0; //zeigt, ob wir das kind von current_node gefunden haben
  151.  
  152.         for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
  153.  
  154.             printf("i = %d\n", i);
  155.  
  156.             int block_num = fs->inodes[index_current].direct_blocks[i]; //schauen in direct_blocks von root
  157.  
  158.             printf("BLOCK_NUM = %d\n", block_num);
  159.                 printf("index_current = %d\n", index_current);
  160.  
  161.             if (block_num != -1){ //if block exist
  162.                 if(fs->inodes[block_num].n_type == directory){ //schauen nur auf verzeichnisse
  163.  
  164.                     if (strcmp(fs->inodes[block_num].name, token) == 0){ // wenn inodes name = home
  165.  
  166.                         printf("CHILD GEFUNDEN\n");
  167.                         index_current = block_num;
  168.                         flag = 1;
  169.                         token = strtok(NULL, "/");
  170.                         break; //for schleife beenden
  171.                     }
  172.                 }
  173.             }
  174.             else if(i>=DIRECT_BLOCKS_COUNT-1) flag = 0;
  175.         }
  176.         printf("WEITER GEHTS\n");
  177.  
  178.         if(flag != 1){ //es wurde in current directory kein kind gefunden
  179.             index_current = -1;
  180.  
  181.             return NULL; // etwas schief, raus aus while schleife
  182.         }
  183.     }
  184.     char* str = (char*)calloc(1,sizeof(char));
  185.     for (int j = 0; j<fs->s_block->num_blocks; j++){
  186.  
  187.         if(fs->inodes[j].parent == index_current){
  188.             if(fs->inodes[j].n_type == directory){
  189.                 str = (char*)realloc(str,5+strlen(str)+strnlen(fs->inodes[j].name, NAME_MAX_LENGTH));
  190.                 strcat(str, "DIR ");
  191.                 strcat(str, fs->inodes[j].name);
  192.                 strcat(str, "\n");
  193.             }
  194.             else if(fs->inodes[j].n_type == reg_file){
  195.                 str = (char*)realloc(str,5+strlen(str)+strnlen(fs->inodes[j].name, NAME_MAX_LENGTH));
  196.                 strcat(str, "FIL ");
  197.                 strcat(str, fs->inodes[j].name);
  198.                 strcat(str, "\n");
  199.             }
  200.         }
  201.     }
  202.     return str;
  203. }
  204.  
  205. int
  206. fs_writef(file_system *fs, char *filename, char *text)
  207. {
  208.     return -1;
  209. }
  210.  
  211. uint8_t *
  212. fs_readf(file_system *fs, char *filename, int *file_size)
  213. {
  214.     return NULL;
  215. }
  216.  
  217.  
  218. int
  219. fs_rm(file_system *fs, char *path)
  220. {
  221.     return -1;
  222. }
  223.  
  224. int
  225. fs_import(file_system *fs, char *int_path, char *ext_path)
  226. {
  227.     return -1;
  228. }
  229.  
  230. int
  231. fs_export(file_system *fs, char *int_path, char *ext_path)
  232. {
  233.     return -1;
  234. }
  235.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement