11 "github.com/zyedidia/micro/v2/internal/config"
12 "github.com/zyedidia/micro/v2/internal/screen"
13 "github.com/zyedidia/micro/v2/internal/util"
14 "golang.org/x/text/encoding"
17 const backupMsg = `A backup was detected for this file. This likely means that micro
18 crashed while editing this file, or another instance of micro is currently
21 The backup was created on %s, and the file is
25 * 'recover' will apply the backup as unsaved changes to the current buffer.
26 When the buffer is closed, the backup will be removed.
27 * 'ignore' will ignore the backup, discarding its changes. The backup file
29 * 'abort' will abort the open operation, and instead open an empty buffer.
31 Options: [r]ecover, [i]gnore, [a]bort: `
33 var backupRequestChan chan *Buffer
37 time.Sleep(time.Second * 8)
39 for len(backupRequestChan) > 0 {
40 b := <-backupRequestChan
41 bfini := atomic.LoadInt32(&(b.fini)) != 0
50 backupRequestChan = make(chan *Buffer, 10)
55 func (b *Buffer) RequestBackup() {
56 if !b.requestedBackup {
58 case backupRequestChan <- b:
62 b.requestedBackup = true
66 // Backup saves the current buffer to ConfigDir/backups
67 func (b *Buffer) Backup() error {
68 if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
72 backupdir, err := util.ReplaceHome(b.Settings["backupdir"].(string))
73 if backupdir == "" || err != nil {
74 backupdir = filepath.Join(config.ConfigDir, "backups")
76 if _, err := os.Stat(backupdir); os.IsNotExist(err) {
77 os.Mkdir(backupdir, os.ModePerm)
80 name := filepath.Join(backupdir, util.EscapePath(b.AbsPath))
82 err = overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
83 if len(b.lines) == 0 {
91 if _, e = file.Write(b.lines[0].data); e != nil {
95 for _, l := range b.lines[1:] {
96 if _, e = file.Write(eol); e != nil {
99 if _, e = file.Write(l.data); e != nil {
106 b.requestedBackup = false
111 // RemoveBackup removes any backup file associated with this buffer
112 func (b *Buffer) RemoveBackup() {
113 if !b.Settings["backup"].(bool) || b.Settings["permbackup"].(bool) || b.Path == "" || b.Type != BTDefault {
116 f := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
120 // ApplyBackup applies the corresponding backup file to this buffer (if one exists)
121 // Returns true if a backup was applied
122 func (b *Buffer) ApplyBackup(fsize int64) (bool, bool) {
123 if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
124 backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
125 if info, err := os.Stat(backupfile); err == nil {
126 backup, err := os.Open(backupfile)
130 msg := fmt.Sprintf(backupMsg, t.Format("Mon Jan _2 at 15:04, 2006"), util.EscapePath(b.AbsPath))
131 choice := screen.TermPrompt(msg, []string{"r", "i", "a", "recover", "ignore", "abort"}, true)
135 b.LineArray = NewLineArray(uint64(fsize), FFAuto, backup)
138 } else if choice%3 == 1 {
140 os.Remove(backupfile)
141 } else if choice%3 == 2 {