2015-05-10 02:56:20 -07:00
|
|
|
// Copyright (C) 2015 The Syncthing Authors.
|
2014-09-29 12:43:32 -07:00
|
|
|
//
|
2015-03-07 13:36:35 -07: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 13:50:14 -07:00
|
|
|
|
2014-05-14 20:26:55 -07:00
|
|
|
package model
|
2014-03-08 15:02:01 -07:00
|
|
|
|
|
|
|
import (
|
2015-05-09 12:37:09 -07:00
|
|
|
"crypto/md5"
|
2014-03-08 15:02:01 -07:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
2015-05-10 02:56:20 -07:00
|
|
|
"runtime"
|
2014-03-08 15:02:01 -07:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
type tempNamer struct {
|
|
|
|
prefix string
|
|
|
|
}
|
|
|
|
|
2015-05-10 02:56:20 -07:00
|
|
|
var defTempNamer tempNamer
|
|
|
|
|
2016-06-27 04:47:40 -07: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 02:56:20 -07:00
|
|
|
func init() {
|
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
defTempNamer = tempNamer{"~syncthing~"}
|
|
|
|
} else {
|
|
|
|
defTempNamer = tempNamer{".syncthing."}
|
|
|
|
}
|
|
|
|
}
|
2014-03-08 15:02:01 -07:00
|
|
|
|
|
|
|
func (t tempNamer) IsTemporary(name string) bool {
|
2014-03-28 06:36:57 -07:00
|
|
|
return strings.HasPrefix(filepath.Base(name), t.prefix)
|
2014-03-08 15:02:01 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (t tempNamer) TempName(name string) string {
|
2014-03-28 06:36:57 -07:00
|
|
|
tdir := filepath.Dir(name)
|
2015-05-10 02:56:20 -07:00
|
|
|
tbase := filepath.Base(name)
|
2016-06-27 04:47:40 -07:00
|
|
|
if len(tbase) > maxFilenameLength {
|
2015-05-10 02:56:20 -07: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 06:36:57 -07:00
|
|
|
return filepath.Join(tdir, tname)
|
2014-03-08 15:02:01 -07:00
|
|
|
}
|