Пример #1
0
 /// <summary>
 ///     Gets all matched files.
 /// </summary>
 /// <param name="query"> The query. </param>
 /// <param name="result"> The result. </param>
 /// <remarks>
 /// </remarks>
 private void GetAllMatchedFiles(FileSearchQuery query, List <string> result)
 {
     foreach (var probefile in query.ProbeFiles)
     {
         foreach (var dir in query.ProbePaths)
         {
             query.UserLog.Debug("enter dir " + dir, this);
             var ndir = dir;
             if (ndir.StartsWith("~/"))
             {
                 ndir = ndir.Substring(2);
             }
             var path = ResolveStandardPath(ndir);
             var pf   = probefile;
             if (pf.StartsWith("/"))
             {
                 pf = pf.Substring(1);
             }
             var full          = Path.Combine(path, pf);
             var directoryName = Path.GetDirectoryName(full);
             var fileName      = Path.GetFileName(full);
             if (directoryName != null && Directory.Exists(directoryName))
             {
                 if (fileName != null)
                 {
                     result.AddRange(Directory.GetFiles(directoryName, fileName).Select(file => AdaptFilePath(query.PathType, file)));
                 }
             }
         }
     }
 }
Пример #2
0
		/// <summary>
		/// 	Запрос на поиск одного конкретного файла с разрешением относительно типовых уровней
		/// </summary>
		/// <param name="name"> </param>
		/// <param name="existed"> </param>
		/// <param name="pathType"> </param>
		/// <returns> </returns>
		public static FileSearchQuery Leveled(string name, bool existed = false,
		                                      FileSearchResultType pathType = FileSearchResultType.FullPath) {
			var result = new FileSearchQuery
				{
					ProbeFiles = new[] {name},
					ProbePaths = new[] {"~/usr", "~/mod", "~/sys", "~/"},
					ExistedOnly = existed,
					PathType = pathType
				};
			return result;
		}
Пример #3
0
        /// <summary>
        ///     Запрос на поиск одного конкретного файла с разрешением относительно типовых уровней
        /// </summary>
        /// <param name="name"> </param>
        /// <param name="existed"> </param>
        /// <param name="pathType"> </param>
        /// <returns> </returns>
        public static FileSearchQuery Leveled(string name, bool existed     = false,
                                              FileSearchResultType pathType = FileSearchResultType.FullPath)
        {
            var result = new FileSearchQuery
            {
                ProbeFiles  = new[] { name },
                ProbePaths  = new[] { "~/usr", "~/mod", "~/sys", "~/" },
                ExistedOnly = existed,
                PathType    = pathType
            };

            return(result);
        }
Пример #4
0
        /// <summary>
        ///     Internals the resolve.
        /// </summary>
        /// <param name="query"> The query. </param>
        /// <returns> </returns>
        /// <remarks>
        /// </remarks>
        private string[] InternalResolve(FileSearchQuery query)
        {
            if (!query.ExistedOnly)
            {
                return(new[] { ResolveBestPosiblePath(query.PathType, query.ProbePaths[0], query.ProbeFiles[0], query.UserLog) });
            }
            var result = new List <string>();

            if (query.All)
            {
                GetAllMatchedFiles(query, result);
            }
            else
            {
                GetSingleFile(query, result);
            }
            return(result.Distinct().ToArray());
        }
