// Execute ChownJob.
func (job *ChownJob) Execute() {
	defer job.finalize()
	// -1 means not change.
	uid := -1
	gid := -1
	if job.owner != "" {
		newUser, err := user.Lookup(job.owner)
		if err != nil {
			job.setError(err)
			return
		}
		uid, _ = strconv.Atoi(newUser.Uid)
	}

	if job.group != "" {
		cGroupName := C.CString(job.group)
		defer C.free(unsafe.Pointer(cGroupName))
		group := C.getgrnam(cGroupName)
		if group == nil {
			job.setError(errors.New("no such a group"))
			return
		}
		gid = int(group.gr_gid)
	}

	job.setError(os.Chown(job.file.GetPath(), uid, gid))
}
Beispiel #2
0
// getGroupId returns the gid for the
// given group, or -1 if it does not
// exists.
func getGroupId(name string) int {
	groupMutex.Lock()
	defer groupMutex.Unlock()
	cname := C.CString(name)
	defer C.free(unsafe.Pointer(cname))
	group := C.getgrnam(cname)
	if group == nil {
		return -1
	}
	return int(group.gr_gid)
}