Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/go/internal/lockedfile/lockedfile_plan9.go

Documentation: cmd/go/internal/lockedfile

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

View as plain text