aboutsummaryrefslogtreecommitdiffstats
path: root/formats
diff options
context:
space:
mode:
authorJoshua Sjoding <joshua.sjoding@scjalliance.com>2016-02-24 22:40:30 -0800
committerJoshua Sjoding <joshua.sjoding@scjalliance.com>2016-02-25 00:38:51 -0800
commit0d999e1db6cd8736ab697de8ce848fa3a5274b9f (patch)
tree6107f49405bb605793f1bcd7ef4961ceadcb11e9 /formats
parent07ca1ac7f3058ea6d3274a01973541fb84782f5e (diff)
downloadgo-git-0d999e1db6cd8736ab697de8ce848fa3a5274b9f.tar.gz
Refactor to use core.ObjectReader and core.ObjectWriter
* New function signatures provide the necessary interface to stream data from disk when using filesystem-based storage in the future * New function signatures provide proper error handling * ObjectReader and ObjectWriter interfaces added to avoid future refactoring, currently are type aliases for io.ReadCloser and io.WriteCloser respectively * Object.Reader now returns (ObjectReader, error) * Object.Writer now returns (ObjectWriter, error) * File.Contents now returns (string, error) * File.Lines now returns ([]string, error) * Blob.Reader now returns (core.ObjectReader, error) * Added internal close helper function for deferred calls to Close that need to check the return value
Diffstat (limited to 'formats')
-rw-r--r--formats/packfile/common.go20
-rw-r--r--formats/packfile/reader.go64
2 files changed, 74 insertions, 10 deletions
diff --git a/formats/packfile/common.go b/formats/packfile/common.go
index 57bc0b9..d207563 100644
--- a/formats/packfile/common.go
+++ b/formats/packfile/common.go
@@ -41,3 +41,23 @@ func (t *trackingReader) ReadByte() (c byte, err error) {
t.position++
return p[0], nil
}
+
+// close is used with defer to close the given io.Closer and check its
+// returned error value. If Close returns an error and the given *error
+// is not nil, *error is set to the error returned by Close.
+//
+// close is typically used with named return values like so:
+//
+// func do(obj *Object) (err error) {
+// w, err := obj.Writer()
+// if err != nil {
+// return nil
+// }
+// defer close(w, &err)
+// // work with w
+// }
+func close(c io.Closer, err *error) {
+ if cerr := c.Close(); cerr != nil && *err == nil {
+ *err = cerr
+ }
+}
diff --git a/formats/packfile/reader.go b/formats/packfile/reader.go
index 6f30f8a..e14b84e 100644
--- a/formats/packfile/reader.go
+++ b/formats/packfile/reader.go
@@ -187,7 +187,7 @@ func (r *Reader) newObject() (core.Object, error) {
return raw, err
}
-func (r *Reader) readREFDelta(raw core.Object) error {
+func (r *Reader) readREFDelta(raw core.Object) (err error) {
var ref core.Hash
if _, err := io.ReadFull(r.r, ref[:]); err != nil {
return err
@@ -206,7 +206,17 @@ func (r *Reader) readREFDelta(raw core.Object) error {
return err
}
- d, _ := ioutil.ReadAll(referenced.Reader())
+ reader, err := referenced.Reader()
+ if err != nil {
+ return err
+ }
+ defer close(reader, &err)
+
+ d, err := ioutil.ReadAll(reader)
+ if err != nil {
+ return err
+ }
+
patched := patchDelta(d, buf.Bytes())
if patched == nil {
return PatchingErr.n("hash %q", ref)
@@ -214,12 +224,19 @@ func (r *Reader) readREFDelta(raw core.Object) error {
raw.SetType(referenced.Type())
raw.SetSize(int64(len(patched)))
- raw.Writer().Write(patched)
+
+ writer, err := raw.Writer()
+ if err != nil {
+ return err
+ }
+ defer close(writer, &err)
+
+ writer.Write(patched)
return nil
}
-func (r *Reader) readOFSDelta(raw core.Object, steps int64) error {
+func (r *Reader) readOFSDelta(raw core.Object, steps int64) (err error) {
start := r.r.position
offset, err := decodeOffset(r.r, steps)
if err != nil {
@@ -227,7 +244,7 @@ func (r *Reader) readOFSDelta(raw core.Object, steps int64) error {
}
buf := bytes.NewBuffer(nil)
- if err := r.inflate(buf); err != nil {
+ if err = r.inflate(buf); err != nil {
return err
}
@@ -236,8 +253,22 @@ func (r *Reader) readOFSDelta(raw core.Object, steps int64) error {
return PackEntryNotFoundErr.n("offset %d", start+offset)
}
- referenced, _ := r.s.Get(ref) // FIXME: Handle error returned from Get()
- d, _ := ioutil.ReadAll(referenced.Reader())
+ referenced, err := r.s.Get(ref)
+ if err != nil {
+ return err
+ }
+
+ reader, err := referenced.Reader()
+ if err != nil {
+ return err
+ }
+ defer close(reader, &err)
+
+ d, err := ioutil.ReadAll(reader)
+ if err != nil {
+ return err
+ }
+
patched := patchDelta(d, buf.Bytes())
if patched == nil {
return PatchingErr.n("hash %q", ref)
@@ -245,13 +276,26 @@ func (r *Reader) readOFSDelta(raw core.Object, steps int64) error {
raw.SetType(referenced.Type())
raw.SetSize(int64(len(patched)))
- raw.Writer().Write(patched)
+
+ writer, err := raw.Writer()
+ if err != nil {
+ return err
+ }
+ defer close(writer, &err)
+
+ writer.Write(patched)
return nil
}
-func (r *Reader) readObject(raw core.Object) error {
- return r.inflate(raw.Writer())
+func (r *Reader) readObject(raw core.Object) (err error) {
+ writer, err := raw.Writer()
+ if err != nil {
+ return err
+ }
+ defer close(writer, &err)
+
+ return r.inflate(writer)
}
func (r *Reader) inflate(w io.Writer) error {