Пример #5
0
 /// <summary>
 ///     Gets the single file.
 /// </summary>
 /// <param name="query"> The query. </param>
 /// <param name="result"> The result. </param>
 /// <remarks>
 /// </remarks>
 private void GetSingleFile(FileSearchQuery query, List <string> result)
 {
     foreach (var probefile in query.ProbeFiles)
     {
         if (0 != result.Count)
         {
             break;
         }
         if (probefile.StartsWith("~/"))
         {
             var path = ResolveStandardPath(probefile.Substring(2));
             query.UserLog.Debug("try " + path, this);
             if (File.Exists(path) || Directory.Exists(path))
             {
                 query.UserLog.Debug("existed", this);
                 result.Add(AdaptFilePath(query.PathType, path));
                 break;
             }
         }
         foreach (var dir in query.ProbePaths)
         {
             query.UserLog.Debug("enter dir " + dir, this);
             var path = (Root + "/" + dir).NormalizePath();
             if (dir.StartsWith("~/"))
             {
                 path = ResolveStandardPath(dir.Substring(2));
             }
             var probe = (path + "/" + probefile).NormalizePath();
             query.UserLog.Debug("try " + probe, Root);
             if (File.Exists(probe) || Directory.Exists(probe))
             {
                 query.UserLog.Debug("existed", this);
                 result.Add(AdaptFilePath(query.PathType, probe));
                 break;
             }
         }
     }
 }
Пример #6
0
		/// <summary>
		/// 	Gets the single file.
		/// </summary>
		/// <param name="query"> The query. </param>
		/// <param name="result"> The result. </param>
		/// <remarks>
		/// </remarks>
		private void GetSingleFile(FileSearchQuery query, List<string> result) {
			foreach (var probefile in query.ProbeFiles) {
				if (0 != result.Count) {
					break;
				}
				if (probefile.StartsWith("~/"))
				{
					var path = ResolveStandardPath(probefile.Substring(2));
					query.UserLog.Debug("try " + path, this);
					if (File.Exists(path) || Directory.Exists(path)) {
						query.UserLog.Debug("existed", this);
						result.Add(AdaptFilePath(query.PathType, path));
						break;
					}
				}
				foreach (var dir in query.ProbePaths) {
					query.UserLog.Debug("enter dir " + dir, this);
					var path = (Root + "/" + dir).NormalizePath();
					if (dir.StartsWith("~/"))
					{
						path = ResolveStandardPath(dir.Substring(2));
					}
					var probe = (path + "/" + probefile).NormalizePath();
					query.UserLog.Debug("try " + probe, Root);
					if (File.Exists(probe) || Directory.Exists(probe)) {
						query.UserLog.Debug("existed", this);
						result.Add(AdaptFilePath(query.PathType, probe));
						break;
					}
				}
			}
		}
Пример #7
0
		/// <summary>
		/// 	Gets all matched files.
		/// </summary>
		/// <param name="query"> The query. </param>
		/// <param name="result"> The result. </param>
		/// <remarks>
		/// </remarks>
		private void GetAllMatchedFiles(FileSearchQuery query, List<string> result) {
			foreach (var probefile in query.ProbeFiles) {
				foreach (var dir in query.ProbePaths) {
					query.UserLog.Debug("enter dir " + dir, this);
					var ndir = dir;
					if (ndir.StartsWith("~/")) {
						ndir = ndir.Substring(2);
					}
					var path = ResolveStandardPath(ndir);
					var pf = probefile;
					if (pf.StartsWith("/")) {
						pf = pf.Substring(1);
					}
					var full = Path.Combine(path, pf);
					var directoryName = Path.GetDirectoryName(full);
					var fileName = Path.GetFileName(full);
					if (directoryName != null && Directory.Exists(directoryName)) {
						if (fileName != null) {
							result.AddRange(Directory.GetFiles(directoryName, fileName).Select(file => AdaptFilePath(query.PathType, file)));
						}
					}
				}
			}
		}
Пример #8
0
		/// <summary>
		/// 	Internals the resolve.
		/// </summary>
		/// <param name="query"> The query. </param>
		/// <returns> </returns>
		/// <remarks>
		/// </remarks>
		private string[] InternalResolve(FileSearchQuery query) {
			if (!query.ExistedOnly) {
				return new[] {ResolveBestPosiblePath(query.PathType, query.ProbePaths[0], query.ProbeFiles[0], query.UserLog)};
			}
			var result = new List<string>();
			if (query.All) {
				GetAllMatchedFiles(query, result);
			}
			else {
				GetSingleFile(query, result);
			}
			return result.Distinct().ToArray();
		}
