Пример #1
0
 private void Validate()
 {
     if (Path.IsPathRooted(_path))
     {
         throw new InvalidDataException($"Cannot create relative path from absolute path string, got {_path}");
     }
 }
Пример #2
0
        /// </summary>
        // Iterates through each file entry within the supplied tar,
        // extracting them to the nominated folder.
        /// </summary>
        public static void ExtractTarByEntry(TarInputStream tarIn, string targetDir)
        {
            TarEntry tarEntry;

            while ((tarEntry = tarIn.GetNextEntry()) != null)
            {
                // Converts the unix forward slashes in the filenames to windows backslashes
                string name = tarEntry.Name.Replace('/', Path.DirectorySeparatorChar);

                // Remove any root e.g. '\' because a PathRooted filename defeats Path.Combine
                if (Path.IsPathRooted(name))
                {
                    name = name.Substring(System.IO.Path.GetPathRoot(name).Length);
                }

                // Apply further name transformations here as necessary
                string outName = Path.Combine(targetDir, name);

                string directoryName = Path.GetDirectoryName(outName);

                try {
                    if (tarEntry.IsDirectory)
                    {
                        Directory.CreateDirectory(outName);
                        continue;
                    }

                    // Does nothing if directory exists
                    Directory.CreateDirectory(directoryName);

                    try {
                        using (var outStr = File.Open(outName, FileMode.Create)) {
                            tarIn.CopyEntryContents(outStr);
                        }

                        // Set the modification date/time. This approach seems to solve timezone issues.
                        DateTime myDt = DateTime.SpecifyKind(tarEntry.ModTime, DateTimeKind.Utc);
                        File.SetLastWriteTime(outName, myDt);
                    } catch (NotSupportedException) {
                        Console.WriteLine($"[!] invalid file name: {outName}");
                    } catch (PathTooLongException) {
                        Console.WriteLine($"[!] file name too long?! {outName}");
                    }
                } catch (NotSupportedException) {
                    Console.WriteLine($"[!] invalid directory name: {directoryName}");
                }
            }
        }
Пример #3
0
        public static string GetBaseFolder(string searchText)
        {
            if (string.IsNullOrWhiteSpace(searchText))
            {
                return(string.Empty);
            }

            try
            {
                string path = searchText.Trim();

                if (Directory.Exists(path))
                {
                    return(path);
                }

                path = RemovePrefixes(path);

                int pos = -1;
                if (path.StartsWith("\""))
                {
                    pos = path.IndexOf('"', 1);
                    if (pos > -1)
                    {
                        path = path.Substring(1, pos - 1).Trim();
                    }

                    path = RemovePrefixes(path);
                }
                else
                {
                    pos = path.IndexOf(' ');
                    if (pos > -1)
                    {
                        path = path.Substring(0, pos).Trim();
                    }
                }

                // Check for paths OR'd together
                pos = path.IndexOf('|');
                if (pos > -1)
                {
                    path = path.Substring(0, pos);
                }

                if (!string.IsNullOrWhiteSpace(path))
                {
                    if (Directory.Exists(path) && Path.IsPathRooted(path))
                    {
                        return(path);
                    }

                    if (File.Exists(path))
                    {
                        return(Path.GetDirectoryName(path));
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Error in EverythingSearch GetBaseFolder");
            }

            return(string.Empty);
        }
Пример #4
0
 public override bool IsPathRooted(string path)
 {
     return(AfsPath.IsPathRooted(path));
 }