Exemplo n.º 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
}
Exemplo n.º 2
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
}
Exemplo n.º 3
0
// Load must be used to load the the colon separated list of database files
// passed in as filename, or "" for the default database file before any
// magic queries can performed.
func (m *Magic) Load(file string) error {
	if m.ptr == nil {
		return ConnectionError
	}

	cf := C.CString(file)
	defer C.free(unsafe.Pointer(cf))

	C.magic_load(m.ptr, cf)
	return m.check()
}
Exemplo n.º 4
0
// Load must be used to load database files before any magic queries can
// be performed.
//
// We expect either a colon separated list of database file paths, or an
// empty string ("") to use the default, system-installed database.
func (m *Magic) Load(file string) error {
	if m.ptr == nil {
		return ConnectionError
	}

	var cf *C.char

	if file != "" {
		cf = C.CString(file)
		defer C.free(unsafe.Pointer(cf))
	}

	C.magic_load(m.ptr, cf)
	return m.check()
}
Exemplo n.º 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
}
Exemplo n.º 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
}
Exemplo n.º 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
}