github.com/koko1123/flow-go-1@v0.29.6/utils/io/file.go (about)

     1  package io
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"go.uber.org/multierr"
    10  )
    11  
    12  // ReadFile reads the file from path, if not found, it will print the absolute path, instead of
    13  // relative path.
    14  func ReadFile(path string) ([]byte, error) {
    15  	absPath, err := filepath.Abs(path)
    16  	if err != nil {
    17  		return nil, fmt.Errorf("could not get absolution path: %w", err)
    18  	}
    19  
    20  	data, err := os.ReadFile(absPath)
    21  	if err != nil {
    22  		return nil, fmt.Errorf("could not read file: %w", err)
    23  	}
    24  
    25  	return data, nil
    26  }
    27  
    28  func FileExists(filename string) bool {
    29  	info, err := os.Stat(filename)
    30  	if os.IsNotExist(err) {
    31  		return false
    32  	}
    33  	return !info.IsDir()
    34  }
    35  
    36  // CopyDirectory recursively copies a directory.
    37  // From https://stackoverflow.com/questions/51779243/copy-a-folder-in-go
    38  func CopyDirectory(scrDir, dest string) error {
    39  	entries, err := os.ReadDir(scrDir)
    40  	if err != nil {
    41  		return err
    42  	}
    43  	for _, entry := range entries {
    44  		sourcePath := filepath.Join(scrDir, entry.Name())
    45  		destPath := filepath.Join(dest, entry.Name())
    46  
    47  		fileInfo, err := os.Stat(sourcePath)
    48  		if err != nil {
    49  			return err
    50  		}
    51  
    52  		switch fileInfo.Mode() & os.ModeType {
    53  		case os.ModeDir:
    54  			if err := CreateIfNotExists(destPath, 0755); err != nil {
    55  				return err
    56  			}
    57  			if err := CopyDirectory(sourcePath, destPath); err != nil {
    58  				return err
    59  			}
    60  		case os.ModeSymlink:
    61  			if err := CopySymLink(sourcePath, destPath); err != nil {
    62  				return err
    63  			}
    64  		default:
    65  			if err := Copy(sourcePath, destPath); err != nil {
    66  				return err
    67  			}
    68  		}
    69  
    70  		if err := chown(destPath, fileInfo); err != nil {
    71  			return err
    72  		}
    73  
    74  		fInfo, err := entry.Info()
    75  		if err != nil {
    76  			return err
    77  		}
    78  
    79  		isSymlink := fInfo.Mode()&os.ModeSymlink != 0
    80  		if !isSymlink {
    81  			if err := os.Chmod(destPath, fInfo.Mode()); err != nil {
    82  				return err
    83  			}
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  func Copy(srcFile, dstFile string) (errToReturn error) {
    90  	in, err := os.Open(srcFile)
    91  	if err != nil {
    92  		return fmt.Errorf("can not open file %v to copy from: %w", srcFile, err)
    93  	}
    94  
    95  	defer multierr.AppendInvoke(&errToReturn, multierr.Close(in))
    96  
    97  	out, err := os.Create(dstFile)
    98  	if err != nil {
    99  		return fmt.Errorf("can not create file %v to copy to: %w", dstFile, err)
   100  	}
   101  
   102  	defer multierr.AppendInvoke(&errToReturn, multierr.Close(out))
   103  
   104  	_, err = io.Copy(out, in)
   105  	if err != nil {
   106  		return fmt.Errorf("can not copy file: %w", err)
   107  	}
   108  
   109  	return nil
   110  }
   111  
   112  func Exists(filePath string) bool {
   113  	if _, err := os.Stat(filePath); os.IsNotExist(err) {
   114  		return false
   115  	}
   116  
   117  	return true
   118  }
   119  
   120  func CreateIfNotExists(dir string, perm os.FileMode) error {
   121  	if Exists(dir) {
   122  		return nil
   123  	}
   124  
   125  	if err := os.MkdirAll(dir, perm); err != nil {
   126  		return fmt.Errorf("failed to create directory: '%s', error: '%s'", dir, err.Error())
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func CopySymLink(source, dest string) error {
   133  	link, err := os.Readlink(source)
   134  	if err != nil {
   135  		return err
   136  	}
   137  	return os.Symlink(link, dest)
   138  }