ar

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

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

Go to latest
Published: Oct 15, 2014 License: MIT Imports: 11 Imported by: 0

README

ar

GoDoc

Package ar implements access to read and write ar archives.

Install

go get github.com/larzconwell/ar

License

MIT licensed, see here

Documentation

Overview

Package ar implements access to read and write ar archives.

Reading supports both GNU, BSD, and Go ar variants, and writing creates archives of the GNU variant.

References:

https://mebsd.com/man/ar/5
http://www.unix.com/man-page/all/3head/ar.h/

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrHeader       = errors.New("ar: invalid ar header")
	ErrStringsEntry = errors.New("ar: entry name not in strings table")
)
View Source
var (
	ErrWriteAfterClose = errors.New("ar: write after close")
	ErrWriteTooLong    = errors.New("ar: write too long")
	ErrHeaderTooLong   = errors.New("ar: header too long")
)

Functions

This section is empty.

Types

type Header struct {
	Name    string    // Name of file.
	ModTime time.Time // Modification time.
	Uid     int       // User id of owner.
	Gid     int       // Group id of owner.
	Mode    int64     // Permission and mode bits.
	Size    int64     // Length in bytes.
}

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

func FileInfoHeader

func FileInfoHeader(info os.FileInfo) *Header

FileInfoHeader creates a populated Header from info. 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 (header *Header) FileInfo() os.FileInfo

FileInfo returns a os.FileInfo for the Header.

type Reader

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

Reader provides sequential access to an ar archive. The Next method advances to the next file entry, which afterwards can be treated as an io.Reader.

Example
package main

import (
	"github.com/larzconwell/ar"
	"io"
	"os"
)

func main() {
	in, err := os.Open("libz.a")
	if err != nil {
		panic(err)
	}
	defer in.Close()
	arReader := ar.NewReader(in)

	for {
		header, err := arReader.Next()
		if err != nil {
			panic(err)
		}
		if header == nil {
			break
		}

		out, err := os.Create(header.Name)
		if err != nil {
			panic(err)
		}

		_, err = io.Copy(out, arReader)
		if err != nil {
			panic(err)
		}

		err = out.Close()
		if err != nil {
			panic(err)
		}
	}
}
Output:

func NewReader

func NewReader(r io.Reader) *Reader

NewReader creates a Reader reading from r.

func (*Reader) Next

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

Next advances to the next file entry. A nil, nil return indicates there are no entries left to read.

func (*Reader) Read

func (arr *Reader) Read(b []byte) (int, error)

Read reads from the current entry. It returns 0, io.EOF when the end is reached until Next is called.

type Writer

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

Writer provides sequential writing to an ar archive using the GNU format. WriteHeader triggers a new entry to be written, aftwards the writer can be used as an io.Writer.

Example
package main

import (
	"github.com/larzconwell/ar"
	"io"
	"os"
)

func main() {
	out, err := os.Create("libz.a")
	if err != nil {
		panic(err)
	}
	defer out.Close()
	arWriter := ar.NewWriter(out)

	object, err := os.Open("hasher.o")
	if err != nil {
		panic(err)
	}
	defer object.Close()

	stat, err := object.Stat()
	if err != nil {
		panic(err)
	}

	header := ar.FileInfoHeader(stat)
	err = arWriter.WriteHeader(header)
	if err != nil {
		panic(err)
	}

	_, err = io.Copy(arWriter, object)
	if err != nil {
		panic(err)
	}

	err = arWriter.Close()
	if err != nil {
		panic(err)
	}
}
Output:

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter creates a Writer writing to w.

func (*Writer) Close

func (arw *Writer) Close() error

Close closes the ar archive creating the symbol/string tables. All writing to the underlying writer is delayed until Close.

func (*Writer) Write

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

Write writes b to the current file entry. It returns ErrWriteTooLong if more bytes are being written than the header allows.

func (*Writer) WriteHeader

func (arw *Writer) WriteHeader(header *Header) error

WriteHeader creates a new file entry for header. Calling after it's closed will return ErrWriteAfterClose. ErrHeaderTooLong is returned if the header won't fit.

Jump to

Keyboard shortcuts

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