예제 #1
0
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
}
예제 #2
0
파일: magic.go 프로젝트: golang-lib/magic
// 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
}
예제 #3
0
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
}
예제 #4
0
// 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
}
예제 #5
0
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
}
예제 #6
0
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
}
예제 #7
0
// 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
}
예제 #8
0
파일: magic.go 프로젝트: saschpe/gomagic
func Open(flags int) Magic_t {
	return (Magic_t)(C.magic_open(C.int(flags)))
}