mv x/<tool> => xtool/<tool>
This commit is contained in:
60
xtool/ar/common.go
Normal file
60
xtool/ar/common.go
Normal file
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ar
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
errInvalidHeader = errors.New("ar: invalid header")
|
||||
errWriteTooLong = errors.New("ar: write too long")
|
||||
)
|
||||
|
||||
const (
|
||||
globalHeader = "!<arch>\n"
|
||||
globalHeaderLen = len(globalHeader)
|
||||
headerByteSize = 60
|
||||
)
|
||||
|
||||
type recHeader struct {
|
||||
name [16]byte
|
||||
modTime [12]byte
|
||||
uid [6]byte
|
||||
gid [6]byte
|
||||
mode [8]byte
|
||||
size [10]byte
|
||||
eol [2]byte
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
140
xtool/ar/reader.go
Normal file
140
xtool/ar/reader.go
Normal file
@@ -0,0 +1,140 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ar
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
"strings"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
// 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, error) {
|
||||
buf := make([]byte, globalHeaderLen)
|
||||
if _, err := io.ReadFull(r, buf); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if string(buf) != globalHeader {
|
||||
return nil, errInvalidHeader
|
||||
}
|
||||
|
||||
return &Reader{r: r}, nil
|
||||
}
|
||||
|
||||
func stringVal(b []byte) string {
|
||||
return strings.TrimRight(string(b), " ")
|
||||
}
|
||||
|
||||
func intVal(b []byte) (int64, error) {
|
||||
return strconv.ParseInt(stringVal(b), 10, 64)
|
||||
}
|
||||
|
||||
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, io.SeekCurrent)
|
||||
return err
|
||||
}
|
||||
|
||||
_, err := io.CopyN(io.Discard, rd.r, skip)
|
||||
return err
|
||||
}
|
||||
|
||||
func (rd *Reader) readHeader() (header *Header, err error) {
|
||||
var rec recHeader
|
||||
var buf = (*[headerByteSize]byte)(unsafe.Pointer(&rec))[:]
|
||||
if _, err = io.ReadFull(rd.r, buf); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
header = new(Header)
|
||||
header.Name = stringVal(rec.name[:])
|
||||
if header.Size, err = intVal(rec.size[:]); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if header.Size%2 == 1 {
|
||||
rd.pad = 1
|
||||
} else {
|
||||
rd.pad = 0
|
||||
}
|
||||
|
||||
if rec.name[0] == '#' {
|
||||
if n, e := strconv.ParseInt(strings.TrimPrefix(header.Name[3:], "#1/"), 10, 64); e == nil {
|
||||
name := make([]byte, n)
|
||||
if _, err = io.ReadFull(rd.r, name); err != nil {
|
||||
return
|
||||
}
|
||||
header.Name = string(name)
|
||||
header.Size -= n
|
||||
}
|
||||
}
|
||||
|
||||
rd.nb = int64(header.Size)
|
||||
return
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
116
xtool/ar/writer.go
Normal file
116
xtool/ar/writer.go
Normal file
@@ -0,0 +1,116 @@
|
||||
/*
|
||||
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ar
|
||||
|
||||
import (
|
||||
"io"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// 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(globalHeader))
|
||||
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, headerByteSize)
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user