-
Notifications
You must be signed in to change notification settings - Fork 26
/
volume.go
117 lines (103 loc) · 4.54 KB
/
volume.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
107
108
109
110
111
112
113
114
115
116
117
/*
* 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"
)
type FileSystemFlags uint32
const (
FileSystemCaseSensitiveSearch FileSystemFlags = wrappers.FILE_CASE_SENSITIVE_SEARCH
FileSystemCasePreservedNames FileSystemFlags = wrappers.FILE_CASE_PRESERVED_NAMES
FileSystemUnicodeOnDisk FileSystemFlags = wrappers.FILE_UNICODE_ON_DISK
FileSystemPersistentACLs FileSystemFlags = wrappers.FILE_PERSISTENT_ACLS
FileSystemFileCompression FileSystemFlags = wrappers.FILE_FILE_COMPRESSION
FileSystemVolumeQuotas FileSystemFlags = wrappers.FILE_VOLUME_QUOTAS
FileSystemSupportsSparseFiles FileSystemFlags = wrappers.FILE_SUPPORTS_SPARSE_FILES
FileSystemSupportsReparsePoints FileSystemFlags = wrappers.FILE_SUPPORTS_REPARSE_POINTS
FileSystemSupportsRemoteStorage FileSystemFlags = wrappers.FILE_SUPPORTS_REMOTE_STORAGE
FileSystemVolumeIsCompressed FileSystemFlags = wrappers.FILE_VOLUME_IS_COMPRESSED
FileSystemSupportsObjectIDs FileSystemFlags = wrappers.FILE_SUPPORTS_OBJECT_IDS
FileSystemSupportsEncryption FileSystemFlags = wrappers.FILE_SUPPORTS_ENCRYPTION
FileSystemNamedStreams FileSystemFlags = wrappers.FILE_NAMED_STREAMS
FileSystemReadOnlyVolume FileSystemFlags = wrappers.FILE_READ_ONLY_VOLUME
FileSystemSequentialWriteOnce FileSystemFlags = wrappers.FILE_SEQUENTIAL_WRITE_ONCE
FileSystemSupportsTransactions FileSystemFlags = wrappers.FILE_SUPPORTS_TRANSACTIONS
FileSystemSupportsHardLinks FileSystemFlags = wrappers.FILE_SUPPORTS_HARD_LINKS
FileSystemSupportsExtendedAttributes FileSystemFlags = wrappers.FILE_SUPPORTS_EXTENDED_ATTRIBUTES
FileSystemSupportsOpenByFileID FileSystemFlags = wrappers.FILE_SUPPORTS_OPEN_BY_FILE_ID
FileSystemSupportsUSNJournal FileSystemFlags = wrappers.FILE_SUPPORTS_USN_JOURNAL
)
type VolumeInfo struct {
VolumeName string
VolumeSerialNumber uint
MaximumComponentLength uint
FileSystemFlags FileSystemFlags
FileSystemName string
}
type DriveType uint32
const (
DriveTypeUnknown DriveType = wrappers.DRIVE_UNKNOWN
DriveNoRootDir DriveType = wrappers.DRIVE_NO_ROOT_DIR
DriveRemovable DriveType = wrappers.DRIVE_REMOVABLE
DriveFixed DriveType = wrappers.DRIVE_FIXED
DriveRemote DriveType = wrappers.DRIVE_REMOTE
DriveCDROM DriveType = wrappers.DRIVE_CDROM
DriveRAMDisk DriveType = wrappers.DRIVE_RAMDISK
)
func GetVolumeInfo(rootPathName string) (*VolumeInfo, error) {
var volumeSerialNumber uint32
var maximumComponentLength uint32
var fileSystemFlags uint32
volumeNameBuffer := make([]uint16, syscall.MAX_PATH+1)
fileSystemNameBuffer := make([]uint16, syscall.MAX_PATH+1)
err := wrappers.GetVolumeInformation(
syscall.StringToUTF16Ptr(rootPathName),
&volumeNameBuffer[0],
syscall.MAX_PATH+1,
&volumeSerialNumber,
&maximumComponentLength,
&fileSystemFlags,
&fileSystemNameBuffer[0],
syscall.MAX_PATH+1)
if err != nil {
return nil, NewWindowsError("GetVolumeInformation", err)
}
return &VolumeInfo{
VolumeName: syscall.UTF16ToString(volumeNameBuffer),
VolumeSerialNumber: uint(volumeSerialNumber),
MaximumComponentLength: uint(maximumComponentLength),
FileSystemFlags: FileSystemFlags(fileSystemFlags),
FileSystemName: syscall.UTF16ToString(fileSystemNameBuffer),
}, nil
}
func GetVolumeDriveType(rootPathName string) DriveType {
return DriveType(wrappers.GetDriveType(syscall.StringToUTF16Ptr(rootPathName)))
}
const (
MaximumVolumeGUIDPath = 50
)
func GetVolumeNameFromMountPoint(volumeMountPoint string) (string, error) {
volumeName := make([]uint16, MaximumVolumeGUIDPath)
err := wrappers.GetVolumeNameForVolumeMountPoint(
syscall.StringToUTF16Ptr(volumeMountPoint),
&volumeName[0],
MaximumVolumeGUIDPath)
if err != nil {
return "", NewWindowsError("GetVolumeNameForVolumeMountPoint", err)
}
return syscall.UTF16ToString(volumeName), nil
}