magic

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2015 License: Apache-2.0 Imports: 11 Imported by: 0

README

Simple interface to libmagic for Go Programming Language

Build Status Go Documentation Coverage Status

Documentation

Overview

Package magic implements See: http://www.darwinsys.com/file/

Example (Basic)

This example show the basic usage of the package: Open and initialize Magic library, set appropriate flags, for a given file find its MIME identification (as per the flag set), print the results and close releasing all initialized resources.

// Open and load default Magic database ...
m, err := magic.New()
if err != nil {
	panic(fmt.Sprintf("An error occurred: %s\n", err))
}

m.SetFlags(magic.MIME)
mime, err := m.File("fixtures/gopher.png")
if err != nil {
	panic(fmt.Sprintf("Unable to determine file MIME: %s\n", err))
}
fmt.Printf("File MIME is: %s\n", mime)

m.Close()
Output:

File MIME is: image/png; charset=binary
Example (Closure)

This example shows how to use Open together with a closure.

var s string

// When magic.Open you don't have to worry about
// closing the underlying Magic database.
err := magic.Open(func(m *magic.Magic) error {
	m.SetFlags(magic.MIME)
	mime, err := m.File("fixtures/gopher.png")
	if err != nil {
		return err // Propagate error outside of the closure.
	}
	s = mime // Pass results outside ...
	return nil
})

if err != nil {
	panic(fmt.Sprintf("An error occurred: %s\n", err))
}

fmt.Printf("File MIME type is: %s\n", s)
Output:

File MIME type is: image/png; charset=binary
Example (Separator)

This example shows how to use the Separator to split results when the CONTINUE flag is set and more than one match was returned by the Magic library.

buffer := []byte("#!/bin/bash\n\n")

// Open and load default Magic database ...
m, err := magic.New()
if err != nil {
	panic(fmt.Sprintf("An error occurred: %s\n", err))
}

m.SetFlags(magic.CONTINUE)
result, err := m.Buffer(buffer)
if err != nil {
	panic(fmt.Sprintf("Unable to determine buffer data type: %s\n", err))
}

fmt.Println("Matches for data in the buffer are:")
for _, s := range strings.Split(result, magic.Separator) {
	fmt.Printf("\t%s\n", s)
}

m.Close()
Output:

Matches for data in the buffer are:
	Bourne-Again shell script text executable
	a /bin/bash script, ASCII text executable

Index

Examples

Constants

View Source
const (
	// No special handling and/or flags specified. Default behaviour.
	NONE int = C.MAGIC_NONE

	// Print debugging messages to standard error output.
	DEBUG int = C.MAGIC_DEBUG

	// If the file queried is a symbolic link, follow it.
	SYMLINK int = C.MAGIC_SYMLINK

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

	// If the file is a block or character special device, then open
	// the device and try to look at the contents.
	DEVICES int = C.MAGIC_DEVICES

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

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

	// Check the Magic database for consistency and print warnings to
	// standard error output.
	CHECK int = C.MAGIC_CHECK

	// Attempt to preserve access time (atime, utime or utimes) of the
	// file queried on systems that support such system calls.
	PRESERVE_ATIME int = C.MAGIC_PRESERVE_ATIME

	// Do not translate unprintable characters to an octal representation.
	RAW int = C.MAGIC_RAW

	// Treat operating system errors while trying to open files and follow
	// symbolic links as first class errors, instead of storing them in the
	// Magic library error buffer for retrieval later.
	ERROR int = C.MAGIC_ERROR

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

	// A shorthand for using MIME_TYPE and MIME_ENCODING flags together.
	MIME int = C.MAGIC_MIME

	// Return the Apple creator and type.
	APPLE int = C.MAGIC_APPLE

	// Do not look for, or inside compressed files.
	NO_CHECK_COMPRESS int = C.MAGIC_NO_CHECK_COMPRESS

	// Do not look for, or inside tar archive files.
	NO_CHECK_TAR int = C.MAGIC_NO_CHECK_TAR

	// Do not consult Magic files.
	NO_CHECK_SOFT int = C.MAGIC_NO_CHECK_SOFT

	// Check for EMX application type (only supported on EMX).
	NO_CHECK_APPTYPE int = C.MAGIC_NO_CHECK_APPTYPE

	// Do not check for ELF files (do not examine ELF file details).
	NO_CHECK_ELF int = C.MAGIC_NO_CHECK_ELF

	// Do not check for various types of text files.
	NO_CHECK_TEXT int = C.MAGIC_NO_CHECK_TEXT

	// Do not check for CDF files.
	NO_CHECK_CDF int = C.MAGIC_NO_CHECK_CDF

	// Do not look for known tokens inside ASCII files.
	NO_CHECK_TOKENS int = C.MAGIC_NO_CHECK_TOKENS

	// Return a MIME encoding, instead of a textual description.
	NO_CHECK_ENCODING int = C.MAGIC_NO_CHECK_ENCODING

	// Do not use built-in tests; only consult the Magic files.
	NO_CHECK_BUILTIN int = C.MAGIC_NO_CHECK_BUILTIN

	// Do not check for various types of text files, same as NO_CHECK_TEXT.
	NO_CHECK_ASCII int = C.MAGIC_NO_CHECK_TEXT

	// Do not look for Fortran sequences inside ASCII files.
	NO_CHECK_FORTRAN int = C.MAGIC_NO_CHECK_FORTRAN

	// Do not look for troff sequences inside ASCII files.
	NO_CHECK_TROFF int = C.MAGIC_NO_CHECK_TROFF
)
View Source
const Separator string = "\x5c\x30\x31\x32\x2d\x20"

