Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../lib/operations.h"
- #include <stddef.h>
- #include <stdint.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int get_index_parent_from_path(file_system *fs, char *path){
- printf("ANFANG GET INDEX PARENT\n");
- printf("PATH = %s", path);
- int index_current = fs->root_node; //fangen beim wurzel an
- printf("INDEX CURRENT = %d\n", index_current);
- char* path_temp = strdup(path);
- char* token = NULL;
- char* last_token = (char *)strrchr(path_temp, '/')+1;
- token = strtok(path_temp, "/");
- if(last_token == token) return 0;
- //bei pfad /root/home/folder/...
- while (token!=last_token){ //durchlaufen path
- printf("TOCKEN = %s\n", token);
- int flag = 0; //zeigt, ob wir das kind von current_node gefunden haben
- for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
- printf("i = %d\n", i);
- int block_num = fs->inodes[index_current].direct_blocks[i]; //schauen in direct_blocks von root
- printf("BLOCK_NUM = %d\n", block_num);
- printf("index_current = %d\n", index_current);
- if (block_num != -1){ //if block exist
- if(fs->inodes[block_num].n_type == directory){ //schauen nur auf verzeichnisse
- if (strcmp(fs->inodes[block_num].name, token) == 0){ // wenn inodes name = home
- printf("CHILD GEFUNDEN\n");
- index_current = block_num;
- flag = 1;
- token = strtok(NULL, "/");
- break; //for schleife beenden
- }
- }
- }
- else if(i>=DIRECT_BLOCKS_COUNT-1) flag = 0;
- }
- printf("WEITER GEHTS\n");
- if(flag != 1){ //es wurde in current directory kein kind gefunden
- index_current = -1;
- break; // etwas schief, raus aus while schleife
- }
- }
- printf("index_current = %d\n", index_current);
- free(path_temp);
- return index_current;
- }
- int set_kid(file_system* fs, int index_parent, int index_kind){
- //size of array direct_blocks
- for (int j = 0; j<DIRECT_BLOCKS_COUNT; j++){
- if (fs->inodes[index_parent].direct_blocks[j] == -1){ //no block
- fs->inodes[index_parent].direct_blocks[j] = index_kind;
- printf("......\n");
- return 0;
- }
- }
- return -1;
- }
- int find_free_datablock(file_system *fs){
- int block_num = -1;
- //data blocks in free list durchgehen
- for (int i = 0; i<fs->s_block->num_blocks; i++){
- if (fs->free_list[i] == 1){ //block free
- block_num = i;
- break; //free block mit geringstem index gefunden
- }
- }
- return block_num;
- }
- char write_in_datablock(file_system *fs, int block_num, char* text, int flag){ //flag zeigt, ob der block schon etwas enthaelt
- int size_text = strlen(text);
- int max_size_array = fs->s_block->free_blocks*BLOCK_SIZE; // anzahl freie bloecke * size eines blocks
- //fuer den nicht leeren block relevent, sonst null
- int size_first_block = fs->data_blocks[block_num].size;
- //wenn size_text > max_size_array return NULL
- if(size_text+size_first_block > max_size_array) return NULL;
- int a = size_text/max_size_array; //wie viele male passt text in block = vie viele bloecke man verwenden soll
- int b = size_text%max_size_array; //wie viele tokens werden in den letzten passenden block geschrieben
- if(!flag){ //block leer
- if(a < 1){ // text passt weniger als einmal in den block
- memcpy(fs->data_blocks[block_num].block, text, size_text); //text in data kopieren
- fs->data_blocks[block_num].size = size_text;
- fs->free_list[block_num] = 0; //block is jz besetzt
- }
- else if (a == 1 && b == 0){ //text passt genau ein mal
- memcpy(fs->data_blocks[block_num].block, text, size_text); //text in data kopieren
- fs->data_blocks[block_num].size = BLOCK_SIZE; //maximaler size fuer den block gesetzt
- fs->free_list[block_num] = 0; //block is jz besetzt
- }
- else{ //text muss in mehreren bloeken geschrieben werden
- char* text_temp[size_text]; // array
- strncpy(text_temp, text, size_text);
- char* part_text;
- int current_block_num;
- for (int i=0; i<a; i++){
- if(i!=0){ //befinden uns nicht im ersten block
- current_block_num = find_free_datablock(fs);
- if (current_block_num == -1) return NULL;
- }
- else current_block_num = block_num;
- if(i==a){ //befinden uns im letzten zu schreibenden block
- strncpy(part_text, text_temp[size_text-b],b);
- } else strncpy(part_text, text_temp[i*BLOCK_SIZE], BLOCK_SIZE); // kopieren den naechsten teil des textes in part text
- memcpy(fs->data_blocks[current_block_num].block, part_text, size_text); //text in data kopieren
- fs->data_blocks[current_block_num].size = BLOCK_SIZE; //maximaler size fuer den block gesetzt
- fs->free_list[current_block_num] = 0; //block is jz besetzt
- free(part_text);
- }
- }
- }
- //else{ //block nicht leer
- //}
- }
- int fs_mkdir(file_system *fs, char *path)
- {
- if (fs == NULL || path == NULL) return -1;
- if (path[0] != '/') return -1; //wenn path kein absoluter pfad ist
- int index_parent = get_index_parent_from_path(fs,path);
- if (index_parent == -1) return -1; //kein parent gefunden
- int index = find_free_inode(fs);
- if(index==-1) return -1; //es gibt keinen freien inode
- if(set_kid(fs, index_parent, index)) return -1;
- fs->inodes[index].n_type = directory; //type = directory
- fs->inodes[index].size = 0;
- strncpy(fs->inodes[index].name, strrchr(path, '/')+1, NAME_MAX_LENGTH);
- //printf("root index = %d\n", fs->root_node);
- //printf("index parent = %d\n", fs->root_node);
- fs->inodes[index].parent = index_parent;
- return 0; //wenn diese stelle erreicht alles gut
- }
- int
- fs_mkfile(file_system *fs, char *path_and_name)
- {
- if (fs == NULL || path_and_name == NULL){
- return -1;
- }
- if (path_and_name[0] != '/') return -1; //wenn path kein absoluter pfad ist
- int index_parent = get_index_parent_from_path(fs,path_and_name);
- if (index_parent == -1) return -1; //kein parent gefunden
- int index = find_free_inode(fs);
- if(index==-1) return -1; //es gibt keinen freien inode
- if(set_kid(fs, index_parent, index)) return -1;
- fs->inodes[index].n_type = reg_file; //type = directory
- fs->inodes[index].size = 0;
- strncpy(fs->inodes[index].name, strrchr(path_and_name, '/')+1, NAME_MAX_LENGTH);
- //printf("root index = %d\n", fs->root_node);
- //printf("index parent = %d\n", fs->root_node);
- fs->inodes[index].parent = index_parent;
- return 0; //wenn diese stelle erreicht alles gut
- }
- char *
- fs_list(file_system *fs, char *path)
- {
- if(fs == NULL || path == NULL) return NULL;
- int index_current = fs->root_node; //fangen beim wurzel an
- printf("INDEX CURRENT = %d\n", index_current);
- char* path_temp = strdup(path);
- char* token = strtok(path_temp, "/");
- //bei pfad /root/home/folder/...
- while (token!=NULL){ //durchlaufen path
- printf("TOCKEN = %s\n", token);
- int flag = 0; //zeigt, ob wir das kind von current_node gefunden haben
- for (int i=0; i<DIRECT_BLOCKS_COUNT; i++){
- printf("i = %d\n", i);
- int block_num = fs->inodes[index_current].direct_blocks[i]; //schauen in direct_blocks von root
- printf("BLOCK_NUM = %d\n", block_num);
- printf("index_current = %d\n", index_current);
- if (block_num != -1){ //if block exist
- if(fs->inodes[block_num].n_type == directory){ //schauen nur auf verzeichnisse
- if (strcmp(fs->inodes[block_num].name, token) == 0){ // wenn inodes name = home
- printf("CHILD GEFUNDEN\n");
- index_current = block_num;
- flag = 1;
- token = strtok(NULL, "/");
- break; //for schleife beenden
- }
- }
- }
- else if(i>=DIRECT_BLOCKS_COUNT-1) flag = 0;
- }
- printf("WEITER GEHTS\n");
- if(flag != 1){ //es wurde in current directory kein kind gefunden
- index_current = -1;
- return NULL; // etwas schief, raus aus while schleife
- }
- }
- char* str = (char*)calloc(1,sizeof(char));
- for (int j = 0; j<fs->s_block->num_blocks; j++){
- if(fs->inodes[j].parent == index_current){
- if(fs->inodes[j].n_type == directory){
- str = (char*)realloc(str,5+strlen(str)+strnlen(fs->inodes[j].name, NAME_MAX_LENGTH));
- strcat(str, "DIR ");
- strcat(str, fs->inodes[j].name);
- strcat(str, "\n");
- }
- else if(fs->inodes[j].n_type == reg_file){
- str = (char*)realloc(str,5+strlen(str)+strnlen(fs->inodes[j].name, NAME_MAX_LENGTH));
- strcat(str, "FIL ");
- strcat(str, fs->inodes[j].name);
- strcat(str, "\n");
- }
- }
- }
- return str;
- }
- int
- fs_writef(file_system *fs, char *filename, char *text)
- {
- if(fs == NULL || filename == NULL) return -1;
- char* filename_temp = strdup(filename);
- char* file_name = (char *)strrchr(filename_temp, '/')+1;
- int index_child;
- int index_parent = get_index_parent_from_path(fs, filename);
- if (index_parent == -1) return -1; //kein parent gefunden
- char* parent_name = fs->inodes[index_parent].name;
- int exist = 0; //zeigt, ob die datei existiert
- for (int i = 0; i < DIRECT_BLOCKS_COUNT; i++){
- int current_block_index = fs->inodes[index_parent].direct_blocks[i];
- if (current_block_index !=1){ //es gibt einen block, schauen rein
- if(strcmp(fs->inodes[current_block_index].name, file_name)){ //datei existiert
- index_child = current_block_index;
- exist = 1;
- }
- }
- }
- if(exist){ //datei existiert, kann leer oder nicht leer sein
- int flag = 0; //zeigt, ob in datei schon etwas liegt
- int block_num; //nummer blocks in den geschrieben wird
- for (int j = 0; j < DIRECT_BLOCKS_COUNT; j++){
- if (fs->inodes[index_child].direct_blocks[j] != -1){ //es gibt einen block schon - datei nicht leer
- flag = 1;
- block_num = fs->inodes[index_child].direct_blocks[j];
- if(write_in_datablock(fs, block_num, text,flag) == NULL) return -1; //schreiben in/ab diesen datablock je nachdem ob size uebershritten. falls NULL returned - fehler, return -1
- }
- }
- if (!flag){
- block_num = find_free_datablock(fs); //finden den kleinstmöglichen block
- if (block_num == -1) return -1; //wenn freier block nicht gefunden
- if(write_in_datablock(fs, block_num, text,flag) == NULL) return -1; //schreiben in/ab diesen datablock je nachdem ob size uebershritten. falls NULL returned - fehler, return -1
- }
- }
- else{ //wenn es keine datei gibt
- return -1; //die datei muss bereits existieren
- }
- }
- uint8_t *
- fs_readf(file_system *fs, char *filename, int *file_size)
- {
- return NULL;
- }
- int
- fs_rm(file_system *fs, char *path)
- {
- return -1;
- }
- int
- fs_import(file_system *fs, char *int_path, char *ext_path)
- {
- return -1;
- }
- int
- fs_export(file_system *fs, char *int_path, char *ext_path)
- {
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement