static public Set < String > expandFileNames ( String [ ] filePathList ) throws SnowflakeSQLException { Set < String > result = new HashSet < String > ( ) ; // a location to file pattern map so that we only need to list the // same directory once when they appear in multiple times. Map < String , List < String > > locationToFilePatterns ; locationToFilePatterns = new HashMap < String , List < String > > ( ) ; String cwd = System . getProperty ( "user.dir" ) ; for ( String path : filePathList ) { // replace ~ with user home path = path . replace ( "~" , System . getProperty ( "user.home" ) ) ; // user may also specify files relative to current directory // add the current path if that is the case if ( ! ( new File ( path ) ) . isAbsolute ( ) ) { logger . debug ( "Adding current working dir to relative file path." ) ; path = cwd + localFSFileSep + path ; } // check if the path contains any wildcards if ( ! path . contains ( "*" ) && ! path . contains ( "?" ) && ! ( path . contains ( "[" ) && path . contains ( "]" ) ) ) { /* this file path doesn't have any wildcard, so we don't need to * expand it */ result . add ( path ) ; } else { // get the directory path int lastFileSepIndex = path . lastIndexOf ( localFSFileSep ) ; // SNOW-15203: if we don't find a default file sep, try "/" if it is not // the default file sep. if ( lastFileSepIndex < 0 && ! "/" . equals ( localFSFileSep ) ) { lastFileSepIndex = path . lastIndexOf ( "/" ) ; } String loc = path . substring ( 0 , lastFileSepIndex + 1 ) ; String filePattern = path . substring ( lastFileSepIndex + 1 ) ; List < String > filePatterns = locationToFilePatterns . get ( loc ) ; if ( filePatterns == null ) { filePatterns = new ArrayList < String > ( ) ; locationToFilePatterns . put ( loc , filePatterns ) ; } filePatterns . add ( filePattern ) ; } } // For each location, list files and match against the patterns for ( Map . Entry < String , List < String > > entry : locationToFilePatterns . entrySet ( ) ) { try { java . io . File dir = new java . io . File ( entry . getKey ( ) ) ; logger . debug ( "Listing files under: {} with patterns: {}" , entry . getKey ( ) , entry . getValue ( ) . toString ( ) ) ; // The following currently ignore sub directories for ( Object file : FileUtils . listFiles ( dir , new WildcardFileFilter ( entry . getValue ( ) ) , null ) ) { result . add ( ( ( java . io . File ) file ) . getCanonicalPath ( ) ) ; } } catch ( Exception ex ) { throw new SnowflakeSQLException ( ex , SqlState . DATA_EXCEPTION , ErrorCode . FAIL_LIST_FILES . getMessageCode ( ) , "Exception: " + ex . getMessage ( ) + ", Dir=" + entry . getKey ( ) + ", Patterns=" + entry . getValue ( ) . toString ( ) ) ; } } logger . debug ( "Expanded file paths: " ) ; for ( String filePath : result ) { logger . debug ( "file: {}" , filePath ) ; } return result ; }
process a list of file paths separated by and expand the wildcards if any to generate the list of paths for all files matched by the wildcards