fileSearch.ts 8.7 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

'use strict';

import fs = require('fs');
import paths = require('path');

11
import scorer = require('vs/base/common/scorer');
E
Erich Gamma 已提交
12 13 14
import arrays = require('vs/base/common/arrays');
import strings = require('vs/base/common/strings');
import glob = require('vs/base/common/glob');
B
Benjamin Pasero 已提交
15
import {IProgress} from 'vs/platform/search/common/search';
E
Erich Gamma 已提交
16 17 18 19 20 21 22

import extfs = require('vs/base/node/extfs');
import flow = require('vs/base/node/flow');
import {ISerializedFileMatch, IRawSearch, ISearchEngine} from 'vs/workbench/services/search/node/rawSearchService';

export class FileWalker {
	private config: IRawSearch;
23
	private filePattern: string;
24
	private normalizedFilePatternLowercase: string;
E
Erich Gamma 已提交
25 26 27 28 29 30
	private excludePattern: glob.IExpression;
	private includePattern: glob.IExpression;
	private maxResults: number;
	private isLimitHit: boolean;
	private resultCount: number;
	private isCanceled: boolean;
31
	private searchInPath: boolean;
E
Erich Gamma 已提交
32 33 34 35 36

	private walkedPaths: { [path: string]: boolean; };

	constructor(config: IRawSearch) {
		this.config = config;
37
		this.filePattern = config.filePattern;
E
Erich Gamma 已提交
38 39 40 41
		this.excludePattern = config.excludePattern;
		this.includePattern = config.includePattern;
		this.maxResults = config.maxResults || null;
		this.walkedPaths = Object.create(null);
42 43
		this.resultCount = 0;
		this.isLimitHit = false;
B
Benjamin Pasero 已提交
44

45 46 47 48 49 50
		if (this.filePattern) {
			if (this.filePattern.indexOf(paths.sep) >= 0) {
				this.filePattern = strings.replaceAll(this.filePattern, '\\', '/'); // Normalize file patterns to forward slashes
			}

			this.normalizedFilePatternLowercase = strings.stripWildcards(this.filePattern).toLowerCase();
B
Benjamin Pasero 已提交
51
		}
E
Erich Gamma 已提交
52 53 54 55 56 57
	}

