|
| 1 | +// |
| 2 | +// DiskSizeManager.swift |
| 3 | +// TmpDisk |
| 4 | +// |
| 5 | +// Created on 06/04/25. |
| 6 | +// |
| 7 | +// This file is part of TmpDisk. |
| 8 | +// |
| 9 | +// TmpDisk is free software: you can redistribute it and/or modify |
| 10 | +// it under the terms of the GNU General Public License as published by |
| 11 | +// the Free Software Foundation, either version 3 of the License, or |
| 12 | +// (at your option) any later version. |
| 13 | +// |
| 14 | +// TmpDisk is distributed in the hope that it will be useful, |
| 15 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | +// GNU General Public License for more details. |
| 18 | +// |
| 19 | +// You should have received a copy of the GNU General Public License |
| 20 | +// along with TmpDisk. If not, see <http://www.gnu.org/licenses/>. |
| 21 | + |
| 22 | +import Foundation |
| 23 | +import AppKit |
| 24 | + |
| 25 | +enum DiskSizeUnit: Int { |
| 26 | + case mb = 0 // Megabytes |
| 27 | + case gb = 1 // Gigabytes |
| 28 | + |
| 29 | + var displayName: String { |
| 30 | + switch self { |
| 31 | + case .mb: |
| 32 | + return "MB" |
| 33 | + case .gb: |
| 34 | + return "GB" |
| 35 | + } |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +class DiskSizeManager { |
| 40 | + static let shared = DiskSizeManager() |
| 41 | + |
| 42 | + // MARK: - RAM Size Properties |
| 43 | + |
| 44 | + /// Total physical RAM in megabytes |
| 45 | + var totalRamSizeMB: Double { |
| 46 | + return Double(ProcessInfo.init().physicalMemory) / 1024 / 1024 |
| 47 | + } |
| 48 | + |
| 49 | + /// Maximum allowed size for TmpFS volumes (50% of RAM) in megabytes |
| 50 | + var maxTmpFSSizeMB: Double { |
| 51 | + return totalRamSizeMB * 0.5 |
| 52 | + } |
| 53 | + |
| 54 | + // MARK: - RAM Size Percentages |
| 55 | + |
| 56 | + /// Get RAM size for a specific percentage in MB |
| 57 | + func ramSizeForPercentage(_ percentage: Double) -> Double { |
| 58 | + return totalRamSizeMB * percentage |
| 59 | + } |
| 60 | + |
| 61 | + /// Get the common RAM size percentages (10%, 25%, 50%, 75%) |
| 62 | + func commonRamSizePercentages() -> [Double] { |
| 63 | + return [0.1, 0.25, 0.5, 0.75] |
| 64 | + } |
| 65 | + |
| 66 | + // MARK: - Unit Conversion |
| 67 | + |
| 68 | + /// Convert size from MB to GB |
| 69 | + func convertMBtoGB(_ sizeMB: Double) -> Double { |
| 70 | + return sizeMB / 1000.0 |
| 71 | + } |
| 72 | + |
| 73 | + /// Convert size from GB to MB |
| 74 | + func convertGBtoMB(_ sizeGB: Double) -> Double { |
| 75 | + return sizeGB * 1000.0 |
| 76 | + } |
| 77 | + |
| 78 | + /// Convert size between units |
| 79 | + func convertSize(_ size: Double, from fromUnit: DiskSizeUnit, to toUnit: DiskSizeUnit) -> Double { |
| 80 | + switch (fromUnit, toUnit) { |
| 81 | + case (.mb, .gb): |
| 82 | + return convertMBtoGB(size) |
| 83 | + case (.gb, .mb): |
| 84 | + return convertGBtoMB(size) |
| 85 | + case (.mb, .mb): |
| 86 | + return size |
| 87 | + case (.gb, .gb): |
| 88 | + return size |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // MARK: - String Formatting |
| 93 | + |
| 94 | + /// Format size in MB as a string in the given unit |
| 95 | + func formatSize(_ sizeMB: Double, in unit: DiskSizeUnit) -> String { |
| 96 | + switch unit { |
| 97 | + case .mb: |
| 98 | + return String(Int(sizeMB)) |
| 99 | + case .gb: |
| 100 | + return String(format: "%.2f", convertMBtoGB(sizeMB)) |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + // MARK: - Validation |
| 105 | + |
| 106 | + /// Validates if the size is within the TmpFS limit |
| 107 | + /// - Parameters: |
| 108 | + /// - size: The size to validate |
| 109 | + /// - unit: The unit of the provided size |
| 110 | + /// - Returns: A tuple containing (isValid, correctedSize) |
| 111 | + func validateDiskSize(_ size: Double, in unit: DiskSizeUnit, isTmpFS: Bool = false) -> (isValid: Bool, correctedSizeMB: Double) { |
| 112 | + let sizeInMB = unit == .mb ? size : convertGBtoMB(size) |
| 113 | + |
| 114 | + if isTmpFS && sizeInMB > maxTmpFSSizeMB { |
| 115 | + return (false, maxTmpFSSizeMB) |
| 116 | + } else if sizeInMB < 0 { |
| 117 | + return (false, 1) |
| 118 | + } else if sizeInMB > ( totalRamSizeMB - 2048) { |
| 119 | + // Hold back at least 2GB |
| 120 | + return (false, totalRamSizeMB - 2048) |
| 121 | + } |
| 122 | + |
| 123 | + return (true, sizeInMB) |
| 124 | + } |
| 125 | + |
| 126 | + /// Shows a warning alert about the TmpFS size limitation |
| 127 | + func showTmpFSSizeWarning() { |
| 128 | + let alert = NSAlert() |
| 129 | + alert.messageText = NSLocalizedString("TmpFS volumes are limited to 50% of RAM. Setting to maximum allowed value.", comment: "") |
| 130 | + alert.alertStyle = .informational |
| 131 | + alert.addButton(withTitle: "OK") |
| 132 | + alert.runModal() |
| 133 | + } |
| 134 | + |
| 135 | + func showInsufficientRamWarning() { |
| 136 | + let alert = NSAlert() |
| 137 | + alert.messageText = NSLocalizedString("Insufficient RAM to allocate to TmpDisk. Please reduce the size.", comment: "") |
| 138 | + alert.alertStyle = .warning |
| 139 | + alert.addButton(withTitle: "OK") |
| 140 | + alert.runModal() |
| 141 | + } |
| 142 | +} |
0 commit comments