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,
|
2017-02-09 06:52:18 +00:00
|
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2017-09-02 05:52:38 +00:00
|
|
|
package fs
|
2014-03-08 22:02:01 +00:00
|
|
|
|
|
|
|
import (
|
2024-08-10 11:58:20 +00:00
|
|
|
"crypto/sha256"
|
2014-03-08 22:02:01 +00:00
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
2021-03-17 21:22:49 +00:00
|
|
|
|
2022-07-28 17:36:39 +00:00
|
|
|
"github.com/syncthing/syncthing/lib/build"
|
2014-03-08 22:02:01 +00:00
|
|
|
)
|
|
|
|
|
2016-11-30 21:23:24 +00:00
|
|
|
const (
|
2017-01-17 07:33:48 +00:00
|
|
|
WindowsTempPrefix = "~syncthing~"
|
|
|
|
UnixTempPrefix = ".syncthing."
|
2016-11-30 21:23:24 +00:00
|
|
|
)
|
|
|
|
|
2021-08-29 08:47:53 +00:00
|
|
|
func tempPrefix() string {
|
2022-07-28 17:36:39 +00:00
|
|
|
if build.IsWindows {
|
2021-08-29 08:47:53 +00:00
|
|
|
return WindowsTempPrefix
|
|
|
|
} else {
|
|
|
|
return UnixTempPrefix
|
|
|
|
}
|
|
|
|
}
|
2015-05-10 09:56:20 +00:00
|
|
|
|
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.
|
2017-01-17 07:33:48 +00:00
|
|
|
const maxFilenameLength = 160 - len(UnixTempPrefix) - len(".tmp")
|
2016-06-27 11:47:40 +00:00
|
|
|
|
2016-11-30 21:23:24 +00:00
|
|
|
// IsTemporary is true if the file name has the temporary prefix. Regardless
|
|
|
|
// of the normally used prefix, the standard Windows and Unix temp prefixes
|
|
|
|
// are always recognized as temp files.
|
2017-01-17 07:33:48 +00:00
|
|
|
func IsTemporary(name string) bool {
|
2016-11-30 21:23:24 +00:00
|
|
|
name = filepath.Base(name)
|
2021-08-29 08:47:53 +00:00
|
|
|
return strings.HasPrefix(name, WindowsTempPrefix) ||
|
|
|
|
strings.HasPrefix(name, UnixTempPrefix)
|
2014-03-08 22:02:01 +00:00
|
|
|
}
|
|
|
|
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
func TempNameWithPrefix(name, prefix 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)
|
2021-08-29 08:47:53 +00:00
|
|
|
var tname string
|
2016-06-27 11:47:40 +00:00
|
|
|
if len(tbase) > maxFilenameLength {
|
2021-08-29 08:47:53 +00:00
|
|
|
tname = fmt.Sprintf("%s%x.tmp", prefix, sha256.Sum256([]byte(tbase)))
|
|
|
|
} else {
|
|
|
|
tname = prefix + tbase + ".tmp"
|
2015-05-10 09:56:20 +00:00
|
|
|
}
|
2014-03-28 13:36:57 +00:00
|
|
|
return filepath.Join(tdir, tname)
|
2014-03-08 22:02:01 +00:00
|
|
|
}
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
|
|
|
|
func TempName(name string) string {
|
2021-08-29 08:47:53 +00:00
|
|
|
return TempNameWithPrefix(name, tempPrefix())
|
lib/scanner: Fix UTF-8 normalization on ZFS (fixes #4649)
It turns out that ZFS doesn't do any normalization when storing files,
but does do normalization "as part of any comparison process".
In practice, this seems to mean that if you LStat a normalized filename,
ZFS will return the FileInfo for the un-normalized version of that
filename.
This meant that our test to see whether a separate file with a
normalized version of the filename already exists was failing, as we
were detecting the same file.
The fix is to use os.SameFile, to see whether we're getting the same
FileInfo from the normalized and un-normalized versions of the same
filename.
One complication is that ZFS also seems to apply its magic to os.Rename,
meaning that we can't use it to rename an un-normalized file to its
normalized filename. Instead we have to move via a temporary object. If
the move to the temporary object fails, that's OK, we can skip it and
move on. If the move from the temporary object fails however, I'm not
sure of the best approach: the current one is to leave the temporary
file name as-is, and get Syncthing to syncronize it, so at least we
don't lose the file. I'm not sure if there are any implications of this
however.
As part of reworking normalizePath, I spotted that it appeared to be
returning the wrong thing: the doc and the surrounding code expecting it
to return the normalized filename, but it was returning the
un-normalized one. I fixed this, but it seems suspicious that, if the
previous behaviour was incorrect, noone ever ran afoul of it. Maybe all
filesystems will do some searching and give you a normalized filename if
you request an unnormalized one.
As part of this, I found that TestNormalization was broken: it was
passing, when in fact one of the files it should have verified was
present was missing. Maybe this was related to the above issue with
normalizePath's return value, I'm not sure. Fixed en route.
Kindly tested by @khinsen on the forum, and it appears to work.
GitHub-Pull-Request: https://github.com/syncthing/syncthing/pull/4646
2018-01-05 18:11:09 +00:00
|
|
|
}
|