#include #include #include #include #include #include #include #include "dir.h" #include "levdist.h" // todo: use a callback function when files are found int dir_list_files_in_dir(levdist_t* levdist, const char* path, bool recursive) { assert(levdist); assert(path); assert(*path); // For error reporting int error = 0; // Try to open the directory DIR* dir = opendir(path); if ( dir == NULL ) return (void)fprintf(stderr, "levdist: error: could not open directory %s\n", path), 1; // Load the directory entries (contents) one by one struct dirent* entry; while ( (entry = readdir(dir)) != NULL ) { // If the entry is a directory if ( entry->d_type == DT_DIR ) { // Ignore hidden files and directories if ( *entry->d_name == '.' ) continue; // If files should be listed recursively if ( recursive ) { // Concatenate the directory to the path char relative_path[PATH_MAX]; sprintf(relative_path, "%s/%s", path, entry->d_name); // Load files in the subdirectory dir_list_files_in_dir(levdist, relative_path, recursive); } } else // The entry is not a directory { // Concatenate the directory to the path char relative_path[PATH_MAX]; sprintf(relative_path, "%s/%s", path, entry->d_name); // Append the file or symlink to the queue if ( (error = levdist_load_file(levdist, relative_path, true) ) ) return (void)closedir(dir), error; } } // Sucess closedir(dir); return error; } bool dir_is_file(const char* path) { // Ask OS for attributes about the path struct stat file_att; if ( stat(path, &file_att) ) return false; // Check the file type attribute return S_ISREG(file_att.st_mode); } off_t dir_file_size(const char* filename) { // Ask OS for attributes about the file struct stat file_att; if ( stat(filename, &file_att) ) return -1; // Check the file type attribute return file_att.st_size; }