Esempio n. 1
0
        private long SizeOnDisk(FileInfo path)
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                try
                {
                    uint clusterSize = 0;
                    var  root        = path.Directory.Root.FullName;
                    if (!ClusterSizes.ContainsKey(root))
                    {
                        ClusterSizes[root] = 0;
                        NativeMethods.GetDiskFreeSpace(root, out uint lpSectorsPerCluster, out uint lpBytesPerSector, out _, out _);
                        ClusterSizes[root] = lpSectorsPerCluster * lpBytesPerSector;
                    }
                    clusterSize = ClusterSizes[root];

                    if (clusterSize > 0)
                    {
                        uint lowSize = NativeMethods.GetCompressedFileSizeW(path.FullName, out uint highSize);
                        long size    = (long)highSize << 32 | lowSize;
                        return(((size + clusterSize - 1) / clusterSize) * clusterSize);
                    }
                }
                catch (Exception e)
                {
                    Log.Debug("Failed to GetDiskFreeSpace for {0} ({1}:{2})", path.FullName, e.GetType(), e.Message);
                }
                return(-1);
            }
            else
            {
                if (sizesOnDisk.ContainsKey(path.FullName))
                {
                    return(sizesOnDisk[path.FullName]);
                }
                var exitCode = ExternalCommandRunner.RunExternalCommand("du", path.FullName, out string StdOut, out string StdErr);
                if (exitCode == 0 && long.TryParse(StdOut.Split('\t')[0], out long result))
                {
                    return(result);
                }
                else
                {
                    return(-1);
                }
            }
        }
Esempio n. 2
0
        protected override void OutputHandler(object sendingProcess, System.Diagnostics.DataReceivedEventArgs ConsoleOutput)
        {
            try
            {
                string StdOut;
                float  perc;

                base.OutputHandler(sendingProcess, ConsoleOutput);
                if (ConsoleOutput.Data == null)
                {
                    return;
                }

                if (!String.IsNullOrEmpty(ConsoleOutput.Data))
                {
                    StdOut = ConsoleOutput.Data;
                    if (StdOut.Contains("%") && StdOut.Contains("|") && StdOut.Contains(":"))
                    {
                        string[] details = StdOut.Split('|');

                        // Update the % completion
                        string percStr = details[0].Substring(0, details[0].IndexOf('%'));
                        float.TryParse(percStr, System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out perc);
                        _jobStatus.PercentageComplete = perc;
                        UpdateETAByPercentageComplete();
                    }

                    // Potential format reading error
                    if (StdOut.Contains("Not a recognized header. Searching for next header."))
                    {
                        _formatError = true;
                    }

                    // Success Criteria
                    if (StdOut.Contains("Done, processing time"))
                    {
                        _success = true;
                    }
                }
            }
            catch (Exception e)
            {
                _jobLog.WriteEntry(this, "ERROR Processing Console Output.\n" + e.ToString(), Log.LogEntryType.Error);
            }
        }
Esempio n. 3
0
 private static long SizeOnDisk(string path)
 {
     if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
     {
         FileInfo info        = new FileInfo(path);
         uint     clusterSize = ClusterSizes[info.Directory.Root.FullName];
         uint     lowSize     = GetCompressedFileSizeW(path, out uint highSize);
         long     size        = (long)highSize << 32 | lowSize;
         return(((size + clusterSize - 1) / clusterSize) * clusterSize);
     }
     else
     {
         var exitCode = ExternalCommandRunner.RunExternalCommand("du", path, out string StdOut, out string StdErr);
         if (exitCode == 0 && long.TryParse(StdOut.Split('\t')[0], out long result))
         {
             return(result);
         }
         else
         {
             return(0);
         }
     }
 }
Esempio n. 4
0
        public override void ExecuteInternal()
        {
            if (!roots.Any())
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    foreach (var driveInfo in DriveInfo.GetDrives())
                    {
                        if (driveInfo.IsReady && driveInfo.DriveType == DriveType.Fixed)
                        {
                            roots.Add(driveInfo.Name);
                        }
                    }
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    roots.Add("/");
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    roots.Add("/");
                }
            }
            Action <string>?IterateOnDirectory = null;

            IterateOnDirectory = Path =>
            {
                Log.Verbose("Started parsing {0}", Path);

                // To optimize calls to du on non-windows platforms we run du on the whole directory ahead of time
                if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var exitCode = ExternalCommandRunner.RunExternalCommand("du", Path, out string StdOut, out string StdErr);
                    if (exitCode == 0)
                    {
                        foreach (var line in StdOut.Split(Environment.NewLine))
                        {
                            var fields = line.Split('\t');
                            if (long.TryParse(fields[0], out long result))
                            {
                                sizesOnDisk[fields[1]] = result;
                            }
                        }
                    }
                }

                var files = Directory.EnumerateFiles(Path, "*", new System.IO.EnumerationOptions()
                {
                    IgnoreInaccessible = true
                });
                foreach (var file in files)
                {
                    StallIfHighMemoryUsageAndLowMemoryModeEnabled();
                    Log.Verbose("Started parsing {0}", file);
                    FileSystemObject obj = FilePathToFileSystemObject(file);
                    if (obj != null)
                    {
                        Results.Push(obj);

                        // TODO: Also try parse .DER as a key
                        if (Path.EndsWith(".cer", StringComparison.CurrentCulture) ||
                            Path.EndsWith(".der", StringComparison.CurrentCulture) ||
                            Path.EndsWith(".p7b", StringComparison.CurrentCulture) ||
                            Path.EndsWith(".pfx", StringComparison.CurrentCulture))
                        {
                            try
                            {
                                using var certificate = new X509Certificate2(Path);

                                var certObj = new CertificateObject(
                                    StoreLocation: StoreLocation.LocalMachine.ToString(),
                                    StoreName: StoreName.Root.ToString(),
                                    Certificate: new SerializableCertificate(certificate));

                                Results.Push(certObj);
                            }
                            catch (Exception e)
                            {
                                Log.Verbose($"Could not parse certificate from file: {file}, {e.GetType().ToString()}");
                            }
                        }
                    }
                    Log.Verbose("Finished parsing {0}", file);
                }

                Log.Verbose("Finished parsing {0}", Path);
            };

            foreach (var root in roots)
            {
                Log.Information("{0} root {1}", Strings.Get("Scanning"), root);
                var directories = Directory.EnumerateDirectories(root, "*", new System.IO.EnumerationOptions()
                {
                    ReturnSpecialDirectories = false,
                    IgnoreInaccessible       = true,
                    RecurseSubdirectories    = true
                });

                //First do root
                IterateOnDirectory?.Invoke(root);

                if (!opts.SingleThread == true)
                {
                    Parallel.ForEach(directories, filePath =>
                    {
                        IterateOnDirectory?.Invoke(filePath);
                    });
                }
                else
                {
                    foreach (var filePath in directories)
                    {
                        IterateOnDirectory?.Invoke(filePath);
                    }
                }
            }
        }