From a3ce9fff79da30bcc57fdddc4feda3060b8e6877 Mon Sep 17 00:00:00 2001 From: David Symonds Date: Mon, 5 Oct 2009 04:08:24 -0700 Subject: Add write support for the GNU tar binary numeric field extension. R=rsc APPROVED=rsc DELTA=102 (89 added, 1 deleted, 12 changed) OCL=35321 CL=35327 --- src/pkg/testing/iotest/Makefile | 1 + src/pkg/testing/iotest/writer.go | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 src/pkg/testing/iotest/writer.go (limited to 'src/pkg/testing') diff --git a/src/pkg/testing/iotest/Makefile b/src/pkg/testing/iotest/Makefile index b223fb932..a37a9d622 100644 --- a/src/pkg/testing/iotest/Makefile +++ b/src/pkg/testing/iotest/Makefile @@ -8,5 +8,6 @@ TARG=testing/iotest GOFILES=\ logger.go\ reader.go\ + writer.go\ include $(GOROOT)/src/Make.pkg diff --git a/src/pkg/testing/iotest/writer.go b/src/pkg/testing/iotest/writer.go new file mode 100644 index 000000000..7bd5ddda6 --- /dev/null +++ b/src/pkg/testing/iotest/writer.go @@ -0,0 +1,38 @@ +// Copyright 2009 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package iotest + +import ( + "io"; + "os"; +) + +// TruncateWriter returns a Writer that writes to w +// but stops silently after n bytes. +func TruncateWriter(w io.Writer, n int64) io.Writer { + return &truncateWriter{w, n}; +} + +type truncateWriter struct { + w io.Writer; + n int64; +} + +func (t *truncateWriter) Write(p []byte) (n int, err os.Error) { + if t.n <= 0 { + return len(p), nil + } + // real write + n = len(p); + if int64(n) > t.n { + n = int(t.n); + } + n, err = t.w.Write(p[0:n]); + t.n -= int64(n); + if err == nil { + n = len(p); + } + return +} -- cgit v1.2.3