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 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)
- {
- return -1;
- }
- 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