From 67267aa9aebbd56342e02c5c0d3ed295c15be867 Mon Sep 17 00:00:00 2001 From: Evan Purkhiser Date: Tue, 24 Jul 2018 00:32:12 -0700 Subject: [PATCH] Improve handing of mode / permissions on install --- installer/installer.go | 13 ++++++++----- installer/prepare.go | 35 +++++++++++++++++++++-------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/installer/installer.go b/installer/installer.go index e205e5d..21dca11 100644 --- a/installer/installer.go +++ b/installer/installer.go @@ -36,8 +36,7 @@ func InstallDotfile(dotfile *PreparedDotfile, config InstallConfig) error { return fmt.Errorf("Source files are not all regular files") } - // No change - if !dotfile.IsNew && !dotfile.ContentsDiffer && dotfile.Permissions.IsSame() { + if !dotfile.IsChanged() { return nil } @@ -46,10 +45,14 @@ func InstallDotfile(dotfile *PreparedDotfile, config InstallConfig) error { return os.Remove(installPath) } - targetMode := dotfile.Permissions.New | dotfile.Mode + targetMode := dotfile.Permissions.New | dotfile.Mode.New - // Only permissions differ - if !dotfile.IsNew && !dotfile.Permissions.IsSame() && !dotfile.ContentsDiffer { + // Only filemode differs + modeChanged := !dotfile.IsNew && + !dotfile.ContentsDiffer && + (dotfile.Permissions.IsChanged() || dotfile.Mode.IsChanged()) + + if modeChanged { return os.Chmod(installPath, targetMode) } diff --git a/installer/prepare.go b/installer/prepare.go index 9234b96..bdaac68 100644 --- a/installer/prepare.go +++ b/installer/prepare.go @@ -26,14 +26,15 @@ type PreparedDotfile struct { // compiled or installed. SourcesAreIrregular bool - // Mode represents the mode bits of the os.FileMode (does not include - // permissions). This will not be set if the sources are irregular. - Mode os.FileMode + // Mode represents the change in the file mode bits of the source and + // target os.FileMode (does not include permissions). This will not be set + // if the sources are irregular. + Mode *FileMode // Permissions represents the change in permission between the compiled source // and the currently installed dotfile. Equal permissions can be verified // by calling Permissions.IsSame. - Permissions Permissions + Permissions *FileMode // SourcePermissionsDiffer indicates that a compiled dotfile (one with // multiple sources) does not have consistent permissions across all @@ -55,15 +56,21 @@ type PreparedDotfile struct { sourceInfo []os.FileInfo } -// Permissions represents a change in file permissions. -type Permissions struct { +// IsChanged reports if the prepared dotfile has changes from the target +// dotfile. +func (p *PreparedDotfile) IsChanged() bool { + return p.IsNew || p.ContentsDiffer || p.Permissions.IsChanged() || p.Mode.IsChanged() +} + +// FileMode represents the new and old dotfile file mode. +type FileMode struct { Old os.FileMode New os.FileMode } -// IsSame returns a boolean value indicating if the modes are equal. -func (d Permissions) IsSame() bool { - return d.New == d.Old +// IsChanged returns a boolean value indicating if the modes are equal. +func (d FileMode) IsChanged() bool { + return d.New != d.Old } // PreparedDotfiles is a list of prepared dotfiles. @@ -129,7 +136,7 @@ func PrepareDotfiles(dotfiles resolver.Dotfiles, config config.SourceConfig) Pre sourcePermissions, tookLowest := flattenPermissions(sourceInfo) - prepared.Permissions = Permissions{ + prepared.Permissions = &FileMode{ Old: targetInfo.Mode() & os.ModePerm, New: sourcePermissions, } @@ -137,11 +144,11 @@ func PrepareDotfiles(dotfiles resolver.Dotfiles, config config.SourceConfig) Pre prepared.SourcesAreIrregular = !isAllRegular(sourceInfo) - // NOTE: This currently drops non-mode bits (permissions are included - // seperately), however should the dotfile set ModeAppend, ModeSticky, - // etc, these modes will not be included here. if !prepared.SourcesAreIrregular { - prepared.Mode = sourceInfo[0].Mode() & os.ModeType + prepared.Mode = &FileMode{ + Old: targetInfo.Mode() &^ os.ModePerm, + New: sourceInfo[0].Mode() &^ os.ModePerm, + } } // If the dotfile does not require compilation we can directly compare