Advertisement
madegoff

sysprog2ha_nicht_fertig3

Jul 8th, 2023 (edited)
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 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. char* strannocopy(char* dst, const char* src, size_t nmax) {
  9. size_t i;
  10. for(i = 0; i < nmax; ++i) {
  11. if (src[i] == 0) break;
  12. dst[i] = src[i];
  13. printf("element = ");
  14. printf(dst[i]);
  15. printf("\n");
  16. }
  17. while(i < nmax) {
  18. dst[i] = 0;
  19. ++i;
  20. }
  21. return dst;
  22. }
  23.  
  24. int get_index_parent_from_path(file_system *fs, char *path){
  25.  
  26.     int index_kid;
  27.     int index_parent = fs->root_node; //fangen beim wurzel an
  28.     char* path_temp[strlen(path)+1];
  29.     strcpy(path_temp, path);
  30.  
  31.     char* parent_name = NULL;
  32.     char* child_name = NULL;
  33.  
  34.     child_name = strtok(path_temp, "/");
  35.  
  36.     //bei pfad root/home/folder/...
  37.  
  38.     while (child_name!=NULL){
  39.  
  40.         parent_name = child_name;
  41.         child_name = strtok(NULL, "/");
  42.  
  43.         // parent_name = root
  44.         // child_name = home
  45.  
  46.         for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
  47.             int block = fs->inodes[index_parent].direct_blocks[i]; //schauen in direct_blocks von root
  48.             if (block != -1){ //if block exist
  49.  
  50.                 if(fs->inodes[block].n_type == directory){ //schauen nur auf verzeichnisse
  51.                     if (fs->inodes[block].name == child_name){ // wenn inodes name = home
  52.                         index_kid = block;
  53.                         break; //for schleife beenden
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.         char* temp = strtok(NULL, "/");
  59.         if (temp != NULL) //übernächstes verzeichnis existiert
  60.         {
  61.             index_parent = index_kid;
  62.         }
  63.     }
  64.  
  65.     return index_parent;
  66. }
  67.  
  68. void set_kid(file_system* fs, int index_parent, int index_kind){
  69.  
  70.     //size of array direct_blocks
  71.     //int length = sizeof(fs->inodes[index_parent].direct_blocks)/ sizeof(fs->inodes[index_parent].direct_blocks[0]);
  72.         for (int j = 0; j<DIRECT_BLOCKS_COUNT; j++){
  73.                 if (fs->inodes[index_parent].direct_blocks[j] == -1){ //no block
  74.                         fs->inodes[index_parent].direct_blocks[j] = index_kind;
  75.                         break;
  76.                 }
  77.             }
  78. }
  79.  
  80. int fs_mkdir(file_system *fs, char *path)
  81. {
  82.     if (fs == NULL || path == NULL){
  83.         return -1;
  84.     }
  85.  
  86.     int index = -1;
  87.  
  88.     //inode finden
  89.     index = find_free_inode(fs);
  90.  
  91.     if (index == -1){
  92.         return -1; //kein free inode gefunden
  93.     }
  94.  
  95.     //bestimmen parameter für diesen inode
  96.     fs->inodes[index].n_type = directory; //type = directory
  97.     //fs->inodes[index].name = (char *) malloc(NAME_MAX_LENGTH);
  98.     printf(strrchr(path, '/'));
  99.     strannocopy(fs->inodes[index].name, strrchr(path, '/')+1, NAME_MAX_LENGTH);
  100.     //strncpy(fs->inodes[index].name, (char *) strrchr(path, '/') + 1, NAME_MAX_LENGTH); //name setzen SEGMENTATION FAULT??
  101.     int index_parent = get_index_parent_from_path(fs, path);
  102.     fs->inodes[index].parent = index_parent; //parent setzen
  103.  
  104.     //index_kid in parent.direct_blocks hinzufuegen
  105.     set_kid(fs, index_parent, index);
  106.  
  107.     return 0; //wenn diese stelle erreicht alles gut
  108. }
  109.  
  110. int
  111. fs_mkfile(file_system *fs, char *path_and_name)
  112. {
  113.     return -1;
  114. }
  115.  
  116. char *
  117. fs_list(file_system *fs, char *path)
  118. {
  119.     return NULL;
  120. }
  121.  
  122. int
  123. fs_writef(file_system *fs, char *filename, char *text)
  124. {
  125.     return -1;
  126. }
  127.  
  128. uint8_t *
  129. fs_readf(file_system *fs, char *filename, int *file_size)
  130. {
  131.     return NULL;
  132. }
  133.  
  134.  
  135. int
  136. fs_rm(file_system *fs, char *path)
  137. {
  138.     return -1;
  139. }
  140.  
  141. int
  142. fs_import(file_system *fs, char *int_path, char *ext_path)
  143. {
  144.     return -1;
  145. }
  146.  
  147. int
  148. fs_export(file_system *fs, char *int_path, char *ext_path)
  149. {
  150.     return -1;
  151. }
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement