1// Copyright 2017 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
5package dep
6
7import (
8	"io"
9	"os"
10	"syscall"
11)
12
13// makeUnreadable opens the file at path in exclusive mode. A file opened in
14// exclusive mode cannot be opened again until the exclusive mode file handle
15// is closed.
16func makeUnreadable(path string) (io.Closer, error) {
17	if len(path) == 0 {
18		return nil, syscall.ERROR_FILE_NOT_FOUND
19	}
20	pathp, err := syscall.UTF16PtrFromString(path)
21	if err != nil {
22		return nil, err
23	}
24	access := uint32(syscall.GENERIC_READ | syscall.GENERIC_WRITE)
25	sharemode := uint32(0) // no sharing == exclusive mode
26	sa := (*syscall.SecurityAttributes)(nil)
27	createmode := uint32(syscall.OPEN_EXISTING)
28	h, err := syscall.CreateFile(pathp, access, sharemode, sa, createmode, syscall.FILE_ATTRIBUTE_NORMAL, 0)
29	if err != nil {
30		return nil, err
31	}
32	return os.NewFile(uintptr(h), path), nil
33}
34