	public cancel(): void {
		this.isCanceled = true;
	}

58
	public walk(rootFolders: string[], extraFiles: string[], onResult: (result: ISerializedFileMatch) => void, done: (error: Error, isLimitHit: boolean) => void): void {
E
Erich Gamma 已提交
59

60
		// Support that the file pattern is a full path to a file that exists
61
		this.checkFilePatternAbsoluteMatch((exists) => {
62 63 64
			if (this.isCanceled) {
				return done(null, this.isLimitHit);
			}
E
Erich Gamma 已提交
65

66 67 68
			// Report result from file pattern if matching
			if (exists) {
				onResult({ path: this.filePattern });
69 70 71 72

				// Optimization: a match on an absolute path is a good result and we do not
				// continue walking the entire root paths array for other matches because
				// it is very unlikely that another file would match on the full absolute path
73
				return done(null, this.isLimitHit);
74
			}
E
Erich Gamma 已提交
75

76 77 78 79 80
			// For each extra file
			if (extraFiles) {
				extraFiles.forEach(extraFilePath => {
					if (glob.match(this.excludePattern, extraFilePath)) {
						return; // excluded
E
Erich Gamma 已提交
81 82
					}

83
					// File: Check for match on file pattern and include pattern
B
polish  
Benjamin Pasero 已提交
84
					this.matchFile(onResult, extraFilePath, extraFilePath /* no workspace relative path */);
85 86
				});
			}
87

88 89 90 91 92
			// For each root folder
			flow.parallel(rootFolders, (absolutePath, perEntryCallback) => {
				extfs.readdir(absolutePath, (error: Error, files: string[]) => {
					if (error || this.isCanceled || this.isLimitHit) {
						return perEntryCallback(null, null);
93 94
					}

95 96 97 98
					// Support relative paths to files from a root resource
					return this.checkFilePatternRelativeMatch(absolutePath, (match) => {
						if (this.isCanceled || this.isLimitHit) {
							return perEntryCallback(null, null);
E
Erich Gamma 已提交
99 100
						}

101 102 103
						// Report result from file pattern if matching
						if (match) {
							onResult({ path: match });
E
Erich Gamma 已提交
104 105
						}

106 107
						return this.doWalk(absolutePath, '', files, onResult, perEntryCallback);
					});
108 109 110
				});
			}, (err, result) => {
				done(err ? err[0] : null, this.isLimitHit);
E
Erich Gamma 已提交
111
			});
112 113 114
		});
	}

115
	private checkFilePatternAbsoluteMatch(clb: (exists: boolean) => void): void {
116 117 118 119 120 121
		if (!this.filePattern || !paths.isAbsolute(this.filePattern)) {
			return clb(false);
		}

		return fs.stat(this.filePattern, (error, stat) => {
			return clb(!error && !stat.isDirectory()); // only existing files
E
Erich Gamma 已提交
122 123 124
		});
	}

125
	private checkFilePatternRelativeMatch(basePath: string, clb: (matchPath: string) => void): void {
B
polish  
Benjamin Pasero 已提交
126
		if (!this.filePattern || paths.isAbsolute(this.filePattern)) {
127 128 129 130 131 132 133 134 135 136
			return clb(null);
		}

		const absolutePath = paths.join(basePath, this.filePattern);

		return fs.stat(absolutePath, (error, stat) => {
			return clb(!error && !stat.isDirectory() ? absolutePath : null); // only existing files
		});
	}

E
Erich Gamma 已提交
137 138 139 140 141 142 143 144 145 146 147 148 149 150
	private doWalk(absolutePath: string, relativeParentPath: string, files: string[], onResult: (result: ISerializedFileMatch) => void, done: (error: Error, result: any) => void): void {

		// Execute tasks on each file in parallel to optimize throughput
		flow.parallel(files, (file: string, clb: (error: Error) => void): void => {

			// Check canceled
			if (this.isCanceled || this.isLimitHit) {
				return clb(null);
			}

			// If the user searches for the exact file name, we adjust the glob matching
			// to ignore filtering by siblings because the user seems to know what she
			// is searching for and we want to include the result in that case anyway
			let siblings = files;
151
			if (this.config.filePattern === file) {
E
Erich Gamma 已提交
152 153 154 155 156 157 158 159 160
				siblings = [];
			}

			// Check exclude pattern
			let relativeFilePath = strings.trim([relativeParentPath, file].join('/'), '/');
			if (glob.match(this.excludePattern, relativeFilePath, siblings)) {
				return clb(null);
			}

161
			// Use lstat to detect links
E
Erich Gamma 已提交
162
			let currentPath = paths.join(absolutePath, file);
163 164 165 166
			fs.lstat(currentPath, (error, lstat) => {
				if (error || this.isCanceled || this.isLimitHit) {
					return clb(null);
				}
E
Erich Gamma 已提交
167

168 169
				// Directory: Follow directories
				if (lstat.isDirectory()) {
E
Erich Gamma 已提交
170 171

					// to really prevent loops with links we need to resolve the real path of them
172 173 174
					return this.realPathIfNeeded(currentPath, lstat, (error, realpath) => {
						if (error || this.isCanceled || this.isLimitHit) {
							return clb(null);
E
Erich Gamma 已提交
175 176 177 178 179 180
						}

						if (this.walkedPaths[realpath]) {
							return clb(null); // escape when there are cycles (can happen with symlinks)
						}

181 182
						this.walkedPaths[realpath] = true; // remember as walked

E
Erich Gamma 已提交
183
						// Continue walking
184 185 186 187 188 189 190
						return extfs.readdir(currentPath, (error: Error, children: string[]): void => {
							if (error || this.isCanceled || this.isLimitHit) {
								return clb(null);
							}

							this.doWalk(currentPath, relativeFilePath, children, onResult, clb);
						});
E
Erich Gamma 已提交
191 192 193
					});
				}

194
				// File: Check for match on file pattern and include pattern
195 196 197
				else {
					if (relativeFilePath === this.filePattern) {
						return clb(null); // ignore file if its path matches with the file pattern because checkFilePatternRelativeMatch() takes care of those
198
					}
E
Erich Gamma 已提交
199

B
polish  
Benjamin Pasero 已提交
200
					this.matchFile(onResult, currentPath, relativeFilePath);
E
Erich Gamma 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214
				}

				// Unwind
				return clb(null);
			});
		}, (error: Error[]): void => {
			if (error) {
				error = arrays.coalesce(error); // find any error by removing null values first
			}

			return done(error && error.length > 0 ? error[0] : null, null);
		});
	}

B
polish  
Benjamin Pasero 已提交
215 216
	private matchFile(onResult: (result: ISerializedFileMatch) => void, absolutePath: string, relativePath: string): void {
		if (this.isFilePatternMatch(relativePath) && (!this.includePattern || glob.match(this.includePattern, relativePath))) {
217 218 219 220 221 222 223 224 225 226 227 228 229 230
			this.resultCount++;

			if (this.maxResults && this.resultCount > this.maxResults) {
				this.isLimitHit = true;
			}

			if (!this.isLimitHit) {
				onResult({
					path: absolutePath
				});
			}
		}
	}

B
polish  
Benjamin Pasero 已提交
231
	private isFilePatternMatch(path: string): boolean {
232 233 234

		// Check for search pattern
		if (this.filePattern) {
235
			return scorer.matches(path, this.normalizedFilePatternLowercase);
236 237 238 239 240 241
		}

		// No patterns means we match all
		return true;
	}

242 243 244 245 246 247
	private realPathIfNeeded(path: string, lstat: fs.Stats, clb: (error: Error, realpath?: string) => void): void {
		if (lstat.isSymbolicLink()) {
			return fs.realpath(path, (error, realpath) => {
				if (error) {
					return clb(error);
				}
E
Erich Gamma 已提交
248

249 250 251
				return clb(null, realpath);
			});
		}
E
Erich Gamma 已提交
252

253
		return clb(null, path);
E
Erich Gamma 已提交
254 255 256 257
	}
}

export class Engine implements ISearchEngine {
258 259
	private rootFolders: string[];
	private extraFiles: string[];
E
Erich Gamma 已提交
260 261 262
	private walker: FileWalker;

	constructor(config: IRawSearch) {
263 264 265
		this.rootFolders = config.rootFolders;
		this.extraFiles = config.extraFiles;

E
Erich Gamma 已提交
266 267 268 269
		this.walker = new FileWalker(config);
	}

	public search(onResult: (result: ISerializedFileMatch) => void, onProgress: (progress: IProgress) => void, done: (error: Error, isLimitHit: boolean) => void): void {
270
		this.walker.walk(this.rootFolders, this.extraFiles, onResult, done);
E
Erich Gamma 已提交
271 272 273 274 275
	}

	public cancel(): void {
		this.walker.cancel();
	}
276
}