github.com/cockroachdb/pebble@v1.1.1-0.20240513155919-3622ade60459/vfs/file_lock_windows.go (about)

     1  // Copyright 2013 The LevelDB-Go and Pebble Authors. All rights reserved. Use
     2  // of this source code is governed by a BSD-style license that can be found in
     3  // the LICENSE file.
     4  
     5  //go:build windows
     6  // +build windows
     7  
     8  package vfs
     9  
    10  import (
    11  	"io"
    12  
    13  	"golang.org/x/sys/windows"
    14  )
    15  
    16  // lockCloser hides all of an windows.Handle's methods, except for Close.
    17  type lockCloser struct {
    18  	fd windows.Handle
    19  }
    20  
    21  func (l lockCloser) Close() error {
    22  	return windows.Close(l.fd)
    23  }
    24  
    25  // Lock locks the given file. On Windows, Locking will fail if the file is
    26  // already open by the current process.
    27  func (defaultFS) Lock(name string) (io.Closer, error) {
    28  	p, err := windows.UTF16PtrFromString(name)
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  	fd, err := windows.CreateFile(p,
    33  		windows.GENERIC_READ|windows.GENERIC_WRITE,
    34  		0, nil, windows.CREATE_ALWAYS,
    35  		windows.FILE_ATTRIBUTE_NORMAL,
    36  		0,
    37  	)
    38  	if err != nil {
    39  		return nil, err
    40  	}
    41  	return lockCloser{fd: fd}, nil
    42  }