2 * Copyright 2018 Intel Corporation, Inc
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 pkgerrors "github.com/pkg/errors"
28 utils "github.com/onap/multicloud-k8s/src/k8splugin/internal"
31 func isTarGz(r io.Reader) error {
32 //Check if it is a valid gz
33 gzf, err := gzip.NewReader(r)
35 return pkgerrors.Wrap(err, "Invalid gzip format")
38 //Check if it is a valid tar file
39 //Unfortunately this can only be done by inspecting all the tar contents
40 tarR := tar.NewReader(gzf)
44 header, err := tarR.Next()
47 //Check if we have just a gzip file without a tar archive inside
49 return pkgerrors.New("Empty or non-existant Tar file found")
56 return pkgerrors.Errorf("Error reading tar file %s", err.Error())
59 //Check if files are of type directory and regular file
60 if header.Typeflag != tar.TypeDir &&
61 header.Typeflag != tar.TypeReg {
62 return pkgerrors.Errorf("Unknown header in tar %s, %s",
63 header.Name, string(header.Typeflag))
72 //ExtractTarBall provides functionality to extract a tar.gz file
73 //into a temporary location for later use.
74 //It returns the path to the new location
75 func ExtractTarBall(r io.Reader) (string, error) {
76 //Check if it is a valid gz
77 gzf, err := gzip.NewReader(r)
79 return "", pkgerrors.Wrap(err, "Invalid gzip format")
82 //Check if it is a valid tar file
83 //Unfortunately this can only be done by inspecting all the tar contents
84 tarR := tar.NewReader(gzf)
87 outDir, _ := ioutil.TempDir("", "k8s-ext-")
90 header, err := tarR.Next()
93 //Check if we have just a gzip file without a tar archive inside
95 return "", pkgerrors.New("Empty or non-existant Tar file found")
102 return "", pkgerrors.Wrap(err, "Error reading tar file")
105 target := filepath.Join(outDir, header.Name)
107 switch header.Typeflag {
109 if _, err := os.Stat(target); err != nil {
110 // Using 755 read, write, execute for owner
111 // groups and others get read and execute permissions
113 if err := os.MkdirAll(target, 0755); err != nil {
114 return "", pkgerrors.Wrap(err, "Creating directory")
118 if target == outDir { // Handle '.' substituted to '' entry
122 err = utils.EnsureDirectory(target)
124 return "", pkgerrors.Wrap(err, "Creating Directory")
127 f, err := os.OpenFile(target, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
129 return "", pkgerrors.Wrap(err, "Creating file")
132 // copy over contents
133 if _, err := io.Copy(f, tarR); err != nil {
134 return "", pkgerrors.Wrap(err, "Copying file content")
137 // close for each file instead of a defer for all
138 // at the end of the function