예제 #1
0
        public void PrintFreeDiskSpace(long freeDiskSpace)
        {
            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("Free space: ");

            Console.ForegroundColor = ConsoleColor.Gray;
            Console.WriteLine(SizeFormatter.Format(freeDiskSpace));

            Console.WriteLine();
        }
예제 #2
0
            /// <summary>
            /// Rebalances files on pool to ensure a good average across all drives.
            /// </summary>
            public void Rebalance()
            {
                var mountPoint = this;

                Logger($"Pool {mountPoint.Name}({mountPoint.Description})");

                var drives = mountPoint.Volumes.ToArray();
                var drivesWithSpaceFree = drives.ToDictionary(d => d, d => d.BytesFree);

                foreach (var drive in drives.OrderBy(i => i.Name))
                {
                    Logger(
                        $@" + Drive {drive.Name} {drive.BytesUsed * 100f / drive.BytesTotal:0.#}% ({
                SizeFormatter.Format(drive.BytesUsed)} used, {
                SizeFormatter.Format(drive.BytesFree)} free, {
                SizeFormatter.Format(drive.BytesTotal)} total)");
                }

                var avgBytesFree = drives.Sum(i => drivesWithSpaceFree[i]) / (ulong)drives.Length;

                Logger($" * Average free {SizeFormatter.Format(avgBytesFree)}");

                const ulong MIN_BYTES_DIFFERENCE_BEFORE_ACTING = 2 * 1024 * 1024UL;

                Logger($" * Difference per drive before balancing {SizeFormatter.Format(MIN_BYTES_DIFFERENCE_BEFORE_ACTING)}");

                if (avgBytesFree < MIN_BYTES_DIFFERENCE_BEFORE_ACTING)
                {
                    return;
                }

                var valueBeforeGettingDataFrom = avgBytesFree - MIN_BYTES_DIFFERENCE_BEFORE_ACTING;
                var valueBeforePuttingDataTo   = avgBytesFree + MIN_BYTES_DIFFERENCE_BEFORE_ACTING;

                while (_DoRebalanceRun(
                           drives,
                           drivesWithSpaceFree,
                           valueBeforeGettingDataFrom,
                           valueBeforePuttingDataTo,
                           avgBytesFree))
                {
                    ;
                }
            }
예제 #3
0
        public void Format_SizeIs2600Megabytes_ReturnsDecimal()
        {
            string sizeString = SizeFormatter.Format(2600000000);

            sizeString.Should().Be("2.6 GB");
        }
예제 #4
0
        public void Format_SizeIs1600Kilobytes_ReturnsDecimal()
        {
            string sizeString = SizeFormatter.Format(1600000);

            sizeString.Should().Be("1.6 MB");
        }
예제 #5
0
        public void Format_SizeIs1500Bytes_ReturnsDecimal()
        {
            string sizeString = SizeFormatter.Format(1500);

            sizeString.Should().Be("1.5 KB");
        }
예제 #6
0
        public void Format_SizeIs12Terabytes_Returns12TB()
        {
            string sizeString = SizeFormatter.Format(12100300123888L);

            sizeString.Should().Be(" 12 TB");
        }
예제 #7
0
        public void Format_SizeIs12Megabytes_Returns12MB()
        {
            string sizeString = SizeFormatter.Format(12100300);

            sizeString.Should().Be(" 12 MB");
        }
예제 #8
0
        public void Format_SizeIs19986MB_ReturnsNoDecimalPortion()
        {
            string sizeString = SizeFormatter.Format(9986000);

            sizeString.Should().Be(" 10 MB");
        }
예제 #9
0
        public void Format_SizeIs512KB_Returns512KB()
        {
            string sizeString = SizeFormatter.Format(512000);

            sizeString.Should().Be("512 KB");
        }
예제 #10
0
        public void Format_SizeIs1000Bytes_Returns1KB()
        {
            string sizeString = SizeFormatter.Format(1000);

            sizeString.Should().Be("1.0 KB");
        }
예제 #11
0
        public void Render(StreamWriter sw)
        {
            if (!isListing())
            {
                sw.WriteLine(ErrorProvider.GetHtml(403));
                LoggerModule.Log(ErrorProvider.GetHtml(403).StripHtml());

                return;
            }

            LoggerModule.Log("Lisiting Directory");

            string content = Resources.Listing;

            var dict = new Dictionary <string, object>();

            dict.Add("Folder", _p.Request.Url.LocalPath);
            dict.Add("Path", _p.Request.Url.AbsoluteUri);

            content    = Template.Render(content, dict);
            folderItem = Template.Render(folderItem, dict);
            fileItem   = Template.Render(fileItem, dict);

            var sb = new StringBuilder();

            var pdi = new DirectoryInfo(_fi.ToString());

            parentItem = parentItem.Replace("{{ParentFolder}}", "..");

            sb.AppendLine(parentItem);

            foreach (var folder in Directory.GetDirectories(_fi.ToString()))
            {
                var di = new DirectoryInfo(folder);

                if (folder != "Listing")
                {
                    sb.AppendLine(folderItem.Replace("{{folder}}", di.Name).Replace("{{Size}}", "0"));
                }
            }
            foreach (var file in Directory.GetFiles(_fi.ToString()))
            {
                var fi = new FileInfo(file);

                if (isIgnored(fi.Name))
                {
                    continue;
                }

                sb.AppendLine(fileItem.Replace("{{File}}", fi.Name).Replace("{{Size}}", SizeFormatter.Format(fi.Length, 2)));
            }

            content = content.Replace("{{Items}}", sb.ToString());

            sw.WriteLine(content);
        }
예제 #12
0
            private static bool _DoRebalanceRun(
                IVolume[] drives,
                IDictionary <IVolume, ulong> drivesWithSpaceFree,
                ulong valueBeforeGettingDataFrom,
                ulong valueBeforePuttingDataTo,
                ulong avgBytesFree
                )
            {
                var drivesToGetFilesFrom = drives.Where(i => drivesWithSpaceFree[i] < valueBeforeGettingDataFrom).ToArray();
                var drivesToPutFilesTo   = drives.Where(i => drivesWithSpaceFree[i] > valueBeforePuttingDataTo).ToArray();

                if (!(drivesToPutFilesTo.Any() && drivesToGetFilesFrom.Any()))
                {
                    return(false);
                }

                Logger($@" * Drives overfilled {string.Join(", ", drivesToGetFilesFrom.Select(i => i.Name))}");
                Logger($@" * Drives underfilled {string.Join(", ", drivesToPutFilesTo.Select(i => i.Name))}");

                var movedAtLeastOneFile = false;

                foreach (var sourceDrive in drivesToGetFilesFrom)
                {
                    // get all files which could be moved somewhere else
                    var files =
                        sourceDrive
                        .Items
                        .EnumerateFiles(true)
                        .Where(t => t.Size >= 4096) /* minimum file size before moving file */
                        .OrderByDescending(t => t.Size)
                        .ToList()
                    ;

                    // as long as the source drive has less than the calculated average bytes free
                    while (drivesWithSpaceFree[sourceDrive] < avgBytesFree)
                    {
                        // calculate how many bytes are left until the average is reached
                        var bestFit = avgBytesFree - drivesWithSpaceFree[sourceDrive];

                        // find the first file, that is nearly big enough
                        var fileToMove = files.FirstOrDefault(f => f.Size <= bestFit);
                        if (fileToMove == null)
                        {
                            Logger($" # No more files available to move");
                            return(movedAtLeastOneFile); /* no file found to move */
                        }

                        var fileSize = fileToMove.Size;

                        // avoid to move file again
                        files.Remove(fileToMove);

                        // find a drive to put the file onto (basically it should not be already there and the drive should have enough free bytes available)
                        var targetDrive =
                            drivesToPutFilesTo.FirstOrDefault(d => drivesWithSpaceFree[d] > fileSize && !fileToMove.ExistsOnDrive(d));
                        if (targetDrive == null)
                        {
                            //logger($@" # Trying to move file {fileToMove.FullName} but it is already present allowed target drive");
                            continue; /* no target drive big enough */
                        }

                        // move file to target drive
                        Logger($" - Moving file {fileToMove.FullName} from {sourceDrive.Name} to {targetDrive.Name}, {SizeFormatter.Format(fileSize)}");
                        fileToMove.MoveToDrive(targetDrive);

                        drivesWithSpaceFree[targetDrive] -= fileSize;
                        drivesWithSpaceFree[sourceDrive] += fileSize;
                        movedAtLeastOneFile = true;
                    }
                } /* next overloaded drive */

                return(movedAtLeastOneFile);
            }
