OLD | NEW |
| (Empty) |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "mojo/shell/domain_socket/net_errors.h" | |
6 | |
7 #include <errno.h> | |
8 #include <stdlib.h> | |
9 #if defined(OS_POSIX) | |
10 #include <unistd.h> | |
11 #endif | |
12 #if defined(OS_WIN) | |
13 #include <winsock2.h> | |
14 #define EDQUOT WSAEDQUOT | |
15 #define EHOSTDOWN WSAEHOSTDOWN | |
16 #define EUSERS WSAEUSERS | |
17 #endif | |
18 | |
19 #include <string> | |
20 | |
21 #include "base/basictypes.h" | |
22 #include "base/files/file.h" | |
23 #include "base/logging.h" | |
24 | |
25 namespace mojo { | |
26 namespace shell { | |
27 namespace net { | |
28 | |
29 std::string ErrorToString(int error) { | |
30 if (error == 0) | |
31 return "OK"; | |
32 | |
33 const char* error_string; | |
34 switch (error) { | |
35 #define NET_ERROR(label, value) \ | |
36 case ERR_ ## label: \ | |
37 error_string = # label; \ | |
38 break; | |
39 #include "mojo/shell/domain_socket/net_error_list.h" | |
40 #undef NET_ERROR | |
41 default: | |
42 NOTREACHED(); | |
43 error_string = "<unknown>"; | |
44 } | |
45 return std::string("ERR_") + error_string; | |
46 } | |
47 | |
48 Error FileErrorToNetError(base::File::Error file_error) { | |
49 switch (file_error) { | |
50 case base::File::FILE_OK: | |
51 return net::OK; | |
52 case base::File::FILE_ERROR_ACCESS_DENIED: | |
53 return net::ERR_ACCESS_DENIED; | |
54 case base::File::FILE_ERROR_NOT_FOUND: | |
55 return net::ERR_FILE_NOT_FOUND; | |
56 default: | |
57 return net::ERR_FAILED; | |
58 } | |
59 } | |
60 | |
61 Error MapSystemError(int os_error) { | |
62 if (os_error != 0) | |
63 DVLOG(2) << "Error " << os_error; | |
64 | |
65 // There are numerous posix error codes, but these are the ones we thus far | |
66 // find interesting. | |
67 switch (os_error) { | |
68 case EAGAIN: | |
69 #if EWOULDBLOCK != EAGAIN | |
70 case EWOULDBLOCK: | |
71 #endif | |
72 return ERR_IO_PENDING; | |
73 case EACCES: | |
74 return ERR_ACCESS_DENIED; | |
75 case ENETDOWN: | |
76 return ERR_INTERNET_DISCONNECTED; | |
77 case ETIMEDOUT: | |
78 return ERR_TIMED_OUT; | |
79 case ECONNRESET: | |
80 case ENETRESET: // Related to keep-alive. | |
81 case EPIPE: | |
82 return ERR_CONNECTION_RESET; | |
83 case ECONNABORTED: | |
84 return ERR_CONNECTION_ABORTED; | |
85 case ECONNREFUSED: | |
86 return ERR_CONNECTION_REFUSED; | |
87 case EHOSTUNREACH: | |
88 case EHOSTDOWN: | |
89 case ENETUNREACH: | |
90 case EAFNOSUPPORT: | |
91 return ERR_ADDRESS_UNREACHABLE; | |
92 case EADDRNOTAVAIL: | |
93 return ERR_ADDRESS_INVALID; | |
94 case EMSGSIZE: | |
95 return ERR_MSG_TOO_BIG; | |
96 case ENOTCONN: | |
97 return ERR_SOCKET_NOT_CONNECTED; | |
98 case EISCONN: | |
99 return ERR_SOCKET_IS_CONNECTED; | |
100 case EINVAL: | |
101 return ERR_INVALID_ARGUMENT; | |
102 case EADDRINUSE: | |
103 return ERR_ADDRESS_IN_USE; | |
104 case E2BIG: // Argument list too long. | |
105 return ERR_INVALID_ARGUMENT; | |
106 case EBADF: // Bad file descriptor. | |
107 return ERR_INVALID_HANDLE; | |
108 case EBUSY: // Device or resource busy. | |
109 return ERR_INSUFFICIENT_RESOURCES; | |
110 case ECANCELED: // Operation canceled. | |
111 return ERR_ABORTED; | |
112 case EDEADLK: // Resource deadlock avoided. | |
113 return ERR_INSUFFICIENT_RESOURCES; | |
114 case EDQUOT: // Disk quota exceeded. | |
115 return ERR_FILE_NO_SPACE; | |
116 case EEXIST: // File exists. | |
117 return ERR_FILE_EXISTS; | |
118 case EFAULT: // Bad address. | |
119 return ERR_INVALID_ARGUMENT; | |
120 case EFBIG: // File too large. | |
121 return ERR_FILE_TOO_BIG; | |
122 case EISDIR: // Operation not allowed for a directory. | |
123 return ERR_ACCESS_DENIED; | |
124 case ENAMETOOLONG: // Filename too long. | |
125 return ERR_FILE_PATH_TOO_LONG; | |
126 case ENFILE: // Too many open files in system. | |
127 return ERR_INSUFFICIENT_RESOURCES; | |
128 case ENOBUFS: // No buffer space available. | |
129 return ERR_OUT_OF_MEMORY; | |
130 case ENODEV: // No such device. | |
131 return ERR_INVALID_ARGUMENT; | |
132 case ENOENT: // No such file or directory. | |
133 return ERR_FILE_NOT_FOUND; | |
134 case ENOLCK: // No locks available. | |
135 return ERR_INSUFFICIENT_RESOURCES; | |
136 case ENOMEM: // Not enough space. | |
137 return ERR_OUT_OF_MEMORY; | |
138 case ENOSPC: // No space left on device. | |
139 return ERR_FILE_NO_SPACE; | |
140 case ENOSYS: // Function not implemented. | |
141 return ERR_NOT_IMPLEMENTED; | |
142 case ENOTDIR: // Not a directory. | |
143 return ERR_FILE_NOT_FOUND; | |
144 case ENOTSUP: // Operation not supported. | |
145 return ERR_NOT_IMPLEMENTED; | |
146 case EPERM: // Operation not permitted. | |
147 return ERR_ACCESS_DENIED; | |
148 case EROFS: // Read-only file system. | |
149 return ERR_ACCESS_DENIED; | |
150 case ETXTBSY: // Text file busy. | |
151 return ERR_ACCESS_DENIED; | |
152 case EUSERS: // Too many users. | |
153 return ERR_INSUFFICIENT_RESOURCES; | |
154 case EMFILE: // Too many open files. | |
155 return ERR_INSUFFICIENT_RESOURCES; | |
156 | |
157 case 0: | |
158 return OK; | |
159 default: | |
160 LOG(WARNING) << "Unknown error " << os_error | |
161 << " mapped to net::ERR_FAILED"; | |
162 return ERR_FAILED; | |
163 } | |
164 } | |
165 | |
166 } // namespace net | |
167 } // namespace shell | |
168 } // namespace mojo | |
OLD | NEW |