github.com/zntrio/harp/v2@v2.0.9/pkg/sdk/fsutil/dump.go (about)

     1  // Licensed to Elasticsearch B.V. under one or more contributor
     2  // license agreements. See the NOTICE file distributed with
     3  // this work for additional information regarding copyright
     4  // ownership. Elasticsearch B.V. licenses this file to you under
     5  // the Apache License, Version 2.0 (the "License"); you may
     6  // not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing,
    12  // software distributed under the License is distributed on an
    13  // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    14  // KIND, either express or implied.  See the License for the
    15  // specific language governing permissions and limitations
    16  // under the License.
    17  
    18  package fsutil
    19  
    20  import (
    21  	"errors"
    22  	"fmt"
    23  	"io"
    24  	"io/fs"
    25  	"os"
    26  	"path/filepath"
    27  	"strings"
    28  
    29  	"go.uber.org/zap"
    30  
    31  	"github.com/zntrio/harp/v2/pkg/sdk/log"
    32  )
    33  
    34  // Dump the given vfs to the outputpath.
    35  func Dump(srcFs fs.FS, outPath string) error {
    36  	return fs.WalkDir(srcFs, ".", func(path string, d fs.DirEntry, errWalk error) error {
    37  		// Raise immediately the error if any.
    38  		if errWalk != nil {
    39  			return fmt.Errorf("%s: %w", path, errWalk)
    40  		}
    41  
    42  		// Ignore directory
    43  		if d.IsDir() {
    44  			return nil
    45  		}
    46  
    47  		// Compute the target path
    48  		targetPath := filepath.Join(outPath, path)
    49  
    50  		// Extract relative directory
    51  		relativeDir := filepath.Dir(targetPath)
    52  
    53  		// Check folder hierarchy existence.
    54  		if _, err := os.Stat(relativeDir); os.IsNotExist(err) {
    55  			if err := os.MkdirAll(relativeDir, 0o750); err != nil {
    56  				return fmt.Errorf("unable to create intermediate directories for path %q: %w", relativeDir, err)
    57  			}
    58  		}
    59  
    60  		// Encsure not out of safe directory file creation.
    61  		cleanTargetPath := filepath.Clean(targetPath)
    62  		if !strings.HasPrefix(cleanTargetPath, outPath) {
    63  			return fmt.Errorf("unable to create %q file, the path is not in the expected output path", targetPath)
    64  		}
    65  
    66  		// Create file
    67  		targetFile, err := os.Create(cleanTargetPath)
    68  		if err != nil {
    69  			return fmt.Errorf("unable to create the output file: %w", err)
    70  		}
    71  
    72  		// Open input file
    73  		srcFile, err := srcFs.Open(path)
    74  		if err != nil {
    75  			return fmt.Errorf("unable to open source file: %w", err)
    76  		}
    77  
    78  		log.Bg().Debug("Copy file ...", zap.String("file", path))
    79  
    80  		// Open the target file
    81  		if _, err := io.Copy(targetFile, srcFile); err != nil {
    82  			if !errors.Is(err, io.EOF) {
    83  				return fmt.Errorf("unable to copy content from %q to %q: %w", path, targetPath, err)
    84  			}
    85  		}
    86  
    87  		// Close the file
    88  		return srcFile.Close()
    89  	})
    90  }