1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #include "tensorflow/core/platform/posix/error.h"
17 
18 #include <errno.h>
19 #include <string.h>
20 
21 #include "tensorflow/core/lib/core/status.h"
22 #include "tensorflow/core/lib/strings/strcat.h"
23 
24 namespace tensorflow {
25 
ErrnoToCode(int err_number)26 error::Code ErrnoToCode(int err_number) {
27   error::Code code;
28   switch (err_number) {
29     case 0:
30       code = error::OK;
31       break;
32     case EINVAL:        // Invalid argument
33     case ENAMETOOLONG:  // Filename too long
34     case E2BIG:         // Argument list too long
35     case EDESTADDRREQ:  // Destination address required
36     case EDOM:          // Mathematics argument out of domain of function
37     case EFAULT:        // Bad address
38     case EILSEQ:        // Illegal byte sequence
39     case ENOPROTOOPT:   // Protocol not available
40     case ENOSTR:        // Not a STREAM
41     case ENOTSOCK:      // Not a socket
42     case ENOTTY:        // Inappropriate I/O control operation
43     case EPROTOTYPE:    // Protocol wrong type for socket
44     case ESPIPE:        // Invalid seek
45       code = error::INVALID_ARGUMENT;
46       break;
47     case ETIMEDOUT:  // Connection timed out
48     case ETIME:      // Timer expired
49       code = error::DEADLINE_EXCEEDED;
50       break;
51     case ENODEV:  // No such device
52     case ENOENT:  // No such file or directory
53     case ENXIO:   // No such device or address
54     case ESRCH:   // No such process
55       code = error::NOT_FOUND;
56       break;
57     case EEXIST:         // File exists
58     case EADDRNOTAVAIL:  // Address not available
59     case EALREADY:       // Connection already in progress
60       code = error::ALREADY_EXISTS;
61       break;
62     case EPERM:   // Operation not permitted
63     case EACCES:  // Permission denied
64     case EROFS:   // Read only file system
65       code = error::PERMISSION_DENIED;
66       break;
67     case ENOTEMPTY:   // Directory not empty
68     case EISDIR:      // Is a directory
69     case ENOTDIR:     // Not a directory
70     case EADDRINUSE:  // Address already in use
71     case EBADF:       // Invalid file descriptor
72     case EBUSY:       // Device or resource busy
73     case ECHILD:      // No child processes
74     case EISCONN:     // Socket is connected
75 #if !defined(_WIN32) && !defined(__HAIKU__)
76     case ENOTBLK:  // Block device required
77 #endif
78     case ENOTCONN:  // The socket is not connected
79     case EPIPE:     // Broken pipe
80 #if !defined(_WIN32)
81     case ESHUTDOWN:  // Cannot send after transport endpoint shutdown
82 #endif
83     case ETXTBSY:  // Text file busy
84       code = error::FAILED_PRECONDITION;
85       break;
86     case ENOSPC:  // No space left on device
87 #if !defined(_WIN32)
88     case EDQUOT:  // Disk quota exceeded
89 #endif
90     case EMFILE:   // Too many open files
91     case EMLINK:   // Too many links
92     case ENFILE:   // Too many open files in system
93     case ENOBUFS:  // No buffer space available
94     case ENODATA:  // No message is available on the STREAM read queue
95     case ENOMEM:   // Not enough space
96     case ENOSR:    // No STREAM resources
97 #if !defined(_WIN32) && !defined(__HAIKU__)
98     case EUSERS:  // Too many users
99 #endif
100       code = error::RESOURCE_EXHAUSTED;
101       break;
102     case EFBIG:      // File too large
103     case EOVERFLOW:  // Value too large to be stored in data type
104     case ERANGE:     // Result too large
105       code = error::OUT_OF_RANGE;
106       break;
107     case ENOSYS:        // Function not implemented
108     case ENOTSUP:       // Operation not supported
109     case EAFNOSUPPORT:  // Address family not supported
110 #if !defined(_WIN32)
111     case EPFNOSUPPORT:  // Protocol family not supported
112 #endif
113     case EPROTONOSUPPORT:  // Protocol not supported
114 #if !defined(_WIN32) && !defined(__HAIKU__)
115     case ESOCKTNOSUPPORT:  // Socket type not supported
116 #endif
117     case EXDEV:  // Improper link
118       code = error::UNIMPLEMENTED;
119       break;
120     case EAGAIN:        // Resource temporarily unavailable
121     case ECONNREFUSED:  // Connection refused
122     case ECONNABORTED:  // Connection aborted
123     case ECONNRESET:    // Connection reset
124     case EINTR:         // Interrupted function call
125 #if !defined(_WIN32)
126     case EHOSTDOWN:  // Host is down
127 #endif
128     case EHOSTUNREACH:  // Host is unreachable
129     case ENETDOWN:      // Network is down
130     case ENETRESET:     // Connection aborted by network
131     case ENETUNREACH:   // Network unreachable
132     case ENOLCK:        // No locks available
133     case ENOLINK:       // Link has been severed
134 #if !(defined(__APPLE__) || defined(__FreeBSD__) || defined(_WIN32) || \
135       defined(__HAIKU__))
136     case ENONET:  // Machine is not on the network
137 #endif
138       code = error::UNAVAILABLE;
139       break;
140     case EDEADLK:  // Resource deadlock avoided
141 #if !defined(_WIN32)
142     case ESTALE:  // Stale file handle
143 #endif
144       code = error::ABORTED;
145       break;
146     case ECANCELED:  // Operation cancelled
147       code = error::CANCELLED;
148       break;
149     // NOTE: If you get any of the following (especially in a
150     // reproducible way) and can propose a better mapping,
151     // please email the owners about updating this mapping.
152     case EBADMSG:      // Bad message
153     case EIDRM:        // Identifier removed
154     case EINPROGRESS:  // Operation in progress
155     case EIO:          // I/O error
156     case ELOOP:        // Too many levels of symbolic links
157     case ENOEXEC:      // Exec format error
158     case ENOMSG:       // No message of the desired type
159     case EPROTO:       // Protocol error
160 #if !defined(_WIN32) && !defined(__HAIKU__)
161     case EREMOTE:  // Object is remote
162 #endif
163       code = error::UNKNOWN;
164       break;
165     default: {
166       code = error::UNKNOWN;
167       break;
168     }
169   }
170   return code;
171 }
172 
IOError(const string & context,int err_number)173 Status IOError(const string& context, int err_number) {
174   auto code = ErrnoToCode(err_number);
175   return Status(code, strings::StrCat(context, "; ", strerror(err_number)));
176 }
177 
178 }  // namespace tensorflow
179