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

Method Write

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

Source from the content-addressed store, hash-verified

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