Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/config: Add file inside folder marker directory #9525

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 36 additions & 10 deletions lib/config/folderconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package config

import (
"bytes"
"crypto/sha256"
"errors"
"fmt"
"path"
Expand Down Expand Up @@ -90,27 +92,51 @@ func (f *FolderConfiguration) CreateMarker() error {
return nil
}

permBits := fs.FileMode(0o777)
if build.IsWindows {
// Windows has no umask so we must chose a safer set of bits to
// begin with.
permBits = 0o700
}
Comment on lines -94 to -98
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is no longer needed, it should be noted in the commit message. I suspect it stems from the time we transitioned from a marker file to a marker directory?

Isn't the same logic needed for the content file then?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's based on a misunderstanding to begin with. The only thing these bits do on Windows is set the read only flag, which we don't do here. Having the defaults remove group & other write bits on unixes is best practice. I'll make a note of it.

fs := f.Filesystem(nil)
err := fs.Mkdir(DefaultMarkerName, permBits)
ffs := f.Filesystem(nil)

// Create the marker as a directory
err := ffs.Mkdir(DefaultMarkerName, 0o755)
if err != nil {
return err
}
if dir, err := fs.Open("."); err != nil {

// Create a file inside it, reducing the risk of the marker directory
// being removed by automated cleanup tools.
markerFile := filepath.Join(DefaultMarkerName, f.markerFilename())
if err := fs.WriteFile(ffs, markerFile, f.markerContents(), 0o644); err != nil {
return err
}

// Sync & hide the containing directory
if dir, err := ffs.Open("."); err != nil {
l.Debugln("folder marker: open . failed:", err)
} else if err := dir.Sync(); err != nil {
l.Debugln("folder marker: fsync . failed:", err)
}
fs.Hide(DefaultMarkerName)
ffs.Hide(DefaultMarkerName)

return nil
}

func (f *FolderConfiguration) RemoveMarker() error {
ffs := f.Filesystem(nil)
_ = ffs.Remove(filepath.Join(DefaultMarkerName, f.markerFilename()))
return ffs.Remove(DefaultMarkerName)
}

func (f *FolderConfiguration) markerFilename() string {
h := sha256.Sum256([]byte(f.ID))
return fmt.Sprintf("syncthing-folder-%x.txt", h[:3])
}

func (f *FolderConfiguration) markerContents() []byte {
var buf bytes.Buffer
buf.WriteString("# This directory is a Syncthing folder marker.\n# Do not delete.\n\n")
fmt.Fprintf(&buf, "folderID: %s\n", f.ID)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also folderLabel, or do you think this info is too fragile (not guaranteed to be immutable)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that, it can change and this file won't.

fmt.Fprintf(&buf, "created: %s\n", time.Now().Format(time.RFC3339))
return buf.Bytes()
}

// CheckPath returns nil if the folder root exists and contains the marker file
func (f *FolderConfiguration) CheckPath() error {
return f.checkFilesystemPath(f.Filesystem(nil), ".")
Expand Down
4 changes: 2 additions & 2 deletions lib/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,9 @@ func (m *model) removeFolder(cfg config.FolderConfiguration) {
if isPathUnique {
// Remove (if empty and removable) or move away (if non-empty or
// otherwise not removable) Syncthing-specific marker files.
fs := cfg.Filesystem(nil)
if err := fs.Remove(config.DefaultMarkerName); err != nil {
if err := cfg.RemoveMarker(); err != nil && !errors.Is(err, os.ErrNotExist) {
moved := config.DefaultMarkerName + time.Now().Format(".removed-20060102-150405")
fs := cfg.Filesystem(nil)
_ = fs.Rename(config.DefaultMarkerName, moved)
}
}
Expand Down