18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2008, 2016 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2008, 2017 , Oracle and/or its affiliates. All rights reserved.
22
22
*/
23
23
package org .opensolaris .opengrok .index ;
24
24
76
76
import org .opensolaris .opengrok .history .HistoryGuru ;
77
77
import org .opensolaris .opengrok .logger .LoggerFactory ;
78
78
import org .opensolaris .opengrok .search .QueryBuilder ;
79
+ import org .opensolaris .opengrok .util .AcceptHelper ;
79
80
import org .opensolaris .opengrok .util .IOUtils ;
80
81
import org .opensolaris .opengrok .web .Util ;
81
82
@@ -665,150 +666,6 @@ private void cleanupResources(Document doc) {
665
666
}
666
667
}
667
668
668
- /**
669
- * Check if I should accept this file into the index database
670
- *
671
- * @param file the file to check
672
- * @return true if the file should be included, false otherwise
673
- */
674
- private boolean accept (File file ) {
675
-
676
- if (!includedNames .isEmpty ()
677
- && // the filter should not affect directory names
678
- (!(file .isDirectory () || includedNames .match (file )))) {
679
- return false ;
680
- }
681
-
682
- String absolutePath = file .getAbsolutePath ();
683
-
684
- if (ignoredNames .ignore (file )) {
685
- LOGGER .log (Level .FINER , "ignoring {0}" , absolutePath );
686
- return false ;
687
- }
688
-
689
- if (!file .canRead ()) {
690
- LOGGER .log (Level .WARNING , "Could not read {0}" , absolutePath );
691
- return false ;
692
- }
693
-
694
- try {
695
- String canonicalPath = file .getCanonicalPath ();
696
- if (!absolutePath .equals (canonicalPath )
697
- && !acceptSymlink (absolutePath , canonicalPath )) {
698
-
699
- LOGGER .log (Level .FINE , "Skipped symlink ''{0}'' -> ''{1}''" ,
700
- new Object []{absolutePath , canonicalPath });
701
- return false ;
702
- }
703
- //below will only let go files and directories, anything else is considered special and is not added
704
- if (!file .isFile () && !file .isDirectory ()) {
705
- LOGGER .log (Level .WARNING , "Ignored special file {0}" ,
706
- absolutePath );
707
- return false ;
708
- }
709
- } catch (IOException exp ) {
710
- LOGGER .log (Level .WARNING , "Failed to resolve name: {0}" ,
711
- absolutePath );
712
- LOGGER .log (Level .FINE , "Stack Trace: " , exp );
713
- }
714
-
715
- if (file .isDirectory ()) {
716
- // always accept directories so that their files can be examined
717
- return true ;
718
- }
719
-
720
- if (HistoryGuru .getInstance ().hasHistory (file )) {
721
- // versioned files should always be accepted
722
- return true ;
723
- }
724
-
725
- // this is an unversioned file, check if it should be indexed
726
- return !RuntimeEnvironment .getInstance ().isIndexVersionedFilesOnly ();
727
- }
728
-
729
- boolean accept (File parent , File file ) {
730
- try {
731
- File f1 = parent .getCanonicalFile ();
732
- File f2 = file .getCanonicalFile ();
733
- if (f1 .equals (f2 )) {
734
- LOGGER .log (Level .INFO , "Skipping links to itself...: {0} {1}" ,
735
- new Object []{parent .getAbsolutePath (), file .getAbsolutePath ()});
736
- return false ;
737
- }
738
-
739
- // Now, let's verify that it's not a link back up the chain...
740
- File t1 = f1 ;
741
- while ((t1 = t1 .getParentFile ()) != null ) {
742
- if (f2 .equals (t1 )) {
743
- LOGGER .log (Level .INFO , "Skipping links to parent...: {0} {1}" ,
744
- new Object []{parent .getAbsolutePath (), file .getAbsolutePath ()});
745
- return false ;
746
- }
747
- }
748
-
749
- return accept (file );
750
- } catch (IOException ex ) {
751
- LOGGER .log (Level .WARNING , "Failed to resolve name: {0} {1}" ,
752
- new Object []{parent .getAbsolutePath (), file .getAbsolutePath ()});
753
- }
754
- return false ;
755
- }
756
-
757
- /**
758
- * Check if I should accept the path containing a symlink
759
- *
760
- * @param absolutePath the path with a symlink to check
761
- * @param canonicalPath the canonical path to the file
762
- * @return true if the file should be accepted, false otherwise
763
- */
764
- private boolean acceptSymlink (String absolutePath , String canonicalPath ) throws IOException {
765
- // Always accept local symlinks
766
- if (isLocal (canonicalPath )) {
767
- return true ;
768
- }
769
-
770
- for (String allowedSymlink : RuntimeEnvironment .getInstance ().getAllowedSymlinks ()) {
771
- if (absolutePath .startsWith (allowedSymlink )) {
772
- String allowedTarget = new File (allowedSymlink ).getCanonicalPath ();
773
- if (canonicalPath .startsWith (allowedTarget )
774
- && absolutePath .substring (allowedSymlink .length ()).equals (canonicalPath .substring (allowedTarget .length ()))) {
775
- return true ;
776
- }
777
- }
778
- }
779
- return false ;
780
- }
781
-
782
- /**
783
- * Check if a file is local to the current project. If we don't have
784
- * projects, check if the file is in the source root.
785
- *
786
- * @param path the path to a file
787
- * @return true if the file is local to the current repository
788
- */
789
- private boolean isLocal (String path ) {
790
- RuntimeEnvironment env = RuntimeEnvironment .getInstance ();
791
- String srcRoot = env .getSourceRootPath ();
792
-
793
- boolean local = false ;
794
-
795
- if (path .startsWith (srcRoot )) {
796
- if (env .hasProjects ()) {
797
- String relPath = path .substring (srcRoot .length ());
798
- if (project .equals (Project .getProject (relPath ))) {
799
- // File is under the current project, so it's local.
800
- local = true ;
801
- }
802
- } else {
803
- // File is under source root, and we don't have projects, so
804
- // consider it local.
805
- local = true ;
806
- }
807
- }
808
-
809
- return local ;
810
- }
811
-
812
669
/**
813
670
* Generate indexes recursively
814
671
*
@@ -828,7 +685,7 @@ private int indexDown(File dir, String parent, boolean count_only,
828
685
return lcur_count ;
829
686
}
830
687
831
- if (!accept (dir )) {
688
+ if (!AcceptHelper . accept (project , dir )) {
832
689
return lcur_count ;
833
690
}
834
691
@@ -841,7 +698,7 @@ private int indexDown(File dir, String parent, boolean count_only,
841
698
Arrays .sort (files , fileComparator );
842
699
843
700
for (File file : files ) {
844
- if (accept (dir , file )) {
701
+ if (AcceptHelper . accept (project , dir , file )) {
845
702
String path = parent + '/' + file .getName ();
846
703
847
704
if (file .isDirectory ()) {
0 commit comments