Skip to content

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

@scrouthtv

Description

@scrouthtv

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions