I have the following Go code which will eventually fill the disk and fail with ENOSPC
(just a proof of concept). How can I determine from the err
returned by os.Write
that it indeed failed because of ENOSPC
(so I need a way to grab errno
after the write operation) ?
package main
import (
"log"
"os"
)
func main() {
fd, _ := os.Create("dump.txt")
defer fd.Close()
for {
buf := make([]byte, 1024)
_, err := fd.Write(buf)
if err != nil {
log.Fatalf("%T %v", err, err)
}
}
}
EDIT: Updated the program as @FUZxxl suggested:
package main
import (
"log"
"os"
"syscall"
)
func main() {
fd, _ := os.Create("dump.txt")
defer fd.Close()
for {
buf := make([]byte, 1024)
_, err := fd.Write(buf)
if err != nil {
log.Printf("%T %v\n", err, err)
errno, ok := err.(syscall.Errno)
if ok {
log.Println("type assert ok")
if errno == syscall.ENOSPC {
log.Println("got ENOSPC")
}
} else {
log.Println("type assert not ok")
}
break
}
}
}
However, I'm not getting the expected result. Here is the output:
2015/02/15 10:13:27 *os.PathError write dump.txt: no space left on device
2015/02/15 10:13:27 type assert not ok
Although it is unlikely that an issue with any of the hardware you are using is causing the "complete (write) operation failed" error, it is possible. If there is a slight disturbance of connection with Odin from your mobile device, the flashing process will likely fail. This can be caused by two main factors.
On some ancient systems, <errno.h> was not present or did not declare errno, so that it was necessary to declare errno manually (i.e., extern int errno ). Do not do this. It long ago ceased to be necessary, and it will cause problems with modern versions of the C library. This page is part of release 5.13 of the Linux man-pages project.
#include <errno.h> DESCRIPTION top The <errno.h> header file defines the integer variable errno, which is set by system calls and some library functions in the event of an error to indicate what went wrong.
Instead, on failure they return an error number as the function result. These error numbers have the same meanings as the error numbers returned in errno by other APIs. On some ancient systems, <errno.h> was not present or did not declare errno, so that it was necessary to declare errno manually (i.e., extern int errno ). Do not do this.
File operations generally return an *os.PathError
; cast err
to os.PathError
and use the Err
field to examine the underlying cause, like this:
patherr, ok := err.(*os.PathError)
if ok && patherr.Err == syscall.ENOSPC {
log.Println("Out of disk space!")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With