github.com/sdboyer/gps@v0.16.3/remove_go16.go (about)

     1  // +build !go1.7
     2  
     3  package gps
     4  
     5  import (
     6  	"os"
     7  	"path/filepath"
     8  	"runtime"
     9  )
    10  
    11  // removeAll removes path and any children it contains. It deals correctly with
    12  // removal on Windows where, prior to Go 1.7, there were issues when files were
    13  // set to read-only.
    14  func removeAll(path string) error {
    15  	// Only need special handling for windows
    16  	if runtime.GOOS != "windows" {
    17  		return os.RemoveAll(path)
    18  	}
    19  
    20  	// Simple case: if Remove works, we're done.
    21  	err := os.Remove(path)
    22  	if err == nil || os.IsNotExist(err) {
    23  		return nil
    24  	}
    25  
    26  	// make sure all files are writable so we can delete them
    27  	err = filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
    28  		if err != nil && err != filepath.SkipDir {
    29  			// walk gave us some error, give it back.
    30  			return err
    31  		}
    32  		mode := info.Mode()
    33  		if mode|0200 == mode {
    34  			return nil
    35  		}
    36  
    37  		return os.Chmod(path, mode|0200)
    38  	})
    39  	if err != nil {
    40  		return err
    41  	}
    42  
    43  	return os.Remove(path)
    44  }