예제 #1
0
// Load
//
// If there is an error, it will be of type *Error.
func (mgc *Magic) Load(files ...string) (bool, error) {
	mgc.Lock()
	defer mgc.Unlock()
	runtime.KeepAlive(mgc.magic)

	if mgc.cookie == nil {
		return false, mgc.error()
	}

	var cfiles *C.char
	runtime.KeepAlive(cfiles)
	defer C.free(unsafe.Pointer(cfiles))

	// Assemble the list of custom Magic files into a colon-separated
	// list that is required by the underlying Magic library, otherwise
	// defer to the default list of paths provided by the Magic library.
	if len(files) > 0 {
		cfiles = C.CString(strings.Join(files, ":"))
	} else {
		cfiles = C.magic_getpath_wrapper()
	}

	if rv := C.magic_load_wrapper(mgc.cookie, cfiles, C.int(mgc.flags)); rv < 0 {
		return false, mgc.error()
	}
	mgc.path = strings.Split(C.GoString(cfiles), ":")
	return true, nil
}
예제 #2
0
// Path returns a slice containing fully-qualified path for each
// of Magic database files that was loaded and is currently in use.
// If there is an error, it will be of type *Error.
//
// Optionally, if the "MAGIC" environment variable is present,
// then each path from it will be taken into the account and the
// value that this function returns will be updated accordingly.
func (mgc *Magic) Path() ([]string, error) {
	mgc.Lock()
	defer mgc.Unlock()
	runtime.KeepAlive(mgc.magic)

	if mgc.cookie == nil {
		return []string{}, mgc.error()
	}

	// Respect the "MAGIC" environment variable, if present.
	if len(mgc.path) > 0 && os.Getenv("MAGIC") == "" {
		return mgc.path, nil
	}
	rv := C.GoString(C.magic_getpath_wrapper())
	mgc.path = strings.Split(rv, ":")
	return mgc.path, nil
}