]> git.lizzy.rs Git - micro.git/blobdiff - internal/buffer/backup.go
Use filepath.Join more
[micro.git] / internal / buffer / backup.go
index 2574c7adbd80a1fa749e63b888292c82dc79eeac..56308843e87598d9d87ae1fecc527f7ecda5f65e 100644 (file)
@@ -3,8 +3,8 @@ package buffer
 import (
        "fmt"
        "io"
-       "log"
        "os"
+       "path/filepath"
        "time"
 
        "github.com/zyedidia/micro/internal/config"
@@ -30,29 +30,25 @@ Options: [r]ecover, [i]gnore: `
 
 // Backup saves the current buffer to ConfigDir/backups
 func (b *Buffer) Backup(checkTime bool) error {
-       if !b.Settings["backup"].(bool) || b.Path == "" {
+       if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
                return nil
        }
 
        if checkTime {
                sub := time.Now().Sub(b.lastbackup)
                if sub < time.Duration(backupTime)*time.Millisecond {
-                       log.Println("Backup event but not enough time has passed", sub)
                        return nil
                }
        }
 
        b.lastbackup = time.Now()
 
-       backupdir := config.ConfigDir + "/backups/"
+       backupdir := filepath.Join(config.ConfigDir, "backups")
        if _, err := os.Stat(backupdir); os.IsNotExist(err) {
                os.Mkdir(backupdir, os.ModePerm)
-               log.Println("Creating backup dir")
        }
 
-       name := backupdir + util.EscapePath(b.AbsPath)
-
-       log.Println("Backing up to", name)
+       name := filepath.Join(backupdir, util.EscapePath(b.AbsPath))
 
        err := overwriteFile(name, encoding.Nop, func(file io.Writer) (e error) {
                if len(b.lines) == 0 {
@@ -76,25 +72,25 @@ func (b *Buffer) Backup(checkTime bool) error {
                        }
                }
                return
-       })
+       }, false)
 
        return err
 }
 
 // RemoveBackup removes any backup file associated with this buffer
 func (b *Buffer) RemoveBackup() {
-       if !b.Settings["backup"].(bool) || b.Path == "" {
+       if !b.Settings["backup"].(bool) || b.Path == "" || b.Type != BTDefault {
                return
        }
-       f := config.ConfigDir + "/backups/" + util.EscapePath(b.AbsPath)
+       f := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
        os.Remove(f)
 }
 
 // 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 {
-       if b.Settings["backup"].(bool) && len(b.Path) > 0 {
-               backupfile := config.ConfigDir + "/backups/" + util.EscapePath(b.AbsPath)
+       if b.Settings["backup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
+               backupfile := filepath.Join(config.ConfigDir, "backups", util.EscapePath(b.AbsPath))
                if info, err := os.Stat(backupfile); err == nil {
                        backup, err := os.Open(backupfile)
                        if err == nil {