github.com/scaleoutsean/fusego@v0.0.0-20220224074057-4a6429e46bb8/samples/unmount.go (about)

     1  // Copyright 2015 Google Inc. All Rights Reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package samples
    16  
    17  import (
    18  	"fmt"
    19  	"log"
    20  	"strings"
    21  	"time"
    22  
    23  	"github.com/scaleoutsean/fusego"
    24  )
    25  
    26  // Unmount the file system mounted at the supplied directory. Try again on
    27  // "resource busy" errors, which happen from time to time on OS X (due to weird
    28  // requests from the Finder) and when tests don't or can't synchronize all
    29  // events.
    30  func unmount(dir string) error {
    31  	delay := 10 * time.Millisecond
    32  	for {
    33  		err := fuse.Unmount(dir)
    34  		if err == nil {
    35  			return err
    36  		}
    37  
    38  		if strings.Contains(err.Error(), "resource busy") {
    39  			log.Println("Resource busy error while unmounting; trying again")
    40  			time.Sleep(delay)
    41  			delay = time.Duration(1.3 * float64(delay))
    42  			continue
    43  		}
    44  
    45  		return fmt.Errorf("Unmount: %v", err)
    46  	}
    47  }