Esempio n. 1
0
// TypeByFile looks up for a file's mimetype by its content.
// It uses a magic number database which is described in magic(5).
func (d *Decoder) TypeByFile(filename string) (string, error) {
	path := C.CString(filename)
	defer C.free(unsafe.Pointer(path))
	out := C.magic_file(d.db, path)
	if out == nil {
		return "", errors.New(C.GoString(C.magic_error(d.db)))
	}
	return C.GoString(out), nil
}
Esempio n. 2
0
// TypeByFile looks up for a file's mimetype by its content.
// It uses a magic number database which is described in magic(5).
func (m *Magic) TypeByFile(filePath string) (string, error) {
	path := C.CString(filePath)
	defer C.free(unsafe.Pointer(path))
	out := C.magic_file(m.db, path)
	if out == nil {
		return "", errors.New(C.GoString(C.magic_error(m.db)))
	}
	return C.GoString(out), nil
}
Esempio n. 3
0
// File returns a textual description of the contents of the filename argument.
// If the filename is nil, then stdin is used.
func (m *Magic) File(file string) (string, error) {
	if m.ptr == nil {
		return "", ConnectionError
	}

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

	cr := C.magic_file(m.ptr, cf)
	if cr == nil {
		return "", m.check()
	}

	r := C.GoString(cr)
	C.free(unsafe.Pointer(cr))
	return r, nil
}
Esempio n. 4
0
// File returns a textual description of the contents of the filename argument.
// If the filename is nil, then stdin is used.
func (m *Magic) File(file string) (string, error) {
	if m.ptr == nil {
		return "", ConnectionError
	}

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

	cr := C.magic_file(m.ptr, cf)
	if cr == nil {
		return "", m.check()
	}

	// The cr pointer is tracked by the Magic Cookie
	// and does not need to be freed here. Doing so
	// causes an error when Close() is called because
	// a free attempt is made against an item that has
	// already been freed.
	r := C.GoString(cr)
	return r, nil
}
Esempio n. 5
0
func File(cookie Magic_t, filename string) string {
	cfilename := C.CString(filename)
	defer C.free(unsafe.Pointer(cfilename))
	return C.GoString(C.magic_file(cookie, cfilename))
}