|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package common |
| 6 | + |
| 7 | +import ( |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "math" |
| 11 | + "regexp" |
| 12 | + "strconv" |
| 13 | + |
| 14 | + "gopkg.in/yaml.v3" |
| 15 | +) |
| 16 | + |
| 17 | +// Common units for sizes in bytes. |
| 18 | +const ( |
| 19 | + Byte = ByteSize(1) |
| 20 | + KiloByte = 1024 * Byte |
| 21 | + MegaByte = 1024 * KiloByte |
| 22 | + GigaByte = 1024 * MegaByte |
| 23 | +) |
| 24 | + |
| 25 | +const ( |
| 26 | + byteString = "B" |
| 27 | + kiloByteString = "KB" |
| 28 | + megaByteString = "MB" |
| 29 | + gigaByteString = "GB" |
| 30 | +) |
| 31 | + |
| 32 | +// ByteSize represents the size of a file. |
| 33 | +type ByteSize uint64 |
| 34 | + |
| 35 | +// Ensure FileSize implements these interfaces. |
| 36 | +var ( |
| 37 | + _ json.Marshaler = new(ByteSize) |
| 38 | + _ json.Unmarshaler = new(ByteSize) |
| 39 | + _ yaml.Marshaler = new(ByteSize) |
| 40 | + _ yaml.Unmarshaler = new(ByteSize) |
| 41 | +) |
| 42 | + |
| 43 | +func parseFileSizeInt(s string) (uint64, error) { |
| 44 | + // os.FileInfo reports size as int64, don't support bigger numbers. |
| 45 | + maxBitSize := 63 |
| 46 | + return strconv.ParseUint(s, 10, maxBitSize) |
| 47 | +} |
| 48 | + |
| 49 | +// MarshalJSON implements the json.Marshaler interface for FileSize, it returns |
| 50 | +// the string representation in a format that can be unmarshaled back to an |
| 51 | +// equivalent value. |
| 52 | +func (s ByteSize) MarshalJSON() ([]byte, error) { |
| 53 | + return []byte(`"` + s.String() + `"`), nil |
| 54 | +} |
| 55 | + |
| 56 | +// MarshalYAML implements the yaml.Marshaler interface for FileSize, it returns |
| 57 | +// the string representation in a format that can be unmarshaled back to an |
| 58 | +// equivalent value. |
| 59 | +func (s ByteSize) MarshalYAML() (interface{}, error) { |
| 60 | + return s.String(), nil |
| 61 | +} |
| 62 | + |
| 63 | +// UnmarshalJSON implements the json.Unmarshaler interface for FileSize. |
| 64 | +func (s *ByteSize) UnmarshalJSON(d []byte) error { |
| 65 | + // Support unquoted plain numbers. |
| 66 | + bytes, err := parseFileSizeInt(string(d)) |
| 67 | + if err == nil { |
| 68 | + *s = ByteSize(bytes) |
| 69 | + return nil |
| 70 | + } |
| 71 | + |
| 72 | + var text string |
| 73 | + err = json.Unmarshal(d, &text) |
| 74 | + if err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + return s.unmarshalString(text) |
| 79 | +} |
| 80 | + |
| 81 | +// UnmarshalYAML implements the yaml.Unmarshaler interface for FileSize. |
| 82 | +func (s *ByteSize) UnmarshalYAML(value *yaml.Node) error { |
| 83 | + // Support unquoted plain numbers. |
| 84 | + bytes, err := parseFileSizeInt(value.Value) |
| 85 | + if err == nil { |
| 86 | + *s = ByteSize(bytes) |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + return s.unmarshalString(value.Value) |
| 91 | +} |
| 92 | + |
| 93 | +var bytesPattern = regexp.MustCompile(fmt.Sprintf(`^(\d+(\.\d+)?)(%s|%s|%s|%s|)$`, byteString, kiloByteString, megaByteString, gigaByteString)) |
| 94 | + |
| 95 | +func (s *ByteSize) unmarshalString(text string) error { |
| 96 | + match := bytesPattern.FindStringSubmatch(text) |
| 97 | + if len(match) < 3 { |
| 98 | + return fmt.Errorf("invalid format for size in bytes (%s)", text) |
| 99 | + } |
| 100 | + |
| 101 | + if match[2] == "" { |
| 102 | + q, err := parseFileSizeInt(match[1]) |
| 103 | + if err != nil { |
| 104 | + return fmt.Errorf("invalid format for size in bytes (%s): %w", text, err) |
| 105 | + } |
| 106 | + |
| 107 | + unit := match[3] |
| 108 | + switch unit { |
| 109 | + case gigaByteString: |
| 110 | + *s = ByteSize(q) * GigaByte |
| 111 | + case megaByteString: |
| 112 | + *s = ByteSize(q) * MegaByte |
| 113 | + case kiloByteString: |
| 114 | + *s = ByteSize(q) * KiloByte |
| 115 | + case byteString, "": |
| 116 | + *s = ByteSize(q) * Byte |
| 117 | + default: |
| 118 | + return fmt.Errorf("invalid unit for filesize (%s): %s", text, unit) |
| 119 | + } |
| 120 | + } else { |
| 121 | + q, err := strconv.ParseFloat(match[1], 64) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("invalid format for size in bytes (%s): %w", text, err) |
| 124 | + } |
| 125 | + |
| 126 | + unit := match[3] |
| 127 | + switch unit { |
| 128 | + case gigaByteString: |
| 129 | + *s = approxFloat(q, GigaByte) |
| 130 | + case megaByteString: |
| 131 | + *s = approxFloat(q, MegaByte) |
| 132 | + case kiloByteString: |
| 133 | + *s = approxFloat(q, KiloByte) |
| 134 | + case byteString, "": |
| 135 | + *s = approxFloat(q, Byte) |
| 136 | + default: |
| 137 | + return fmt.Errorf("invalid unit for filesize (%s): %s", text, unit) |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
| 143 | + |
| 144 | +func approxFloat(n float64, unit ByteSize) ByteSize { |
| 145 | + approx := n * float64(unit) |
| 146 | + return ByteSize(math.Round(approx)) |
| 147 | +} |
| 148 | + |
| 149 | +// String returns the string representation of the FileSize. |
| 150 | +func (s ByteSize) String() string { |
| 151 | + format := func(q ByteSize, unit string) string { |
| 152 | + return fmt.Sprintf("%d%s", q, unit) |
| 153 | + } |
| 154 | + |
| 155 | + if s >= GigaByte && (s%GigaByte == 0) { |
| 156 | + return format(s/GigaByte, gigaByteString) |
| 157 | + } |
| 158 | + |
| 159 | + if s >= MegaByte && (s%MegaByte == 0) { |
| 160 | + return format(s/MegaByte, megaByteString) |
| 161 | + } |
| 162 | + |
| 163 | + if s >= KiloByte && (s%KiloByte == 0) { |
| 164 | + return format(s/KiloByte, kiloByteString) |
| 165 | + } |
| 166 | + |
| 167 | + return format(s, byteString) |
| 168 | +} |
0 commit comments