Skip to content

Commit e84d44d

Browse files
committed
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.
1 parent f938f62 commit e84d44d

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

internal/buffer/backup.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ Options: [r]ecover, [i]gnore, [a]bort: `
3434

3535
const backupSeconds = 8
3636

37-
var BackupCompleteChan chan *Buffer
37+
var BackupCompleteChan chan *SharedBuffer
3838

3939
func init() {
40-
BackupCompleteChan = make(chan *Buffer, 10)
40+
BackupCompleteChan = make(chan *SharedBuffer, 10)
4141
}
4242

43-
func (b *Buffer) RequestBackup() {
43+
func (b *SharedBuffer) RequestBackup() {
4444
if !b.RequestedBackup {
4545
select {
4646
case backupRequestChan <- b:
@@ -51,20 +51,20 @@ func (b *Buffer) RequestBackup() {
5151
}
5252
}
5353

54-
func (b *Buffer) backupDir() string {
54+
func (b *SharedBuffer) backupDir() string {
5555
backupdir, err := util.ReplaceHome(b.Settings["backupdir"].(string))
5656
if backupdir == "" || err != nil {
5757
backupdir = filepath.Join(config.ConfigDir, "backups")
5858
}
5959
return backupdir
6060
}
6161

62-
func (b *Buffer) keepBackup() bool {
62+
func (b *SharedBuffer) keepBackup() bool {
6363
return b.forceKeepBackup || b.Settings["permbackup"].(bool)
6464
}
6565

6666
// Backup saves the current buffer to the backups directory
67-
func (b *Buffer) Backup() error {
67+
func (b *SharedBuffer) Backup() error {
6868
if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
6969
return nil
7070
}
@@ -101,7 +101,7 @@ func (b *Buffer) Backup() error {
101101
}
102102

103103
// RemoveBackup removes any backup file associated with this buffer
104-
func (b *Buffer) RemoveBackup() {
104+
func (b *SharedBuffer) RemoveBackup() {
105105
if !b.Settings["backup"].(bool) || b.keepBackup() || b.Path == "" || b.Type != BTDefault {
106106
return
107107
}
@@ -111,7 +111,7 @@ func (b *Buffer) RemoveBackup() {
111111

112112
// ApplyBackup applies the corresponding backup file to this buffer (if one exists)
113113
// Returns true if a backup was applied
114-
func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
114+
func (b *SharedBuffer) ApplyBackup(fsize int64) (bool, bool) {
115115
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
116116
backupfile := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
117117
if info, err := os.Stat(backupfile); err == nil {

internal/buffer/buffer.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ type SharedBuffer struct {
123123

124124
// Hash of the original buffer -- empty if fastdirty is on
125125
origHash [md5.Size]byte
126+
127+
fini int32
126128
}
127129

128130
func (b *SharedBuffer) insert(pos Loc, value []byte) {
@@ -223,7 +225,6 @@ type Buffer struct {
223225
*EventHandler
224226
*SharedBuffer
225227

226-
fini int32
227228
cursors []*Cursor
228229
curCursor int
229230
StartCursor Loc

internal/buffer/save.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,11 @@ type saveRequest struct {
4747
}
4848

4949
var saveRequestChan chan saveRequest
50-
var backupRequestChan chan *Buffer
50+
var backupRequestChan chan *SharedBuffer
5151

5252
func init() {
5353
saveRequestChan = make(chan saveRequest, 10)
54-
backupRequestChan = make(chan *Buffer, 10)
54+
backupRequestChan = make(chan *SharedBuffer, 10)
5555

5656
go func() {
5757
duration := backupSeconds * float64(time.Second)
@@ -116,7 +116,7 @@ func openFile(name string, withSudo bool) (wrappedFile, error) {
116116
return wrappedFile{writeCloser, withSudo, screenb, cmd, sigChan}, nil
117117
}
118118

119-
func (wf wrappedFile) Write(b *Buffer) (int, error) {
119+
func (wf wrappedFile) Write(b *SharedBuffer) (int, error) {
120120
file := bufio.NewWriter(transform.NewWriter(wf.writeCloser, b.encoding.NewEncoder()))
121121

122122
b.Lock()
@@ -184,7 +184,7 @@ func (wf wrappedFile) Close() error {
184184
return err
185185
}
186186

187-
func (b *Buffer) overwriteFile(name string) (int, error) {
187+
func (b *SharedBuffer) overwriteFile(name string) (int, error) {
188188
file, err := openFile(name, false)
189189
if err != nil {
190190
return 0, err
@@ -335,7 +335,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
335335
return err
336336
}
337337

338-
func (b *Buffer) writeBackup(path string) (string, error) {
338+
func (b *SharedBuffer) writeBackup(path string) (string, error) {
339339
backupDir := b.backupDir()
340340
if _, err := os.Stat(backupDir); err != nil {
341341
if !errors.Is(err, fs.ErrNotExist) {
@@ -360,7 +360,7 @@ func (b *Buffer) writeBackup(path string) (string, error) {
360360
// contents of the file if it fails to write the new contents.
361361
// This means that the file is not overwritten directly but by writing to the
362362
// backup file first.
363-
func (b *Buffer) safeWrite(path string, withSudo bool, newFile bool) (int, error) {
363+
func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int, error) {
364364
file, err := openFile(path, withSudo)
365365
if err != nil {
366366
return 0, err

0 commit comments

Comments
 (0)