Help us shape the future of Go! Take the 2026 Go Developer Survey

magic

package module
v0.0.0-...-cbc00aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 18, 2016 License: BSD-3-Clause Imports: 3 Imported by: 3

README

magic

golang libmagic bindings

Installation

sudo apt-get install libmagic-dev

go get github.com/hosom/gomagic

Usage

Bindings are 1:1 mappings of the C library, so if you're familiar with libmagic's C interface, then you should be set. This feels a little bit unnattural in Go, but it keeps the implementation simple and easy to support in the long run.

Initialize a libmagic instance
m, err := magic.Open(magic.MAGIC_NONE)
if err != nil {
  log.Fatal(err)
}
Check a file
result, err := m.File("/path/to/file")
if err != nil {
  log.Fatal(err)
}
fmt.Println(result)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ConnectionFailure = errors.New("libmagic: Failed to open magic database.")
	ConnectionError   = errors.New("libmagic: Connection already closed.")
	ATimeError        = errors.New("libmagic: MAGIC_PRESERVE_ATIME unsupported")
)

Functions

This section is empty.

Types

type Flag

type Flag int
const (
	// No special handling
	MAGIC_NONE Flag = C.MAGIC_NONE

	// Prints debugging messages to stderr
	MAGIC_DEBUG Flag = C.MAGIC_DEBUG

	// If the file queried is a symlink, follow it.
	MAGIC_SYMLINK Flag = C.MAGIC_SYMLINK

	// If the file is compressed, unpack it and look at the contents.
	MAGIC_COMPRESS Flag = C.MAGIC_COMPRESS

	// If the file is a block or character special device, then open the device
	// and try to look in its contents.
	MAGIC_DEVICES Flag = C.MAGIC_DEVICES

	// Return a MIME type string, instead of a textual description.
	MAGIC_MIME_TYPE Flag = C.MAGIC_MIME_TYPE

	// Return a MIME encoding, instead of a textual description.
	MAGIC_MIME_ENCODING Flag = C.MAGIC_MIME_ENCODING

	// A shorthand for MAGIC_MIME_TYPE | MAGIC_MIME_ENCODING.
	MAGIC_MIME Flag = C.MAGIC_MIME

	// Return all matches, not just the first.
	MAGIC_CONTINUE Flag = C.MAGIC_CONTINUE

	// Check the magic database for consistency and print warnings to stderr.
	MAGIC_CHECK Flag = C.MAGIC_CHECK

	// On systems that support utime(2) or utimes(2), attempt to preserve the
	// access time of files analyzed.
	MAGIC_PRESERVE_ATIME Flag = C.MAGIC_PRESERVE_ATIME

	// Don't translate unprintable characters to a \ooo octal representation.
	MAGIC_RAW Flag = C.MAGIC_RAW

	// Treat operating system errors while trying to open files and follow
	// symlinks as real errors, instead of printing them in the magic buffer
	MAGIC_ERROR Flag = C.MAGIC_ERROR

	// Return the Apple creator and type.
	MAGIC_APPLE Flag = C.MAGIC_APPLE

	// Don't check for EMX application type (only on EMX).
	MAGIC_NO_CHECK_APPTYPE Flag = C.MAGIC_NO_CHECK_APPTYPE

	// Don't get extra information on MS Composite Document Files.
	MAGIC_NO_CHECK_CDF Flag = C.MAGIC_NO_CHECK_CDF

	// Don't look inside compressed files.
	MAGIC_NO_CHECK_COMPRESS Flag = C.MAGIC_NO_CHECK_COMPRESS

	// Don't print ELF details.
	MAGIC_NO_CHECK_ELF Flag = C.MAGIC_NO_CHECK_ELF

	// Don't check text encodings.
	MAGIC_NO_CHECK_ENCODING Flag = C.MAGIC_NO_CHECK_ENCODING

	// Don't consult magic files.
	MAGIC_NO_CHECK_SOFT Flag = C.MAGIC_NO_CHECK_SOFT

	// Don't examine tar files.
	MAGIC_NO_CHECK_TAR Flag = C.MAGIC_NO_CHECK_TAR

	// Don't check for various types of text files.
	MAGIC_NO_CHECK_TEXT Flag = C.MAGIC_NO_CHECK_TEXT

	// Don't look for known tokens inside ascii files.
	MAGIC_NO_CHECK_TOKENS Flag = C.MAGIC_NO_CHECK_TOKENS
)

type Magic

type Magic struct {
	// contains filtered or unexported fields
}

func Open

func Open(flags Flag) (*Magic, error)

Open a default instance of libmagic

func (*Magic) Buffer

func (m *Magic) Buffer(binary []byte) (string, error)

Use a byte array to identify a file

func (*Magic) Close

func (m *Magic) Close() (err error)

Close and cleanup libmagic instance

func (*Magic) File

func (m *Magic) File(filename string) (string, error)

Use a libmagic instance to identify a file on the disk

func (*Magic) Load

func (m *Magic) Load(filename string) error

Choose libmagic file to use, to use the system default pass an empty string of ""

func (*Magic) SetFlags

func (m *Magic) SetFlags(flags Flag) error

Set flags for a libmagic instance

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL