107 lines
1.8 KiB
Go
107 lines
1.8 KiB
Go
package conf
|
|
|
|
import (
|
|
`io/fs`
|
|
`os`
|
|
`os/user`
|
|
`strconv`
|
|
|
|
`r00t2.io/sysutils/paths`
|
|
)
|
|
|
|
// Chmod enforces perms for a file or directory.
|
|
func (p *Perms) Chmod(path string, isNew bool) (err error) {
|
|
|
|
var fi fs.FileInfo
|
|
|
|
if err = paths.RealPath(&path); err != nil {
|
|
return
|
|
}
|
|
if fi, err = os.Stat(path); err != nil {
|
|
return
|
|
}
|
|
|
|
// If we add additional spec types (e.g. sockets, etc.), make this a switch.
|
|
if fi.IsDir() {
|
|
if p.ParentDir != nil {
|
|
if err = p.ParentDir.chmod(path, isNew); err != nil {
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if p.File != nil {
|
|
if err = p.File.chmod(path, isNew); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// Chown enforces owner/group for a file or directory.
|
|
func (p *Perms) Chown(path string) (err error) {
|
|
|
|
var fi fs.FileInfo
|
|
|
|
if err = paths.RealPath(&path); err != nil {
|
|
return
|
|
}
|
|
|
|
// If we add additional spec types (e.g. sockets, etc.), make this a switch.
|
|
if fi.IsDir() {
|
|
if p.ParentDir != nil {
|
|
if err = p.ParentDir.chown(path); err != nil {
|
|
return
|
|
}
|
|
}
|
|
} else {
|
|
if p.File != nil {
|
|
if err = p.File.chown(path); err != nil {
|
|
return
|
|
}
|
|
}
|
|
}
|
|
|
|
return
|
|
}
|
|
|
|
// SetMissing populates any missing fields.
|
|
func (p *Perms) SetMissing() (err error) {
|
|
|
|
if p.curUser == nil {
|
|
if p.curUser, err = user.Current(); err != nil {
|
|
return
|
|
}
|
|
if p.curUid, err = strconv.Atoi(p.curUser.Uid); err != nil {
|
|
return
|
|
}
|
|
}
|
|
if p.curGroup == nil {
|
|
if p.curGroup, err = user.LookupGroupId(p.curGroup.Gid); err != nil {
|
|
return
|
|
}
|
|
if p.curGid, err = strconv.Atoi(p.curGroup.Gid); err != nil {
|
|
return
|
|
}
|
|
}
|
|
|
|
if p.File == nil {
|
|
p.File = new(PermSpec)
|
|
}
|
|
p.File.parent = p
|
|
if p.ParentDir == nil {
|
|
p.ParentDir = new(PermSpec)
|
|
}
|
|
p.ParentDir.parent = p
|
|
|
|
if err = p.File.setMissing(false); err != nil {
|
|
return
|
|
}
|
|
if err = p.ParentDir.setMissing(true); err != nil {
|
|
return
|
|
}
|
|
|
|
return
|
|
}
|