const char* os_readdir() { #if defined(PLATFORM_IS_WINDOWS) if(opendir_handle == INVALID_HANDLE_VALUE) return NULL; size_t len = strlen(opendir_data.cFileName) + 1; if(skip_entry(opendir_data.cFileName, len - 1)) { if(!FindNextFile(opendir_handle, &opendir_data)) { os_closedir(); return NULL; } return os_readdir(); } char* cstring = (char*)pony_alloc(len); memcpy(cstring, opendir_data.cFileName, len); if(!FindNextFile(opendir_handle, &opendir_data)) os_closedir(); return cstring; #elif defined(PLATFORM_IS_POSIX_BASED) if(opendir_handle == NULL) return NULL; struct dirent entry; struct dirent* result; if(readdir_r(opendir_handle, &entry, &result) != 0) return NULL; if(result == NULL) return NULL; size_t len = strlen(result->d_name) + 1; if(skip_entry(result->d_name, len - 1)) return os_readdir(); char* cstring = pony_alloc(len); memcpy(cstring, result->d_name, len); return cstring; #else return NULL; #endif }
void dump() { os_dirent_t *dd = NULL; os_dir_t ldir = os_opendir(logdir->value()); if(!ldir) { fprintf(stdout, "Could not open directory %s\n", logdir->value()); return; } fprintf(stdout, "---------------------- %s {\n", logdir->value()); while ((dd = os_readdir(ldir))) { fprintf(stdout, "%s\n", dd->d_name); } os_closedir(ldir); fprintf(stdout, "---------------------- %s }\n", logdir->value()); }
static int server_readdir( u8 *p ) { const char* name; u32 fsize = 0, d; int fd; char separator[ 2 ] = { PLATFORM_PATH_SEPARATOR, 0 }; log_msg( "server_readdir: request handler starting\n" ); if( remotefs_readdir_read_request( p, &d ) == ELUARPC_ERR ) { log_msg( "server_readdir: unable to read request\n" ); return SERVER_ERR; } log_msg( "server_readdir: DIR = %08X\n", d ); os_readdir( d, &name ); if( name ) { // Need to compute size now // Get real filename server_fullname[ 0 ] = server_fullname[ PLATFORM_MAX_FNAME_LEN ] = 0; strncpy( server_fullname, server_basedir, PLATFORM_MAX_FNAME_LEN ); if( name && strlen( name ) > 0 ) { if( server_fullname[ strlen( server_fullname ) - 1 ] != PLATFORM_PATH_SEPARATOR ) strncat( server_fullname, separator, PLATFORM_MAX_FNAME_LEN ); strncat( server_fullname, name, PLATFORM_MAX_FNAME_LEN ); } fd = os_open( server_fullname, RFS_OPEN_FLAG_RDONLY, 0 ); if( fd ) { fsize = os_lseek( fd, 0, RFS_LSEEK_END ); os_close( fd ); } else { log_msg( "server_readdir: unable to open file %s\n", server_fullname ); name = NULL; } } log_msg( "server_readdir: OS response is fname = %s, fsize = %u\n", name, ( unsigned )fsize ); remotefs_readdir_write_response( p, name, fsize, 0 ); return SERVER_OK; }
int is_dir_empty(const char path[]) { DIR *dir; struct dirent *d; if((dir = os_opendir(path)) == NULL) { return 0; } while((d = os_readdir(dir)) != NULL) { if(!is_builtin_dir(d->d_name)) { break; } } os_closedir(dir); return d == NULL; }
/* Traverses passed directory recursively and adds it and all found * subdirectories to PATH environment variable. */ static void add_dirs_to_path(const char *path) { DIR *dir; struct dirent *dentry; const char *slash = ""; dir = os_opendir(path); if(dir == NULL) return; slash = ends_with_slash(path) ? "" : "/"; add_to_path(path); while((dentry = os_readdir(dir)) != NULL) { char buf[PATH_MAX]; if(is_builtin_dir(dentry->d_name)) { continue; } snprintf(buf, sizeof(buf), "%s%s%s", path, slash, dentry->d_name); #ifndef _WIN32 if(dentry->d_type == DT_DIR) #else if(is_dir(buf)) #endif { add_dirs_to_path(buf); } } os_closedir(dir); }
/* A generic subtree traversing. Returns zero on success, otherwise non-zero is * returned. */ static int traverse_subtree(const char path[], subtree_visitor visitor, void *param) { DIR *dir; struct dirent *d; int result; VisitResult enter_result; dir = os_opendir(path); if(dir == NULL) { return 1; } enter_result = visitor(path, VA_DIR_ENTER, param); if(enter_result == VR_ERROR) { (void)os_closedir(dir); return 1; } result = 0; while((d = os_readdir(dir)) != NULL) { char *full_path; if(is_builtin_dir(d->d_name)) { continue; } full_path = join_paths(path, d->d_name); if(entry_is_link(full_path, d)) { /* Treat symbolic links to directories as files as well. */ result = visitor(full_path, VA_FILE, param); } else if(entry_is_dir(full_path, d)) { result = traverse_subtree(full_path, visitor, param); } else { result = visitor(full_path, VA_FILE, param); } free(full_path); if(result != 0) { break; } } (void)os_closedir(dir); if(result == 0 && enter_result != VR_SKIP_DIR_LEAVE && enter_result != VR_CANCELLED) { result = visitor(path, VA_DIR_LEAVE, param); } return result; }