// AddFile compiles rules from a file. Rules are added to the // specified namespace. func (c *Compiler) AddFile(file *os.File, namespace string) (err error) { fd := C.dup(C.int(file.Fd())) fh, err := C.fdopen(fd, C.CString("r")) if err != nil { return err } defer C.fclose(fh) var ns *C.char if namespace != "" { ns = C.CString(namespace) defer C.free(unsafe.Pointer(ns)) } filename := C.CString(file.Name()) defer C.free(unsafe.Pointer(filename)) id := callbackData.Put(c) defer callbackData.Delete(id) C.yr_compiler_set_callback(c.cptr, C.YR_COMPILER_CALLBACK_FUNC(C.compilerCallback), unsafe.Pointer(id)) numErrors := int(C.yr_compiler_add_file(c.cptr, fh, ns, filename)) if numErrors > 0 { var buf [1024]C.char msg := C.GoString(C.yr_compiler_get_error_message( c.cptr, (*C.char)(unsafe.Pointer(&buf[0])), 1024)) err = errors.New(msg) } return }
func (c *Compiler) AddFile(ns, path string) error { cpath := C.CString(path) cmode := C.CString("r") cns := C.CString(ns) defer C.free(unsafe.Pointer(cpath)) defer C.free(unsafe.Pointer(cmode)) defer C.free(unsafe.Pointer(cns)) fd := C.fopen(cpath, cmode) if fd == nil { return fmt.Errorf("libyara: failed to open %q", path) } defer C.fclose(fd) errors := C.yr_compiler_add_file(c.handle, fd, nil, cpath) if errors > 0 { return fmt.Errorf("libyara: failed to compile %q", path) } return nil }