github.com/searKing/golang/go@v1.2.117/io/copy_windows.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package io
     6  
     7  import (
     8  	"fmt"
     9  	"os"
    10  )
    11  
    12  func copyFileClone(srcFile, dstFile *os.File) error {
    13  	return ErrNotImplemented
    14  }
    15  
    16  func copyFileRange(rfd int, roff *int64, wfd int, woff *int64, len int, flags int) (n int, err error) {
    17  	return 0, ErrNotImplemented
    18  }
    19  
    20  func copyPath(srcPath, dstPath string, f os.FileInfo, copyMode Mode) error {
    21  	isHardlink := false
    22  
    23  	switch mode := f.Mode(); {
    24  	case mode.IsRegular():
    25  		// the type is 32bit on mips
    26  		if copyMode == Hardlink {
    27  			isHardlink = true
    28  			if err := os.Link(srcPath, dstPath); err != nil {
    29  				return err
    30  			}
    31  		} else {
    32  			if err := CopyRegular(srcPath, dstPath, f); err != nil {
    33  				return err
    34  			}
    35  		}
    36  
    37  	case mode.IsDir():
    38  		if err := os.Mkdir(dstPath, f.Mode()); err != nil && !os.IsExist(err) {
    39  			return err
    40  		}
    41  
    42  	case mode&os.ModeSymlink != 0:
    43  		link, err := os.Readlink(srcPath)
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		if err := os.Symlink(link, dstPath); err != nil {
    49  			return err
    50  		}
    51  
    52  	default:
    53  		return fmt.Errorf("unknown file type (%d / %s) for %s", f.Mode(), f.Mode().String(), srcPath)
    54  	}
    55  
    56  	// Everything below is copying metadata from src to dst. All this metadata
    57  	// already shares an inode for hardlinks.
    58  	if isHardlink {
    59  		return nil
    60  	}
    61  
    62  	isSymlink := f.Mode()&os.ModeSymlink != 0
    63  
    64  	// There is no LChmod, so ignore mode for symlink. Also, this
    65  	// must happen after chown, as that can modify the file mode
    66  	if !isSymlink {
    67  		if err := os.Chmod(dstPath, f.Mode()); err != nil {
    68  			return err
    69  		}
    70  	}
    71  
    72  	return nil
    73  }