Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Canread
Gopsutil
提交
4f591291
G
Gopsutil
项目概览
Canread
/
Gopsutil
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
Gopsutil
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
4f591291
编写于
4月 20, 2014
作者:
W
WAKAYAMA Shirou
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
implements Disk_partition on windows
上级
8204d224
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
92 addition
and
4 deletion
+92
-4
disk.go
disk.go
+7
-0
disk_test.go
disk_test.go
+11
-0
disk_windows.go
disk_windows.go
+74
-4
未找到文件。
disk.go
浏览文件 @
4f591291
...
...
@@ -10,6 +10,13 @@ type Disk_usage struct {
UsedPercent
float64
`json:"usedPercent"`
}
type
Disk_partition
struct
{
Device
string
`json:"device"`
Mountpoint
string
`json:"mountpoint"`
Fstype
string
`json:"fstype"`
Opts
string
`json:"opts"`
}
type
Disk_IO_Counters
struct
{
ReadCount
uint64
`json:"readCount"`
WriteCount
uint64
`json:"writeCount"`
...
...
disk_test.go
浏览文件 @
4f591291
...
...
@@ -21,3 +21,14 @@ func TestDisk_usage(t *testing.T) {
d
,
_
:=
json
.
Marshal
(
v
)
fmt
.
Printf
(
"%s
\n
"
,
d
)
}
func
TestDisk_partitions
(
t
*
testing
.
T
)
{
disk
:=
NewDisk
()
v
,
err
:=
disk
.
Disk_partitions
()
if
err
!=
nil
{
t
.
Errorf
(
"error %v"
,
err
)
}
d
,
_
:=
json
.
Marshal
(
v
)
fmt
.
Printf
(
"%s
\n
"
,
d
)
}
disk_windows.go
浏览文件 @
4f591291
...
...
@@ -3,13 +3,21 @@
package
main
import
(
"bytes"
"syscall"
"unsafe"
)
var
(
procGetDiskFreeSpaceExW
=
modkernel32
.
NewProc
(
"GetDiskFreeSpaceExW"
)
//GetLogicalDriveStrings, _ = syscall.GetProcAddress(modkernel32, "GetLogicalDriveStringsW")
procGetDiskFreeSpaceExW
=
modkernel32
.
NewProc
(
"GetDiskFreeSpaceExW"
)
procGetLogicalDriveStringsW
=
modkernel32
.
NewProc
(
"GetLogicalDriveStringsW"
)
procGetDriveType
=
modkernel32
.
NewProc
(
"GetDriveTypeW"
)
provGetVolumeInformation
=
modKernel32
.
NewProc
(
"GetVolumeInformationW"
)
)
var
(
FILE_FILE_COMPRESSION
=
int64
(
16
)
// 0x00000010
FILE_READ_ONLY_VOLUME
=
int64
(
524288
)
// 0x00080000
)
func
(
d
Disk
)
Disk_usage
(
path
string
)
(
Disk_usage
,
error
)
{
...
...
@@ -19,13 +27,13 @@ func (d Disk) Disk_usage(path string) (Disk_usage, error) {
lpFreeBytesAvailable
:=
int64
(
0
)
lpTotalNumberOfBytes
:=
int64
(
0
)
lpTotalNumberOfFreeBytes
:=
int64
(
0
)
diskret
,
_
,
_
:=
procGetDiskFreeSpaceExW
.
Call
(
diskret
,
_
,
err
:=
procGetDiskFreeSpaceExW
.
Call
(
uintptr
(
unsafe
.
Pointer
(
syscall
.
StringToUTF16Ptr
(
path
))),
uintptr
(
unsafe
.
Pointer
(
&
lpFreeBytesAvailable
)),
uintptr
(
unsafe
.
Pointer
(
&
lpTotalNumberOfBytes
)),
uintptr
(
unsafe
.
Pointer
(
&
lpTotalNumberOfFreeBytes
)))
if
diskret
==
0
{
return
ret
,
syscall
.
GetLastError
()
return
ret
,
err
}
ret
.
Total
=
uint64
(
lpTotalNumberOfBytes
)
// ret.Free = uint64(lpFreeBytesAvailable) // python psutil does not use this
...
...
@@ -35,3 +43,65 @@ func (d Disk) Disk_usage(path string) (Disk_usage, error) {
return
ret
,
nil
}
func
(
d
Disk
)
Disk_partitions
()
([]
Disk_partition
,
error
)
{
ret
:=
make
([]
Disk_partition
,
0
)
lpBuffer
:=
make
([]
byte
,
254
)
diskret
,
_
,
err
:=
procGetLogicalDriveStringsW
.
Call
(
uintptr
(
len
(
lpBuffer
)),
uintptr
(
unsafe
.
Pointer
(
&
lpBuffer
[
0
])))
if
diskret
==
0
{
return
ret
,
err
}
for
_
,
v
:=
range
lpBuffer
{
if
v
>=
65
&&
v
<=
90
{
path
:=
string
(
v
)
+
":"
if
path
==
"A:"
||
path
==
"B:"
{
// skip floppy drives
continue
}
typepath
,
_
:=
syscall
.
UTF16PtrFromString
(
path
)
typeret
,
_
,
_
:=
procGetDriveType
.
Call
(
uintptr
(
unsafe
.
Pointer
(
typepath
)))
if
typeret
==
0
{
return
ret
,
syscall
.
GetLastError
()
}
// 2: DRIVE_REMOVABLE 3: DRIVE_FIXED 5: DRIVE_CDROM
if
typeret
==
2
||
typeret
==
3
||
typeret
==
5
{
lpVolumeNameBuffer
:=
make
([]
byte
,
256
)
lpVolumeSerialNumber
:=
int64
(
0
)
lpMaximumComponentLength
:=
int64
(
0
)
lpFileSystemFlags
:=
int64
(
0
)
lpFileSystemNameBuffer
:=
make
([]
byte
,
256
)
volpath
,
_
:=
syscall
.
UTF16PtrFromString
(
string
(
v
)
+
":/"
)
driveret
,
_
,
err
:=
provGetVolumeInformation
.
Call
(
uintptr
(
unsafe
.
Pointer
(
volpath
)),
uintptr
(
unsafe
.
Pointer
(
&
lpVolumeNameBuffer
[
0
])),
uintptr
(
len
(
lpVolumeNameBuffer
)),
uintptr
(
unsafe
.
Pointer
(
&
lpVolumeSerialNumber
)),
uintptr
(
unsafe
.
Pointer
(
&
lpMaximumComponentLength
)),
uintptr
(
unsafe
.
Pointer
(
&
lpFileSystemFlags
)),
uintptr
(
unsafe
.
Pointer
(
&
lpFileSystemNameBuffer
[
0
])),
uintptr
(
len
(
lpFileSystemNameBuffer
)))
if
driveret
==
0
{
return
ret
,
err
}
opts
:=
"rw"
if
lpFileSystemFlags
&
FILE_READ_ONLY_VOLUME
!=
0
{
opts
=
"ro"
}
if
lpFileSystemFlags
&
FILE_FILE_COMPRESSION
!=
0
{
opts
+=
".compress"
}
d
:=
Disk_partition
{
Mountpoint
:
path
,
Device
:
path
,
Fstype
:
string
(
bytes
.
Replace
(
lpFileSystemNameBuffer
,
[]
byte
(
"
\x00
"
),
[]
byte
(
""
),
-
1
)),
Opts
:
opts
,
}
ret
=
append
(
ret
,
d
)
}
}
}
return
ret
,
nil
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录