func lookupUnix(gid int, groupname string, lookupByName bool) (*Group, error) { var grp C.struct_group var result *C.struct_group var bufSize C.long if runtime.GOOS == "freebsd" { panic("Don't know how to deal with freebsd.") } else { bufSize = C.sysconf(C._SC_GETGR_R_SIZE_MAX) * 20 if bufSize <= 0 || bufSize > 1<<20 { return nil, fmt.Errorf("group: unreasonable _SC_GETGR_R_SIZE_MAX of %d", bufSize) } } buf := C.malloc(C.size_t(bufSize)) defer C.free(buf) var rv C.int if lookupByName { nameC := C.CString(groupname) defer C.free(unsafe.Pointer(nameC)) rv = C.mygetgrnam_r(nameC, &grp, (*C.char)(buf), C.size_t(bufSize), &result) if rv != 0 { return nil, fmt.Errorf("group: lookup groupname %s: %s", groupname, syscall.Errno(rv)) } if result == nil { return nil, UnknownGroupError(groupname) } } else { rv = C.mygetgrgid_r(C.int(gid), &grp, (*C.char)(buf), C.size_t(bufSize), &result) if rv != 0 { return nil, fmt.Errorf("group: lookup groupid %d: %s", gid, syscall.Errno(rv)) } if result == nil { return nil, UnknownGroupIdError(gid) } } u := &Group{ Gid: strconv.Itoa(int(grp.gr_gid)), Groupname: C.GoString(grp.gr_name), } return u, nil }
func getGroupId(group string) (int, error) { var rv C.int var grp C.struct_group var grpres *C.struct_group var bufSize C.long var result int bufSize = 1024 buf := C.malloc(C.size_t(bufSize)) defer C.free(buf) rv = C.mygetgrnam_r(C.CString(group), &grp, (*C.char)(buf), C.size_t(bufSize), &grpres) if rv != 0 { return -1, errors.New("Could not read groupid") } if grpres != nil { result = int(C.int(grp.gr_gid)) } else { return -1, errors.New("Could not convert groupid") } return result, nil }
func lookupGroup(groupname string) (*Group, error) { var grp C.struct_group var result *C.struct_group buf := alloc(groupBuffer) defer buf.free() cname := C.CString(groupname) defer C.free(unsafe.Pointer(cname)) err := retryWithBuffer(buf, func() syscall.Errno { return syscall.Errno(C.mygetgrnam_r(cname, &grp, (*C.char)(buf.ptr), C.size_t(buf.size), &result)) }) if err != nil { return nil, fmt.Errorf("user: lookup groupname %s: %v", groupname, err) } if result == nil { return nil, UnknownGroupError(groupname) } return buildGroup(&grp), nil }