Skip to content

Commit 3750a3c

Browse files
committed
Use IFile.isContentRestricted() when restricting file search
This change adjusts the initial change for file search restrictions, to use the newly added API: IFile.isContentRestricted() In addition, the respective preference is changed to: disableRestrictedFileSearch Example preference for product customization: org.eclipse.search.core/disableRestrictedFileSearch=true Fixes: #3815
1 parent 0b9521c commit 3750a3c

3 files changed

Lines changed: 25 additions & 34 deletions

File tree

bundles/org.eclipse.search.core/search/org/eclipse/search/internal/core/text/TextSearchVisitor.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import org.eclipse.core.runtime.NullProgressMonitor;
3939
import org.eclipse.core.runtime.OperationCanceledException;
4040
import org.eclipse.core.runtime.Platform;
41-
import org.eclipse.core.runtime.QualifiedName;
4241
import org.eclipse.core.runtime.Status;
4342
import org.eclipse.core.runtime.SubMonitor;
4443
import org.eclipse.core.runtime.content.IContentType;
@@ -72,7 +71,7 @@ public class TextSearchVisitor {
7271

7372
public static final boolean TRACING= "true".equalsIgnoreCase(Platform.getDebugOption("org.eclipse.search/perf")); //$NON-NLS-1$ //$NON-NLS-2$
7473
private static final int NUMBER_OF_LOGICAL_THREADS= Runtime.getRuntime().availableProcessors();
75-
private static final String EXCLUSION_PREFERENCE_NAME = "search_exclusion_property"; //$NON-NLS-1$
74+
private static final String DISABLE_RESTRICTED_FILE_SEARCH_PREFERENCE = "disableRestrictedFileSearch"; //$NON-NLS-1$
7675

7776
/**
7877
* Queue of files to be searched. IFile pointing to the same local file are
@@ -301,7 +300,7 @@ public Map<IFile, IDocument> getDocumentsInEditors() {
301300
private volatile boolean fIsLightweightAutoRefresh;
302301
private final DirtyFileProvider fDirtyDiscovery;
303302

304-
private final QualifiedName fExclusionProperty;
303+
private final boolean fDisableRestrictedFileSearch;
305304

306305
public TextSearchVisitor(TextSearchRequestor collector, Pattern searchPattern, DirtyFileProvider dirtyDiscovery) {
307306
fCollector= collector;
@@ -315,8 +314,8 @@ public TextSearchVisitor(TextSearchRequestor collector, Pattern searchPattern, D
315314
fileBatches = new ConcurrentLinkedQueue<>();
316315

317316
IPreferencesService prefs = Platform.getPreferencesService();
318-
String exclusionPropertyName = prefs.getString(SearchCorePlugin.PLUGIN_ID, EXCLUSION_PREFERENCE_NAME, "", null); //$NON-NLS-1$
319-
fExclusionProperty = exclusionPropertyName.isEmpty() ? null : new QualifiedName(null, exclusionPropertyName);
317+
String disableRestrictedFileSearch = prefs.getString(SearchCorePlugin.PLUGIN_ID, DISABLE_RESTRICTED_FILE_SEARCH_PREFERENCE, "", null); //$NON-NLS-1$
318+
fDisableRestrictedFileSearch = Boolean.TRUE.toString().equals(disableRestrictedFileSearch);
320319
}
321320

322321
public IStatus search(IFile[] files, IProgressMonitor monitor) {
@@ -557,16 +556,16 @@ private String getCharSetName(IFile file) {
557556
}
558557

559558
private boolean excluded(IFile file) {
560-
if (fExclusionProperty != null) {
559+
if (fDisableRestrictedFileSearch) {
561560
try {
562-
return file.getSessionProperty(fExclusionProperty) != null;
561+
return file.isContentRestricted();
563562
} catch (CoreException e) {
564563
/*
565-
* The preference 'search_exclusion_property' indicates we
566-
* should skip files with the respective session property, but
567-
* we ran into an exception while reading the properties of the
568-
* file. Skip the file from the search, since we don't know if
569-
* the property is set or not.
564+
* The preference 'disableRestrictedFileSearch' indicates we
565+
* should skip restricted files, but we ran into an exception
566+
* while checking if the file is restricted. Skip the file from
567+
* the search, since we don't know if the file is restricted or
568+
* not.
570569
*/
571570
fStatus.add(errorStatusForFile(file, e));
572571
return true;

tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/AllFileSearchTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
SearchResultPageTest.class,
2929
SortingTest.class,
3030
TextSearchResultTest.class,
31-
ExcludedFilesSearchTest.class,
31+
RestrictedFilesSearchTest.class,
3232
})
3333
public class AllFileSearchTests {
3434
}

tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/ExcludedFilesSearchTest.java renamed to tests/org.eclipse.search.tests/src/org/eclipse/search/tests/filesearch/RestrictedFilesSearchTest.java

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.eclipse.core.runtime.CoreException;
3131
import org.eclipse.core.runtime.IPath;
3232
import org.eclipse.core.runtime.Path;
33-
import org.eclipse.core.runtime.QualifiedName;
3433
import org.eclipse.core.runtime.preferences.IEclipsePreferences;
3534
import org.eclipse.core.runtime.preferences.InstanceScope;
3635

@@ -54,13 +53,9 @@
5453
* property {@code search_excluded_file} on some test files and performs a search. Search matches
5554
* are not expected for the files with the session property.
5655
*/
57-
public class ExcludedFilesSearchTest {
56+
public class RestrictedFilesSearchTest {
5857

59-
private static final String EXCLUSION_PREFERENCE_NAME= "search_exclusion_property";
60-
61-
private static final QualifiedName SESSION_PROPERTY_QN= new QualifiedName(null, "search_excluded_file");
62-
63-
private static final String SESSION_PROPERTY= "search_excluded_file";
58+
private static final String DISABLE_PREFERENCE_NAME= "disableRestrictedFileSearch";
6459

6560
private static final String EXCLUDED_FILE_PREFIX= "excluded_";
6661

@@ -69,7 +64,7 @@ public class ExcludedFilesSearchTest {
6964
@AfterEach
7065
public void cleanUp() throws Exception {
7166
ResourceHelper.deleteProject(PROJECT_NAME);
72-
setExcludedSearchEnabled(null);
67+
setDisableRestrictedFileSearch(false);
7368
}
7469

7570
@Test
@@ -102,10 +97,10 @@ private void doLinkTest(int n, int index) throws Exception {
10297
for (int i= 0; i < n; ++i) {
10398
IFile link= ResourceHelper.createLinkedFile(project, new Path("link_file_" + i), file);
10499
if (i == index) {
105-
setExcludedSearchSessionProperty(link);
100+
link.setContentRestricted(true);
106101
}
107102
}
108-
setExcludedSearchEnabled(SESSION_PROPERTY);
103+
setDisableRestrictedFileSearch(true);
109104
TestResultCollector collector= new TestResultCollector(true);
110105
doSearch(project, collector, searchString);
111106
TestResult[] results= collector.getResults();
@@ -165,7 +160,7 @@ private static void doSearchTest(int n, int m, boolean parallel, boolean session
165160
String searchString= "hello";
166161
IProject project= prepareProject(n, m, PROJECT_NAME, searchString);
167162
if (sessionProperty) {
168-
setExcludedSearchEnabled(SESSION_PROPERTY);
163+
setDisableRestrictedFileSearch(true);
169164
setSessionProperty(project);
170165
}
171166
TestResultCollector collector= new TestResultCollector(parallel);
@@ -177,8 +172,9 @@ private static void setSessionProperty(IProject project) throws CoreException {
177172
project.accept(new IResourceVisitor() {
178173
@Override
179174
public boolean visit(IResource resource) throws CoreException {
180-
if (resource.getName().startsWith(EXCLUDED_FILE_PREFIX)) {
181-
setExcludedSearchSessionProperty(resource);
175+
if (resource.getType() == IResource.FILE && resource.getName().startsWith(EXCLUDED_FILE_PREFIX)) {
176+
IFile file= (IFile) resource;
177+
file.setContentRestricted(true);
182178
}
183179
return true;
184180
}
@@ -234,16 +230,12 @@ private static void assertMatches(TestResult[] results, int expectedCount, Strin
234230
assertEquals(expectedCount, k, "Wrong number of results in file");
235231
}
236232

237-
private static void setExcludedSearchSessionProperty(IResource resource) throws CoreException {
238-
resource.setSessionProperty(SESSION_PROPERTY_QN, "true");
239-
}
240-
241-
private static void setExcludedSearchEnabled(String value) throws BackingStoreException {
233+
private static void setDisableRestrictedFileSearch(boolean enabled) throws BackingStoreException {
242234
IEclipsePreferences node= InstanceScope.INSTANCE.getNode(SearchCorePlugin.PLUGIN_ID);
243-
if (value != null) {
244-
node.put(EXCLUSION_PREFERENCE_NAME, value);
235+
if (enabled) {
236+
node.put(DISABLE_PREFERENCE_NAME, Boolean.TRUE.toString());
245237
} else {
246-
node.remove(EXCLUSION_PREFERENCE_NAME);
238+
node.remove(DISABLE_PREFERENCE_NAME);
247239
}
248240
node.flush();
249241
}

0 commit comments

Comments
 (0)