From 303ce02271c669c582baa2c2579dbf6ddde4c1e9 Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Fri, 23 May 2014 13:10:26 +0200 Subject: [PATCH] Internal support for ignoring permissions --- model/puller.go | 6 ++++-- scanner/walk.go | 25 +++++++++++++++++++++---- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/model/puller.go b/model/puller.go index f6b9a77ac..e5e69fc1a 100644 --- a/model/puller.go +++ b/model/puller.go @@ -538,7 +538,7 @@ func (p *puller) handleEmptyBlock(b bqBlock) { delete(p.openFiles, f.Name) return } - if os.Chmod(of.temp, os.FileMode(f.Flags&0777)) != nil { + if protocol.HasPermissionBits(f.Flags) && os.Chmod(of.temp, os.FileMode(f.Flags&0777)) != nil { delete(p.openFiles, f.Name) return } @@ -607,7 +607,9 @@ func (p *puller) closeFile(f scanner.File) { t := time.Unix(f.Modified, 0) os.Chtimes(of.temp, t, t) - os.Chmod(of.temp, os.FileMode(f.Flags&0777)) + if protocol.HasPermissionBits(f.Flags) { + os.Chmod(of.temp, os.FileMode(f.Flags&0777)) + } defTempNamer.Show(of.temp) if debug { l.Debugf("pull: rename %q / %q: %q", p.repo, f.Name, of.filepath) diff --git a/scanner/walk.go b/scanner/walk.go index cb7af3468..5ab6ca4c3 100644 --- a/scanner/walk.go +++ b/scanner/walk.go @@ -29,6 +29,10 @@ type Walker struct { // Suppressed files will be returned with empty metadata and the Suppressed flag set. // Requires CurrentFiler to be set. Suppressor Suppressor + // If IgnorePermissions is true, changes to permission bits will not be + // detected. Scanned files will get zero permission bits and the + // NoPermissionBits flag set. + IgnorePermissions bool } type TempNamer interface { @@ -162,16 +166,23 @@ func (w *Walker) walkAndHashFiles(res *[]File, ign map[string][]string) filepath if info.Mode().IsDir() { if w.CurrentFiler != nil { cf := w.CurrentFiler.CurrentFile(rn) - if cf.Modified == info.ModTime().Unix() && protocol.IsDirectory(cf.Flags) && PermsEqual(cf.Flags, uint32(info.Mode())) { + permUnchanged := w.IgnorePermissions || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode())) + if cf.Modified == info.ModTime().Unix() && protocol.IsDirectory(cf.Flags) && permUnchanged { if debug { l.Debugln("unchanged:", cf) } *res = append(*res, cf) } else { + var flags uint32 = protocol.FlagDirectory + if w.IgnorePermissions { + flags |= protocol.FlagNoPermBits + } else { + flags |= uint32(info.Mode() & os.ModePerm) + } f := File{ Name: rn, Version: lamport.Default.Tick(0), - Flags: uint32(info.Mode()&os.ModePerm) | protocol.FlagDirectory, + Flags: flags, Modified: info.ModTime().Unix(), } if debug { @@ -186,7 +197,8 @@ func (w *Walker) walkAndHashFiles(res *[]File, ign map[string][]string) filepath if info.Mode().IsRegular() { if w.CurrentFiler != nil { cf := w.CurrentFiler.CurrentFile(rn) - if !protocol.IsDeleted(cf.Flags) && cf.Modified == info.ModTime().Unix() && PermsEqual(cf.Flags, uint32(info.Mode())) { + permUnchanged := w.IgnorePermissions || !protocol.HasPermissionBits(cf.Flags) || PermsEqual(cf.Flags, uint32(info.Mode())) + if !protocol.IsDeleted(cf.Flags) && cf.Modified == info.ModTime().Unix() && permUnchanged { if debug { l.Debugln("unchanged:", cf) } @@ -235,11 +247,16 @@ func (w *Walker) walkAndHashFiles(res *[]File, ign map[string][]string) filepath t1 := time.Now() l.Debugln("hashed:", rn, ";", len(blocks), "blocks;", info.Size(), "bytes;", int(float64(info.Size())/1024/t1.Sub(t0).Seconds()), "KB/s") } + + var flags = uint32(info.Mode() & os.ModePerm) + if w.IgnorePermissions { + flags = protocol.FlagNoPermBits + } f := File{ Name: rn, Version: lamport.Default.Tick(0), Size: info.Size(), - Flags: uint32(info.Mode()), + Flags: flags, Modified: info.ModTime().Unix(), Blocks: blocks, }