static char *dir_path(struct dir_info *parent, const char *name) { char *parent_path; char *path; if (!parent) return cat_paths(tests_file_system_mount_dir, name); parent_path = dir_path(parent->parent, parent->name); path = cat_paths(parent_path, name); free(parent_path); return path; }
int work_directory(char * dir_name, dummy_dir * dir_st, char * root_name) { struct dirent *dp = NULL; LOG("[enter]Ptr is %x\n", (unsigned int) dir_st); LOG("[enter]DirName is %s\n", dir_name); // List subdirs and files // Get count; alocate accordingly int dir_c = dir_count( dir_name ); dir_st->subdirs_count = dir_c; LOG_S("Parsing directories\n"); if( dir_c == 0 ) { dir_st->subdirs = NULL; } else { dir_st->subdirs = malloc(sizeof(dummy_dir) * dir_c); DIR *dir = opendir(dir_name); int i = 0; while( (dp = readdir(dir)) != NULL ) { if(dp->d_type != DT_DIR || !strcmp(dp->d_name, ".") || !strcmp(dp->d_name, "..")) continue; // Allocate and call recursively (for each dir) dummy_dir * new_child = dir_st->subdirs + i++; LOG("Ptr is %x\n" , (unsigned int)new_child); LOG("Working directory %s\n", dp->d_name); new_child->parent = dir_st; // Init name here, its easier than decomposing the full path in the recursive call. init( &new_child->name ); LOG("Before adding dir %s\n", dp->d_name); write( &new_child->name, dp->d_name, strlen(dp->d_name) + 1 ); // Prepare for recursive call buffer * full_path = malloc(sizeof(full_path)); init( full_path ); write( full_path, dir_name, strlen(dir_name) + 1); cat_paths( full_path, dp->d_name ); LOG( "Cat'ed dir is = %s\n", full_path->buffer ); // Call recursively work_directory( full_path->buffer, new_child, root_name ); destroy( full_path ); free( full_path ); } closedir(dir); } LOG_S("Parsing files\n"); // Get count; alocate accordingly int file_c = file_count( dir_name ); dir_st->files_count = file_c; if( file_c == 0 ) { dir_st->files = NULL; } else { dir_st->files = malloc(sizeof(dummy_file) * file_c); DIR *dir = opendir(dir_name); int i = 0; while( (dp = readdir(dir)) != NULL ) { if(dp->d_type != DT_REG) continue; // Allocate and call recursively (for each dir) dummy_file * new_child = dir_st->files + i++; LOG("Ptr is %x\n" , (unsigned int)new_child); LOG("Working file %s\n", dp->d_name); init( &new_child->name ); LOG("Before adding file %s\n", dp->d_name); write( &new_child->name, dp->d_name, strlen(dp->d_name) + 1 ); } closedir(dir); } return WRKDIR_SUCCESS; }