CreateTestFile creates a test file with the specified size filled with either zeros or random data.
(dir, name string, size int64, random bool)
| 25 | // CreateTestFile creates a test file with the specified size filled |
| 26 | // with either zeros or random data. |
| 27 | func CreateTestFile(dir, name string, size int64, random bool) (string, error) { |
| 28 | path := filepath.Join(dir, name) |
| 29 | |
| 30 | f, err := os.Create(path) |
| 31 | if err != nil { |
| 32 | return "", err |
| 33 | } |
| 34 | defer func() { _ = f.Close() }() |
| 35 | |
| 36 | if random { |
| 37 | // Write in chunks for large files |
| 38 | chunkSize := int64(64 * types.KB) // 64KB |
| 39 | chunk := make([]byte, chunkSize) |
| 40 | remaining := size |
| 41 | |
| 42 | for remaining > 0 { |
| 43 | if remaining < chunkSize { |
| 44 | chunk = make([]byte, remaining) |
| 45 | } |
| 46 | _, _ = rand.Read(chunk) |
| 47 | n, err := f.Write(chunk) |
| 48 | if err != nil { |
| 49 | return "", err |
| 50 | } |
| 51 | remaining -= int64(n) |
| 52 | } |
| 53 | } else { |
| 54 | // Pre-allocate with zeros (sparse file) |
| 55 | if err := f.Truncate(size); err != nil { |
| 56 | return "", err |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | return path, nil |
| 61 | } |
| 62 | |
| 63 | // CreateSurgeFile creates a .surge partial download file for resume testing. |
| 64 | func CreateSurgeFile(dir, name string, totalSize, downloadedSize int64) (string, error) { |