lib/scanner: Centralise protocol.FileInfo creation (#5202)

This commit is contained in:
Simon Frei 2018-09-18 15:34:17 +02:00 committed by GitHub
parent 6a87aac84f
commit 4f4781d254
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -309,13 +309,6 @@ func (w *walker) handleItem(ctx context.Context, path string, fchan, dchan chan
func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileInfo, fchan chan protocol.FileInfo) error { func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileInfo, fchan chan protocol.FileInfo) error {
curFile, hasCurFile := w.CurrentFiler.CurrentFile(relPath) curFile, hasCurFile := w.CurrentFiler.CurrentFile(relPath)
newMode := uint32(info.Mode())
if runtime.GOOS == "windows" && hasCurFile {
// If we have an existing index entry, copy the executable bits
// from there.
newMode |= (curFile.Permissions & 0111)
}
blockSize := protocol.MinBlockSize blockSize := protocol.MinBlockSize
if w.UseLargeBlocks { if w.UseLargeBlocks {
@ -336,19 +329,10 @@ func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileIn
} }
} }
f := protocol.FileInfo{ f, _ := CreateFileInfo(info, relPath, nil)
Name: relPath, f = w.updateFileInfo(f, curFile)
Type: protocol.FileInfoTypeFile, f.NoPermissions = w.IgnorePerms
Version: curFile.Version.Update(w.ShortID), f.RawBlockSize = int32(blockSize)
Permissions: newMode & uint32(fs.ModePerm),
NoPermissions: w.IgnorePerms,
ModifiedS: info.ModTime().Unix(),
ModifiedNs: int32(info.ModTime().Nanosecond()),
ModifiedBy: w.ShortID,
Size: info.Size(),
RawBlockSize: int32(blockSize),
LocalFlags: w.LocalFlags,
}
if hasCurFile { if hasCurFile {
if curFile.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) { if curFile.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) {
@ -377,25 +361,17 @@ func (w *walker) walkRegular(ctx context.Context, relPath string, info fs.FileIn
} }
func (w *walker) walkDir(ctx context.Context, relPath string, info fs.FileInfo, dchan chan protocol.FileInfo) error { func (w *walker) walkDir(ctx context.Context, relPath string, info fs.FileInfo, dchan chan protocol.FileInfo) error {
cf, ok := w.CurrentFiler.CurrentFile(relPath) curFile, hasCurFile := w.CurrentFiler.CurrentFile(relPath)
f := protocol.FileInfo{ f, _ := CreateFileInfo(info, relPath, nil)
Name: relPath, f = w.updateFileInfo(f, curFile)
Type: protocol.FileInfoTypeDirectory, f.NoPermissions = w.IgnorePerms
Version: cf.Version.Update(w.ShortID),
Permissions: uint32(info.Mode() & fs.ModePerm),
NoPermissions: w.IgnorePerms,
ModifiedS: info.ModTime().Unix(),
ModifiedNs: int32(info.ModTime().Nanosecond()),
ModifiedBy: w.ShortID,
LocalFlags: w.LocalFlags,
}
if ok { if hasCurFile {
if cf.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) { if curFile.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) {
return nil return nil
} }
if cf.ShouldConflict() { if curFile.ShouldConflict() {
// The old file was invalid for whatever reason and probably not // The old file was invalid for whatever reason and probably not
// up to date with what was out there in the cluster. Drop all // up to date with what was out there in the cluster. Drop all
// others from the version vector to indicate that we haven't // others from the version vector to indicate that we haven't
@ -436,23 +412,21 @@ func (w *walker) walkSymlink(ctx context.Context, relPath string, dchan chan pro
return nil return nil
} }
cf, ok := w.CurrentFiler.CurrentFile(relPath) curFile, hasCurFile := w.CurrentFiler.CurrentFile(relPath)
f := protocol.FileInfo{ f := protocol.FileInfo{
Name: relPath, Name: relPath,
Type: protocol.FileInfoTypeSymlink, Type: protocol.FileInfoTypeSymlink,
Version: cf.Version.Update(w.ShortID),
NoPermissions: true, // Symlinks don't have permissions of their own NoPermissions: true, // Symlinks don't have permissions of their own
SymlinkTarget: target, SymlinkTarget: target,
ModifiedBy: w.ShortID,
LocalFlags: w.LocalFlags,
} }
f = w.updateFileInfo(f, curFile)
if ok { if hasCurFile {
if cf.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) { if curFile.IsEquivalentOptional(f, w.IgnorePerms, true, w.LocalFlags) {
return nil return nil
} }
if cf.ShouldConflict() { if curFile.ShouldConflict() {
// The old file was invalid for whatever reason and probably not // The old file was invalid for whatever reason and probably not
// up to date with what was out there in the cluster. Drop all // up to date with what was out there in the cluster. Drop all
// others from the version vector to indicate that we haven't // others from the version vector to indicate that we haven't
@ -536,6 +510,19 @@ func (w *walker) normalizePath(path string, info fs.FileInfo) (normPath string,
return normPath, false return normPath, false
} }
// updateFileInfo updates walker specific members of protocol.FileInfo that do not depend on type
func (w *walker) updateFileInfo(file, curFile protocol.FileInfo) protocol.FileInfo {
if file.Type == protocol.FileInfoTypeFile && runtime.GOOS == "windows" {
// If we have an existing index entry, copy the executable bits
// from there.
file.Permissions |= (curFile.Permissions & 0111)
}
file.Version = curFile.Version.Update(w.ShortID)
file.ModifiedBy = w.ShortID
file.LocalFlags = w.LocalFlags
return file
}
// A byteCounter gets bytes added to it via Update() and then provides the // A byteCounter gets bytes added to it via Update() and then provides the
// Total() and one minute moving average Rate() in bytes per second. // Total() and one minute moving average Rate() in bytes per second.
type byteCounter struct { type byteCounter struct {
@ -590,24 +577,24 @@ func (noCurrentFiler) CurrentFile(name string) (protocol.FileInfo, bool) {
} }
func CreateFileInfo(fi fs.FileInfo, name string, filesystem fs.Filesystem) (protocol.FileInfo, error) { func CreateFileInfo(fi fs.FileInfo, name string, filesystem fs.Filesystem) (protocol.FileInfo, error) {
f := protocol.FileInfo{ f := protocol.FileInfo{Name: name}
Name: name, if fi.IsSymlink() {
Type: protocol.FileInfoTypeFile,
Permissions: uint32(fi.Mode() & fs.ModePerm),
ModifiedS: fi.ModTime().Unix(),
ModifiedNs: int32(fi.ModTime().Nanosecond()),
Size: fi.Size(),
}
switch {
case fi.IsDir():
f.Type = protocol.FileInfoTypeDirectory
case fi.IsSymlink():
f.Type = protocol.FileInfoTypeSymlink f.Type = protocol.FileInfoTypeSymlink
target, err := filesystem.ReadSymlink(name) target, err := filesystem.ReadSymlink(name)
if err != nil { if err != nil {
return protocol.FileInfo{}, err return protocol.FileInfo{}, err
} }
f.SymlinkTarget = target f.SymlinkTarget = target
return f, nil
} }
f.Permissions = uint32(fi.Mode() & fs.ModePerm)
f.ModifiedS = fi.ModTime().Unix()
f.ModifiedNs = int32(fi.ModTime().Nanosecond())
if fi.IsDir() {
f.Type = protocol.FileInfoTypeDirectory
return f, nil
}
f.Size = fi.Size()
f.Type = protocol.FileInfoTypeFile
return f, nil return f, nil
} }