mirror of
https://github.com/zyedidia/micro.git
synced 2026-02-04 14:10:23 +09:00
Merge pull request #3806 from JoeKar/fix/backup-path
backup+util: Prevent too long backup file names with hashing + resolve file
This commit is contained in:
@@ -250,7 +250,7 @@ func LoadInput(args []string) []*buffer.Buffer {
|
||||
|
||||
func checkBackup(name string) error {
|
||||
target := filepath.Join(config.ConfigDir, name)
|
||||
backup := util.AppendBackupSuffix(target)
|
||||
backup := target + util.BackupSuffix
|
||||
if info, err := os.Stat(backup); err == nil {
|
||||
input, err := os.ReadFile(backup)
|
||||
if err == nil {
|
||||
|
||||
@@ -92,32 +92,46 @@ func (b *SharedBuffer) keepBackup() bool {
|
||||
return b.forceKeepBackup || b.Settings["permbackup"].(bool)
|
||||
}
|
||||
|
||||
func (b *SharedBuffer) writeBackup(path string) (string, error) {
|
||||
func (b *SharedBuffer) writeBackup(path string) (string, string, error) {
|
||||
backupdir := b.backupDir()
|
||||
if _, err := os.Stat(backupdir); err != nil {
|
||||
if !errors.Is(err, fs.ErrNotExist) {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
if err = os.Mkdir(backupdir, os.ModePerm); err != nil {
|
||||
return "", err
|
||||
return "", "", err
|
||||
}
|
||||
}
|
||||
|
||||
name := util.DetermineEscapePath(backupdir, path)
|
||||
tmp := util.AppendBackupSuffix(name)
|
||||
name, resolveName := util.DetermineEscapePath(backupdir, path)
|
||||
tmp := name + util.BackupSuffix
|
||||
|
||||
_, err := b.overwriteFile(tmp)
|
||||
if err != nil {
|
||||
os.Remove(tmp)
|
||||
return name, err
|
||||
return name, resolveName, err
|
||||
}
|
||||
err = os.Rename(tmp, name)
|
||||
if err != nil {
|
||||
os.Remove(tmp)
|
||||
return name, err
|
||||
return name, resolveName, err
|
||||
}
|
||||
|
||||
return name, nil
|
||||
if resolveName != "" {
|
||||
err = util.SafeWrite(resolveName, []byte(path), true)
|
||||
if err != nil {
|
||||
return name, resolveName, err
|
||||
}
|
||||
}
|
||||
|
||||
return name, resolveName, nil
|
||||
}
|
||||
|
||||
func (b *SharedBuffer) removeBackup(path string, resolveName string) {
|
||||
os.Remove(path)
|
||||
if resolveName != "" {
|
||||
os.Remove(resolveName)
|
||||
}
|
||||
}
|
||||
|
||||
// Backup saves the buffer to the backups directory
|
||||
@@ -126,7 +140,7 @@ func (b *SharedBuffer) Backup() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
_, err := b.writeBackup(b.AbsPath)
|
||||
_, _, err := b.writeBackup(b.AbsPath)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -135,15 +149,15 @@ func (b *SharedBuffer) RemoveBackup() {
|
||||
if b.keepBackup() || b.Path == "" || b.Type != BTDefault {
|
||||
return
|
||||
}
|
||||
f := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
|
||||
os.Remove(f)
|
||||
f, resolveName := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
|
||||
b.removeBackup(f, resolveName)
|
||||
}
|
||||
|
||||
// ApplyBackup applies the corresponding backup file to this buffer (if one exists)
|
||||
// Returns true if a backup was applied
|
||||
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)
|
||||
backupfile, resolveName := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
|
||||
if info, err := os.Stat(backupfile); err == nil {
|
||||
backup, err := os.Open(backupfile)
|
||||
if err == nil {
|
||||
@@ -159,7 +173,7 @@ func (b *SharedBuffer) ApplyBackup(fsize int64) (bool, bool) {
|
||||
return true, true
|
||||
} else if choice%3 == 1 {
|
||||
// delete
|
||||
os.Remove(backupfile)
|
||||
b.removeBackup(backupfile, resolveName)
|
||||
} else if choice%3 == 2 {
|
||||
return false, false
|
||||
}
|
||||
|
||||
@@ -331,6 +331,10 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
|
||||
}
|
||||
|
||||
newPath := b.Path != filename
|
||||
if newPath {
|
||||
b.RemoveBackup()
|
||||
}
|
||||
|
||||
b.Path = filename
|
||||
b.AbsPath = absFilename
|
||||
b.isModified = false
|
||||
@@ -362,7 +366,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
|
||||
}()
|
||||
|
||||
// Try to backup first before writing
|
||||
backupName, err := b.writeBackup(path)
|
||||
backupName, resolveName, err := b.writeBackup(path)
|
||||
if err != nil {
|
||||
file.Close()
|
||||
return 0, err
|
||||
@@ -389,7 +393,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
|
||||
b.forceKeepBackup = false
|
||||
|
||||
if !b.keepBackup() {
|
||||
os.Remove(backupName)
|
||||
b.removeBackup(backupName, resolveName)
|
||||
}
|
||||
|
||||
return size, err
|
||||
|
||||
@@ -39,8 +39,20 @@ func (b *Buffer) Serialize() error {
|
||||
return err
|
||||
}
|
||||
|
||||
name := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
|
||||
return util.SafeWrite(name, buf.Bytes(), true)
|
||||
name, resolveName := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
|
||||
err = util.SafeWrite(name, buf.Bytes(), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if resolveName != "" {
|
||||
err = util.SafeWrite(resolveName, []byte(b.AbsPath), true)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Unserialize loads the buffer info from config.ConfigDir/buffers
|
||||
@@ -50,7 +62,8 @@ func (b *Buffer) Unserialize() error {
|
||||
if b.Path == "" {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath))
|
||||
name, _ := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
|
||||
file, err := os.Open(name)
|
||||
if err == nil {
|
||||
defer file.Close()
|
||||
var buffer SerializedBuffer
|
||||
|
||||
@@ -3,6 +3,7 @@ package util
|
||||
import (
|
||||
"archive/zip"
|
||||
"bytes"
|
||||
"crypto/md5"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
@@ -53,6 +54,8 @@ var (
|
||||
// To be used for file writes before umask is applied
|
||||
const FileMode os.FileMode = 0666
|
||||
|
||||
const BackupSuffix = ".micro-backup"
|
||||
|
||||
const OverwriteFailMsg = `An error occurred while writing to the file:
|
||||
|
||||
%s
|
||||
@@ -446,8 +449,8 @@ func GetModTime(path string) (time.Time, error) {
|
||||
return info.ModTime(), nil
|
||||
}
|
||||
|
||||
func AppendBackupSuffix(path string) string {
|
||||
return path + ".micro-backup"
|
||||
func HashStringMd5(str string) string {
|
||||
return fmt.Sprintf("%x", md5.Sum([]byte(str)))
|
||||
}
|
||||
|
||||
// EscapePathUrl encodes the path in URL query form
|
||||
@@ -469,18 +472,28 @@ func EscapePathLegacy(path string) string {
|
||||
// using URL encoding (preferred, since it encodes unambiguously) or
|
||||
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
|
||||
// path exists in the given directory).
|
||||
func DetermineEscapePath(dir string, path string) string {
|
||||
// In case the length of the escaped path (plus the backup extension) exceeds
|
||||
// the filename length limit, a hash of the path is returned instead. In such
|
||||
// case the second return value is the name of a file the original path should
|
||||
// be saved to (since the original path cannot be derived from its hash).
|
||||
// Otherwise the second return value is an empty string.
|
||||
func DetermineEscapePath(dir string, path string) (string, string) {
|
||||
url := filepath.Join(dir, EscapePathUrl(path))
|
||||
if _, err := os.Stat(url); err == nil {
|
||||
return url
|
||||
return url, ""
|
||||
}
|
||||
|
||||
legacy := filepath.Join(dir, EscapePathLegacy(path))
|
||||
if _, err := os.Stat(legacy); err == nil {
|
||||
return legacy
|
||||
return legacy, ""
|
||||
}
|
||||
|
||||
return url
|
||||
if len(url)+len(BackupSuffix) > 255 {
|
||||
hash := HashStringMd5(path)
|
||||
return filepath.Join(dir, hash), filepath.Join(dir, hash+".path")
|
||||
}
|
||||
|
||||
return url, ""
|
||||
}
|
||||
|
||||
// GetLeadingWhitespace returns the leading whitespace of the given byte array
|
||||
@@ -697,7 +710,7 @@ func SafeWrite(path string, bytes []byte, rename bool) error {
|
||||
defer file.Close()
|
||||
}
|
||||
|
||||
tmp := AppendBackupSuffix(path)
|
||||
tmp := path + BackupSuffix
|
||||
err = os.WriteFile(tmp, bytes, FileMode)
|
||||
if err != nil {
|
||||
os.Remove(tmp)
|
||||
|
||||
Reference in New Issue
Block a user