| 1 |
package dotgit |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"io" |
| 6 |
"os" |
| 7 |
"sync/atomic" |
| 8 |
|
| 9 |
"github.com/go-git/go-git/v5/plumbing" |
| 10 |
"github.com/go-git/go-git/v5/plumbing/format/idxfile" |
| 11 |
"github.com/go-git/go-git/v5/plumbing/format/objfile" |
| 12 |
"github.com/go-git/go-git/v5/plumbing/format/packfile" |
| 13 |
"github.com/go-git/go-git/v5/plumbing/hash" |
| 14 |
|
| 15 |
"github.com/go-git/go-billy/v5" |
| 16 |
) |
| 17 |
|
| 18 |
// PackWriter is a io.Writer that generates the packfile index simultaneously, |
| 19 |
// a packfile.Decoder is used with a file reader to read the file being written |
| 20 |
// this operation is synchronized with the write operations. |
| 21 |
// The packfile is written in a temp file, when Close is called this file |
| 22 |
// is renamed/moved (depends on the Filesystem implementation) to the final |
| 23 |
// location, if the PackWriter is not used, nothing is written |
| 24 |
type PackWriter struct { |
| 25 |
Notify func(plumbing.Hash, *idxfile.Writer) |
| 26 |
|
| 27 |
fs billy.Filesystem |
| 28 |
fr, fw billy.File |
| 29 |
synced *syncedReader |
| 30 |
checksum plumbing.Hash |
| 31 |
parser *packfile.Parser |
| 32 |
writer *idxfile.Writer |
| 33 |
result chan error |
| 34 |
} |
| 35 |
|
| 36 |
func newPackWrite(fs billy.Filesystem) (*PackWriter, error) { |
| 37 |
fw, err := fs.TempFile(fs.Join(objectsPath, packPath), "tmp_pack_") |
| 38 |
if err != nil { |
| 39 |
return nil, err |
| 40 |
} |
| 41 |
|
| 42 |
fr, err := fs.Open(fw.Name()) |
| 43 |
if err != nil { |
| 44 |
return nil, err |
| 45 |
} |
| 46 |
|
| 47 |
writer := &PackWriter{ |
| 48 |
fs: fs, |
| 49 |
fw: fw, |
| 50 |
fr: fr, |
| 51 |
synced: newSyncedReader(fw, fr), |
| 52 |
result: make(chan error), |
| 53 |
} |
| 54 |
|
| 55 |
go writer.buildIndex() |
| 56 |
return writer, nil |
| 57 |
} |
| 58 |
|
| 59 |
func (w *PackWriter) buildIndex() { |
| 60 |
s := packfile.NewScanner(w.synced) |
| 61 |
w.writer = new(idxfile.Writer) |
| 62 |
var err error |
| 63 |
w.parser, err = packfile.NewParser(s, w.writer) |
| 64 |
if err != nil { |
| 65 |
w.result <- err |
| 66 |
return |
| 67 |
} |
| 68 |
|
| 69 |
checksum, err := w.parser.Parse() |
| 70 |
if err != nil { |
| 71 |
w.result <- err |
| 72 |
return |
| 73 |
} |
| 74 |
|
| 75 |
w.checksum = checksum |
| 76 |
w.result <- err |
| 77 |
} |
| 78 |
|
| 79 |
// waitBuildIndex waits until buildIndex function finishes, this can terminate |
| 80 |
// with a packfile.ErrEmptyPackfile, this means that nothing was written so we |
| 81 |
// ignore the error |
| 82 |
func (w *PackWriter) waitBuildIndex() error { |
| 83 |
err := <-w.result |
| 84 |
if err == packfile.ErrEmptyPackfile { |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
return err |
| 89 |
} |
| 90 |
|
| 91 |
func (w *PackWriter) Write(p []byte) (int, error) { |
| 92 |
return w.synced.Write(p) |
| 93 |
} |
| 94 |
|
| 95 |
// Close closes all the file descriptors and save the final packfile, if nothing |
| 96 |
// was written, the tempfiles are deleted without writing a packfile. |
| 97 |
func (w *PackWriter) Close() error { |
| 98 |
defer func() { |
| 99 |
if w.Notify != nil && w.writer != nil && w.writer.Finished() { |
| 100 |
w.Notify(w.checksum, w.writer) |
| 101 |
} |
| 102 |
|
| 103 |
close(w.result) |
| 104 |
}() |
| 105 |
|
| 106 |
if err := w.synced.Close(); err != nil { |
| 107 |
return err |
| 108 |
} |
| 109 |
|
| 110 |
if err := w.waitBuildIndex(); err != nil { |
| 111 |
return err |
| 112 |
} |
| 113 |
|
| 114 |
if err := w.fr.Close(); err != nil { |
| 115 |
return err |
| 116 |
} |
| 117 |
|
| 118 |
if err := w.fw.Close(); err != nil { |
| 119 |
return err |
| 120 |
} |
| 121 |
|
| 122 |
if w.writer == nil || !w.writer.Finished() { |
| 123 |
return w.clean() |
| 124 |
} |
| 125 |
|
| 126 |
return w.save() |
| 127 |
} |
| 128 |
|
| 129 |
func (w *PackWriter) clean() error { |
| 130 |
return w.fs.Remove(w.fw.Name()) |
| 131 |
} |
| 132 |
|
| 133 |
func (w *PackWriter) save() error { |
| 134 |
base := w.fs.Join(objectsPath, packPath, fmt.Sprintf("pack-%s", w.checksum)) |
| 135 |
|
| 136 |
// Pack files are content addressable. Each file is checked |
| 137 |
// individually — if it already exists on disk, skip creating it. |
| 138 |
idxPath := fmt.Sprintf("%s.idx", base) |
| 139 |
exists, err := fileExists(w.fs, idxPath) |
| 140 |
if err != nil { |
| 141 |
return err |
| 142 |
} |
| 143 |
if !exists { |
| 144 |
idx, err := w.fs.Create(idxPath) |
| 145 |
if err != nil { |
| 146 |
return err |
| 147 |
} |
| 148 |
|
| 149 |
if err := w.encodeIdx(idx); err != nil { |
| 150 |
_ = idx.Close() |
| 151 |
return err |
| 152 |
} |
| 153 |
|
| 154 |
if err := idx.Close(); err != nil { |
| 155 |
return err |
| 156 |
} |
| 157 |
fixPermissions(w.fs, idxPath) |
| 158 |
} |
| 159 |
|
| 160 |
packPath := fmt.Sprintf("%s.pack", base) |
| 161 |
exists, err = fileExists(w.fs, packPath) |
| 162 |
if err != nil { |
| 163 |
return err |
| 164 |
} |
| 165 |
if !exists { |
| 166 |
if err := w.fs.Rename(w.fw.Name(), packPath); err != nil { |
| 167 |
return err |
| 168 |
} |
| 169 |
fixPermissions(w.fs, packPath) |
| 170 |
} else { |
| 171 |
// Pack already exists, clean up the temp file. |
| 172 |
return w.clean() |
| 173 |
} |
| 174 |
|
| 175 |
return nil |
| 176 |
} |
| 177 |
|
| 178 |
// fileExists checks whether path already exists as a regular file. |
| 179 |
// It returns (true, nil) for an existing regular file, (false, nil) when the |
| 180 |
// path does not exist, and (false, err) if the path exists but is not a |
| 181 |
// regular file (e.g. a directory or symlink). |
| 182 |
func fileExists(fs billy.Filesystem, path string) (bool, error) { |
| 183 |
fi, err := fs.Lstat(path) |
| 184 |
if err != nil { |
| 185 |
return false, nil |
| 186 |
} |
| 187 |
if !fi.Mode().IsRegular() { |
| 188 |
return false, fmt.Errorf("unexpected file type for %q: %s", path, fi.Mode().Type()) |
| 189 |
} |
| 190 |
return true, nil |
| 191 |
} |
| 192 |
|
| 193 |
func (w *PackWriter) encodeIdx(writer io.Writer) error { |
| 194 |
idx, err := w.writer.Index() |
| 195 |
if err != nil { |
| 196 |
return err |
| 197 |
} |
| 198 |
|
| 199 |
e := idxfile.NewEncoder(writer) |
| 200 |
_, err = e.Encode(idx) |
| 201 |
return err |
| 202 |
} |
| 203 |
|
| 204 |
type syncedReader struct { |
| 205 |
w io.Writer |
| 206 |
r io.ReadSeeker |
| 207 |
|
| 208 |
blocked, done uint32 |
| 209 |
written, read uint64 |
| 210 |
news chan bool |
| 211 |
} |
| 212 |
|
| 213 |
func newSyncedReader(w io.Writer, r io.ReadSeeker) *syncedReader { |
| 214 |
return &syncedReader{ |
| 215 |
w: w, |
| 216 |
r: r, |
| 217 |
news: make(chan bool), |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
func (s *syncedReader) Write(p []byte) (n int, err error) { |
| 222 |
defer func() { |
| 223 |
written := atomic.AddUint64(&s.written, uint64(n)) |
| 224 |
read := atomic.LoadUint64(&s.read) |
| 225 |
if written > read { |
| 226 |
s.wake() |
| 227 |
} |
| 228 |
}() |
| 229 |
|
| 230 |
n, err = s.w.Write(p) |
| 231 |
return |
| 232 |
} |
| 233 |
|
| 234 |
func (s *syncedReader) Read(p []byte) (n int, err error) { |
| 235 |
defer func() { atomic.AddUint64(&s.read, uint64(n)) }() |
| 236 |
|
| 237 |
for { |
| 238 |
s.sleep() |
| 239 |
n, err = s.r.Read(p) |
| 240 |
if err == io.EOF && !s.isDone() && n == 0 { |
| 241 |
continue |
| 242 |
} |
| 243 |
|
| 244 |
break |
| 245 |
} |
| 246 |
|
| 247 |
return |
| 248 |
} |
| 249 |
|
| 250 |
func (s *syncedReader) isDone() bool { |
| 251 |
return atomic.LoadUint32(&s.done) == 1 |
| 252 |
} |
| 253 |
|
| 254 |
func (s *syncedReader) isBlocked() bool { |
| 255 |
return atomic.LoadUint32(&s.blocked) == 1 |
| 256 |
} |
| 257 |
|
| 258 |
func (s *syncedReader) wake() { |
| 259 |
if s.isBlocked() { |
| 260 |
atomic.StoreUint32(&s.blocked, 0) |
| 261 |
s.news <- true |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
func (s *syncedReader) sleep() { |
| 266 |
read := atomic.LoadUint64(&s.read) |
| 267 |
written := atomic.LoadUint64(&s.written) |
| 268 |
if read >= written { |
| 269 |
atomic.StoreUint32(&s.blocked, 1) |
| 270 |
<-s.news |
| 271 |
} |
| 272 |
} |
| 273 |
|
| 274 |
func (s *syncedReader) Seek(offset int64, whence int) (int64, error) { |
| 275 |
if whence == io.SeekCurrent { |
| 276 |
return s.r.Seek(offset, whence) |
| 277 |
} |
| 278 |
|
| 279 |
p, err := s.r.Seek(offset, whence) |
| 280 |
atomic.StoreUint64(&s.read, uint64(p)) |
| 281 |
|
| 282 |
return p, err |
| 283 |
} |
| 284 |
|
| 285 |
func (s *syncedReader) Close() error { |
| 286 |
atomic.StoreUint32(&s.done, 1) |
| 287 |
close(s.news) |
| 288 |
return nil |
| 289 |
} |
| 290 |
|
| 291 |
type ObjectWriter struct { |
| 292 |
objfile.Writer |
| 293 |
fs billy.Filesystem |
| 294 |
f billy.File |
| 295 |
} |
| 296 |
|
| 297 |
func newObjectWriter(fs billy.Filesystem) (*ObjectWriter, error) { |
| 298 |
f, err := fs.TempFile(fs.Join(objectsPath, packPath), "tmp_obj_") |
| 299 |
if err != nil { |
| 300 |
return nil, err |
| 301 |
} |
| 302 |
|
| 303 |
return &ObjectWriter{ |
| 304 |
Writer: (*objfile.NewWriter(f)), |
| 305 |
fs: fs, |
| 306 |
f: f, |
| 307 |
}, nil |
| 308 |
} |
| 309 |
|
| 310 |
func (w *ObjectWriter) Close() error { |
| 311 |
if err := w.Writer.Close(); err != nil { |
| 312 |
return err |
| 313 |
} |
| 314 |
|
| 315 |
if err := w.f.Close(); err != nil { |
| 316 |
return err |
| 317 |
} |
| 318 |
|
| 319 |
return w.save() |
| 320 |
} |
| 321 |
|
| 322 |
func (w *ObjectWriter) save() error { |
| 323 |
hex := w.Hash().String() |
| 324 |
file := w.fs.Join(objectsPath, hex[0:2], hex[2:hash.HexSize]) |
| 325 |
|
| 326 |
// Loose objects are content addressable, if they already exist |
| 327 |
// we can safely delete the temporary file and short-circuit the |
| 328 |
// operation. |
| 329 |
if _, err := w.fs.Lstat(file); err == nil || os.IsExist(err) { |
| 330 |
return w.fs.Remove(w.f.Name()) |
| 331 |
} |
| 332 |
|
| 333 |
if err := w.fs.Rename(w.f.Name(), file); err != nil { |
| 334 |
return err |
| 335 |
} |
| 336 |
fixPermissions(w.fs, file) |
| 337 |
|
| 338 |
return nil |
| 339 |
} |