github.com/rsampaio/docker@v0.7.2-0.20150827203920-fdc73cc3fc31/api/server/copy.go (about)

     1  package server
     2  
     3  import (
     4  	"encoding/base64"
     5  	"encoding/json"
     6  	"fmt"
     7  	"io"
     8  	"net/http"
     9  	"os"
    10  	"strings"
    11  
    12  	"github.com/docker/docker/api/types"
    13  	"github.com/docker/docker/pkg/version"
    14  )
    15  
    16  // postContainersCopy is deprecated in favor of getContainersArchive.
    17  func (s *Server) postContainersCopy(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    18  	if vars == nil {
    19  		return fmt.Errorf("Missing parameter")
    20  	}
    21  
    22  	if err := checkForJSON(r); err != nil {
    23  		return err
    24  	}
    25  
    26  	cfg := types.CopyConfig{}
    27  	if err := json.NewDecoder(r.Body).Decode(&cfg); err != nil {
    28  		return err
    29  	}
    30  
    31  	if cfg.Resource == "" {
    32  		return fmt.Errorf("Path cannot be empty")
    33  	}
    34  
    35  	data, err := s.daemon.ContainerCopy(vars["name"], cfg.Resource)
    36  	if err != nil {
    37  		if strings.Contains(strings.ToLower(err.Error()), "no such id") {
    38  			w.WriteHeader(http.StatusNotFound)
    39  			return nil
    40  		}
    41  		if os.IsNotExist(err) {
    42  			return fmt.Errorf("Could not find the file %s in container %s", cfg.Resource, vars["name"])
    43  		}
    44  		return err
    45  	}
    46  	defer data.Close()
    47  
    48  	w.Header().Set("Content-Type", "application/x-tar")
    49  	if _, err := io.Copy(w, data); err != nil {
    50  		return err
    51  	}
    52  
    53  	return nil
    54  }
    55  
    56  // // Encode the stat to JSON, base64 encode, and place in a header.
    57  func setContainerPathStatHeader(stat *types.ContainerPathStat, header http.Header) error {
    58  	statJSON, err := json.Marshal(stat)
    59  	if err != nil {
    60  		return err
    61  	}
    62  
    63  	header.Set(
    64  		"X-Docker-Container-Path-Stat",
    65  		base64.StdEncoding.EncodeToString(statJSON),
    66  	)
    67  
    68  	return nil
    69  }
    70  
    71  func (s *Server) headContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    72  	v, err := archiveFormValues(r, vars)
    73  	if err != nil {
    74  		return err
    75  	}
    76  
    77  	stat, err := s.daemon.ContainerStatPath(v.name, v.path)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return setContainerPathStatHeader(stat, w.Header())
    83  }
    84  
    85  func (s *Server) getContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
    86  	v, err := archiveFormValues(r, vars)
    87  	if err != nil {
    88  		return err
    89  	}
    90  
    91  	tarArchive, stat, err := s.daemon.ContainerArchivePath(v.name, v.path)
    92  	if err != nil {
    93  		return err
    94  	}
    95  	defer tarArchive.Close()
    96  
    97  	if err := setContainerPathStatHeader(stat, w.Header()); err != nil {
    98  		return err
    99  	}
   100  
   101  	w.Header().Set("Content-Type", "application/x-tar")
   102  	_, err = io.Copy(w, tarArchive)
   103  
   104  	return err
   105  }
   106  
   107  func (s *Server) putContainersArchive(version version.Version, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
   108  	v, err := archiveFormValues(r, vars)
   109  	if err != nil {
   110  		return err
   111  	}
   112  
   113  	noOverwriteDirNonDir := boolValue(r, "noOverwriteDirNonDir")
   114  	return s.daemon.ContainerExtractToDir(v.name, v.path, noOverwriteDirNonDir, r.Body)
   115  }