Files
syncthing/lib/protocol/nativemodel_windows.go
T

85 lines
2.1 KiB
Go
Raw Normal View History

// Copyright (C) 2014 The Syncthing Authors.
//
// 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 https://mozilla.org/MPL/2.0/.
2014-09-22 21:42:11 +02:00
//go:build windows
2014-09-22 21:42:11 +02:00
// +build windows
package protocol
// Windows uses backslashes as file separator
import (
2025-08-07 11:19:36 +02:00
"log/slog"
"path/filepath"
"strings"
2025-08-07 11:19:36 +02:00
"github.com/syncthing/syncthing/internal/slogutil"
)
2014-09-22 21:42:11 +02:00
func makeNative(m rawModel) rawModel { return nativeModel{m} }
2021-10-03 12:13:04 +02:00
2014-09-22 21:42:11 +02:00
type nativeModel struct {
rawModel
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (m nativeModel) Index(idx *Index) error {
idx.Files = fixupFiles(idx.Files)
return m.rawModel.Index(idx)
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (m nativeModel) IndexUpdate(idxUp *IndexUpdate) error {
idxUp.Files = fixupFiles(idxUp.Files)
return m.rawModel.IndexUpdate(idxUp)
2014-09-22 21:42:11 +02:00
}
2024-01-31 08:18:27 +01:00
func (m nativeModel) Request(req *Request) (RequestResponse, error) {
if strings.Contains(req.Name, `\`) {
2025-08-07 11:19:36 +02:00
slog.Debug("Dropping request containing invalid path separator", slogutil.FilePath(req.Name))
return nil, ErrNoSuchFile
}
2024-01-31 08:18:27 +01:00
req.Name = filepath.FromSlash(req.Name)
return m.rawModel.Request(req)
2014-09-22 21:42:11 +02:00
}
2015-02-11 22:11:44 +00:00
func fixupFiles(files []FileInfo) []FileInfo {
var out []FileInfo
for i := range files {
if strings.Contains(files[i].Name, `\`) {
if files[i].Deleted {
// Dropping a deleted item doesn't have any consequences.
2025-08-07 11:19:36 +02:00
slog.Debug("Dropping index entry containing invalid path separator", slogutil.FilePath(files[i].Name))
} else {
2025-08-07 11:19:36 +02:00
slog.Error("Dropping index entry containing invalid path separator", slogutil.FilePath(files[i].Name))
}
if out == nil {
// Most incoming updates won't contain anything invalid, so
// we delay the allocation and copy to output slice until we
// really need to do it, then copy all the so-far valid
// files to it.
out = make([]FileInfo, i, len(files)-1)
copy(out, files)
}
continue
}
// Fixup the path separators
2015-02-11 22:11:44 +00:00
files[i].Name = filepath.FromSlash(files[i].Name)
if out != nil {
out = append(out, files[i])
}
2015-02-11 22:11:44 +00:00
}
if out != nil {
// We did some filtering
return out
}
// Unchanged
return files
2015-02-11 22:11:44 +00:00
}