github.com/searKing/golang/go@v1.2.117/sync/filelock/lockerfile_filelock_plan9.go (about)

     1  // Copyright 2018 The Go Authors. 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  //go:build plan9
     6  
     7  // Copied from go/gc/src/cmd/go/internal/lockedfile/lockedfile_filelock_plan9.go
     8  
     9  package filelock
    10  
    11  import (
    12  	"io"
    13  	"io/fs"
    14  	"math/rand"
    15  	"os"
    16  	"strings"
    17  	"time"
    18  )
    19  
    20  // Opening an exclusive-use file returns an error.
    21  // The expected error strings are:
    22  //
    23  //   - "open/create -- file is locked" (cwfs, kfs)
    24  //   - "exclusive lock" (fossil)
    25  //   - "exclusive use file already open" (ramfs)
    26  var lockedErrStrings = [...]string{
    27  	"file is locked",
    28  	"exclusive lock",
    29  	"exclusive use file already open",
    30  }
    31  
    32  // Even though plan9 doesn't support the Lock/RLock/Unlock functions to
    33  // manipulate already-open files, IsLocked is still meaningful: os.OpenFile
    34  // itself may return errors that indicate that a file with the ModeExclusive bit
    35  // set is already open.
    36  func isLocked(err error) bool {
    37  	s := err.Error()
    38  
    39  	for _, frag := range lockedErrStrings {
    40  		if strings.Contains(s, frag) {
    41  			return true
    42  		}
    43  	}
    44  
    45  	return false
    46  }
    47  
    48  func openFile(name string, flag int, perm fs.FileMode) (*os.File, error) {
    49  	// Plan 9 uses a mode bit instead of explicit lock/unlock syscalls.
    50  	//
    51  	// Per http://man.cat-v.org/plan_9/5/stat: “Exclusive use files may be open
    52  	// for I/O by only one fid at a time across all clients of the server. If a
    53  	// second open is attempted, it draws an error.”
    54  	//
    55  	// So we can try to open a locked file, but if it fails we're on our own to
    56  	// figure out when it becomes available. We'll use exponential backoff with
    57  	// some jitter and an arbitrary limit of 500ms.
    58  
    59  	// If the file was unpacked or created by some other program, it might not
    60  	// have the ModeExclusive bit set. Set it before we call OpenFile, so that we
    61  	// can be confident that a successful OpenFile implies exclusive use.
    62  	if fi, err := os.Stat(name); err == nil {
    63  		if fi.Mode()&fs.ModeExclusive == 0 {
    64  			if err := os.Chmod(name, fi.Mode()|fs.ModeExclusive); err != nil {
    65  				return nil, err
    66  			}
    67  		}
    68  	} else if !os.IsNotExist(err) {
    69  		return nil, err
    70  	}
    71  
    72  	nextSleep := 1 * time.Millisecond
    73  	const maxSleep = 500 * time.Millisecond
    74  	for {
    75  		f, err := os.OpenFile(name, flag, perm|fs.ModeExclusive)
    76  		if err == nil {
    77  			return f, nil
    78  		}
    79  
    80  		if !isLocked(err) {
    81  			return nil, err
    82  		}
    83  
    84  		time.Sleep(nextSleep)
    85  
    86  		nextSleep += nextSleep
    87  		if nextSleep > maxSleep {
    88  			nextSleep = maxSleep
    89  		}
    90  		// Apply 10% jitter to avoid synchronizing collisions.
    91  		nextSleep += time.Duration((0.1*rand.Float64() - 0.05) * float64(nextSleep))
    92  	}
    93  }
    94  
    95  func closeFile(f File) error {
    96  	if f, ok := f.(io.Closer); ok {
    97  		return f.Close()
    98  	}
    99  	return nil
   100  }