github.com/kaydxh/golang@v0.0.131/go/io/copy_linux.go (about)

     1  /*
     2  MIT License
     3  
     4  Copyright (c) 2020 kay
     5  
     6  Permission is hereby granted, free of charge, to any person obtaining a copy
     7  of this software and associated documentation files (the "Software"), to deal
     8  in the Software without restriction, including without limitation the rights
     9  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10  copies of the Software, and to permit persons to whom the Software is
    11  furnished to do so, subject to the following conditions:
    12  
    13  The above copyright notice and this permission notice shall be included in all
    14  copies or substantial portions of the Software.
    15  
    16  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22  SOFTWARE.
    23  */
    24  
    25  package io
    26  
    27  import (
    28  	"fmt"
    29  	"golang.org/x/sys/unix"
    30  	"os"
    31  	"syscall"
    32  )
    33  
    34  func CopyPath(srcPath, dstPath string, f os.FileInfo, copyMode Mode) error {
    35  	stat, ok := f.Sys().(*syscall.Stat_t)
    36  	if !ok {
    37  		return fmt.Errorf("Unable to get raw syscall.Stat_t data for %s", srcPath)
    38  	}
    39  
    40  	isHardlink := false
    41  
    42  	switch mode := f.Mode(); {
    43  	case mode.IsRegular():
    44  		if copyMode == Hardlink {
    45  			isHardlink = true
    46  			if err := os.Link(srcPath, dstPath); err != nil {
    47  				return err
    48  			}
    49  		} else {
    50  			if err := CopyRegular(srcPath, dstPath, f); err != nil {
    51  				return err
    52  			}
    53  		}
    54  
    55  	case mode.IsDir():
    56  		if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
    57  			return err
    58  		}
    59  	case mode&os.ModeSymlink != 0:
    60  		link, err := os.Readlink(srcPath)
    61  		if err != nil {
    62  			return err
    63  		}
    64  
    65  		if err := os.Symlink(link, dstPath); err != nil {
    66  			return err
    67  		}
    68  
    69  	case mode&os.ModeNamedPipe != 0:
    70  		fallthrough
    71  
    72  	case mode&os.ModeSocket != 0:
    73  		if err := unix.Mkfifo(dstPath, uint32(stat.Mode)); err != nil {
    74  			return err
    75  		}
    76  	case mode&os.ModeDevice != 0:
    77  		if err := unix.Mknod(dstPath, uint32(stat.Mode), int(stat.Rdev)); err != nil {
    78  			return err
    79  		}
    80  
    81  	default:
    82  		return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
    83  
    84  	}
    85  
    86  	// Everything below is copying metadata from src to dst. All this metadata
    87  	// already shares an inode for hardlinks.
    88  	if isHardlink {
    89  		return nil
    90  	}
    91  
    92  	if err := os.Lchown(dstPath, int(stat.Uid), int(stat.Gid)); err != nil {
    93  		return err
    94  	}
    95  
    96  	isSymlink := f.Mode()&os.ModeSymlink != 0
    97  	// There is no LChmod, so ignore mode for symlink. Also, this
    98  	// must happen after chown, as that can modify the file mode
    99  	if !isSymlink {
   100  		if err := os.Chmod(dstPath, f.Mode()); err != nil {
   101  			return err
   102  		}
   103  	}
   104  
   105  	return nil
   106  }
   107  
   108  func doCopyWithFileClone(srcFile, dstFile *os.File) error {
   109  	return unix.IoctlFileClone(int(dstFile.Fd()), int(srcFile.Fd()))
   110  }
   111  
   112  func doCopyWithFileRange(srcFile, dstFile *os.File, fileinfo os.FileInfo) error {
   113  	amountLeftToCopy := fileinfo.Size()
   114  
   115  	for amountLeftToCopy > 0 {
   116  		n, err := unix.CopyFileRange(int(srcFile.Fd()), nil, int(dstFile.Fd()), nil, int(amountLeftToCopy), 0)
   117  		if err != nil {
   118  			return err
   119  		}
   120  
   121  		amountLeftToCopy = amountLeftToCopy - int64(n)
   122  	}
   123  
   124  	return nil
   125  
   126  }