aboutsummaryrefslogtreecommitdiffstats
path: root/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/storage/file_storage_windows.go
blob: 50c3c454e7511bd182843bbb0a48b2bf947c7ac5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2013, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

package storage

import (
    "syscall"
    "unsafe"
)

var (
    modkernel32 = syscall.NewLazyDLL("kernel32.dll")

    procMoveFileExW = modkernel32.NewProc("MoveFileExW")
)

const (
    _MOVEFILE_REPLACE_EXISTING = 1
)

type windowsFileLock struct {
    fd syscall.Handle
}

func (fl *windowsFileLock) release() error {
    return syscall.Close(fl.fd)
}

func newFileLock(path string) (fl fileLock, err error) {
    pathp, err := syscall.UTF16PtrFromString(path)
    if err != nil {
        return
    }
    fd, err := syscall.CreateFile(pathp, syscall.GENERIC_READ|syscall.GENERIC_WRITE, 0, nil, syscall.CREATE_ALWAYS, syscall.FILE_ATTRIBUTE_NORMAL, 0)
    if err != nil {
        return
    }
    fl = &windowsFileLock{fd: fd}
    return
}

func moveFileEx(from *uint16, to *uint16, flags uint32) error {
    r1, _, e1 := syscall.Syscall(procMoveFileExW.Addr(), 3, uintptr(unsafe.Pointer(from)), uintptr(unsafe.Pointer(to)), uintptr(flags))
    if r1 == 0 {
        if e1 != 0 {
            return error(e1)
        } else {
            return syscall.EINVAL
        }
    }
    return nil
}

func rename(oldpath, newpath string) error {
    from, err := syscall.UTF16PtrFromString(oldpath)
    if err != nil {
        return err
    }
    to, err := syscall.UTF16PtrFromString(newpath)
    if err != nil {
        return err
    }
    return moveFileEx(from, to, _MOVEFILE_REPLACE_EXISTING)
}

func syncDir(name string) error { return nil }