github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/fs/util.go (about)

     1  /*
     2  Copyright 2013 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package fs
    18  
    19  import (
    20  	"errors"
    21  	"os/exec"
    22  	"runtime"
    23  	"time"
    24  )
    25  
    26  // Unmount attempts to unmount the provided FUSE mount point, forcibly
    27  // if necessary.
    28  func Unmount(point string) error {
    29  	var cmd *exec.Cmd
    30  	switch runtime.GOOS {
    31  	case "darwin":
    32  		cmd = exec.Command("diskutil", "umount", "force", point)
    33  	case "linux":
    34  		cmd = exec.Command("fusermount", "-u", point)
    35  	default:
    36  		return errors.New("unmount: unimplemented")
    37  	}
    38  
    39  	errc := make(chan error, 1)
    40  	go func() {
    41  		if err := exec.Command("umount", point).Run(); err == nil {
    42  			errc <- err
    43  		}
    44  		// retry to unmount with the fallback cmd
    45  		errc <- cmd.Run()
    46  	}()
    47  	select {
    48  	case <-time.After(1 * time.Second):
    49  		return errors.New("umount timeout")
    50  	case err := <-errc:
    51  		return err
    52  	}
    53  }