Skip to content

Commit 8ed071e

Browse files
committed
using include and ignore names in history generation
fixes #1351 fixes #1315
1 parent 741c4ac commit 8ed071e

File tree

5 files changed

+292
-153
lines changed

5 files changed

+292
-153
lines changed

src/org/opensolaris/opengrok/history/FileHistoryCache.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
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.
2222
*/
2323

2424
package org.opensolaris.opengrok.history;
@@ -52,8 +52,10 @@
5252
import java.util.logging.Logger;
5353
import java.util.zip.GZIPInputStream;
5454
import java.util.zip.GZIPOutputStream;
55+
import org.opensolaris.opengrok.configuration.Project;
5556
import org.opensolaris.opengrok.configuration.RuntimeEnvironment;
5657
import org.opensolaris.opengrok.logger.LoggerFactory;
58+
import org.opensolaris.opengrok.util.AcceptHelper;
5759
import org.opensolaris.opengrok.util.IOUtils;
5860

5961
/*
@@ -364,6 +366,29 @@ public void store(History history, Repository repository)
364366
continue;
365367
}
366368

369+
if (env.hasProjects()) {
370+
/*
371+
* Problem here is that if the original file was a symlink
372+
* it's been already dereferenced by
373+
* getPathRelativeToSourceRoot(). We have to solve the only
374+
* case which is that the symlink led to the project's root.
375+
*
376+
* @see
377+
* RuntimeEnvironment#getPathRelativeToSourceRoot(java.io.File, int)
378+
*/
379+
Project project = Project.getProject(test);
380+
File parent = test.equals(new File(env.getSourceRootPath(), project.getPath()))
381+
/* this is a project's root */
382+
? test
383+
/* this isn't a project's root */
384+
: test.getParentFile();
385+
if (!AcceptHelper.accept(project, parent, test)) {
386+
continue;
387+
}
388+
} else if (!AcceptHelper.accept(null, test.getParentFile(), test)) {
389+
continue;
390+
}
391+
367392
List<HistoryEntry> list = map.get(s);
368393
if (list == null) {
369394
list = new ArrayList<>();
@@ -380,7 +405,7 @@ public void store(History history, Repository repository)
380405
}
381406
}
382407
}
383-
408+
384409
/*
385410
* Now traverse the list of files from the hash map built above
386411
* and for each file store its history (saved in the value of the
@@ -492,6 +517,10 @@ public History get(File file, Repository repository, boolean withFiles)
492517
return null;
493518
}
494519

520+
if (!AcceptHelper.accept(Project.getProject(file), file)) {
521+
return null;
522+
}
523+
495524
final History history;
496525
long time;
497526
try {
@@ -584,6 +613,7 @@ private String getRepositoryCachedRevPath(Repository repository) {
584613

585614
/**
586615
* Store latest indexed revision for the repository under data directory.
616+
*
587617
* @param repository repository
588618
* @param rev latest revision which has been just indexed
589619
*/
@@ -592,11 +622,11 @@ private void storeLatestCachedRevision(Repository repository, String rev) {
592622

593623
try {
594624
writer = new BufferedWriter(new OutputStreamWriter(
595-
new FileOutputStream(getRepositoryCachedRevPath(repository))));
625+
new FileOutputStream(getRepositoryCachedRevPath(repository))));
596626
writer.write(rev);
597627
} catch (IOException ex) {
598628
LOGGER.log(Level.WARNING, "Cannot write latest cached revision to file for "+repository.getDirectoryName(),
599-
ex);
629+
ex);
600630
} finally {
601631
try {
602632
if (writer != null) {

src/org/opensolaris/opengrok/index/IndexDatabase.java

Lines changed: 4 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
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.
2222
*/
2323
package org.opensolaris.opengrok.index;
2424

@@ -76,6 +76,7 @@
7676
import org.opensolaris.opengrok.history.HistoryGuru;
7777
import org.opensolaris.opengrok.logger.LoggerFactory;
7878
import org.opensolaris.opengrok.search.QueryBuilder;
79+
import org.opensolaris.opengrok.util.AcceptHelper;
7980
import org.opensolaris.opengrok.util.IOUtils;
8081
import org.opensolaris.opengrok.web.Util;
8182

@@ -665,150 +666,6 @@ private void cleanupResources(Document doc) {
665666
}
666667
}
667668

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-
812669
/**
813670
* Generate indexes recursively
814671
*
@@ -828,7 +685,7 @@ private int indexDown(File dir, String parent, boolean count_only,
828685
return lcur_count;
829686
}
830687

831-
if (!accept(dir)) {
688+
if (!AcceptHelper.accept(project, dir)) {
832689
return lcur_count;
833690
}
834691

@@ -841,7 +698,7 @@ private int indexDown(File dir, String parent, boolean count_only,
841698
Arrays.sort(files, fileComparator);
842699

843700
for (File file : files) {
844-
if (accept(dir, file)) {
701+
if (AcceptHelper.accept(project, dir, file)) {
845702
String path = parent + '/' + file.getName();
846703

847704
if (file.isDirectory()) {

0 commit comments

Comments
 (0)