Esempio n. 1
0
        public ulong GetSpaceRequiredToBeFreed(string directoryPath)
        {
            if (CalamariEnvironment.IsRunningOnMono && CalamariEnvironment.IsRunningOnMac)
            {
                Log.Verbose("Unable to determine disk free space under Mono on macOS.  Assuming there's enough.");
                return(0);
            }

            var requiredSpaceInBytes = GetRequiredSpaceInBytes(out var spaceOverrideSpecified);

            if (spaceOverrideSpecified)
            {
                Log.Verbose($"{FreeDiskSpaceOverrideInMegaBytesVariable} has been specified. We will check and ensure that the drive containing the directory '{directoryPath}' on machine '{Environment.MachineName}' has {((ulong)requiredSpaceInBytes).ToFileSizeString()} free disk space.");
            }

            var success = fileSystem.GetDiskFreeSpace(directoryPath, out ulong totalNumberOfFreeBytes);

            if (!success)
            {
                Log.Verbose("Unable to determine disk free space.  Assuming there's enough.");
                return(0);
            }

            if (totalNumberOfFreeBytes < requiredSpaceInBytes)
            {
                return(requiredSpaceInBytes - totalNumberOfFreeBytes);
            }

            return(0);
        }
        public IEnumerable <PackageIdentity> GetPackagesToRemove(IEnumerable <JournalEntry> journalEntries)
        {
            if (!fileSystem.GetDiskFreeSpace(packageUtils.RootDirectory, out var totalNumberOfFreeBytes) || !fileSystem.GetDiskTotalSpace(packageUtils.RootDirectory, out var totalNumberOfBytes))
            {
                log.Info("Unable to determine disk space. Skipping free space package retention.");
                return(new PackageIdentity[0]);
            }

            var percentFreeDiskSpaceDesired = variables.GetInt32(PackageRetentionPercentFreeDiskSpace) ?? DefaultPercentFreeDiskSpace;
            var desiredSpaceInBytes         = totalNumberOfBytes * (ulong)percentFreeDiskSpaceDesired / 100;

            if (totalNumberOfFreeBytes > desiredSpaceInBytes)
            {
                log.VerboseFormat("Detected enough space for new packages. ({0}/{1})", totalNumberOfFreeBytes.ToFileSizeString(), totalNumberOfBytes.ToFileSizeString());
                return(new PackageIdentity[0]);
            }

            var spaceToFree = (desiredSpaceInBytes - totalNumberOfFreeBytes) * (100 + FreeSpacePercentBuffer) / 100;

            log.VerboseFormat("Cleaning {0} space from the package cache.", spaceToFree.ToFileSizeString());
            ulong spaceFreed            = 0L;
            var   orderedJournalEntries = sortJournalEntries.Sort(journalEntries);

            return(orderedJournalEntries.TakeWhile(entry =>
            {
                var moreToClean = spaceFreed < spaceToFree;
                spaceFreed += entry.FileSizeBytes;
                return moreToClean;
            })
                   .Select(entry => entry.Package));
        }
Esempio n. 3
0
        public void EnsureDiskHasEnoughFreeSpace(string directoryPath)
        {
            if (CalamariEnvironment.IsRunningOnMono && CalamariEnvironment.IsRunningOnMac)
            {
                //After upgrading to macOS 10.15.2, and mono 5.14.0, drive.TotalFreeSpace and drive.AvailableFreeSpace both started returning 0.
                //see https://github.com/mono/mono/issues/17151, which was fixed in mono 6.4.xx
                //If we upgrade mono past 5.14.x, scriptcs stops working.
                //Rock and a hard place.
                Log.Verbose("Unable to determine disk free space under Mono on macOS. Assuming there's enough.");
                return;
            }

            if (variables.GetFlag(skipFreeDiskSpaceCheckVariable))
            {
                Log.Verbose($"{skipFreeDiskSpaceCheckVariable} is enabled. The check to ensure that the drive containing the directory '{directoryPath}' on machine '{Environment.MachineName}' has enough free space will be skipped.");
                return;
            }

            ulong requiredSpaceInBytes         = 500L * 1024 * 1024;
            var   freeSpaceOverrideInMegaBytes = variables.GetInt32(freeDiskSpaceOverrideInMegaBytesVariable);

            if (freeSpaceOverrideInMegaBytes.HasValue)
            {
                requiredSpaceInBytes = (ulong)freeSpaceOverrideInMegaBytes * 1024 * 1024;
                Log.Verbose($"{freeDiskSpaceOverrideInMegaBytesVariable} has been specified. We will check and ensure that the drive containing the directory '{directoryPath}' on machine '{Environment.MachineName}' has {((ulong) requiredSpaceInBytes).ToFileSizeString()} free disk space.");
            }

            var success = fileSystem.GetDiskFreeSpace(directoryPath, out ulong totalNumberOfFreeBytes);

            if (!success)
            {
                return;
            }

            if (totalNumberOfFreeBytes < requiredSpaceInBytes)
            {
                throw new CommandException($"The drive containing the directory '{directoryPath}' on machine '{Environment.MachineName}' does not have enough free disk space available for this operation to proceed. The disk only has {totalNumberOfFreeBytes.ToFileSizeString()} available; please free up at least {requiredSpaceInBytes.ToFileSizeString()}.");
            }
        }