aboutsummaryrefslogtreecommitdiff
path: root/weed/util/file_util.go
diff options
context:
space:
mode:
authorKonstantin Lebedev <9497591+kmlebedev@users.noreply.github.com>2023-09-27 17:40:51 +0500
committerGitHub <noreply@github.com>2023-09-27 05:40:51 -0700
commit44906f1f3b91b9def7e1424fec18a3237b1a02f0 (patch)
treeb01123aa276e62193e2a6fc704d78c776dd369e2 /weed/util/file_util.go
parent9d589b48e6b59cf60df4e0c00fdc69edb62a216a (diff)
downloadseaweedfs-44906f1f3b91b9def7e1424fec18a3237b1a02f0.tar.xz
seaweedfs-44906f1f3b91b9def7e1424fec18a3237b1a02f0.zip
fix: avoid error file name too long when writing a file (#4876)
Diffstat (limited to 'weed/util/file_util.go')
-rw-r--r--weed/util/file_util.go21
1 files changed, 19 insertions, 2 deletions
diff --git a/weed/util/file_util.go b/weed/util/file_util.go
index 346de76db..430b6bc86 100644
--- a/weed/util/file_util.go
+++ b/weed/util/file_util.go
@@ -1,16 +1,20 @@
package util
import (
+ "bytes"
+ "crypto/sha256"
"errors"
+ "fmt"
+ "github.com/seaweedfs/seaweedfs/weed/glog"
"os"
"os/user"
"path/filepath"
"strings"
"time"
-
- "github.com/seaweedfs/seaweedfs/weed/glog"
)
+const maxFilenameLength = 255
+
func TestFolderWritable(folder string) (err error) {
fileInfo, err := os.Stat(folder)
if err != nil {
@@ -106,6 +110,19 @@ func FileNameBase(filename string) string {
return filename[:lastDotIndex]
}
+func ToShortFileName(path string) string {
+ fileName := filepath.Base(path)
+ if fileNameBytes := []byte(fileName); len(fileNameBytes) > maxFilenameLength {
+ shaStr := fmt.Sprintf("%x", sha256.Sum256(fileNameBytes))
+ fileNameBase := FileNameBase(fileName)
+ fileExt := fileName[len(fileNameBase):]
+ fileNameBaseBates := bytes.ToValidUTF8([]byte(fileNameBase)[:maxFilenameLength-len([]byte(fileExt))-8], []byte{})
+ shortFileName := string(fileNameBaseBates) + shaStr[len(shaStr)-8:]
+ return filepath.Join(filepath.Dir(path), shortFileName) + fileExt
+ }
+ return path
+}
+
// Copied from os.WriteFile(), adding file sync.
// see https://github.com/golang/go/issues/20599
func WriteFile(name string, data []byte, perm os.FileMode) error {