mirror of
https://github.com/syncthing/syncthing.git
synced 2026-02-07 12:44:14 -05:00
This updates our logging framework from legacy freetext strings using the `log` package to structured log entries using `log/slog`. I have updated all INFO or higher level entries, but not yet DEBUG (😓)... So, at a high level: There is a slight change in log levels, effectively adding a new warning level: - DEBUG is still debug (ideally not for users but developers, though this is something we need to work on) - INFO is still info, though I've added more data here, effectively making Syncthing more verbose by default (more on this below) - WARNING is a new log level that is different from the _old_ WARNING (more below) - ERROR is what was WARNING before -- problems that must be dealt with, and also bubbled as a popup in the GUI. A new feature is that the logging level can be set per package to something other than just debug or info, and hence I feel that we can add a bit more things into INFO while moving some (in fact, most) current INFO level warnings into WARNING. For example, I think it's justified to get a log of synced files in INFO and sync failures in WARNING. These are things that have historically been tricky to debug properly, and having more information by default will be useful to many, while still making it possible get close to told level of inscrutability by setting the log level to WARNING. I'd like to get to a stage where DEBUG is never necessary to just figure out what's going on, as opposed to trying to narrow down a likely bug. Code wise: - Our logging object, generally known as `l` in each package, is now a new adapter object that provides the old API on top of the newer one. (This should go away once all old log entries are migrated.) This is only for `l.Debugln` and `l.Debugf`. - There is a new level tracker that keeps the log level for each package. - There is a nested setup of handlers, since the structure mandated by `log/slog` is slightly convoluted (imho). We do this because we need to do formatting at a "medium" level internally so we can buffer log lines in text format but with separate timestamp and log level for the API/GUI to consume. - The `debug` API call becomes a `loglevels` API call, which can set the log level to `DEBUG`, `INFO`, `WARNING` or `ERROR` per package. The GUI is updated to handle this. - Our custom `sync` package provided some debugging of mutexes quite strongly integrated into the old logging framework, only turned on when `STTRACE` was set to certain values at startup, etc. It's been a long time since this has been useful; I removed it. - The `STTRACE` env var remains and can be used the same way as before, while additionally permitting specific log levels to be specified, `STTRACE=model:WARN,scanner:DEBUG`. - There is a new command line option `--log-level=INFO` to set the default log level. - The command line options `--log-flags` and `--verbose` go away, but are currently retained as hidden & ignored options since we set them by default in some of our startup examples and Syncthing would otherwise fail to start. Sample format messages: ``` 2009-02-13 23:31:30 INF A basic info line (attr1="val with spaces" attr2=2 attr3="val\"quote" a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values (attr1=val1 foo.attr2=2 foo.bar.attr3=3 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with grouped values via logger (foo.attr1=val1 foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 INF An info line with nested grouped values via logger (bar.foo.attr1=val1 bar.foo.attr2=2 a=a log.pkg=slogutil) 2009-02-13 23:31:30 WRN A warning entry (a=a log.pkg=slogutil) 2009-02-13 23:31:30 ERR An error (a=a log.pkg=slogutil) ``` --------- Co-authored-by: Ross Smith II <ross@smithii.com>
392 lines
9.8 KiB
Go
392 lines
9.8 KiB
Go
// Copyright (C) 2016 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/.
|
|
|
|
package fs
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"os"
|
|
"os/user"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/shirou/gopsutil/v4/disk"
|
|
"github.com/syncthing/syncthing/lib/build"
|
|
)
|
|
|
|
const FilesystemTypeBasic FilesystemType = "basic"
|
|
|
|
var (
|
|
errInvalidFilenameEmpty = errors.New("name is invalid, must not be empty")
|
|
errInvalidFilenameWindowsSpacePeriod = errors.New("name is invalid, must not end in space or period on Windows")
|
|
errInvalidFilenameWindowsReservedName = errors.New("name is invalid, contains Windows reserved name")
|
|
errInvalidFilenameWindowsReservedChar = errors.New("name is invalid, contains Windows reserved character")
|
|
)
|
|
|
|
type OptionJunctionsAsDirs struct{}
|
|
|
|
func (*OptionJunctionsAsDirs) apply(fs Filesystem) Filesystem {
|
|
if basic, ok := fs.(*BasicFilesystem); !ok {
|
|
slog.Warn("WithJunctionsAsDirs must only be used with FilesystemTypeBasic")
|
|
} else {
|
|
basic.junctionsAsDirs = true
|
|
}
|
|
return fs
|
|
}
|
|
|
|
func (*OptionJunctionsAsDirs) String() string {
|
|
return "junctionsAsDirs"
|
|
}
|
|
|
|
// The BasicFilesystem implements all aspects by delegating to package os.
|
|
// All paths are relative to the root and cannot (should not) escape the root directory.
|
|
type BasicFilesystem struct {
|
|
root string
|
|
junctionsAsDirs bool
|
|
options []Option
|
|
userCache *userCache
|
|
groupCache *groupCache
|
|
}
|
|
|
|
type (
|
|
userCache = valueCache[string, *user.User]
|
|
groupCache = valueCache[string, *user.Group]
|
|
)
|
|
|
|
func init() {
|
|
RegisterFilesystemType(FilesystemTypeBasic, func(root string, opts ...Option) (Filesystem, error) {
|
|
return newBasicFilesystem(root, opts...), nil
|
|
})
|
|
}
|
|
|
|
func newBasicFilesystem(root string, opts ...Option) *BasicFilesystem {
|
|
if root == "" {
|
|
root = "." // Otherwise "" becomes "/" below
|
|
}
|
|
|
|
// The reason it's done like this:
|
|
// C: -> C:\ -> C:\ (issue that this is trying to fix)
|
|
// C:\somedir -> C:\somedir\ -> C:\somedir
|
|
// C:\somedir\ -> C:\somedir\ -> C:\somedir
|
|
// This way in the tests, we get away without OS specific separators
|
|
// in the test configs.
|
|
sep := string(filepath.Separator)
|
|
root = filepath.Clean(filepath.Dir(root + sep))
|
|
|
|
// Attempt tilde expansion; leave unchanged in case of error
|
|
if path, err := ExpandTilde(root); err == nil {
|
|
root = path
|
|
}
|
|
|
|
// Attempt absolutification; leave unchanged in case of error
|
|
if !filepath.IsAbs(root) {
|
|
// Abs() looks like a fairly expensive syscall on Windows, while
|
|
// IsAbs() is a whole bunch of string mangling. I think IsAbs() may be
|
|
// somewhat faster in the general case, hence the outer if...
|
|
if path, err := filepath.Abs(root); err == nil {
|
|
root = path
|
|
}
|
|
}
|
|
|
|
// Attempt to enable long filename support on Windows. We may still not
|
|
// have an absolute path here if the previous steps failed.
|
|
if build.IsWindows {
|
|
root = longFilenameSupport(root)
|
|
}
|
|
|
|
fs := &BasicFilesystem{
|
|
root: root,
|
|
options: opts,
|
|
userCache: newValueCache(time.Hour, user.LookupId),
|
|
groupCache: newValueCache(time.Hour, user.LookupGroupId),
|
|
}
|
|
for _, opt := range opts {
|
|
opt.apply(fs)
|
|
}
|
|
return fs
|
|
}
|
|
|
|
// rooted expands the relative path to the full path that is then used with os
|
|
// package. If the relative path somehow causes the final path to escape the root
|
|
// directory, this returns an error, to prevent accessing files that are not in the
|
|
// shared directory.
|
|
func (f *BasicFilesystem) rooted(rel string) (string, error) {
|
|
return rooted(rel, f.root)
|
|
}
|
|
|
|
func rooted(rel, root string) (string, error) {
|
|
// The root must not be empty.
|
|
if root == "" {
|
|
return "", errInvalidFilenameEmpty
|
|
}
|
|
|
|
var err error
|
|
// Takes care that rel does not try to escape
|
|
rel, err = Canonicalize(rel)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return filepath.Join(root, rel), nil
|
|
}
|
|
|
|
func (f *BasicFilesystem) unrooted(path string) string {
|
|
return rel(path, f.root)
|
|
}
|
|
|
|
func (f *BasicFilesystem) Chmod(name string, mode FileMode) error {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Chmod(name, os.FileMode(mode))
|
|
}
|
|
|
|
func (f *BasicFilesystem) Chtimes(name string, atime time.Time, mtime time.Time) error {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Chtimes(name, atime, mtime)
|
|
}
|
|
|
|
func (f *BasicFilesystem) Mkdir(name string, perm FileMode) error {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Mkdir(name, os.FileMode(perm))
|
|
}
|
|
|
|
// MkdirAll creates a directory named path, along with any necessary parents,
|
|
// and returns nil, or else returns an error.
|
|
// The permission bits perm are used for all directories that MkdirAll creates.
|
|
// If path is already a directory, MkdirAll does nothing and returns nil.
|
|
func (f *BasicFilesystem) MkdirAll(path string, perm FileMode) error {
|
|
path, err := f.rooted(path)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return f.mkdirAll(path, os.FileMode(perm))
|
|
}
|
|
|
|
func (f *BasicFilesystem) Lstat(name string) (FileInfo, error) {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fi, err := f.underlyingLstat(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFileInfo{fi}, err
|
|
}
|
|
|
|
func (f *BasicFilesystem) RemoveAll(name string) error {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.RemoveAll(name)
|
|
}
|
|
|
|
func (f *BasicFilesystem) Rename(oldpath, newpath string) error {
|
|
oldpath, err := f.rooted(oldpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
newpath, err = f.rooted(newpath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(oldpath, newpath)
|
|
}
|
|
|
|
func (f *BasicFilesystem) Stat(name string) (FileInfo, error) {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fi, err := os.Stat(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFileInfo{fi}, err
|
|
}
|
|
|
|
func (f *BasicFilesystem) DirNames(name string) ([]string, error) {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fd, err := os.OpenFile(name, OptReadOnly, 0o777)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer fd.Close()
|
|
|
|
names, err := fd.Readdirnames(-1)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return names, nil
|
|
}
|
|
|
|
func (f *BasicFilesystem) Open(name string) (File, error) {
|
|
rootedName, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fd, err := os.Open(rootedName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFile{fd, name}, err
|
|
}
|
|
|
|
func (f *BasicFilesystem) OpenFile(name string, flags int, mode FileMode) (File, error) {
|
|
rootedName, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fd, err := os.OpenFile(rootedName, flags, os.FileMode(mode))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFile{fd, name}, err
|
|
}
|
|
|
|
func (f *BasicFilesystem) Create(name string) (File, error) {
|
|
rootedName, err := f.rooted(name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
fd, err := os.Create(rootedName)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFile{fd, name}, err
|
|
}
|
|
|
|
func (*BasicFilesystem) Walk(_ string, _ WalkFunc) error {
|
|
// implemented in WalkFilesystem
|
|
return errors.New("not implemented")
|
|
}
|
|
|
|
func (f *BasicFilesystem) Glob(pattern string) ([]string, error) {
|
|
pattern, err := f.rooted(pattern)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
files, err := filepath.Glob(pattern)
|
|
unrooted := make([]string, len(files))
|
|
for i := range files {
|
|
unrooted[i] = f.unrooted(files[i])
|
|
}
|
|
return unrooted, err
|
|
}
|
|
|
|
func (f *BasicFilesystem) Usage(name string) (Usage, error) {
|
|
name, err := f.rooted(name)
|
|
if err != nil {
|
|
return Usage{}, err
|
|
}
|
|
u, err := disk.Usage(name)
|
|
if err != nil {
|
|
return Usage{}, err
|
|
}
|
|
return Usage{
|
|
Free: u.Free,
|
|
Total: u.Total,
|
|
}, nil
|
|
}
|
|
|
|
func (*BasicFilesystem) Type() FilesystemType {
|
|
return FilesystemTypeBasic
|
|
}
|
|
|
|
func (f *BasicFilesystem) URI() string {
|
|
return strings.TrimPrefix(f.root, `\\?\`)
|
|
}
|
|
|
|
func (f *BasicFilesystem) Options() []Option {
|
|
return f.options
|
|
}
|
|
|
|
func (*BasicFilesystem) SameFile(fi1, fi2 FileInfo) bool {
|
|
// Like os.SameFile, we always return false unless fi1 and fi2 were created
|
|
// by this package's Stat/Lstat method.
|
|
f1, ok1 := fi1.(basicFileInfo)
|
|
f2, ok2 := fi2.(basicFileInfo)
|
|
if !ok1 || !ok2 {
|
|
return false
|
|
}
|
|
|
|
return os.SameFile(f1.osFileInfo(), f2.osFileInfo())
|
|
}
|
|
|
|
func (*BasicFilesystem) underlying() (Filesystem, bool) {
|
|
return nil, false
|
|
}
|
|
|
|
// basicFile implements the fs.File interface on top of an os.File
|
|
type basicFile struct {
|
|
*os.File
|
|
name string
|
|
}
|
|
|
|
func (f basicFile) Name() string {
|
|
return f.name
|
|
}
|
|
|
|
func (f basicFile) Stat() (FileInfo, error) {
|
|
info, err := f.File.Stat()
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return basicFileInfo{info}, nil
|
|
}
|
|
|
|
// basicFileInfo implements the fs.FileInfo interface on top of an os.FileInfo.
|
|
type basicFileInfo struct {
|
|
os.FileInfo
|
|
}
|
|
|
|
func (e basicFileInfo) IsSymlink() bool {
|
|
// Must use basicFileInfo.Mode() because it may apply magic.
|
|
return e.Mode()&ModeSymlink != 0
|
|
}
|
|
|
|
func (e basicFileInfo) IsRegular() bool {
|
|
// Must use basicFileInfo.Mode() because it may apply magic.
|
|
return e.Mode()&ModeType == 0
|
|
}
|
|
|
|
// longFilenameSupport adds the necessary prefix to the path to enable long
|
|
// filename support on windows if necessary.
|
|
// This does NOT check the current system, i.e. will also take effect on unix paths.
|
|
func longFilenameSupport(path string) string {
|
|
if filepath.IsAbs(path) && !strings.HasPrefix(path, `\\`) {
|
|
return `\\?\` + path
|
|
}
|
|
return path
|
|
}
|
|
|
|
type WatchEventOutsideRootError struct{ msg string }
|
|
|
|
func (e *WatchEventOutsideRootError) Error() string {
|
|
return e.msg
|
|
}
|
|
|
|
func (f *BasicFilesystem) newErrWatchEventOutsideRoot(absPath string, roots []string) *WatchEventOutsideRootError {
|
|
return &WatchEventOutsideRootError{fmt.Sprintf("Watching for changes encountered an event outside of the filesystem root: f.root==%v, roots==%v, path==%v. This should never happen, please report this message to forum.syncthing.net.", f.root, roots, absPath)}
|
|
}
|