github.com/coreos/rocket@v1.30.1-0.20200224141603-171c416fac02/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/hashicorp/errwrap"
    27  	"github.com/rkt/rkt/pkg/lock"
    28  	"github.com/rkt/rkt/store/imagestore"
    29  
    30  	"github.com/coreos/ioprogress"
    31  )
    32  
    33  // writeSyncer is an interface that wraps io.Writer and a Sync method.
    34  type writeSyncer interface {
    35  	io.Writer
    36  	Sync() error
    37  }
    38  
    39  // readSeekCloser is an interface that wraps io.ReadSeeker and
    40  // io.Closer
    41  type readSeekCloser interface {
    42  	io.ReadSeeker
    43  	io.Closer
    44  }
    45  
    46  type nopReadSeekCloser struct {
    47  	io.ReadSeeker
    48  }
    49  
    50  func (nopReadSeekCloser) Close() error { return nil }
    51  
    52  // NopReadSeekCloser wraps the given ReadSeeker
    53  // and returns one that does nothing when Close() is being invoked.
    54  func NopReadSeekCloser(rs io.ReadSeeker) readSeekCloser {
    55  	return nopReadSeekCloser{rs}
    56  }
    57  
    58  // getIoProgressReader returns a reader that wraps the HTTP response
    59  // body, so it prints a pretty progress bar when reading data from it.
    60  func getIoProgressReader(label string, res *http.Response) io.Reader {
    61  	prefix := "Downloading " + label
    62  	fmtBytesSize := 18
    63  	barSize := int64(80 - len(prefix) - fmtBytesSize)
    64  	bar := ioprogress.DrawTextFormatBarForW(barSize, os.Stderr)
    65  	fmtfunc := func(progress, total int64) string {
    66  		// Content-Length is set to -1 when unknown.
    67  		if total == -1 {
    68  			return fmt.Sprintf(
    69  				"%s: %v of an unknown total size",
    70  				prefix,
    71  				ioprogress.ByteUnitStr(progress),
    72  			)
    73  		}
    74  		return fmt.Sprintf(
    75  			"%s: %s %s",
    76  			prefix,
    77  			bar(progress, total),
    78  			ioprogress.DrawTextFormatBytes(progress, total),
    79  		)
    80  	}
    81  	return &ioprogress.Reader{
    82  		Reader:       res.Body,
    83  		Size:         res.ContentLength,
    84  		DrawFunc:     ioprogress.DrawTerminalf(os.Stderr, fmtfunc),
    85  		DrawInterval: time.Second,
    86  	}
    87  }
    88  
    89  // removeOnClose is a wrapper around os.File that removes the file
    90  // when closing it. removeOnClose implements a readSeekCloser
    91  // interface.
    92  type removeOnClose struct {
    93  	// File is a wrapped os.File
    94  	File *os.File
    95  }
    96  
    97  func (f *removeOnClose) Read(p []byte) (int, error) {
    98  	return f.File.Read(p)
    99  }
   100  
   101  func (f *removeOnClose) Seek(offset int64, whence int) (int64, error) {
   102  	return f.File.Seek(offset, whence)
   103  }
   104  
   105  // Close closes the file and then removes it from disk. No error is
   106  // returned if the file did not exist at the point of removal.
   107  func (f *removeOnClose) Close() error {
   108  	if f == nil || f.File == nil {
   109  		return nil
   110  	}
   111  	name := f.File.Name()
   112  	if err := f.File.Close(); err != nil {
   113  		return err
   114  	}
   115  	if err := os.Remove(name); err != nil && !os.IsNotExist(err) {
   116  		return err
   117  	}
   118  	return nil
   119  }
   120  
   121  // getTmpROC returns a removeOnClose instance wrapping a temporary
   122  // file provided by the passed store. The actual file name is based on
   123  // a hash of the passed path.
   124  func getTmpROC(s *imagestore.Store, path string) (*removeOnClose, error) {
   125  	h := sha512.New()
   126  	h.Write([]byte(path))
   127  	pathHash := s.HashToKey(h)
   128  
   129  	tmp, err := s.TmpNamedFile(pathHash)
   130  	if err != nil {
   131  		return nil, errwrap.Wrap(errors.New("error setting up temporary file"), err)
   132  	}
   133  
   134  	// let's lock the file to avoid concurrent writes to the temporary file, it
   135  	// will go away when removing the temp file
   136  	_, err = lock.TryExclusiveLock(tmp.Name(), lock.RegFile)
   137  	if err != nil {
   138  		if err != lock.ErrLocked {
   139  			return nil, errwrap.Wrap(errors.New("failed to lock temporary file"), err)
   140  		}
   141  		log.Printf("another rkt instance is downloading this file, waiting...")
   142  		_, err = lock.ExclusiveLock(tmp.Name(), lock.RegFile)
   143  		if err != nil {
   144  			return nil, errwrap.Wrap(errors.New("failed to lock temporary file"), err)
   145  		}
   146  	}
   147  
   148  	return &removeOnClose{File: tmp}, nil
   149  }