1000KB readConfigData reads the config from either stdin or the given fileName. It reads up to twice the maximum size of the config ([maxConfigSize]), just in case swarm's limit changes; this is only a safeguard to prevent reading arbitrary files into memory.
(in io.Reader, fileName string)
| 108 | // just in case swarm's limit changes; this is only a safeguard to prevent |
| 109 | // reading arbitrary files into memory. |
| 110 | func readConfigData(in io.Reader, fileName string) ([]byte, error) { |
| 111 | switch fileName { |
| 112 | case "-": |
| 113 | data, err := io.ReadAll(io.LimitReader(in, 2*maxConfigSize)) |
| 114 | if err != nil { |
| 115 | return nil, fmt.Errorf("error reading from STDIN: %w", err) |
| 116 | } |
| 117 | if len(data) == 0 { |
| 118 | return nil, errors.New("error reading from STDIN: data is empty") |
| 119 | } |
| 120 | return data, nil |
| 121 | case "": |
| 122 | return nil, errors.New("config file is required") |
| 123 | default: |
| 124 | // Open file with [FILE_FLAG_SEQUENTIAL_SCAN] on Windows, which |
| 125 | // prevents Windows from aggressively caching it. We expect this |
| 126 | // file to be only read once. Given that this is expected to be |
| 127 | // a small file, this may not be a significant optimization, so |
| 128 | // we could choose to omit this, and use a regular [os.Open]. |
| 129 | // |
| 130 | // [FILE_FLAG_SEQUENTIAL_SCAN]: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-createfilea#FILE_FLAG_SEQUENTIAL_SCAN |
| 131 | f, err := sequential.Open(fileName) |
| 132 | if err != nil { |
| 133 | return nil, fmt.Errorf("error reading from %s: %w", fileName, err) |
| 134 | } |
| 135 | defer f.Close() |
| 136 | data, err := io.ReadAll(io.LimitReader(f, 2*maxConfigSize)) |
| 137 | if err != nil { |
| 138 | return nil, fmt.Errorf("error reading from %s: %w", fileName, err) |
| 139 | } |
| 140 | if len(data) == 0 { |
| 141 | return nil, fmt.Errorf("error reading from %s: data is empty", fileName) |
| 142 | } |
| 143 | return data, nil |
| 144 | } |
| 145 | } |