Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting errno after a write operation

Tags:

linux

go

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
like image 808
Unknown Avatar asked Feb 14 '15 19:02

Unknown


People also ask

Why am I getting a complete (write) operation failed error?

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.

Is it necessary to declare errno manually in C?

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.

What is the use of errno in Linux?

#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.

What do the error numbers returned by the function errno mean?

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.


1 Answers

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!")
}
like image 162
fuz Avatar answered Sep 20 '22 07:09

fuz