2015-05-10 09:56:20 +00:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-05-15 03:26:55 +00:00
|
|
|
package model
|
2014-03-08 22:02:01 +00:00
|
|
|
|
|
|
|
import (
|
2015-05-09 19:37:09 +00:00
|
|
|
"crypto/md5"
|
2014-03-08 22:02:01 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2015-05-10 09:56:20 +00:00
|
|
|
"runtime"
|
2014-03-08 22:02:01 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tempNamer struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
2015-05-10 09:56:20 +00:00
|
|
|
var defTempNamer tempNamer
|
|
|
|
|
2016-06-27 11:47:40 +00:00
|
|
|
// Real filesystems usually handle 255 bytes. encfs has varying and
|
|
|
|
// confusing file name limits. We take a safe way out and switch to hashing
|
|
|
|
// quite early.
|
|
|
|
const maxFilenameLength = 160 - len(".syncthing.") - len(".tmp")
|
|
|
|
|
2015-05-10 09:56:20 +00:00
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
defTempNamer = tempNamer{"~syncthing~"}
|
|
|
|
} else {
|
|
|
|
defTempNamer = tempNamer{".syncthing."}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 22:02:01 +00:00
|
|
|
|
|
|
|
func (t tempNamer) IsTemporary(name string) bool {
|
2014-03-28 13:36:57 +00:00
|
|
|
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
2014-03-08 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t tempNamer) TempName(name string) string {
|
2014-03-28 13:36:57 +00:00
|
|
|
tdir := filepath.Dir(name)
|
2015-05-10 09:56:20 +00:00
|
|
|
tbase := filepath.Base(name)
|
2016-06-27 11:47:40 +00:00
|
|
|
if len(tbase) > maxFilenameLength {
|
2015-05-10 09:56:20 +00:00
|
|
|
hash := md5.New()
|
|
|
|
hash.Write([]byte(name))
|
|
|
|
tbase = fmt.Sprintf("%x", hash.Sum(nil))
|
|
|
|
}
|
|
|
|
tname := fmt.Sprintf("%s%s.tmp", t.prefix, tbase)
|
2014-03-28 13:36:57 +00:00
|
|
|
return filepath.Join(tdir, tname)
|
2014-03-08 22:02:01 +00:00
|
|
|
}
|