Separator is a field separator that can be used to split results when the CONTINUE flag is set causing all valid matches found by the Magic library to be returned.

Variables

This section is empty.

Functions

func BufferEncoding

func BufferEncoding(buffer []byte, files ...string) (string, error)

BufferEncoding returns MIME encoding only, rather than a textual description, for the content of the buffer. If there is an error, it will be of type *MagicError.

Example

This example show how to identify encoding type of a buffer.

buffer := []byte("Hello, 世界")

mime, err := magic.BufferEncoding(buffer)
if err != nil {
	panic(fmt.Sprintf("An error occurred: %s\n", err))
}
fmt.Printf("Data in the buffer is encoded as: %s\n", mime)
Output:

Data in the buffer is encoded as: utf-8

func BufferMime

func BufferMime(buffer []byte, files ...string) (string, error)

BufferMime returns MIME identification (both the MIME type and MIME encoding), rather than a textual description, for the content of the buffer. If there is an error, it will be of type *MagicError.

func BufferType

func BufferType(buffer []byte, files ...string) (string, error)

BufferType returns MIME type only, rather than a textual description, for the content of the buffer. If there is an error, it will be of type *MagicError.

func Check

func Check(files ...string) (bool, error)

Check

If there is an error, it will be of type *MagicError.

func Compile

func Compile(files ...string) (bool, error)

Compile

If there is an error, it will be of type *MagicError.

func FileEncoding

func FileEncoding(name string, files ...string) (string, error)

FileEncoding returns MIME encoding only, rather than a textual description, for the content of the buffer. If there is an error, it will be of type *MagicError.

func FileMime

func FileMime(name string, files ...string) (string, error)

FileMime returns MIME identification (both the MIME type and MIME encoding), rather than a textual description, for the named file. If there is an error, it will be of type *MagicError.

func FileType

func FileType(name string, files ...string) (string, error)

FileType returns MIME type only, rather than a textual description, for the named file. If there is an error, it will be of type *MagicError.

Example

This example shows how to quickly find MIME type for a file.

mime, err := magic.FileType("fixtures/gopher.png")
if err != nil {
	panic(fmt.Sprintf("An error occurred: %s\n", err))
}
fmt.Printf("File MIME type is: %s\n", mime)
Output:

File MIME type is: image/png

func Open

func Open(f func(magic *Magic) error, files ...string) (err error)

Open

If there is an error, it will be of type *MagicError.

func Version

func Version() (int, error)

Version returns the underlying Magic library version as in integer value in the format "XYY", where X is the major version and Y is the minor version number. If there is an error, it will be of type *MagicError.

func VersionSlice

func VersionSlice() ([]int, error)

VersionSlice returns a slice containing values of both the major and minor version numbers separated from one another. If there is an error, it will be of type *MagicError.

func VersionString

func VersionString() (string, error)

VersionString returns the underlying Magic library version as string in the format "X.YY". If there is an error, it will be of type *MagicError.

Types

type Magic

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

Magic represents the underlying Magic library.

func New

func New(files ...string) (*Magic, error)

New opens and initializes Magic library.

Optionally, a multiple distinct Magic database files can be provided to load, otherwise a default database (usually available system-wide) will be loaded. Alternatively, the "MAGIC" environment variable can be used to name any desired Magic database files to be loaded, but it must be set prior to calling this function for it to take effect.

Remember to call Close to release initialized resources and close currently opened Magic library, or use Open which will ensure that Close is called once the closure finishes.

If there is an error originating from the underlying Magic library, it will be of type *MagicError.

func (*Magic) Buffer

func (mgc *Magic) Buffer(buffer []byte) (string, error)

Buffer

If there is an error, it will be of type *MagicError.

func (*Magic) Check

func (mgc *Magic) Check(files ...string) (bool, error)

Check

If there is an error, it will be of type *MagicError.

func (*Magic) Close

func (mgc *Magic) Close()

Close releases all initialized resources and closes currently open Magic library.

func (*Magic) Compile

func (mgc *Magic) Compile(files ...string) (bool, error)

Compile

If there is an error, it will be of type *MagicError.

func (*Magic) Descriptor

func (mgc *Magic) Descriptor(fd uintptr) (string, error)

Descriptor

If there is an error, it will be of type *MagicError.

func (*Magic) File

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

File

If there is an error, it will be of type *MagicError.

func (*Magic) Flags

func (mgc *Magic) Flags() (int, error)

Flags returns a value (bitmask) representing current flags set. If there is an error, it will be of type *MagicError.

func (*Magic) FlagsSlice

func (mgc *Magic) FlagsSlice() ([]int, error)

FlagsSlice returns a slice containing each distinct flag that is currently set and included as a part of the current value (bitmask) of flags. Results are sorted in an ascending order. If there is an error, it will be of type *MagicError.

func (*Magic) Load

func (mgc *Magic) Load(files ...string) (bool, error)

Load

If there is an error, it will be of type *MagicError.

func (*Magic) Path

func (mgc *Magic) Path() ([]string, error)

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 *MagicError.

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 (*Magic) SetFlags

func (mgc *Magic) SetFlags(flags int) error

SetFlags sets the flags to the new value (bitmask). Depending on which flags are current set the results and/or behavior of the Magic library will change accordingly. If there is an error, it will be of type *MagicError.

func (*Magic) String

func (mgc *Magic) String() string

String returns a string representation of the Magic type.

type MagicError

type MagicError struct {
	Errno   int    // The value of errno, if any.
	Message string // The actual error message.
}

MagicError represents an error originating from the underlying Magic library.

func (*MagicError) Error

func (me *MagicError) Error() string

Error returns a descriptive error message.

Jump to

Keyboard shortcuts

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