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 ¶
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" )
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 ¶
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 ¶
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.
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 ¶
NewReader creates a new Reader reading from r. NewReader automatically reads in the ar file header, and checks it is valid.
func (*Reader) NextString ¶
NextString reads a string up to a given max length. This is useful for reading the first part of .a files.
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 (*Writer) Close ¶
Close closes the ar archive, flushing any unwritten data to the underlying writer.
func (*Writer) Flush ¶
Flush finishes writing the current file (optional. This is called by writeHeader anyway.)
func (*Writer) WriteHeader ¶
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.