Пример #9
0
		/// <summary>
		/// 	internal all-resolver
		/// </summary>
		/// <param name="query"> The query. </param>
		/// <returns> </returns>
		/// <remarks>
		/// </remarks>
		protected string[] InternalCachedResolve(FileSearchQuery query) {
			lock (Sync) {
				if (query.ProbeFiles.Length == 1 && query.PathType == FileSearchResultType.FullPath) {
					var directpath = query.ProbeFiles[0];
					//quick return of existed/non existed full path
					if (Path.IsPathRooted(query.ProbeFiles[0])) {
						
						if(query.All && directpath.Contains("*")) {
							var dir = Path.GetDirectoryName(directpath);
							var mask = Path.GetFileName(directpath);
							return Directory.GetFiles(dir, mask);
						}
						if (query.ExistedOnly) {
							if (!(File.Exists(directpath) || Directory.Exists(directpath))) {
								return new string[] {};
							}
						}
						return new[] {directpath};
					}else if(directpath.Contains("..")) {
						if(!EnvironmentInfo.IsWeb) {
							var dir = Path.GetDirectoryName(directpath);
							dir = Path.Combine(Root, dir);
							var mask = Path.GetFileName(directpath);
							return Directory.GetFiles(dir, mask);
						}
					}
				}
				query.UserLog = query.UserLog ?? Log;
				if (null == query.ProbeFiles || 0 == query.ProbeFiles.Length) {
					throw new ArgumentException("empty probe files");
				}
				if (null != query.ProbeFiles.FirstOrDefault(x => x.Contains(".."))) {
					throw new ArgumentException("cannot use .. path modifiers in files");
				}
				query.ProbeFiles = query.ProbeFiles.Select(p=>FileNameResolverExtensions.NormalizePath(p)).ToArray();
				if (null == query.ProbePaths) {
					var ext = Path.GetExtension(query.ProbeFiles.First());
					query.ProbePaths = FileNameResolverExtensions.GetDefaultProbesPaths(ext);
				}
				else {
					if (null != query.ProbePaths.FirstOrDefault(x => x.Contains(".."))) {
						throw new ArgumentException("cannot use .. path modifiers in dirs");
					}
					query.ProbePaths = query.ProbePaths.Select(p=>FileNameResolverExtensions.NormalizePath(p)).ToArray();
				}

				var key = query.ToString();
				query.UserLog.Trace("start resolving files with key " + key, this);
				string[] result;
				if (!_cache.ContainsKey(key) || !query.UseCache) {
					query.UserLog.Debug("no cached Context found", this);
					result = InternalResolve(query);
					Version++;
					Timestamp = DateTime.Now;
					_cache[key] = result;
				}
				result = _cache[key];
				query.UserLog.Trace("resolution finished with " + result, this);
				return result;
			}
		}
Пример #10
0
		/// <summary>
		/// 	Resolves all files match query
		/// </summary>
		/// <param name="query"> search query </param>
		/// <returns> </returns>
		/// <remarks>
		/// </remarks>
		public string[] ResolveAll(FileSearchQuery query) {
			query.All = true;
			return InternalCachedResolve(query);
		}
Пример #11
0
		/// <summary>
		/// 	Resolves first file that match query
		/// </summary>
		/// <param name="query"> search query </param>
		/// <returns> </returns>
		/// <remarks>
		/// </remarks>
		public string Resolve(FileSearchQuery query) {
			return InternalCachedResolve(query).FirstOrDefault();
		}
