|
| 1 | +package commitremap |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/tar" |
| 5 | + "compress/gzip" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "runtime" |
| 11 | + "strings" |
| 12 | + |
| 13 | + pgzip "github.com/klauspost/pgzip" |
| 14 | +) |
| 15 | + |
| 16 | +const maxMatchedFileSize = 2 << 30 // 2 GB guard for matched entries |
| 17 | + |
| 18 | +// StreamRemap reads a .tar.gz migration archive, remaps SHAs in matching |
| 19 | +// JSON metadata files in-flight, and writes the result to a new .tar.gz. |
| 20 | +// This eliminates the extract→modify→retar cycle entirely. |
| 21 | +// |
| 22 | +// Files whose base name matches "<prefix>_<digits>.json" for any prefix in |
| 23 | +// prefixes are read into memory, processed by replaceSHABytes, and written |
| 24 | +// back. All other entries are streamed through unchanged. |
| 25 | +// |
| 26 | +// The output archive uses parallel gzip (pgzip) at BestSpeed for fast |
| 27 | +// compression. Tar entry order from the input is preserved. |
| 28 | +func StreamRemap(inArchive, outArchive string, commitMap map[string]string, prefixes []string) (Stats, error) { |
| 29 | + stats := Stats{PerFile: make(map[string]int)} |
| 30 | + |
| 31 | + if len(commitMap) == 0 { |
| 32 | + return stats, fmt.Errorf("commit map is empty; nothing to remap") |
| 33 | + } |
| 34 | + |
| 35 | + shaLen, err := commitMapSHALen(commitMap) |
| 36 | + if err != nil { |
| 37 | + return stats, fmt.Errorf("validating commit map: %w", err) |
| 38 | + } |
| 39 | + |
| 40 | + prefixSet := make(map[string]bool, len(prefixes)) |
| 41 | + for _, p := range prefixes { |
| 42 | + prefixSet[p] = true |
| 43 | + } |
| 44 | + |
| 45 | + // Open input tar.gz |
| 46 | + inFile, err := os.Open(inArchive) |
| 47 | + if err != nil { |
| 48 | + return stats, fmt.Errorf("open input archive: %w", err) |
| 49 | + } |
| 50 | + defer inFile.Close() |
| 51 | + |
| 52 | + gzReader, err := gzip.NewReader(inFile) |
| 53 | + if err != nil { |
| 54 | + return stats, fmt.Errorf("create gzip reader: %w", err) |
| 55 | + } |
| 56 | + defer gzReader.Close() |
| 57 | + |
| 58 | + tarReader := tar.NewReader(gzReader) |
| 59 | + |
| 60 | + // Open output tar.gz |
| 61 | + outFile, err := os.Create(outArchive) |
| 62 | + if err != nil { |
| 63 | + return stats, fmt.Errorf("create output archive: %w", err) |
| 64 | + } |
| 65 | + |
| 66 | + gzWriter, _ := pgzip.NewWriterLevel(outFile, pgzip.BestSpeed) |
| 67 | + gzWriter.SetConcurrency(256<<10, runtime.NumCPU()) |
| 68 | + tarWriter := tar.NewWriter(gzWriter) |
| 69 | + |
| 70 | + var tarClosed, gzClosed, fileClosed bool |
| 71 | + cleanup := func(retErr error) { |
| 72 | + if !tarClosed { |
| 73 | + _ = tarWriter.Close() |
| 74 | + } |
| 75 | + if !gzClosed { |
| 76 | + _ = gzWriter.Close() |
| 77 | + } |
| 78 | + if !fileClosed { |
| 79 | + _ = outFile.Close() |
| 80 | + } |
| 81 | + if retErr != nil { |
| 82 | + _ = os.Remove(outArchive) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + for { |
| 87 | + header, err := tarReader.Next() |
| 88 | + if err == io.EOF { |
| 89 | + break |
| 90 | + } |
| 91 | + if err != nil { |
| 92 | + cleanup(err) |
| 93 | + return stats, fmt.Errorf("reading tar entry: %w", err) |
| 94 | + } |
| 95 | + |
| 96 | + // Safety validation (matching UnTar/ReTarDir behavior) |
| 97 | + if filepath.IsAbs(header.Name) { |
| 98 | + err := fmt.Errorf("archive entry %q has absolute path", header.Name) |
| 99 | + cleanup(err) |
| 100 | + return stats, err |
| 101 | + } |
| 102 | + if pathHasParentRef(header.Name) { |
| 103 | + err := fmt.Errorf("archive entry %q escapes destination", header.Name) |
| 104 | + cleanup(err) |
| 105 | + return stats, err |
| 106 | + } |
| 107 | + if header.Typeflag != tar.TypeDir && header.Typeflag != tar.TypeReg { |
| 108 | + err := fmt.Errorf("unsupported tar entry type %d for %q", header.Typeflag, header.Name) |
| 109 | + cleanup(err) |
| 110 | + return stats, err |
| 111 | + } |
| 112 | + |
| 113 | + // Clone the header to avoid aliasing |
| 114 | + hdr := *header |
| 115 | + |
| 116 | + if hdr.Typeflag == tar.TypeDir { |
| 117 | + if err := tarWriter.WriteHeader(&hdr); err != nil { |
| 118 | + cleanup(err) |
| 119 | + return stats, fmt.Errorf("writing dir header %q: %w", hdr.Name, err) |
| 120 | + } |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + // Check if this file matches a SHA-bearing prefix |
| 125 | + if hdr.Size >= 0 && shouldRemap(hdr.Name, prefixSet) { |
| 126 | + if hdr.Size > maxMatchedFileSize { |
| 127 | + err := fmt.Errorf("matched file %q is too large (%d bytes, max %d)", hdr.Name, hdr.Size, maxMatchedFileSize) |
| 128 | + cleanup(err) |
| 129 | + return stats, err |
| 130 | + } |
| 131 | + |
| 132 | + data, err := io.ReadAll(tarReader) |
| 133 | + if err != nil { |
| 134 | + cleanup(err) |
| 135 | + return stats, fmt.Errorf("reading matched file %q: %w", hdr.Name, err) |
| 136 | + } |
| 137 | + |
| 138 | + data, count := replaceSHABytes(data, commitMap, shaLen) |
| 139 | + stats.FilesScanned++ |
| 140 | + if count > 0 { |
| 141 | + stats.PerFile[hdr.Name] = count |
| 142 | + } |
| 143 | + |
| 144 | + // Size is unchanged (same-length SHA replacement), but set it |
| 145 | + // from actual data length for correctness. |
| 146 | + hdr.Size = int64(len(data)) |
| 147 | + |
| 148 | + if err := tarWriter.WriteHeader(&hdr); err != nil { |
| 149 | + cleanup(err) |
| 150 | + return stats, fmt.Errorf("writing header for %q: %w", hdr.Name, err) |
| 151 | + } |
| 152 | + if _, err := tarWriter.Write(data); err != nil { |
| 153 | + cleanup(err) |
| 154 | + return stats, fmt.Errorf("writing data for %q: %w", hdr.Name, err) |
| 155 | + } |
| 156 | + } else { |
| 157 | + // Pass through unchanged |
| 158 | + if err := tarWriter.WriteHeader(&hdr); err != nil { |
| 159 | + cleanup(err) |
| 160 | + return stats, fmt.Errorf("writing passthrough header %q: %w", hdr.Name, err) |
| 161 | + } |
| 162 | + if hdr.Size > 0 { |
| 163 | + if _, err := io.Copy(tarWriter, tarReader); err != nil { |
| 164 | + cleanup(err) |
| 165 | + return stats, fmt.Errorf("copying passthrough file %q: %w", hdr.Name, err) |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + // Close in order: tar → gzip → file |
| 172 | + if err := tarWriter.Close(); err != nil { |
| 173 | + cleanup(err) |
| 174 | + return stats, fmt.Errorf("closing tar writer: %w", err) |
| 175 | + } |
| 176 | + tarClosed = true |
| 177 | + if err := gzWriter.Close(); err != nil { |
| 178 | + cleanup(err) |
| 179 | + return stats, fmt.Errorf("closing gzip writer: %w", err) |
| 180 | + } |
| 181 | + gzClosed = true |
| 182 | + if err := outFile.Close(); err != nil { |
| 183 | + cleanup(err) |
| 184 | + return stats, fmt.Errorf("closing output file: %w", err) |
| 185 | + } |
| 186 | + fileClosed = true |
| 187 | + |
| 188 | + return stats, nil |
| 189 | +} |
| 190 | + |
| 191 | +// shouldRemap checks if a tar entry name matches "<prefix>_<digits>.json" |
| 192 | +// for any prefix in the set. |
| 193 | +func shouldRemap(name string, prefixSet map[string]bool) bool { |
| 194 | + base := filepath.Base(name) |
| 195 | + if !strings.HasSuffix(base, ".json") { |
| 196 | + return false |
| 197 | + } |
| 198 | + stem := strings.TrimSuffix(base, ".json") |
| 199 | + idx := strings.LastIndex(stem, "_") |
| 200 | + if idx <= 0 { |
| 201 | + return false |
| 202 | + } |
| 203 | + suffix := stem[idx+1:] |
| 204 | + if len(suffix) == 0 { |
| 205 | + return false |
| 206 | + } |
| 207 | + for _, r := range suffix { |
| 208 | + if r < '0' || r > '9' { |
| 209 | + return false |
| 210 | + } |
| 211 | + } |
| 212 | + prefix := stem[:idx] |
| 213 | + return prefixSet[prefix] |
| 214 | +} |
| 215 | + |
| 216 | +// pathHasParentRef checks for ".." path components. |
| 217 | +func pathHasParentRef(name string) bool { |
| 218 | + for _, part := range strings.Split(filepath.ToSlash(name), "/") { |
| 219 | + if part == ".." { |
| 220 | + return true |
| 221 | + } |
| 222 | + } |
| 223 | + return false |
| 224 | +} |
0 commit comments