Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/sys/windows: GetDiskFreeSpaceEx can't find any path #47172

Closed
scrouthtv opened this issue Jul 13, 2021 · 1 comment
Closed

x/sys/windows: GetDiskFreeSpaceEx can't find any path #47172

scrouthtv opened this issue Jul 13, 2021 · 1 comment

Comments

@scrouthtv
Copy link

What version of Go are you using (go version)?

$ go version
go version go1.16.6 windows/amd64

Does this issue reproduce with the latest release?

yes.

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
set GO111MODULE=
set GOARCH=amd64
set GOBIN=
set GOCACHE=C:\Users\lenni\AppData\Local\go-build
set GOENV=C:\Users\lenni\AppData\Roaming\go\env
set GOEXE=.exe
set GOFLAGS=
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GOMODCACHE=C:\Users\lenni\go\pkg\mod
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=C:\Users\lenni\go
set GOPRIVATE=
set GOPROXY=https://proxy.golang.org,direct
set GOROOT=C:\Users\lenni\scoop\apps\go\current
set GOSUMDB=sum.golang.org
set GOTMPDIR=
set GOTOOLDIR=C:\Users\lenni\scoop\apps\go\current\pkg\tool\windows_amd64
set GOVCS=
set GOVERSION=go1.16.6
set GCCGO=gccgo
set AR=ar
set CC=gcc
set CXX=g++
set CGO_ENABLED=1
set GOMOD=D:\tmp\drive\go.mod
set CGO_CFLAGS=-g -O2
set CGO_CPPFLAGS=
set CGO_CXXFLAGS=-g -O2
set CGO_FFLAGS=-g -O2
set CGO_LDFLAGS=-g -O2
set PKG_CONFIG=pkg-config
set GOGCCFLAGS=-m64 -mthreads -fmessage-length=0 -fdebug-prefix-map=C:\Users\lenni\AppData\Local\Temp\go-build1962934238=/tmp/go-build -gno-record-gcc-switches

What did you do?

I'm trying to get the disk usage under Windows using Go. This is my code:

package main

import (
	"fmt"
	"unsafe"

	"golang.org/x/sys/windows"
)

func main() {
	p := "C:\\tmp\\"
	var caller, tot, free uint64 = 0, 0, 0

	ptr := (*uint16)(unsafe.Pointer(&p))
	err := windows.GetDiskFreeSpaceEx(ptr, &caller, &tot, &free)

	fmt.Println(err)
	fmt.Println(free, caller, tot)
}

For this to work, the folder C:\tmp\ must exist and be user-readable.

What did you expect to see?

I expected some output along the lines of

nil
1951571968 1951571968 2752507904

because that's how much free space I have on my C: drive.

What did you see instead?

The system cannot find the path specified.

The same program but in C works:

#include <fileapi.h>
#include <errhandlingapi.h>
#include <stdio.h>

int main() {
	LPCWSTR path = L"C:\\tmp\\";
	ULARGE_INTEGER caller = { 0 }, total = { 0 }, free = { 0 };

	BOOL ok = GetDiskFreeSpaceExW(path, &caller, &total, &free);

	if (ok) {
		printf("done: %s has %lu b/%lu b\n", ok ? "ok" : "error", caller, total);
	} else {
		DWORD err = GetLastError();
		printf("error: %d reading %ls\n", err, path);
	}
}

(note: the C program would fail if the folder is specified without a trailing path separator)

Thanks in advance.

@bcmills
Copy link
Contributor

bcmills commented Jul 13, 2021

The functions in x/sys/windows are all the W variants (UTF-16), not the A variants (ASCII).

This line:

	ptr := (*uint16)(unsafe.Pointer(&p))

forcibly casts a pointer to a Go string header (a struct containing a pointer and length) to a *uint16, but that does not encode the string. (A Go *string is not a C LPCWSTR.)

You need to call windows.UTF16PtrFromString instead of using an unsafe.Pointer conversion.

@bcmills bcmills closed this as completed Jul 13, 2021
@bcmills bcmills changed the title [x/sys/windows] GetDiskFreeSpaceEx can't find any path x/sys/windows: GetDiskFreeSpaceEx can't find any path Jul 13, 2021
@golang golang locked and limited conversation to collaborators Jul 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants