-
Notifications
You must be signed in to change notification settings - Fork 25
/
fileop.go
106 lines (95 loc) · 3.09 KB
/
fileop.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
* Copyright (c) 2014-2017 MongoDB, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the license is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gowin32
import (
"github.com/winlabs/gowin32/wrappers"
"syscall"
)
func Copy(oldFileName string, newFileName string, overwrite bool) error {
err := wrappers.CopyFile(
syscall.StringToUTF16Ptr(oldFileName),
syscall.StringToUTF16Ptr(newFileName),
!overwrite)
if err != nil {
return NewWindowsError("CopyFile", err)
}
return nil
}
func Delete(fileName string) error {
if err := wrappers.DeleteFile(syscall.StringToUTF16Ptr(fileName)); err != nil {
return NewWindowsError("DeleteFile", err)
}
return nil
}
func FileExists(fileName string) (bool, error) {
var wfd wrappers.WIN32_FIND_DATA
handle, err := wrappers.FindFirstFile(syscall.StringToUTF16Ptr(fileName), &wfd)
if err == wrappers.ERROR_FILE_NOT_FOUND {
return false, nil
} else if err != nil {
return false, NewWindowsError("FindFirstFile", err)
}
wrappers.FindClose(handle)
return true, nil
}
func GetAttributes(fileName string) (FileAttributes, error) {
attributes, err := wrappers.GetFileAttributes(syscall.StringToUTF16Ptr(fileName))
if err != nil {
return 0, NewWindowsError("GetFileAttributes", err)
}
return FileAttributes(attributes), err
}
func GetCompressedSize(fileName string) (uint64, error) {
var fileSizeHigh uint32
fileSizeLow, err := wrappers.GetCompressedFileSize(syscall.StringToUTF16Ptr(fileName), &fileSizeHigh)
if err != nil {
return 0, NewWindowsError("GetCompressedFileSize", err)
}
return (uint64(fileSizeHigh) << 32) | uint64(fileSizeLow), nil
}
func GetVolumePath(fileName string) (string, error) {
buf := make([]uint16, wrappers.MAX_PATH)
if err := wrappers.GetVolumePathName(syscall.StringToUTF16Ptr(fileName), &buf[0], wrappers.MAX_PATH); err != nil {
return "", NewWindowsError("GetVolumePathName", err)
}
return syscall.UTF16ToString(buf), nil
}
func Move(oldFileName string, newFileName string, overwrite bool) error {
if overwrite {
err := wrappers.MoveFileEx(
syscall.StringToUTF16Ptr(oldFileName),
syscall.StringToUTF16Ptr(newFileName),
wrappers.MOVEFILE_REPLACE_EXISTING)
if err != nil {
return NewWindowsError("MoveFileEx", err)
}
} else {
err := wrappers.MoveFile(
syscall.StringToUTF16Ptr(oldFileName),
syscall.StringToUTF16Ptr(newFileName))
if err != nil {
return NewWindowsError("MoveFile", err)
}
}
return nil
}
func SetAttributes(fileName string, attributes FileAttributes) error {
err := wrappers.SetFileAttributes(syscall.StringToUTF16Ptr(fileName), uint32(attributes))
if err != nil {
return NewWindowsError("SetFileAttributes", err)
}
return nil
}