Пример #12
0
        /// <summary>
        ///     internal all-resolver
        /// </summary>
        /// <param name="query"> The query. </param>
        /// <returns> </returns>
        /// <remarks>
        /// </remarks>
        protected string[] InternalCachedResolve(FileSearchQuery query)
        {
            lock (Sync) {
                if (query.ProbeFiles.Length == 1 && query.PathType == FileSearchResultType.FullPath)
                {
                    var directpath = query.ProbeFiles[0];
                    //quick return of existed/non existed full path
                    if (Path.IsPathRooted(query.ProbeFiles[0]))
                    {
                        if (query.All && directpath.Contains("*"))
                        {
                            var dir  = Path.GetDirectoryName(directpath);
                            var mask = Path.GetFileName(directpath);
                            return(Directory.GetFiles(dir, mask));
                        }
                        if (query.ExistedOnly)
                        {
                            if (!(File.Exists(directpath) || Directory.Exists(directpath)))
                            {
                                return(new string[] {});
                            }
                        }
                        return(new[] { directpath });
                    }
                    else if (directpath.Contains(".."))
                    {
                        if (!EnvironmentInfo.IsWeb)
                        {
                            var dir = Path.GetDirectoryName(directpath);
                            dir = Path.Combine(Root, dir);
                            var mask = Path.GetFileName(directpath);
                            return(Directory.GetFiles(dir, mask));
                        }
                    }
                }
                query.UserLog = query.UserLog ?? Log;
                if (null == query.ProbeFiles || 0 == query.ProbeFiles.Length)
                {
                    throw new ArgumentException("empty probe files");
                }
                if (null != query.ProbeFiles.FirstOrDefault(x => x.Contains("..")))
                {
                    throw new ArgumentException("cannot use .. path modifiers in files");
                }
                query.ProbeFiles = query.ProbeFiles.Select(p => FileNameResolverExtensions.NormalizePath(p)).ToArray();
                if (null == query.ProbePaths)
                {
                    var ext = Path.GetExtension(query.ProbeFiles.First());
                    query.ProbePaths = FileNameResolverExtensions.GetDefaultProbesPaths(ext);
                }
                else
                {
                    if (null != query.ProbePaths.FirstOrDefault(x => x.Contains("..")))
                    {
                        throw new ArgumentException("cannot use .. path modifiers in dirs");
                    }
                    query.ProbePaths = query.ProbePaths.Select(p => FileNameResolverExtensions.NormalizePath(p)).ToArray();
                }

                var key = query.ToString();
                query.UserLog.Trace("start resolving files with key " + key, this);
                string[] result;
                if (!_cache.ContainsKey(key) || !query.UseCache)
                {
                    query.UserLog.Debug("no cached Context found", this);
                    result = InternalResolve(query);
                    Version++;
                    Timestamp   = DateTime.Now;
                    _cache[key] = result;
                }
                result = _cache[key];
                query.UserLog.Trace("resolution finished with " + result, this);
                return(result);
            }
        }
Пример #13
0
 /// <summary>
 ///     Resolves all files match query
 /// </summary>
 /// <param name="query"> search query </param>
 /// <returns> </returns>
 /// <remarks>
 /// </remarks>
 public string[] ResolveAll(FileSearchQuery query)
 {
     query.All = true;
     return(InternalCachedResolve(query));
 }
Пример #14
0
 /// <summary>
 ///     Resolves first file that match query
 /// </summary>
 /// <param name="query"> search query </param>
 /// <returns> </returns>
 /// <remarks>
 /// </remarks>
 public string Resolve(FileSearchQuery query)
 {
     return(InternalCachedResolve(query).FirstOrDefault());
 }
Пример #15
0
 /// <summary>
 ///     Resolves all files match query
 /// </summary>
 /// <param name="query"> search query </param>
 /// <returns> </returns>
 public string[] ResolveAll(FileSearchQuery query)
 {
     return(Fileresolver.ResolveAll(query));
 }
Пример #16
0
 /// <summary>
 ///     Resolves first file that match query
 /// </summary>
 /// <param name="query"> search query </param>
 /// <returns> </returns>
 public string Resolve(FileSearchQuery query)
 {
     return(Fileresolver.Resolve(query));
 }