Allow directory creation mode to be specified in configuration files; bugfixes

This commit is contained in:
Alex Yatskov 2015-04-09 19:07:06 +09:00
parent ef5f8cba8d
commit cccb9b3903
2 changed files with 53 additions and 40 deletions

View File

@ -32,12 +32,8 @@ import (
type command []string type command []string
func (this *command) valid() bool {
return len(*this) >= 1
}
func (this *command) process(dir string, flags int) error { func (this *command) process(dir string, flags int) error {
if !this.valid() { if len(*this) < 1 {
return fmt.Errorf("command element is invalid") return fmt.Errorf("command element is invalid")
} }

87
link.go
View File

@ -27,6 +27,7 @@ import (
"log" "log"
"os" "os"
"path" "path"
"strconv"
) )
type link []string type link []string
@ -55,14 +56,14 @@ func cleanPath(loc string, flags int) error {
return nil return nil
} }
func createPath(loc string, flags int) error { func createPath(loc string, flags int, mode os.FileMode) error {
if flags&flagForce == flagForce { if flags&flagForce == flagForce {
parentDir, _ := path.Split(loc) parentDir, _ := path.Split(loc)
if _, err := os.Stat(parentDir); os.IsNotExist(err) { if _, err := os.Stat(parentDir); os.IsNotExist(err) {
if flags&flagVerbose == flagVerbose { if flags&flagVerbose == flagVerbose {
log.Printf("force creating path %s", parentDir) log.Printf("force creating path %s", parentDir)
} }
if err := os.MkdirAll(parentDir, 0777); err != nil { if err := os.MkdirAll(parentDir, mode); err != nil {
return err return err
} }
} }
@ -71,50 +72,66 @@ func createPath(loc string, flags int) error {
return nil return nil
} }
func (this *link) destination() string { func (this *link) parse() (string, string, os.FileMode, error) {
if len(*this) > 0 {
return (*this)[0]
}
return ""
}
func (this *link) source() string {
if len(*this) > 1 {
return (*this)[1]
}
return this.destination()
}
func (this *link) valid() bool {
length := len(*this) length := len(*this)
return length >= 1 && length <= 2 if length < 1 || length > 3 {
return "", "", 0, fmt.Errorf("link element is invalid")
}
dstPath := (*this)[0]
srcPath := dstPath
if length > 1 {
srcPath = (*this)[1]
}
var mode os.FileMode = 0755
if length > 2 {
parsed, err := strconv.ParseUint((*this)[2], 0, 64)
if err != nil {
return "", "", 0, err
}
mode = os.FileMode(parsed)
}
return srcPath, dstPath, mode, nil
} }
func (this *link) process(srcDir, dstDir string, flags int) error { func (this *link) process(srcDir, dstDir string, flags int) error {
if !this.valid() { srcPath, dstPath, mode, err := this.parse()
return fmt.Errorf("link element is invalid") if err != nil {
}
srcPath := path.Join(srcDir, this.source())
dstPath := path.Join(dstDir, this.destination())
if _, err := os.Stat(srcPath); os.IsNotExist(err) {
return fmt.Errorf("source path %s does not exist in filesystem", srcPath)
}
if err := createPath(dstPath, flags); err != nil {
return err return err
} }
if err := cleanPath(dstPath, flags); err != nil { srcPathAbs := srcPath
if !path.IsAbs(srcPathAbs) {
srcPathAbs = path.Join(srcDir, srcPath)
}
dstPathAbs := dstPath
if !path.IsAbs(dstPathAbs) {
dstPathAbs = path.Join(dstDir, dstPath)
}
if _, err := os.Stat(srcPathAbs); os.IsNotExist(err) {
return fmt.Errorf("source path %s does not exist in filesystem", srcPathAbs)
}
if err := createPath(dstPathAbs, flags, mode); err != nil {
return err
}
if err := cleanPath(dstPathAbs, flags); err != nil {
return err return err
} }
if flags&flagVerbose == flagVerbose { if flags&flagVerbose == flagVerbose {
log.Printf("linking %s to %s", srcPath, dstPath) log.Printf("linking %s to %s", srcPathAbs, dstPathAbs)
} }
return os.Symlink(srcPath, dstPath) if err := os.Symlink(srcPathAbs, dstPathAbs); err != nil {
return err
}
return nil
} }