callWithCount calls the given countFn with the current count in test_count, and updates it with what the function returns. If the function produced an error, that will be returned instead.
(fn countFn)
| 213 | // |
| 214 | // If the function produced an error, that will be returned instead. |
| 215 | func callWithCount(fn countFn) error { |
| 216 | path, err := path(countFile) |
| 217 | if err != nil { |
| 218 | return err |
| 219 | } |
| 220 | |
| 221 | f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0666) |
| 222 | if err != nil { |
| 223 | return err |
| 224 | } |
| 225 | |
| 226 | contents, err := io.ReadAll(f) |
| 227 | if err != nil { |
| 228 | return err |
| 229 | } |
| 230 | |
| 231 | var n int = 0 |
| 232 | if len(contents) != 0 { |
| 233 | n, err = strconv.Atoi(string(contents)) |
| 234 | if err != nil { |
| 235 | return err |
| 236 | } |
| 237 | |
| 238 | if n < 0 { |
| 239 | return errNegativeCount |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | after, err := fn(n) |
| 244 | if err != nil { |
| 245 | return err |
| 246 | } |
| 247 | |
| 248 | // We want to write over the contents in the file, so "truncate" the |
| 249 | // file to a length of 0, and then seek to the beginning of the file to |
| 250 | // update the write head. |
| 251 | if err := f.Truncate(0); err != nil { |
| 252 | return err |
| 253 | } |
| 254 | if _, err := f.Seek(0, io.SeekStart); err != nil { |
| 255 | return err |
| 256 | } |
| 257 | |
| 258 | if _, err := fmt.Fprintf(f, "%d", after); err != nil { |
| 259 | return err |
| 260 | } |
| 261 | return nil |
| 262 | } |
| 263 | |
| 264 | // path returns an absolute path corresponding to any given path relative to the |
| 265 | // 't' directory of the current checkout of Git LFS. |