github.com/blixtra/rkt@v0.8.1-0.20160204105720-ab0d1add1a43/rkt/image/io.go (about)

     1  // Copyright 2015 The rkt Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package image
    16  
    17  import (
    18  	"crypto/sha512"
    19  	"errors"
    20  	"fmt"
    21  	"io"
    22  	"net/http"
    23  	"os"
    24  	"time"
    25  
    26  	"github.com/coreos/rkt/store"
    27  	"github.com/hashicorp/errwrap"
    28  
    29  	"github.com/coreos/ioprogress"
    30  )
    31  
    32  // writeSyncer is an interface that wraps io.Writer and a Sync method.
    33  type writeSyncer interface {
    34  	io.Writer
    35  	Sync() error
    36  }
    37  
    38  // readSeekCloser is an interface that wraps io.ReadSeeker and
    39  // io.Closer
    40  type readSeekCloser interface {
    41  	io.ReadSeeker
    42  	io.Closer
    43  }
    44  
    45  // getIoProgressReader returns a reader that wraps the HTTP response
    46  // body, so it prints a pretty progress bar when reading data from it.
    47  func getIoProgressReader(label string, res *http.Response) io.Reader {
    48  	prefix := "Downloading " + label
    49  	fmtBytesSize := 18
    50  	barSize := int64(80 - len(prefix) - fmtBytesSize)
    51  	bar := ioprogress.DrawTextFormatBarForW(barSize, os.Stderr)
    52  	fmtfunc := func(progress, total int64) string {
    53  		// Content-Length is set to -1 when unknown.
    54  		if total == -1 {
    55  			return fmt.Sprintf(
    56  				"%s: %v of an unknown total size",
    57  				prefix,
    58  				ioprogress.ByteUnitStr(progress),
    59  			)
    60  		}
    61  		return fmt.Sprintf(
    62  			"%s: %s %s",
    63  			prefix,
    64  			bar(progress, total),
    65  			ioprogress.DrawTextFormatBytes(progress, total),
    66  		)
    67  	}
    68  	return &ioprogress.Reader{
    69  		Reader:       res.Body,
    70  		Size:         res.ContentLength,
    71  		DrawFunc:     ioprogress.DrawTerminalf(os.Stderr, fmtfunc),
    72  		DrawInterval: time.Second,
    73  	}
    74  }
    75  
    76  // removeOnClose is a wrapper around os.File that removes the file
    77  // when closing it. removeOnClose implements a readSeekCloser
    78  // interface.
    79  type removeOnClose struct {
    80  	// File is a wrapped os.File
    81  	File *os.File
    82  }
    83  
    84  func (f *removeOnClose) Read(p []byte) (int, error) {
    85  	return f.File.Read(p)
    86  }
    87  
    88  func (f *removeOnClose) Seek(offset int64, whence int) (int64, error) {
    89  	return f.File.Seek(offset, whence)
    90  }
    91  
    92  // Close closes the file and then removes it from disk. No error is
    93  // returned if the file did not exist at the point of removal.
    94  func (f *removeOnClose) Close() error {
    95  	name := f.File.Name()
    96  	if err := f.File.Close(); err != nil {
    97  		return err
    98  	}
    99  	if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
   100  		return err
   101  	}
   102  	return nil
   103  }
   104  
   105  // getTmpROC returns a removeOnClose instance wrapping a temporary
   106  // file provided by the passed store. The actual file name is based on
   107  // a hash of the passed path.
   108  func getTmpROC(s *store.Store, path string) (*removeOnClose, error) {
   109  	h := sha512.New()
   110  	h.Write([]byte(path))
   111  	pathHash := s.HashToKey(h)
   112  
   113  	tmp, err := s.TmpNamedFile(pathHash)
   114  	if err != nil {
   115  		return nil, errwrap.Wrap(errors.New("error setting up temporary file"), err)
   116  	}
   117  	roc := &removeOnClose{File: tmp}
   118  	return roc, nil
   119  }
   120  
   121  // maybeClose is a convenient function for closing io.Closers if they
   122  // are not nil. Useful in defers.
   123  func maybeClose(c io.Closer) {
   124  	if !isReallyNil(c) {
   125  		c.Close()
   126  	}
   127  }