Skip to content

Commit 284942d

Browse files
authored
Merge pull request zyedidia#3806 from JoeKar/fix/backup-path
backup+util: Prevent too long backup file names with hashing + resolve file
2 parents 815ca0b + bab3907 commit 284942d

File tree

5 files changed

+70
-26
lines changed

5 files changed

+70
-26
lines changed

cmd/micro/micro.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func LoadInput(args []string) []*buffer.Buffer {
250250

251251
func checkBackup(name string) error {
252252
target := filepath.Join(config.ConfigDir, name)
253-
backup := util.AppendBackupSuffix(target)
253+
backup := target + util.BackupSuffix
254254
if info, err := os.Stat(backup); err == nil {
255255
input, err := os.ReadFile(backup)
256256
if err == nil {

internal/buffer/backup.go

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,32 +92,46 @@ func (b *SharedBuffer) keepBackup() bool {
9292
return b.forceKeepBackup || b.Settings["permbackup"].(bool)
9393
}
9494

95-
func (b *SharedBuffer) writeBackup(path string) (string, error) {
95+
func (b *SharedBuffer) writeBackup(path string) (string, string, error) {
9696
backupdir := b.backupDir()
9797
if _, err := os.Stat(backupdir); err != nil {
9898
if !errors.Is(err, fs.ErrNotExist) {
99-
return "", err
99+
return "", "", err
100100
}
101101
if err = os.Mkdir(backupdir, os.ModePerm); err != nil {
102-
return "", err
102+
return "", "", err
103103
}
104104
}
105105

106-
name := util.DetermineEscapePath(backupdir, path)
107-
tmp := util.AppendBackupSuffix(name)
106+
name, resolveName := util.DetermineEscapePath(backupdir, path)
107+
tmp := name + util.BackupSuffix
108108

109109
_, err := b.overwriteFile(tmp)
110110
if err != nil {
111111
os.Remove(tmp)
112-
return name, err
112+
return name, resolveName, err
113113
}
114114
err = os.Rename(tmp, name)
115115
if err != nil {
116116
os.Remove(tmp)
117-
return name, err
117+
return name, resolveName, err
118118
}
119119

120-
return name, nil
120+
if resolveName != "" {
121+
err = util.SafeWrite(resolveName, []byte(path), true)
122+
if err != nil {
123+
return name, resolveName, err
124+
}
125+
}
126+
127+
return name, resolveName, nil
128+
}
129+
130+
func (b *SharedBuffer) removeBackup(path string, resolveName string) {
131+
os.Remove(path)
132+
if resolveName != "" {
133+
os.Remove(resolveName)
134+
}
121135
}
122136

123137
// Backup saves the buffer to the backups directory
@@ -126,7 +140,7 @@ func (b *SharedBuffer) Backup() error {
126140
return nil
127141
}
128142

129-
_, err := b.writeBackup(b.AbsPath)
143+
_, _, err := b.writeBackup(b.AbsPath)
130144
return err
131145
}
132146

@@ -135,15 +149,15 @@ func (b *SharedBuffer) RemoveBackup() {
135149
if b.keepBackup() || b.Path == "" || b.Type != BTDefault {
136150
return
137151
}
138-
f := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
139-
os.Remove(f)
152+
f, resolveName := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
153+
b.removeBackup(f, resolveName)
140154
}
141155

142156
// ApplyBackup applies the corresponding backup file to this buffer (if one exists)
143157
// Returns true if a backup was applied
144158
func (b *SharedBuffer) ApplyBackup(fsize int64) (bool, bool) {
145159
if b.Settings["backup"].(bool) && !b.Settings["permbackup"].(bool) && len(b.Path) > 0 && b.Type == BTDefault {
146-
backupfile := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
160+
backupfile, resolveName := util.DetermineEscapePath(b.backupDir(), b.AbsPath)
147161
if info, err := os.Stat(backupfile); err == nil {
148162
backup, err := os.Open(backupfile)
149163
if err == nil {
@@ -159,7 +173,7 @@ func (b *SharedBuffer) ApplyBackup(fsize int64) (bool, bool) {
159173
return true, true
160174
} else if choice%3 == 1 {
161175
// delete
162-
os.Remove(backupfile)
176+
b.removeBackup(backupfile, resolveName)
163177
} else if choice%3 == 2 {
164178
return false, false
165179
}

internal/buffer/save.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,10 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
331331
}
332332

333333
newPath := b.Path != filename
334+
if newPath {
335+
b.RemoveBackup()
336+
}
337+
334338
b.Path = filename
335339
b.AbsPath = absFilename
336340
b.isModified = false
@@ -362,7 +366,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
362366
}()
363367

364368
// Try to backup first before writing
365-
backupName, err := b.writeBackup(path)
369+
backupName, resolveName, err := b.writeBackup(path)
366370
if err != nil {
367371
file.Close()
368372
return 0, err
@@ -389,7 +393,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
389393
b.forceKeepBackup = false
390394

391395
if !b.keepBackup() {
392-
os.Remove(backupName)
396+
b.removeBackup(backupName, resolveName)
393397
}
394398

395399
return size, err

internal/buffer/serialize.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,20 @@ func (b *Buffer) Serialize() error {
3939
return err
4040
}
4141

42-
name := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
43-
return util.SafeWrite(name, buf.Bytes(), true)
42+
name, resolveName := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
43+
err = util.SafeWrite(name, buf.Bytes(), true)
44+
if err != nil {
45+
return err
46+
}
47+
48+
if resolveName != "" {
49+
err = util.SafeWrite(resolveName, []byte(b.AbsPath), true)
50+
if err != nil {
51+
return err
52+
}
53+
}
54+
55+
return nil
4456
}
4557

4658
// Unserialize loads the buffer info from config.ConfigDir/buffers
@@ -50,7 +62,8 @@ func (b *Buffer) Unserialize() error {
5062
if b.Path == "" {
5163
return nil
5264
}
53-
file, err := os.Open(util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath))
65+
name, _ := util.DetermineEscapePath(filepath.Join(config.ConfigDir, "buffers"), b.AbsPath)
66+
file, err := os.Open(name)
5467
if err == nil {
5568
defer file.Close()
5669
var buffer SerializedBuffer

internal/util/util.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package util
33
import (
44
"archive/zip"
55
"bytes"
6+
"crypto/md5"
67
"errors"
78
"fmt"
89
"io"
@@ -53,6 +54,8 @@ var (
5354
// To be used for file writes before umask is applied
5455
const FileMode os.FileMode = 0666
5556

57+
const BackupSuffix = ".micro-backup"
58+
5659
const OverwriteFailMsg = `An error occurred while writing to the file:
5760
5861
%s
@@ -446,8 +449,8 @@ func GetModTime(path string) (time.Time, error) {
446449
return info.ModTime(), nil
447450
}
448451

449-
func AppendBackupSuffix(path string) string {
450-
return path + ".micro-backup"
452+
func HashStringMd5(str string) string {
453+
return fmt.Sprintf("%x", md5.Sum([]byte(str)))
451454
}
452455

453456
// EscapePathUrl encodes the path in URL query form
@@ -469,18 +472,28 @@ func EscapePathLegacy(path string) string {
469472
// using URL encoding (preferred, since it encodes unambiguously) or
470473
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
471474
// path exists in the given directory).
472-
func DetermineEscapePath(dir string, path string) string {
475+
// In case the length of the escaped path (plus the backup extension) exceeds
476+
// the filename length limit, a hash of the path is returned instead. In such
477+
// case the second return value is the name of a file the original path should
478+
// be saved to (since the original path cannot be derived from its hash).
479+
// Otherwise the second return value is an empty string.
480+
func DetermineEscapePath(dir string, path string) (string, string) {
473481
url := filepath.Join(dir, EscapePathUrl(path))
474482
if _, err := os.Stat(url); err == nil {
475-
return url
483+
return url, ""
476484
}
477485

478486
legacy := filepath.Join(dir, EscapePathLegacy(path))
479487
if _, err := os.Stat(legacy); err == nil {
480-
return legacy
488+
return legacy, ""
489+
}
490+
491+
if len(url)+len(BackupSuffix) > 255 {
492+
hash := HashStringMd5(path)
493+
return filepath.Join(dir, hash), filepath.Join(dir, hash+".path")
481494
}
482495

483-
return url
496+
return url, ""
484497
}
485498

486499
// GetLeadingWhitespace returns the leading whitespace of the given byte array
@@ -697,7 +710,7 @@ func SafeWrite(path string, bytes []byte, rename bool) error {
697710
defer file.Close()
698711
}
699712

700-
tmp := AppendBackupSuffix(path)
713+
tmp := path + BackupSuffix
701714
err = os.WriteFile(tmp, bytes, FileMode)
702715
if err != nil {
703716
os.Remove(tmp)

0 commit comments

Comments
 (0)