github.com/bazelbuild/bazel-gazelle@v0.36.1-0.20240520142334-61b277ba6fed/cmd/fetch_repo/copy_tree.go (about) 1 /* Copyright 2019 The Bazel Authors. All rights reserved. 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 16 package main 17 18 import ( 19 "io" 20 "os" 21 "path/filepath" 22 ) 23 24 func copyTree(destRoot, srcRoot string) error { 25 return filepath.Walk(srcRoot, func(src string, info os.FileInfo, e error) (err error) { 26 if e != nil { 27 return e 28 } 29 rel, err := filepath.Rel(srcRoot, src) 30 if err != nil { 31 return err 32 } 33 if rel == "." { 34 return nil 35 } 36 dest := filepath.Join(destRoot, rel) 37 38 if info.IsDir() { 39 return os.Mkdir(dest, 0o777) 40 } 41 42 r, err := os.Open(src) 43 if err != nil { 44 return err 45 } 46 defer r.Close() 47 w, err := os.Create(dest) 48 if err != nil { 49 return err 50 } 51 defer func() { 52 if cerr := w.Close(); err == nil && cerr != nil { 53 err = cerr 54 } 55 }() 56 _, err = io.Copy(w, r) 57 return err 58 }) 59 }