Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions internal/buffer/save.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
const LargeFileThreshold = 50000

type wrappedFile struct {
name string
writeCloser io.WriteCloser
withSudo bool
screenb bool
Expand Down Expand Up @@ -83,7 +84,13 @@ func openFile(name string, withSudo bool) (wrappedFile, error) {
var sigChan chan os.Signal

if withSudo {
cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "bs=4k", "of="+name)
conv := "notrunc"
// TODO: both platforms do not support dd with conv=fsync yet
if !(runtime.GOOS == "illumos" || runtime.GOOS == "netbsd") {
conv += ",fsync"
}

cmd = exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "bs=4k", "conv="+conv, "of="+name)
writeCloser, err = cmd.StdinPipe()
if err != nil {
return wrappedFile{}, err
Expand Down Expand Up @@ -113,7 +120,18 @@ func openFile(name string, withSudo bool) (wrappedFile, error) {
}
}

return wrappedFile{writeCloser, withSudo, screenb, cmd, sigChan}, nil
return wrappedFile{name, writeCloser, withSudo, screenb, cmd, sigChan}, nil
}

func (wf wrappedFile) Truncate() error {
if wf.withSudo {
// we don't need to stop the screen here, since it is still stopped
// by openFile()
// truncate might not be available on every platfom, so use dd instead
cmd := exec.Command(config.GlobalSettings["sucmd"].(string), "dd", "count=0", "of="+wf.name)
return cmd.Run()
}
return wf.writeCloser.(*os.File).Truncate(0)
}

func (wf wrappedFile) Write(b *Buffer) (int, error) {
Expand All @@ -134,12 +152,9 @@ func (wf wrappedFile) Write(b *Buffer) (int, error) {
eol = []byte{'\n'}
}

if !wf.withSudo {
f := wf.writeCloser.(*os.File)
err := f.Truncate(0)
if err != nil {
return 0, err
}
err := wf.Truncate()
if err != nil {
return 0, err
}

// write lines
Expand Down Expand Up @@ -374,7 +389,6 @@ func (b *Buffer) safeWrite(path string, withSudo bool, newFile bool) (int, error
}
}()


// Try to backup first before writing
backupName, err := b.writeBackup(path)
if err != nil {
Expand Down
Loading