fileSearch.ts 10.0 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
import arrays = require('vs/base/common/arrays');
import strings = require('vs/base/common/strings');
14
import types = require('vs/base/common/types');
E
Erich Gamma 已提交
15
import glob = require('vs/base/common/glob');
B
Benjamin Pasero 已提交
16
import {IProgress} from 'vs/platform/search/common/search';
E
Erich Gamma 已提交
17 18 19 20 21 22 23

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;
24
	private filePattern: string;
25
	private normalizedFilePatternLowercase: string;
E
Erich Gamma 已提交
26 27 28
	private excludePattern: glob.IExpression;
	private includePattern: glob.IExpression;
	private maxResults: number;
29
	private maxFilesize: number;
E
Erich Gamma 已提交
30 31 32 33 34 35 36 37
	private isLimitHit: boolean;
	private resultCount: number;
	private isCanceled: boolean;

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

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

47
		if (this.filePattern) {
B
Benjamin Pasero 已提交
48
			this.filePattern = this.filePattern.replace(/\\/g, '/'); // Normalize file patterns to forward slashes
49
			this.normalizedFilePatternLowercase = strings.stripWildcards(this.filePattern).toLowerCase();
B
Benjamin Pasero 已提交
50
		}
E
Erich Gamma 已提交
51 52 53 54 55 56
	}

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

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

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

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

				// 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
72
				return done(null, this.isLimitHit);
73
			}
E
Erich Gamma 已提交
74

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

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

87 88 89 90 91
			// 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);
92 93
					}

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

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

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

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

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

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

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

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

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

		// 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;
150
			if (this.config.filePattern === file) {
E
Erich Gamma 已提交
151 152 153 154
				siblings = [];
			}

			// Check exclude pattern
B
Benjamin Pasero 已提交
155 156
			let currentRelativePathWithSlashes = relativeParentPathWithSlashes ? [relativeParentPathWithSlashes, file].join('/') : file;
			if (glob.match(this.excludePattern, currentRelativePathWithSlashes, siblings)) {
E
Erich Gamma 已提交
157 158 159
				return clb(null);
			}

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

167 168 169 170 171 172 173
				// If the path is a link, we must instead use fs.stat() to find out if the
				// link is a directory or not because lstat will always return the stat of
				// the link which is always a file.
				this.statLinkIfNeeded(currentAbsolutePath, lstat, (error, stat) => {
					if (error || this.isCanceled || this.isLimitHit) {
						return clb(null);
					}
E
Erich Gamma 已提交
174

175 176
					// Directory: Follow directories
					if (stat.isDirectory()) {
177

178 179
						// to really prevent loops with links we need to resolve the real path of them
						return this.realPathIfNeeded(currentAbsolutePath, lstat, (error, realpath) => {
180 181 182 183
							if (error || this.isCanceled || this.isLimitHit) {
								return clb(null);
							}

184 185 186
							if (this.walkedPaths[realpath]) {
								return clb(null); // escape when there are cycles (can happen with symlinks)
							}
E
Erich Gamma 已提交
187

188 189 190 191 192 193 194 195 196 197 198
							this.walkedPaths[realpath] = true; // remember as walked

							// Continue walking
							return extfs.readdir(currentAbsolutePath, (error: Error, children: string[]): void => {
								if (error || this.isCanceled || this.isLimitHit) {
									return clb(null);
								}

								this.doWalk(currentAbsolutePath, currentRelativePathWithSlashes, children, onResult, clb);
							});
						});
199
					}
E
Erich Gamma 已提交
200

201 202 203 204 205
					// File: Check for match on file pattern and include pattern
					else {
						if (currentRelativePathWithSlashes === this.filePattern) {
							return clb(null); // ignore file if its path matches with the file pattern because checkFilePatternRelativeMatch() takes care of those
						}
E
Erich Gamma 已提交
206

207 208 209 210
						if (this.maxFilesize && types.isNumber(stat.size) && stat.size > this.maxFilesize) {
							return clb(null); // ignore file if max file size is hit
						}

211
						this.matchFile(onResult, currentAbsolutePath, currentRelativePathWithSlashes, stat.size);
212 213 214 215 216
					}

					// Unwind
					return clb(null);
				});
E
Erich Gamma 已提交
217 218 219 220 221 222 223 224 225 226
			});
		}, (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);
		});
	}

227
	private matchFile(onResult: (result: ISerializedFileMatch, size: number) => void, absolutePath: string, relativePathWithSlashes: string, size?: number): void {
B
Benjamin Pasero 已提交
228
		if (this.isFilePatternMatch(relativePathWithSlashes) && (!this.includePattern || glob.match(this.includePattern, relativePathWithSlashes))) {
229 230 231 232 233 234 235 236 237
			this.resultCount++;

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

			if (!this.isLimitHit) {
				onResult({
					path: absolutePath
238
				}, size);
239 240 241 242
			}
		}
	}

B
polish  
Benjamin Pasero 已提交
243
	private isFilePatternMatch(path: string): boolean {
244 245 246

		// Check for search pattern
		if (this.filePattern) {
247 248 249 250
			if (this.filePattern === '*') {
				return true; // support the all-matching wildcard
			}

251
			return scorer.matches(path, this.normalizedFilePatternLowercase);
252 253 254 255 256 257
		}

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

258
	private statLinkIfNeeded(path: string, lstat: fs.Stats, clb: (error: Error, stat: fs.Stats) => void): void {
259 260 261 262 263 264 265
		if (lstat.isSymbolicLink()) {
			return fs.stat(path, clb); // stat the target the link points to
		}

		return clb(null, lstat); // not a link, so the stat is already ok for us
	}

266 267 268 269 270 271
	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 已提交
272

273 274 275
				return clb(null, realpath);
			});
		}
E
Erich Gamma 已提交
276

277
		return clb(null, path);
E
Erich Gamma 已提交
278 279 280 281
	}
}

export class Engine implements ISearchEngine {
282 283
	private rootFolders: string[];
	private extraFiles: string[];
E
Erich Gamma 已提交
284 285 286
	private walker: FileWalker;

	constructor(config: IRawSearch) {
287 288 289
		this.rootFolders = config.rootFolders;
		this.extraFiles = config.extraFiles;

E
Erich Gamma 已提交
290 291 292 293
		this.walker = new FileWalker(config);
	}

	public search(onResult: (result: ISerializedFileMatch) => void, onProgress: (progress: IProgress) => void, done: (error: Error, isLimitHit: boolean) => void): void {
294
		this.walker.walk(this.rootFolders, this.extraFiles, onResult, done);
E
Erich Gamma 已提交
295 296 297 298 299
	}

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