Home > database >  golang: how to check if a write fails due to insufficient disk space
golang: how to check if a write fails due to insufficient disk space

Time:11-16

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.

CodePudding user response:

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.

CodePudding user response:

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
}
  • Related