github.com/rclone/rclone@v1.66.1-0.20240517100346-7b89735ae726/backend/local/remove_windows.go (about)

     1  //go:build windows
     2  
     3  package local
     4  
     5  import (
     6  	"os"
     7  	"time"
     8  
     9  	"github.com/rclone/rclone/fs"
    10  	"golang.org/x/sys/windows"
    11  )
    12  
    13  // Removes name, retrying on a sharing violation
    14  func remove(name string) (err error) {
    15  	const maxTries = 10
    16  	var sleepTime = 1 * time.Millisecond
    17  	for i := 0; i < maxTries; i++ {
    18  		err = os.Remove(name)
    19  		if err == nil {
    20  			break
    21  		}
    22  		pathErr, ok := err.(*os.PathError)
    23  		if !ok {
    24  			break
    25  		}
    26  		if pathErr.Err != windows.ERROR_SHARING_VIOLATION {
    27  			break
    28  		}
    29  		fs.Logf(name, "Remove detected sharing violation - retry %d/%d sleeping %v", i+1, maxTries, sleepTime)
    30  		time.Sleep(sleepTime)
    31  		sleepTime <<= 1
    32  	}
    33  	return err
    34  }