Skip to content
Permalink
Browse files

tools: add function to make directories honoring core.sharedRepository

Add a function to make a directory and intervening directories, honoring
core.sharedRepository.

To do so, add a utility function to perform an operation with the umask
set to a certain value. This is required because os.MkdirAll honors the
umask and is potentially recursive (so we can't fix things after
creating them). Without temporarily setting the umask, it isn't possible
to create directories with the right permissions if those permissions
are looser than the original umask.
  • Loading branch information...
bk2204 committed Dec 4, 2018
1 parent 26dcdff commit f5d5f487eb749f96121db80ea9e650df32f5499a
Showing with 33 additions and 0 deletions.
  1. +15 −0 tools/filetools.go
  2. +11 −0 tools/umask_nix.go
  3. +7 −0 tools/umask_windows.go
@@ -115,6 +115,21 @@ func CleanPaths(paths, delim string) (cleaned []string) {
return cleaned
}

// repositoryPermissionFetcher is an interface that matches the configuration
// object and can be used to fetch repository permissions.
type repositoryPermissionFetcher interface {
RepositoryPermissions(executable bool) os.FileMode
}

// MkdirAll makes a directory and any intervening directories with the
// permissions specified by the core.sharedRepository setting.
func MkdirAll(path string, config repositoryPermissionFetcher) error {
umask := 0777 & ^config.RepositoryPermissions(true)
return doWithUmask(int(umask), func() error {
return os.MkdirAll(path, config.RepositoryPermissions(true))
})
}

var (
// currentUser is a wrapper over user.Current(), but instead uses the
// value of os.Getenv("HOME") for the returned *user.User's "HomeDir"
@@ -0,0 +1,11 @@
// +build !windows

package tools

import "syscall"

func doWithUmask(mask int, f func() error) error {
mask = syscall.Umask(mask)
defer syscall.Umask(mask)
return f()
}
@@ -0,0 +1,7 @@
// +build windows

package tools

func doWithUmask(mask int, f func() error) error {
return f()
}

0 comments on commit f5d5f48

Please sign in to comment.
You can’t perform that action at this time.