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.
This commit is contained in:
Jöran Karl
2025-09-19 19:56:02 +02:00
parent 1ce2202d9a
commit 02611f4ad2
4 changed files with 54 additions and 23 deletions

View File

@@ -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)
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
}

View File

@@ -362,7 +362,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 +389,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

View File

@@ -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

View File

@@ -473,23 +473,27 @@ func EscapePathLegacy(path string) string {
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
// path exists in the given directory).
// 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.
func DetermineEscapePath(dir string, path string) string {
// 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, ""
}
if len(url)+len(BackupSuffix) > 255 {
return filepath.Join(dir, HashStringMd5(path))
hash := HashStringMd5(path)
return filepath.Join(dir, hash), filepath.Join(dir, hash+".path")
}
return url
return url, ""
}
// GetLeadingWhitespace returns the leading whitespace of the given byte array