-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,8 @@ | |
package config | ||
|
||
import ( | ||
"bytes" | ||
"crypto/sha256" | ||
"errors" | ||
"fmt" | ||
"path" | ||
|
@@ -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 | ||
} | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also There was a problem hiding this comment. Choose a reason for hiding this commentThe 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), ".") | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.