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.
This commit is contained in:
Dmytro Maluka
2025-07-27 02:41:25 +02:00
parent f938f62e31
commit e84d44d451
3 changed files with 16 additions and 15 deletions

View File

@@ -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 {

View File

@@ -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

View File

@@ -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