void ThemeRepository::parseDirectory( const string &rDir_locale ) { DIR *pDir; char *pszDirContent; vlc_value_t val, text; // Path separator const string &sep = OSFactory::instance( getIntf() )->getDirSeparator(); // Open the dir // FIXME: parseDirectory should be invoked with UTF-8 input instead!! string rDir = sFromLocale( rDir_locale ); pDir = utf8_opendir( rDir.c_str() ); if( pDir == NULL ) { // An error occurred msg_Dbg( getIntf(), "cannot open directory %s", rDir.c_str() ); return; } // While we still have entries in the directory while( ( pszDirContent = utf8_readdir( pDir ) ) != NULL ) { string name = pszDirContent; string extension; if( name.size() > 4 ) { extension = name.substr( name.size() - 4, 4 ); } if( extension == ".vlt" || extension == ".wsz" ) { string path = rDir + sep + name; msg_Dbg( getIntf(), "found skin %s", path.c_str() ); // Add the theme in the popup menu string shortname = name.substr( 0, name.size() - 4 ); val.psz_string = strdup( path.c_str() ); text.psz_string = strdup( shortname.c_str() ); var_Change( getIntf(), "intf-skins", VLC_VAR_ADDCHOICE, &val, &text ); free( val.psz_string ); free( text.psz_string ); } free( pszDirContent ); } closedir( pDir ); }
int playlist_FindArtInCache( input_item_t *p_item ) { char *psz_path = ArtCachePath( p_item ); if( !psz_path ) return VLC_EGENERIC; /* Check if file exists */ DIR *p_dir = utf8_opendir( psz_path ); if( !p_dir ) { free( psz_path ); return VLC_EGENERIC; } bool b_found = false; char *psz_filename; while( !b_found && (psz_filename = utf8_readdir( p_dir )) ) { if( !strncmp( psz_filename, "art", 3 ) ) { char *psz_file; if( asprintf( &psz_file, "%s" DIR_SEP "%s", psz_path, psz_filename ) != -1 ) { char *psz_uri = make_URI( psz_file ); if( psz_uri ) { input_item_SetArtURL( p_item, psz_uri ); free( psz_uri ); } free( psz_file ); } b_found = true; } free( psz_filename ); } /* */ closedir( p_dir ); free( psz_path ); return b_found ? VLC_SUCCESS : VLC_EGENERIC; }
/* Parse a directory and recursively add files */ int ParseDirectory( intf_thread_t *p_intf, char *psz_root, char *psz_dir ) { intf_sys_t *p_sys = p_intf->p_sys; char dir[MAX_DIR_SIZE]; DIR *p_dir; vlc_acl_t *p_acl; FILE *file; char *user = NULL; char *password = NULL; int i_dirlen; if( ( p_dir = utf8_opendir( psz_dir ) ) == NULL ) { if( errno != ENOENT && errno != ENOTDIR ) msg_Err( p_intf, "cannot open directory (%s)", psz_dir ); return VLC_EGENERIC; } i_dirlen = strlen( psz_dir ); if( i_dirlen + 10 > MAX_DIR_SIZE ) { msg_Warn( p_intf, "skipping too deep directory (%s)", psz_dir ); closedir( p_dir ); return 0; } msg_Dbg( p_intf, "dir=%s", psz_dir ); snprintf( dir, sizeof( dir ), "%s"DIR_SEP".access", psz_dir ); if( ( file = utf8_fopen( dir, "r" ) ) != NULL ) { char line[1024]; int i_size; msg_Dbg( p_intf, "find .access in dir=%s", psz_dir ); i_size = fread( line, 1, 1023, file ); if( i_size > 0 ) { char *p; while( i_size > 0 && ( line[i_size-1] == '\n' || line[i_size-1] == '\r' ) ) { i_size--; } line[i_size] = '\0'; p = strchr( line, ':' ); if( p ) { *p++ = '\0'; user = strdup( line ); password = strdup( p ); } } msg_Dbg( p_intf, "using user=%s (read=%d)", user, i_size ); fclose( file ); } snprintf( dir, sizeof( dir ), "%s"DIR_SEP".hosts", psz_dir ); p_acl = ACL_Create( p_intf, false ); if( ACL_LoadFile( p_acl, dir ) ) { ACL_Destroy( p_acl ); struct stat st; if( utf8_stat( dir, &st ) == 0 ) { free( user ); free( password ); closedir( p_dir ); return VLC_EGENERIC; } p_acl = NULL; } for( ;; ) { char *psz_filename; /* parse psz_src dir */ if( ( psz_filename = utf8_readdir( p_dir ) ) == NULL ) { break; } if( ( psz_filename[0] == '.' ) || ( i_dirlen + strlen( psz_filename ) > MAX_DIR_SIZE ) ) { free( psz_filename ); continue; } snprintf( dir, sizeof( dir ), "%s"DIR_SEP"%s", psz_dir, psz_filename ); free( psz_filename ); if( ParseDirectory( p_intf, psz_root, dir ) ) { httpd_file_sys_t *f = NULL; httpd_handler_sys_t *h = NULL; bool b_index; char *psz_name, *psz_ext; psz_name = FileToUrl( &dir[strlen( psz_root )], &b_index ); psz_ext = strrchr( dir, '.' ); if( psz_ext != NULL ) { int i; psz_ext++; for( i = 0; i < p_sys->i_handlers; i++ ) if( !strcmp( p_sys->pp_handlers[i]->psz_ext, psz_ext ) ) break; if( i < p_sys->i_handlers ) { f = malloc( sizeof( httpd_handler_sys_t ) ); h = (httpd_handler_sys_t *)f; f->b_handler = true; h->p_association = p_sys->pp_handlers[i]; } } if( f == NULL ) { f = malloc( sizeof( httpd_file_sys_t ) ); f->b_handler = false; } f->p_intf = p_intf; f->p_file = NULL; f->p_redir = NULL; f->p_redir2 = NULL; f->file = strdup (dir); f->name = psz_name; f->b_html = strstr( &dir[strlen( psz_root )], ".htm" ) || strstr( &dir[strlen( psz_root )], ".xml" ) ? true : false; if( !f->name ) { msg_Err( p_intf , "unable to parse directory" ); closedir( p_dir ); free( f ); return( VLC_ENOMEM ); } msg_Dbg( p_intf, "file=%s (url=%s)", f->file, f->name ); if( !f->b_handler ) { char *psz_type = strdup( "text/html; charset=UTF-8" ); if( strstr( &dir[strlen( psz_root )], ".xml" ) ) { char *psz = strstr( psz_type, "html;" ); if( psz ) { psz[0] = 'x'; psz[1] = 'm'; psz[2] = 'l'; psz[3] = ';'; psz[4] = ' '; } } f->p_file = httpd_FileNew( p_sys->p_httpd_host, f->name, f->b_html ? psz_type : NULL, user, password, p_acl, HttpCallback, f ); free( psz_type ); if( f->p_file != NULL ) { TAB_APPEND( p_sys->i_files, p_sys->pp_files, f ); } } else { h->p_handler = httpd_HandlerNew( p_sys->p_httpd_host, f->name, user, password, p_acl, HandlerCallback, h ); if( h->p_handler != NULL ) { TAB_APPEND( p_sys->i_files, p_sys->pp_files, (httpd_file_sys_t *)h ); } } /* for url that ends by / add * - a redirect from rep to rep/ * - in case of index.* rep/index.html to rep/ */ if( f && f->name[strlen(f->name) - 1] == '/' ) { char *psz_redir = strdup( f->name ); char *p; psz_redir[strlen( psz_redir ) - 1] = '\0'; msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name ); f->p_redir = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir ); free( psz_redir ); if( b_index && ( p = strstr( f->file, "index." ) ) ) { if( asprintf( &psz_redir, "%s%s", f->name, p ) != -1 ) { msg_Dbg( p_intf, "redir=%s -> %s", psz_redir, f->name ); f->p_redir2 = httpd_RedirectNew( p_sys->p_httpd_host, f->name, psz_redir ); free( psz_redir ); } } } } } free( user ); free( password ); ACL_Destroy( p_acl ); closedir( p_dir ); return VLC_SUCCESS; }
bool ThemeLoader::findFile( const string &rootDir, const string &rFileName, string &themeFilePath ) { // Path separator const string &sep = OSFactory::instance( getIntf() )->getDirSeparator(); DIR *pCurrDir; char *pszDirContent; // Open the dir pCurrDir = utf8_opendir( rootDir.c_str() ); if( pCurrDir == NULL ) { // An error occurred msg_Dbg( getIntf(), "cannot open directory %s", rootDir.c_str() ); return false; } // While we still have entries in the directory while( ( pszDirContent = utf8_readdir( pCurrDir ) ) != NULL ) { string newURI = rootDir + sep + pszDirContent; // Skip . and .. if( string( pszDirContent ) != "." && string( pszDirContent ) != ".." ) { #if defined( S_ISDIR ) struct stat stat_data; if( ( utf8_stat( newURI.c_str(), &stat_data ) == 0 ) && S_ISDIR(stat_data.st_mode) ) #elif defined( DT_DIR ) if( pDirContent->d_type & DT_DIR ) #else if( 0 ) #endif { // Can we find the file in this subdirectory? if( findFile( newURI, rFileName, themeFilePath ) ) { free( pszDirContent ); closedir( pCurrDir ); return true; } } else { // Found the theme file? if( rFileName == string( pszDirContent ) ) { themeFilePath = newURI; free( pszDirContent ); closedir( pCurrDir ); return true; } } } free( pszDirContent ); } closedir( pCurrDir ); return false; }