fileSearch.ts 8.6 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 filters = require('vs/base/common/filters');
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;
E
Erich Gamma 已提交
24 25 26 27 28 29
	private excludePattern: glob.IExpression;
	private includePattern: glob.IExpression;
	private maxResults: number;
	private isLimitHit: boolean;
	private resultCount: number;
	private isCanceled: boolean;
30
	private searchInPath: boolean;
E
Erich Gamma 已提交
31 32 33 34 35

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

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

44
		// Normalize file patterns to forward slashes
B
Benjamin Pasero 已提交
45
		if (this.filePattern && this.filePattern.indexOf(paths.sep) >= 0) {
B
Benjamin Pasero 已提交
46 47
			this.filePattern = strings.replaceAll(this.filePattern, '\\', '/');
		}
E
Erich Gamma 已提交
48 49 50 51 52 53
	}

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

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

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

62 63 64
			// Report result from file pattern if matching
			if (exists) {
				onResult({ path: this.filePattern });
65 66 67 68

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

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

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

84 85 86 87 88
			// 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);
89 90
					}

91 92 93 94
					// 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 已提交
95 96
						}

97 98 99
						// Report result from file pattern if matching
						if (match) {
							onResult({ path: match });
E
Erich Gamma 已提交
100 101
						}

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

111
	private checkFilePatternAbsoluteMatch(clb: (exists: boolean) => void): void {
112 113 114 115 116 117
		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 已提交
118 119 120
		});
	}

121
	private checkFilePatternRelativeMatch(basePath: string, clb: (matchPath: string) => void): void {
B
polish  
Benjamin Pasero 已提交
122
		if (!this.filePattern || paths.isAbsolute(this.filePattern)) {
123 124 125 126 127 128 129 130 131 132
			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 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145 146
	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;
147
			if (this.config.filePattern === file) {
E
Erich Gamma 已提交
148 149 150 151 152 153 154 155 156
				siblings = [];
			}

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

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

164 165
				// Directory: Follow directories
				if (lstat.isDirectory()) {
E
Erich Gamma 已提交
166 167

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

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

177 178
						this.walkedPaths[realpath] = true; // remember as walked

E
Erich Gamma 已提交
179
						// Continue walking
180 181 182 183 184 185 186
						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 已提交
187 188 189
					});
				}

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

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

				// 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 已提交
211 212
	private matchFile(onResult: (result: ISerializedFileMatch) => void, absolutePath: string, relativePath: string): void {
		if (this.isFilePatternMatch(relativePath) && (!this.includePattern || glob.match(this.includePattern, relativePath))) {
213 214 215 216 217 218 219 220 221 222 223 224 225 226
			this.resultCount++;

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

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

B
polish  
Benjamin Pasero 已提交
227
	private isFilePatternMatch(path: string): boolean {
228 229 230

		// Check for search pattern
		if (this.filePattern) {
231
			const res = filters.matchesFuzzy(this.filePattern, path, true /* separate substring matching */);
232 233 234 235 236 237 238 239

			return !!res && res.length > 0;
		}

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

240 241 242 243 244 245
	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 已提交
246

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

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

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

	constructor(config: IRawSearch) {
261 262 263
		this.rootFolders = config.rootFolders;
		this.extraFiles = config.extraFiles;

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

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

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