Skip to content

Commit 02611f4

Browse files
committed
backup: Keep path of stored & hashed backup files in a own $hash.path file
Since full escaped backup paths can become longer than the maximum filename size and hashed filenames cannot be restored it is helpful to have a lookup file for the user to resolve the hashed path.
1 parent 1ce2202 commit 02611f4

File tree

4 files changed

+54
-23
lines changed

4 files changed

+54
-23
lines changed

internal/buffer/backup.go

Lines changed: 26 additions & 12 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)
106+
name, resolveName := util.DetermineEscapePath(backupdir, path)
107107
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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
362362
}()
363363

364364
// Try to backup first before writing
365-
backupName, err := b.writeBackup(path)
365+
backupName, resolveName, err := b.writeBackup(path)
366366
if err != nil {
367367
file.Close()
368368
return 0, err
@@ -389,7 +389,7 @@ func (b *SharedBuffer) safeWrite(path string, withSudo bool, newFile bool) (int,
389389
b.forceKeepBackup = false
390390

391391
if !b.keepBackup() {
392-
os.Remove(backupName)
392+
b.removeBackup(backupName, resolveName)
393393
}
394394

395395
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: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -473,23 +473,27 @@ func EscapePathLegacy(path string) string {
473473
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
474474
// path exists in the given directory).
475475
// 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.
477-
func DetermineEscapePath(dir string, path string) string {
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) {
478481
url := filepath.Join(dir, EscapePathUrl(path))
479482
if _, err := os.Stat(url); err == nil {
480-
return url
483+
return url, ""
481484
}
482485

483486
legacy := filepath.Join(dir, EscapePathLegacy(path))
484487
if _, err := os.Stat(legacy); err == nil {
485-
return legacy
488+
return legacy, ""
486489
}
487490

488491
if len(url)+len(BackupSuffix) > 255 {
489-
return filepath.Join(dir, HashStringMd5(path))
492+
hash := HashStringMd5(path)
493+
return filepath.Join(dir, hash), filepath.Join(dir, hash+".path")
490494
}
491495

492-
return url
496+
return url, ""
493497
}
494498

495499
// GetLeadingWhitespace returns the leading whitespace of the given byte array

0 commit comments

Comments
 (0)