|
| 1 | +/* |
| 2 | + * Copyright 2024 The CNAI Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package diskspace |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "path/filepath" |
| 23 | + |
| 24 | + "golang.org/x/sys/unix" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // safetyMargin is the extra space ratio to account for metadata overhead |
| 29 | + // (manifests, temporary files, etc.). 10% extra required. |
| 30 | + safetyMargin = 1.1 |
| 31 | +) |
| 32 | + |
| 33 | +// Check checks if the directory has enough disk space for the required bytes. |
| 34 | +// It returns a descriptive error if space is insufficient, or nil if space is enough. |
| 35 | +// The caller should use the returned error for warning purposes only and not |
| 36 | +// treat it as a fatal error. |
| 37 | +func Check(dir string, requiredBytes int64) error { |
| 38 | + if requiredBytes <= 0 { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + // Ensure the directory exists for statfs; walk up to find an existing parent. |
| 43 | + checkDir := dir |
| 44 | + for { |
| 45 | + if _, err := os.Stat(checkDir); err == nil { |
| 46 | + break |
| 47 | + } |
| 48 | + parent := filepath.Dir(checkDir) |
| 49 | + if parent == checkDir { |
| 50 | + // Reached filesystem root without finding an existing directory. |
| 51 | + return fmt.Errorf("cannot determine disk space: no existing directory found for path %s", dir) |
| 52 | + } |
| 53 | + checkDir = parent |
| 54 | + } |
| 55 | + |
| 56 | + var stat unix.Statfs_t |
| 57 | + if err := unix.Statfs(checkDir, &stat); err != nil { |
| 58 | + return fmt.Errorf("failed to check disk space for %s: %w", dir, err) |
| 59 | + } |
| 60 | + |
| 61 | + // Available space for non-root users. |
| 62 | + availableBytes := int64(stat.Bavail) * int64(stat.Bsize) |
| 63 | + requiredWithMargin := int64(float64(requiredBytes) * safetyMargin) |
| 64 | + |
| 65 | + if availableBytes < requiredWithMargin { |
| 66 | + return fmt.Errorf( |
| 67 | + "insufficient disk space in %s: available %s, required %s (with 10%% safety margin)", |
| 68 | + dir, formatBytes(availableBytes), formatBytes(requiredWithMargin), |
| 69 | + ) |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +// formatBytes formats bytes into a human-readable string. |
| 76 | +func formatBytes(bytes int64) string { |
| 77 | + const ( |
| 78 | + kb = 1024 |
| 79 | + mb = kb * 1024 |
| 80 | + gb = mb * 1024 |
| 81 | + tb = gb * 1024 |
| 82 | + ) |
| 83 | + |
| 84 | + switch { |
| 85 | + case bytes >= tb: |
| 86 | + return fmt.Sprintf("%.2f TB", float64(bytes)/float64(tb)) |
| 87 | + case bytes >= gb: |
| 88 | + return fmt.Sprintf("%.2f GB", float64(bytes)/float64(gb)) |
| 89 | + case bytes >= mb: |
| 90 | + return fmt.Sprintf("%.2f MB", float64(bytes)/float64(mb)) |
| 91 | + case bytes >= kb: |
| 92 | + return fmt.Sprintf("%.2f KB", float64(bytes)/float64(kb)) |
| 93 | + default: |
| 94 | + return fmt.Sprintf("%d B", bytes) |
| 95 | + } |
| 96 | +} |
0 commit comments