MCPcopy Create free account
hub / github.com/alibaba/open-code-review / Write

Method Write

cmd/opencodereview/shared.go:618–673  ·  view source on GitHub ↗
(p []byte)

Source from the content-addressed store, hash-verified

616}
617
618func (w *lazyFileWriter) Write(p []byte) (int, error) {
619 if w.closed {
620 return 0, os.ErrClosed
621 }
622 w.once.Do(func() {
623 commitPath, err := resolveOutputCommitPath(w.path)
624 if err != nil {
625 w.err = err
626 return
627 }
628
629 var existingMode os.FileMode
630 hasExistingMode := false
631 if info, statErr := os.Stat(commitPath); statErr == nil {
632 existingMode = info.Mode() & (os.ModePerm | os.ModeSetuid | os.ModeSetgid | os.ModeSticky)
633 hasExistingMode = true
634 } else if !os.IsNotExist(statErr) {
635 w.err = fmt.Errorf("inspect output file %s: %w", w.path, statErr)
636 return
637 }
638
639 f, err := createOutputTemp(filepath.Dir(commitPath))
640 if err != nil {
641 w.err = fmt.Errorf("create temporary output file for %s: %w", w.path, err)
642 return
643 }
644 if hasExistingMode {
645 if err := f.Chmod(existingMode); err != nil {
646 cleanupErr := cleanupOutputTemp(f, f.Name())
647 w.err = errors.Join(fmt.Errorf("preserve output permissions for %s: %w", w.path, err), cleanupErr)
648 return
649 }
650 }
651
652 w.file = f
653 w.tempPath = f.Name()
654 w.commitPath = commitPath
655 if w.strip {
656 w.stripper = &stripAnsiWriter{dst: f}
657 }
658 })
659 if w.err != nil {
660 return 0, w.err
661 }
662 var n int
663 var err error
664 if w.stripper != nil {
665 n, err = w.stripper.Write(p)
666 } else {
667 n, err = w.file.Write(p)
668 }
669 if err != nil && w.writeErr == nil {
670 w.writeErr = err
671 }
672 return n, err
673}
674
675// Err returns the first error encountered while creating or writing the

Callers 1

Calls 5

resolveOutputCommitPathFunction · 0.85
createOutputTempFunction · 0.85
cleanupOutputTempFunction · 0.85
WriteMethod · 0.65
NameMethod · 0.45

Tested by 1