2

If a go program return error with Write(), it returns something like this:

write failed(*fs.PathError): write mnt/test.dat: no space left on device

Is there anyway other than string matching for 'no space left on device', to know that a PathError is due to insufficient disk space?

Note: I am using Linux, and not caring about how it works on Windows.

xrfang
  • 1,156
  • 1
  • 9
  • 24

2 Answers2

1

Yes, it may be sensitive to the OS you compile for but you can check if the error wraps syscall.ENOSPC.

_, err := file.Write(stuff)
if err != nil {
    if errors.Is(err, syscall.ENOSPC) {
        // There was no space left on the device
        return "get more space"
    }
    // something else is wrong
}
Bracken
  • 840
  • 10
  • 21
0

Is there anyway other than string matching for 'no space left on device', to know that a PathError is due to insufficient disk space?

No.

Volker
  • 36,165
  • 6
  • 76
  • 80
  • The accepted answer works on linux! – xrfang Nov 16 '21 at 01:16
  • 2
    @xrfang Package syscall is not covered by the Go compatibility promise. If this type of hack is good enough for you now and in the future this is fine. – Volker Nov 16 '21 at 06:47