Skip to content

Commit 78d2c61

Browse files
committed
util: Hash the path in DetermineEscapePath()
...in case the escaped path exceeds the file name length limit
1 parent ec8bb7c commit 78d2c61

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

internal/util/util.go

Lines changed: 11 additions & 0 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"
@@ -450,6 +451,10 @@ func AppendBackupSuffix(path string) string {
450451
return path + ".micro-backup"
451452
}
452453

454+
func HashStringMd5(str string) string {
455+
return fmt.Sprintf("%x", md5.Sum([]byte(str)))
456+
}
457+
453458
// EscapePathUrl encodes the path in URL query form
454459
func EscapePathUrl(path string) string {
455460
return url.QueryEscape(filepath.ToSlash(path))
@@ -469,6 +474,8 @@ func EscapePathLegacy(path string) string {
469474
// using URL encoding (preferred, since it encodes unambiguously) or
470475
// legacy encoding with '%' (for backward compatibility, if the legacy-escaped
471476
// path exists in the given directory).
477+
// In case the length of the escaped path (plus the backup extension) exceeds
478+
// the filename length limit, a hash of the path is returned instead.
472479
func DetermineEscapePath(dir string, path string) string {
473480
url := filepath.Join(dir, EscapePathUrl(path))
474481
if _, err := os.Stat(url); err == nil {
@@ -480,6 +487,10 @@ func DetermineEscapePath(dir string, path string) string {
480487
return legacy
481488
}
482489

490+
if len(url)+len(".micro-backup") > 255 {
491+
return filepath.Join(dir, HashStringMd5(path))
492+
}
493+
483494
return url
484495
}
485496

0 commit comments

Comments
 (0)