...

Source file src/k8s.io/kubernetes/cluster/images/etcd/migrate/copy_file.go

Documentation: k8s.io/kubernetes/cluster/images/etcd/migrate

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     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
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    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.
    15  */
    16  
    17  package main
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  	"path/filepath"
    24  )
    25  
    26  func copyFile(source, dest string) error {
    27  	sf, err := os.Open(source)
    28  	if err != nil {
    29  		return fmt.Errorf("unable to open source file [%s]: %q", source, err)
    30  	}
    31  	defer sf.Close()
    32  	fi, err := sf.Stat()
    33  	if err != nil {
    34  		return fmt.Errorf("unable to stat source file [%s]: %q", source, err)
    35  	}
    36  
    37  	dir := filepath.Dir(dest)
    38  	if err := os.MkdirAll(dir, 0755); err != nil {
    39  		return fmt.Errorf("unable to create directory [%s]: %q", dir, err)
    40  	}
    41  	df, err := os.Create(dest)
    42  	if err != nil {
    43  		return fmt.Errorf("unable to create destination file [%s]: %q", dest, err)
    44  	}
    45  	defer df.Close()
    46  
    47  	_, err = io.Copy(df, sf)
    48  	if err != nil {
    49  		return fmt.Errorf("unable to copy [%s] to [%s]: %q", source, dest, err)
    50  	}
    51  
    52  	if err := os.Chmod(dest, fi.Mode()); err != nil {
    53  		return fmt.Errorf("unable to close destination file: %q", err)
    54  	}
    55  	return nil
    56  }
    57  

View as plain text