func magicOpen() (C.magic_t, error) { magic, err := C.magic_open(C.MAGIC_MIME_TYPE) if err != nil { return nil, err } if magic == nil { err = magicError(magic) if err == nil { panic("unreachable") } return nil, err } // magic_load set ENOENT, even if it succeed, // so ignore second value. code, _ := C.magic_load(magic, nil) if int(code) != 0 { err = magicError(magic) if err == nil { panic("unreachable") } return nil, err } return magic, nil }
// Open attempts to create a new connection to the magic database. func Open(flags int) (*Magic, error) { ptr := C.magic_open(C.int(flags)) if ptr == nil { return nil, ConnectionFailure } return &Magic{ptr}, nil }
func New() (*Magic, error) { db := C.magic_open(C.int(0)) C.magic_setflags(db, C.int(C.MAGIC_SYMLINK|C.MAGIC_ERROR)) if code := C.magic_load(db, nil); code != 0 { return nil, errors.New(C.GoString(C.magic_error(db))) } return &Magic{db}, nil }
// open opens and initializes underlying Magic library and sets the // finalizer on the object accordingly. func open() (*Magic, error) { // Can only fail allocating memory in this particular case. rv := C.magic_open(C.int(NONE)) if rv == nil { errno := syscall.ENOMEM return nil, &Error{int(errno), "failed to initialize Magic library"} } mgc := &Magic{&magic{flags: NONE, cookie: rv}} runtime.SetFinalizer(mgc.magic, (*magic).close) runtime.KeepAlive(mgc.magic) return mgc, nil }
func New(flags MagicFlag) (*Magic, error) { db := C.magic_open(C.int(0)) if db == nil { return nil, errors.New("Error allocating magic cookie") } if code := C.magic_setflags(db, C.int(flags)); code != 0 { return nil, errors.New(C.GoString(C.magic_error(db))) } if code := C.magic_load(db, nil); code != 0 { return nil, errors.New(C.GoString(C.magic_error(db))) } return &Magic{db}, nil }
func Open(flags MagicFlag) error { db = C.magic_open(C.int(0)) if db == nil { return errors.New("error opening magic") } if code := C.magic_setflags(db, C.int(flags)); code != 0 { return errors.New(C.GoString(C.magic_error(db))) } if code := C.magic_load(db, nil); code != 0 { return errors.New(C.GoString(C.magic_error(db))) } return nil }
// NewDecoder creates a detector that uses libmagic. It initializes // the opens the magicmime database with the specified flags. Upon // success users are expected to call Close on the returned Decoder // when it is no longer needed. func NewDecoder(flags Flag) (*Decoder, error) { db := C.magic_open(C.int(0)) if db == nil { return nil, errors.New("error opening magic") } d := &Decoder{db: db} if code := C.magic_setflags(db, C.int(flags)); code != 0 { d.Close() return nil, errors.New(C.GoString(C.magic_error(d.db))) } if code := C.magic_load(db, nil); code != 0 { d.Close() return nil, errors.New(C.GoString(C.magic_error(d.db))) } return d, nil }
func Open(flags int) Magic_t { return (Magic_t)(C.magic_open(C.int(flags))) }