go4.org@v0.0.0-20230225012048-214862532bf5/lock/lock_plan9.go (about)

     1  /*
     2  Copyright 2013 The Go Authors
     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 lock
    18  
    19  import (
    20  	"fmt"
    21  	"io"
    22  	"os"
    23  )
    24  
    25  func init() {
    26  	lockFn = lockPlan9
    27  }
    28  
    29  func lockPlan9(name string) (io.Closer, error) {
    30  	fi, err := os.Stat(name)
    31  	if err == nil && fi.Size() > 0 {
    32  		return nil, fmt.Errorf("can't Lock file %q: has non-zero size", name)
    33  	}
    34  
    35  	f, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE, os.ModeExclusive|0644)
    36  	if err != nil {
    37  		return nil, fmt.Errorf("Lock Create of %s failed: %v", name, err)
    38  	}
    39  
    40  	return &unlocker{f: f, abs: name}, nil
    41  }