MCPcopy Index your code
hub / github.com/coder/coder / verifyBinSha1IsCurrent

Function verifyBinSha1IsCurrent

site/bin.go:405–455  ·  view source on GitHub ↗
(dest string, siteFS fs.FS, shaFiles map[string]string)

Source from the content-addressed store, hash-verified

403}
404
405func verifyBinSha1IsCurrent(dest string, siteFS fs.FS, shaFiles map[string]string) (ok bool, err error) {
406 b1, err := fs.ReadFile(siteFS, "bin/coder.sha1")
407 if err != nil {
408 return false, xerrors.Errorf("read coder sha1 from embedded fs failed: %w", err)
409 }
410 b2, err := os.ReadFile(filepath.Join(dest, "coder.sha1"))
411 if err != nil {
412 if xerrors.Is(err, fs.ErrNotExist) {
413 return false, nil
414 }
415 return false, xerrors.Errorf("read coder sha1 failed: %w", err)
416 }
417
418 // Check shasum files for equality for early-exit.
419 if !bytes.Equal(b1, b2) {
420 return false, nil
421 }
422
423 var eg errgroup.Group
424 // Speed up startup by verifying files concurrently. Concurrency
425 // is limited to save resources / early-exit. Early-exit speed
426 // could be improved by using a context aware io.Reader and
427 // passing the context from errgroup.WithContext.
428 eg.SetLimit(3)
429
430 // Verify the hash of each on-disk binary.
431 for file, hash1 := range shaFiles {
432 eg.Go(func() error {
433 hash2, err := sha1HashFile(filepath.Join(dest, file))
434 if err != nil {
435 if xerrors.Is(err, fs.ErrNotExist) {
436 return errHashMismatch
437 }
438 return xerrors.Errorf("hash file failed: %w", err)
439 }
440 if !strings.EqualFold(hash1, hash2) {
441 return errHashMismatch
442 }
443 return nil
444 })
445 }
446 err = eg.Wait()
447 if err != nil {
448 if xerrors.Is(err, errHashMismatch) {
449 return false, nil
450 }
451 return false, err
452 }
453
454 return true, nil
455}
456
457// sha1HashFile computes a SHA1 hash of the file, returning the hex
458// representation.

Callers 1

ExtractOrReadBinFSFunction · 0.85

Calls 7

sha1HashFileFunction · 0.85
GoMethod · 0.80
ReadFileMethod · 0.65
WaitMethod · 0.65
ErrorfMethod · 0.45
IsMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected