|
@ -1,16 +1,20 @@ |
|
|
package util |
|
|
package util |
|
|
|
|
|
|
|
|
import ( |
|
|
import ( |
|
|
|
|
|
"bytes" |
|
|
|
|
|
"crypto/sha256" |
|
|
"errors" |
|
|
"errors" |
|
|
|
|
|
"fmt" |
|
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog" |
|
|
"os" |
|
|
"os" |
|
|
"os/user" |
|
|
"os/user" |
|
|
"path/filepath" |
|
|
"path/filepath" |
|
|
"strings" |
|
|
"strings" |
|
|
"time" |
|
|
"time" |
|
|
|
|
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog" |
|
|
|
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const maxFilenameLength = 255 |
|
|
|
|
|
|
|
|
func TestFolderWritable(folder string) (err error) { |
|
|
func TestFolderWritable(folder string) (err error) { |
|
|
fileInfo, err := os.Stat(folder) |
|
|
fileInfo, err := os.Stat(folder) |
|
|
if err != nil { |
|
|
if err != nil { |
|
@ -106,6 +110,19 @@ func FileNameBase(filename string) string { |
|
|
return filename[:lastDotIndex] |
|
|
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.
|
|
|
// Copied from os.WriteFile(), adding file sync.
|
|
|
// see https://github.com/golang/go/issues/20599
|
|
|
// see https://github.com/golang/go/issues/20599
|
|
|
func WriteFile(name string, data []byte, perm os.FileMode) error { |
|
|
func WriteFile(name string, data []byte, perm os.FileMode) error { |
|
|