예제 #13
0
 /// <summary>
 /// Formats the long length of a file to a more friendly string, e.g. "1GB", "50 bytes", etc.,
 /// </summary>
 /// <param name="fileSize">The file size for which to determine the format.</param>
 /// <returns>The resulting string.</returns>
 public static string FileSizeFormatted(this long fileSize, int decimals)
 {
     return(SizeFormatter.Format(fileSize, decimals));
 }
예제 #14
0
        public void Format_SizeIs5Bytes_Returns5B()
        {
            string sizeString = SizeFormatter.Format(5);

            sizeString.Should().Be("  5 B ");
        }
예제 #15
0
        public void Format_SizeIs400Bytes_Returns400BWithCorrectTrailingSpace()
        {
            string sizeString = SizeFormatter.Format(400);

            sizeString.Should().Be("400 B ");
        }
예제 #16
0
        public void Format_SizeIs5200Gigabytes_ReturnsDecimal()
        {
            string sizeString = SizeFormatter.Format(5200000000000L);

            sizeString.Should().Be("5.2 TB");
        }
예제 #17
0
        public void Format_SizeIs12Kilobytes_Returns12K()
        {
            string sizeString = SizeFormatter.Format(12100);

            sizeString.Should().Be(" 12 KB");
        }
예제 #18
0
        public void Start(string[] arguments)
        {
            string path = ".";

            if (arguments?.Length > 0)
            {
                path = arguments[0];
            }

            string fullPath = Path.GetFullPath(path);

            _console.WriteLine(fullPath);

            _console.ForegroundColor = ConsoleColor.DarkGray;
            _console.WriteLine(new string( '-', fullPath.Length ));

            _console.ForegroundColor = ConsoleColor.Gray;

            var  fileDescriptors = _fileSystem.GetFiles(path);
            long totalSize       = 0;

            if (fileDescriptors.Length == 0)
            {
                _console.WriteLine("Empty");
            }
            else
            {
                foreach (var fileDescriptor in fileDescriptors)
                {
                    var oldColor = _console.ForegroundColor;

                    if (fileDescriptor.IsDirectory)
                    {
                        string trimmedFile = Path.GetFileName(fileDescriptor.FullPath);

                        _console.ForegroundColor = ColorProvider.FolderColor;
                        _console.Write("Folder");

                        _console.ForegroundColor = ConsoleColor.DarkGray;
                        _console.Write(" | ");

                        _console.ForegroundColor = fileDescriptor.IsHidden ? ColorProvider.HiddenColor : ColorProvider.FolderColor;
                        _console.WriteLine(trimmedFile + "/");
                    }
                    else
                    {
                        string trimmedFile = Path.GetFileName(fileDescriptor.FullPath);

                        totalSize += fileDescriptor.Size;
                        string sizeString = SizeFormatter.Format(fileDescriptor.Size);

                        ConsoleColor extensionColor;

                        if (fileDescriptor.IsHidden)
                        {
                            extensionColor = ColorProvider.HiddenColor;
                        }
                        else
                        {
                            string extension = Path.GetExtension(trimmedFile);
                            extensionColor = ColorProvider.GetColor(extension);
                        }

                        _console.ForegroundColor = extensionColor;
                        _console.Write(sizeString);

                        _console.ForegroundColor = ConsoleColor.DarkGray;
                        _console.Write(" | ");

                        _console.ForegroundColor = extensionColor;
                        _console.WriteLine(trimmedFile);
                    }

                    _console.ForegroundColor = oldColor;
                }
            }

            string totalSizeString = SizeFormatter.Format(totalSize);

            _console.ForegroundColor = ConsoleColor.DarkGray;
            _console.WriteLine("-------+-----------");

            _console.ForegroundColor = ConsoleColor.Gray;
            _console.Write(totalSizeString);

            _console.ForegroundColor = ConsoleColor.DarkGray;
            _console.Write(" | ");

            _console.ForegroundColor = ConsoleColor.Gray;
            _console.WriteLine("Total size");
        }
예제 #19
0
 public static string SizeFormat(int target, int deci)
 {
     return(SizeFormatter.Format(target, deci));
 }