pax_global_header00006660000000000000000000000064125000541400014501gustar00rootroot0000000000000052 comment=8bd4349a67f2533b078dbc524689d15dba0f4659 golang-ar-0.0~git20150512/000077500000000000000000000000001250005414000147315ustar00rootroot00000000000000golang-ar-0.0~git20150512/.gitignore000066400000000000000000000000051250005414000167140ustar00rootroot00000000000000*.*~ golang-ar-0.0~git20150512/COPYING000066400000000000000000000020701250005414000157630ustar00rootroot00000000000000Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. golang-ar-0.0~git20150512/README.md000066400000000000000000000005471250005414000162160ustar00rootroot00000000000000# Golang ar (archive) file reader This is a simple library for reading and writing [ar](http://en.wikipedia.org/wiki/Ar_(Unix)) files in common format. It is influenced heavily in style and interface from the golang [tar](http://golang.org/pkg/archive/tar/) package. ## Author Written by Blake Smith Licensed under the MIT license.golang-ar-0.0~git20150512/common.go000066400000000000000000000025521250005414000165540ustar00rootroot00000000000000/* Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ar import ( "time" ) const ( HEADER_BYTE_SIZE = 60 GLOBAL_HEADER = "!\n" ) type Header struct { Name string ModTime time.Time Uid int Gid int Mode int64 Size int64 } type slicer []byte func (sp *slicer) next(n int) (b []byte) { s := *sp b, *sp = s[0:n], s[n:] return } golang-ar-0.0~git20150512/fixtures/000077500000000000000000000000001250005414000166025ustar00rootroot00000000000000golang-ar-0.0~git20150512/fixtures/hello.a000066400000000000000000000001221250005414000200420ustar00rootroot00000000000000! hello.txt 1361157466 501 20 100644 13 ` Hello world! golang-ar-0.0~git20150512/fixtures/hello.txt000066400000000000000000000000151250005414000204420ustar00rootroot00000000000000Hello world! golang-ar-0.0~git20150512/fixtures/lamp.txt000066400000000000000000000000151250005414000202700ustar00rootroot00000000000000I love lamp. golang-ar-0.0~git20150512/fixtures/multi_archive.a000066400000000000000000000002341250005414000215760ustar00rootroot00000000000000! hello.txt 1361157466 501 20 100644 13 ` Hello world! lamp.txt 1361248906 501 20 100644 13 ` I love lamp. golang-ar-0.0~git20150512/reader.go000066400000000000000000000066421250005414000165320ustar00rootroot00000000000000/* Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ar import ( "io" "io/ioutil" "os" "strconv" "time" ) // Provides read access to an ar archive. // Call next to skip files // // Example: // reader := NewReader(f) // var buf bytes.Buffer // for { // _, err := reader.Next() // if err == io.EOF { // break // } // if err != nil { // t.Errorf(err.Error()) // } // io.Copy(&buf, reader) // } type Reader struct { r io.Reader nb int64 pad int64 } // Copies read data to r. Strips the global ar header. func NewReader(r io.Reader) *Reader { io.CopyN(ioutil.Discard, r, 8) // Discard global header return &Reader{r: r} } func (rd *Reader) string(b []byte) string { i := len(b)-1 for i > 0 && b[i] == 32 { i-- } return string(b[0:i+1]) } func (rd *Reader) numeric(b []byte) int64 { i := len(b)-1 for i > 0 && b[i] == 32 { i-- } n, _ := strconv.ParseInt(string(b[0:i+1]), 10, 64) return n } func (rd *Reader) octal(b []byte) int64 { i := len(b)-1 for i > 0 && b[i] == 32 { i-- } n, _ := strconv.ParseInt(string(b[3:i+1]), 8, 64) return n } func (rd *Reader) skipUnread() error { skip := rd.nb + rd.pad rd.nb, rd.pad = 0, 0 if seeker, ok := rd.r.(io.Seeker); ok { _, err := seeker.Seek(skip, os.SEEK_CUR) return err } _, err := io.CopyN(ioutil.Discard, rd.r, skip) return err } func (rd *Reader) readHeader() (*Header, error) { headerBuf := make([]byte, HEADER_BYTE_SIZE) if _, err := io.ReadFull(rd.r, headerBuf); err != nil { return nil, err } header := new(Header) s := slicer(headerBuf) header.Name = rd.string(s.next(16)) header.ModTime = time.Unix(rd.numeric(s.next(12)), 0) header.Uid = int(rd.numeric(s.next(6))) header.Gid = int(rd.numeric(s.next(6))) header.Mode = rd.octal(s.next(8)) header.Size = rd.numeric(s.next(10)) rd.nb = int64(header.Size) if header.Size%2 == 1 { rd.pad = 1 } else { rd.pad = 0 } return header, nil } // Call Next() to skip to the next file in the archive file. // Returns a Header which contains the metadata about the // file in the archive. func (rd *Reader) Next() (*Header, error) { err := rd.skipUnread() if err != nil { return nil, err } return rd.readHeader() } // Read data from the current entry in the archive. func (rd *Reader) Read(b []byte) (n int, err error) { if rd.nb == 0 { return 0, io.EOF } if int64(len(b)) > rd.nb { b = b[0:rd.nb] } n, err = rd.r.Read(b) rd.nb -= int64(n) return } golang-ar-0.0~git20150512/reader_test.go000066400000000000000000000057701250005414000175720ustar00rootroot00000000000000/* Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ar import ( "bytes" "io" "os" "testing" "time" ) func TestReadHeader(t *testing.T) { f, err := os.Open("./fixtures/hello.a") defer f.Close() if err != nil { t.Errorf(err.Error()) } reader := NewReader(f) header, err := reader.Next() if err != nil { t.Errorf(err.Error()) } expectedName := "hello.txt" if header.Name != expectedName { t.Errorf("Header name should be %s but is %s", expectedName, header.Name) } expectedModTime := time.Unix(1361157466, 0) if header.ModTime != expectedModTime { t.Errorf("ModTime should be %s but is %s", expectedModTime, header.ModTime) } expectedUid := 501 if header.Uid != expectedUid { t.Errorf("Uid should be %s but is %s", expectedUid, header.Uid) } expectedGid := 20 if header.Gid != expectedGid { t.Errorf("Gid should be %s but is %s", expectedGid, header.Gid) } expectedMode := int64(0644) if header.Mode != expectedMode { t.Errorf("Mode should be %s but is %s", expectedMode, header.Mode) } } func TestReadBody(t *testing.T) { f, err := os.Open("./fixtures/hello.a") defer f.Close() if err != nil { t.Errorf(err.Error()) } reader := NewReader(f) _, err = reader.Next() if err != nil && err != io.EOF { t.Errorf(err.Error()) } var buf bytes.Buffer io.Copy(&buf, reader) expected := []byte("Hello world!\n") actual := buf.Bytes() if !bytes.Equal(actual, expected) { t.Errorf("Data value should be %s but is %s", expected, actual) } } func TestReadMulti(t *testing.T) { f, err := os.Open("./fixtures/multi_archive.a") defer f.Close() if err != nil { t.Errorf(err.Error()) } reader := NewReader(f) var buf bytes.Buffer for { _, err := reader.Next() if err == io.EOF { break } if err != nil { t.Errorf(err.Error()) } io.Copy(&buf, reader) } expected := []byte("Hello world!\nI love lamp.\n") actual := buf.Bytes() if !bytes.Equal(expected, actual) { t.Errorf("Concatted byte buffer should be %s but is %s", expected, actual) } } golang-ar-0.0~git20150512/writer.go000066400000000000000000000064651250005414000166070ustar00rootroot00000000000000/* Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ar import ( "errors" "io" "strconv" ) var ( ErrWriteTooLong = errors.New("ar: write too long") ) // Writer provides sequential writing of an ar archive. // An ar archive is sequence of header file pairs // Call WriteHeader to begin writing a new file, then call Write to supply the file's data // // Example: // archive := ar.NewWriter(writer) // archive.WriteGlobalHeader() // header := new(ar.Header) // header.Size = 15 // bytes // if err := archive.WriteHeader(header); err != nil { // return err // } // io.Copy(archive, data) type Writer struct { w io.Writer nb int64 // number of unwritten bytes for the current file entry } // Create a new ar writer that writes to w func NewWriter(w io.Writer) *Writer { return &Writer{w: w} } func (aw *Writer) numeric(b []byte, x int64) { s := strconv.FormatInt(x, 10) for len(s) < len(b) { s = s + " " } copy(b, []byte(s)) } func (aw *Writer) octal(b []byte, x int64) { s := "100" + strconv.FormatInt(x, 8) for len(s) < len(b) { s = s + " " } copy(b, []byte(s)) } func (aw *Writer) string(b []byte, str string) { s := str for len(s) < len(b) { s = s + " " } copy(b, []byte(s)) } // Writes to the current entry in the ar archive // Returns ErrWriteTooLong if more than header.Size // bytes are written after a call to WriteHeader func (aw *Writer) Write(b []byte) (n int, err error) { if int64(len(b)) > aw.nb { b = b[0:aw.nb] err = ErrWriteTooLong } n, werr := aw.w.Write(b) aw.nb -= int64(n) if werr != nil { return n, werr } if len(b)%2 == 1 { // data size must be aligned to an even byte n2, _ := aw.w.Write([]byte{'\n'}) return n+n2, err } return } func (aw *Writer) WriteGlobalHeader() error { _, err := aw.w.Write([]byte(GLOBAL_HEADER)) return err } // Writes the header to the underlying writer and prepares // to receive the file payload func (aw *Writer) WriteHeader(hdr *Header) error { aw.nb = int64(hdr.Size) header := make([]byte, HEADER_BYTE_SIZE) s := slicer(header) aw.string(s.next(16), hdr.Name) aw.numeric(s.next(12), hdr.ModTime.Unix()) aw.numeric(s.next(6), int64(hdr.Uid)) aw.numeric(s.next(6), int64(hdr.Gid)) aw.octal(s.next(8), hdr.Mode) aw.numeric(s.next(10), hdr.Size) aw.string(s.next(2), "`\n") _, err := aw.w.Write(header) return err } golang-ar-0.0~git20150512/writer_test.go000066400000000000000000000046631250005414000176440ustar00rootroot00000000000000/* Copyright (c) 2013 Blake Smith Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ar import ( "bytes" "io/ioutil" "os" "testing" "time" ) func TestGlobalHeaderWrite(t *testing.T) { var buf bytes.Buffer writer := NewWriter(&buf) if err := writer.WriteGlobalHeader(); err != nil { t.Errorf(err.Error()) } globalHeader := buf.Bytes() expectedHeader := []byte("!\n") if !bytes.Equal(globalHeader, expectedHeader) { t.Errorf("Global header should be %s but is %s", expectedHeader, globalHeader) } } func TestSimpleFile(t *testing.T) { hdr := new(Header) body := "Hello world!\n" hdr.ModTime = time.Unix(1361157466, 0) hdr.Name = "hello.txt" hdr.Size = int64(len(body)) hdr.Mode = 0644 hdr.Uid = 501 hdr.Gid = 20 var buf bytes.Buffer writer := NewWriter(&buf) writer.WriteGlobalHeader() writer.WriteHeader(hdr) _, err := writer.Write([]byte(body)) if err != nil { t.Errorf(err.Error()) } f, _ := os.Open("./fixtures/hello.a") defer f.Close() b, err := ioutil.ReadAll(f) if err != nil { t.Errorf(err.Error()) } actual := buf.Bytes() if !bytes.Equal(b, actual) { t.Errorf("Expected %s to equal %s", actual, b) } } func TestWriteTooLong(t *testing.T) { body := "Hello world!\n" hdr := new(Header) hdr.Size = 1 var buf bytes.Buffer writer := NewWriter(&buf) writer.WriteHeader(hdr) _, err := writer.Write([]byte(body)) if err != ErrWriteTooLong { t.Errorf("Error should have been: %s", ErrWriteTooLong) } }