/** * Check if I should accept this file into the index database * * @param file the file to check * @return true if the file should be included, false otherwise */ private boolean accept(File file) { if (!includedNames.isEmpty() && // the filter should not affect directory names (!(file.isDirectory() || includedNames.match(file)))) { return false; } String absolutePath = file.getAbsolutePath(); if (ignoredNames.ignore(file)) { LOGGER.log(Level.FINER, "ignoring {0}", absolutePath); return false; } if (!file.canRead()) { LOGGER.log(Level.WARNING, "Could not read {0}", absolutePath); return false; } try { String canonicalPath = file.getCanonicalPath(); if (!absolutePath.equals(canonicalPath) && !acceptSymlink(absolutePath, canonicalPath)) { LOGGER.log( Level.FINE, "Skipped symlink ''{0}'' -> ''{1}''", new Object[] {absolutePath, canonicalPath}); return false; } // below will only let go files and directories, anything else is considered special and is // not added if (!file.isFile() && !file.isDirectory()) { LOGGER.log(Level.WARNING, "Ignored special file {0}", absolutePath); return false; } } catch (IOException exp) { LOGGER.log(Level.WARNING, "Failed to resolve name: {0}", absolutePath); LOGGER.log(Level.FINE, "Stack Trace: ", exp); } if (file.isDirectory()) { // always accept directories so that their files can be examined return true; } if (HistoryGuru.getInstance().hasHistory(file)) { // versioned files should always be accepted return true; } // this is an unversioned file, check if it should be indexed return !RuntimeEnvironment.getInstance().isIndexVersionedFilesOnly(); }
@Test public void testIgnoredPatterns() { IgnoredNames instance = new IgnoredNames(); List<String> names = instance.getItems(); assertNotNull(names); /* self-test */ for (String name : names) { assertTrue(instance.ignore(name)); } /* Make sure common paths are not ignored by default. */ assertFalse(instance.ignore("usr/src/foo/bin")); assertFalse(instance.ignore("usr/src/foo/bin/bar.ksh")); assertFalse(instance.ignore("usr/src/bar/obj")); assertFalse(instance.ignore("usr/src/bar/obj/foo.ksh")); assertFalse(instance.ignore("usr/src/foo/bar/usr.lib/main.c")); assertFalse(instance.ignore("usr/src/foo/bar/usr.lib")); /* cumulative test */ names = new ArrayList<String>(); names.add("*.o"); instance.setItems(names); names = instance.getItems(); assertEquals(1, names.size()); assertTrue(instance.ignore("foo.o")); assertFalse(instance.ignore("foo")); assertTrue(instance.ignore(".o")); assertFalse(instance.ignore("foo.oo")); instance.add("Makefile"); names = instance.getItems(); assertEquals(2, names.size()); assertTrue(instance.ignore(new File("Makefile"))); assertFalse(instance.ignore("main.c")); instance.add("o*o?.a?c*"); assertTrue(instance.ignore("opengrok.abc")); assertTrue(instance.ignore("opengrok.abcd")); assertFalse(instance.ignore("opengrok.ac")); assertFalse(instance.ignore("grok.abcd")); instance.clear(); names = instance.getItems(); assertEquals(0, names.size()); }