ar

package
v0.0.0-...-11d91c8 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2014 License: BSD-3-Clause Imports: 10 Imported by: 24

Documentation

Overview

Package ar implements access to ar archives. argo only implements the 'common' format as used for .deb files, by GNU ar, and by BSD ar. AIX and Coherent variants are not supported. Note that argo is not currently supporting the workarounds for long filenames as defined by GNU ar or BSD ar. Please get in touch if you require this feature.

References:

http://en.wikipedia.org/wiki/Ar_(Unix)
http://linux.die.net/man/1/ar
http://www.freebsd.org/cgi/man.cgi?query=ar&sektion=5&apropos=0&manpath=FreeBSD+9.3-RELEASE
Example
package main

import (
	"bytes"
	"fmt"
	"github.com/laher/argo/ar"
	"io"
	"log"
	"os"
)

func main() {
	// Create a buffer to write our archive to.
	wtr := new(bytes.Buffer)

	// Create a new ar archive.
	aw := ar.NewWriter(wtr)

	// Add some files to the archive.
	var files = []struct {
		Name, Body string
	}{
		{"readme.txt", "This archive contains some text files."},
		{"gopher.txt", "Gopher names:\nGeorge\nGeoffrey\nGonzo"},
		{"todo.txt", "Get animal handling licence."},
	}
	for _, file := range files {
		hdr := &ar.Header{
			Name: file.Name,
			Size: int64(len(file.Body)),
		}
		if err := aw.WriteHeader(hdr); err != nil {
			log.Fatalln(err)
		}
		if _, err := aw.Write([]byte(file.Body)); err != nil {
			log.Fatalln(err)
		}
	}
	// Make sure to check the error on Close.
	if err := aw.Close(); err != nil {
		log.Fatalln(err)
	}

	// Open the ar archive for reading.
	rdr := bytes.NewReader(wtr.Bytes())

	arr, err := ar.NewReader(rdr)
	if err != nil {
		log.Fatalln(err)
	}

	// Iterate through the files in the archive.
	for {
		hdr, err := arr.Next()
		if err == io.EOF {
			// end of ar archive
			break
		}
		if err != nil {
			log.Fatalln(err)
		}
		fmt.Printf("Contents of %s:\n", hdr.Name)
		if _, err := io.Copy(os.Stdout, arr); err != nil {
			log.Fatalln(err)
		}
		fmt.Println()
	}

}
Output:

Contents of readme.txt:
This archive contains some text files.
Contents of gopher.txt:
Gopher names:
George
Geoffrey
Gonzo
Contents of todo.txt:
Get animal handling licence.

Index

Examples

Constants

This section is empty.

Variables

View Source
var (

	// ErrHeader describes an invalid ar file header
	ErrHeader = errors.New("ar: invalid ar header")
	// ArFileHeader is the string used to identify an .ar file
	ArFileHeader = "!<arch>\n"
)
View Source
var (
	//ErrWriteAfterClose shows that a write was attempted after the archive has been closed (and the footer is written)
	ErrWriteAfterClose = errors.New("ar: write after close")
)

Functions

This section is empty.

Types

type Header struct {
	// Name is the name of the file.
	// It must be a relative path: it must not start with a drive
	// letter (e.g. C:) or leading slash, and only forward slashes
	// are allowed.
	Name    string    // name of header file entry
	ModTime time.Time // modified time
	Uid     int       // user id of owner
	Gid     int       // group id of owner
	Mode    int64     // permission and mode bits
	Size    int64     // length in bytes
}

A Header represents a single header in an ar archive. Some fields may not be populated.

func FileInfoHeader

func FileInfoHeader(fi os.FileInfo) (*Header, error)

FileInfoHeader creates a partially-populated Header from an os.FileInfo. Because os.FileInfo's Name method returns only the base name of the file it describes, it may be necessary to modify the Name field of the returned header to provide the full path name of the file.

func (*Header) FileInfo

func (h *Header) FileInfo() os.FileInfo

FileInfo returns an os.FileInfo for the Header.

type Reader

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

A Reader provides sequential access to the contents of an ar archive. An ar archive consists of a sequence of files. The Next method advances to the next file in the archive (including the first), and then it can be treated as an io.Reader to access the file's data.

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader creates a new Reader reading from r. NewReader automatically reads in the ar file header, and checks it is valid.

func (*Reader) Next

func (ar *Reader) Next() (*Header, error)

Next advances to the next entry in the ar archive.

func (*Reader) NextString

func (ar *Reader) NextString(max int) (string, error)

NextString reads a string up to a given max length. This is useful for reading the first part of .a files.

func (*Reader) Read

func (ar *Reader) Read(b []byte) (n int, err error)

Read reads from the current entry in the ar archive. It returns 0, io.EOF when it reaches the end of that entry, until Next is called to advance to the next entry.

type Writer

type Writer struct {
	TerminateFilenamesSlash bool // This flag determines whether to terminate filenames with a slash '/' or not. GNU ar uses slashes, whereas .deb files tend not to use them.
	// contains filtered or unexported fields
}

A Writer provides sequential writing of an ar archive. An ar archive consists of a sequence of files. Call WriteHeader to begin a new file, and then call Write to supply that file's data, writing at most hdr.Size bytes in total.

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a new Writer writing to w.

func (*Writer) Close

func (aw *Writer) Close() error

Close closes the ar archive, flushing any unwritten data to the underlying writer.

func (*Writer) Flush

func (aw *Writer) Flush() error

Flush finishes writing the current file (optional. This is called by writeHeader anyway.)

func (*Writer) Write

func (aw *Writer) Write(b []byte) (int, error)

Write some data to the ar file.

func (*Writer) WriteHeader

func (aw *Writer) WriteHeader(hdr *Header) error

WriteHeader writes hdr and prepares to accept the file's contents. WriteHeader calls Flush if it is not the first header. Calling after a Close will return ErrWriteAfterClose.

Jump to

Keyboard shortcuts

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