From e84d44d451bb548df42b9fec048fc3fa7edcb804 Mon Sep 17 00:00:00 2001 From: Dmytro Maluka Date: Sun, 27 Jul 2025 02:41:25 +0200 Subject: [PATCH] Move backup & save related stuff from Buffer to SharedBuffer Various methods of Buffer should be rather methods of SharedBuffer. This commit doesn't move all of them to SharedBuffer yet, only those that need to be moved to SharedBuffer in order to be able to request creating or removing backups in other SharedBuffer methods. --- internal/buffer/backup.go | 16 ++++++++-------- internal/buffer/buffer.go | 3 ++- internal/buffer/save.go | 12 ++++++------ 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/internal/buffer/backup.go b/internal/buffer/backup.go index 32180c0a..941ac04f 100644 --- a/internal/buffer/backup.go +++ b/internal/buffer/backup.go @@ -34,13 +34,13 @@ Options: [r]ecover, [i]gnore, [a]bort: ` const backupSeconds = 8 -var BackupCompleteChan chan *Buffer +var BackupCompleteChan chan *SharedBuffer func init() { - BackupCompleteChan = make(chan *Buffer, 10) + BackupCompleteChan = make(chan *SharedBuffer, 10) } -func (b *Buffer) RequestBackup() { +func (b *SharedBuffer) RequestBackup() { if !b.RequestedBackup { select { case backupRequestChan <- b: @@ -51,7 +51,7 @@ func (b *Buffer) RequestBackup() { } } -func (b *Buffer) backupDir() string { +func (b *SharedBuffer) backupDir() string { backupdir, err := util.ReplaceHome(b.Settings["backupdir"].(string)) if backupdir == "" || err != nil { backupdir = filepath.Join(config.ConfigDir, "backups") @@ -59,12 +59,12 @@ func (b *Buffer) backupDir() string { return backupdir } -func (b *Buffer) keepBackup() bool { +func (b *SharedBuffer) keepBackup() bool { return b.forceKeepBackup || b.Settings["permbackup"].(bool) } // Backup saves the current buffer to the backups directory -func (b *Buffer) Backup() error { +func (b *SharedBuffer) Backup() error { if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault { return nil } @@ -101,7 +101,7 @@ func (b *Buffer) Backup() error { } // RemoveBackup removes any backup file associated with this buffer -func (b *Buffer) RemoveBackup() { +func (b *SharedBuffer) RemoveBackup() { if !b.Settings["backup"].(bool) || b.keepBackup() || b.Path == "" || b.Type != BTDefault { return } @@ -111,7 +111,7 @@ func (b *Buffer) RemoveBackup() { // ApplyBackup applies the corresponding backup file to this buffer (if one exists) // Returns true if a backup was applied -func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) { +func (b *SharedBuffer) ApplyBackup(fsize int64) (bool, bool) { if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault { backupfile := util.DetermineEscapePath(b.backupDir(), b.AbsPath) if info, err := os.Stat(backupfile); err == nil { diff --git a/internal/buffer/buffer.go b/internal/buffer/buffer.go index 00056fc0..9bd7ecfa 100644 --- a/internal/buffer/buffer.go +++ b/internal/buffer/buffer.go @@ -123,6 +123,8 @@ type SharedBuffer struct { // Hash of the original buffer -- empty if fastdirty is on origHash [md5.Size]byte + + fini int32 } func (b *SharedBuffer) insert(pos Loc, value []byte) { @@ -223,7 +225,6 @@ type Buffer struct { *EventHandler *SharedBuffer - fini int32 cursors []*Cursor curCursor int StartCursor Loc diff --git a/internal/buffer/save.go b/internal/buffer/save.go index 3c79089a..aaab39ba 100644 --- a/internal/buffer/save.go +++ b/internal/buffer/save.go @@ -47,11 +47,11 @@ type saveRequest struct { } var saveRequestChan chan saveRequest -var backupRequestChan chan *Buffer +var backupRequestChan chan *SharedBuffer func init() { saveRequestChan = make(chan saveRequest, 10) - backupRequestChan = make(chan *Buffer, 10) + backupRequestChan = make(chan *SharedBuffer, 10) go func() { duration := backupSeconds * float64(time.Second) @@ -116,7 +116,7 @@ func openFile(name string, withSudo bool) (wrappedFile, error) { return wrappedFile{writeCloser, withSudo, screenb, cmd, sigChan}, nil } -func (wf wrappedFile) Write(b *Buffer) (int, error) { +func (wf wrappedFile) Write(b *SharedBuffer) (int, error) { file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, b.encoding.NewEncoder())) b.Lock() @@ -184,7 +184,7 @@ func (wf wrappedFile) Close() error { return err } -func (b *Buffer) overwriteFile(name string) (int, error) { +func (b *SharedBuffer) overwriteFile(name string) (int, error) { file, err := openFile(name, false) if err != nil { return 0, err @@ -335,7 +335,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error return err } -func (b *Buffer) writeBackup(path string) (string, error) { +func (b *SharedBuffer) writeBackup(path string) (string, error) { backupDir := b.backupDir() if _, err := os.Stat(backupDir); err != nil { if !errors.Is(err, fs.ErrNotExist) { @@ -360,7 +360,7 @@ func (b *Buffer) writeBackup(path string) (string, error) { // contents of the file if it fails to write the new contents. // This means that the file is not overwritten directly but by writing to the // backup file first. -func (b *Buffer) safeWrite(path string, withSudo bool, newFile bool) (int, error) { +func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int, error) { file, err := openFile(path, withSudo) if err != nil { return 0, err