Josh Gao | d6d5c38 | 2016-11-21 16:06:43 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "sysdeps/errno.h" |
| 18 | |
| 19 | #include <errno.h> |
| 20 | |
| 21 | #include <thread> |
| 22 | #include <unordered_map> |
| 23 | |
| 24 | #include "adb.h" |
| 25 | |
| 26 | #if defined(_WIN32) |
| 27 | #define ETXTBSY EBUSY |
| 28 | #endif |
| 29 | |
| 30 | static std::unordered_map<int, int> initialize_translations() { |
| 31 | std::unordered_map<int, int> translations; |
| 32 | #if defined(__linux__) |
| 33 | #define ERRNO_VALUE(error_name, linux_value) static_assert((error_name) == (linux_value), "") |
| 34 | #else |
| 35 | #define ERRNO_VALUE(error_name, linux_value) \ |
| 36 | translations.insert(std::make_pair((linux_value), (error_name))) |
| 37 | #endif |
| 38 | // Untranslated errno values returned by open: EDQUOT, ENODEV, ENXIO, EWOULDBLOCK |
| 39 | ERRNO_VALUE(EACCES, 13); |
| 40 | ERRNO_VALUE(EEXIST, 17); |
| 41 | ERRNO_VALUE(EFAULT, 14); |
| 42 | ERRNO_VALUE(EFBIG, 27); |
| 43 | ERRNO_VALUE(EINTR, 4); |
| 44 | ERRNO_VALUE(EINVAL, 22); |
| 45 | ERRNO_VALUE(EIO, 5); |
| 46 | ERRNO_VALUE(EISDIR, 21); |
| 47 | ERRNO_VALUE(ELOOP, 40); |
| 48 | ERRNO_VALUE(EMFILE, 24); |
| 49 | ERRNO_VALUE(ENAMETOOLONG, 36); |
| 50 | ERRNO_VALUE(ENFILE, 23); |
| 51 | ERRNO_VALUE(ENOENT, 2); |
| 52 | ERRNO_VALUE(ENOMEM, 12); |
| 53 | ERRNO_VALUE(ENOSPC, 28); |
| 54 | ERRNO_VALUE(ENOTDIR, 20); |
| 55 | ERRNO_VALUE(EOVERFLOW, 75); |
| 56 | ERRNO_VALUE(EPERM, 1); |
| 57 | ERRNO_VALUE(EROFS, 30); |
| 58 | ERRNO_VALUE(ETXTBSY, 26); |
| 59 | return translations; |
| 60 | } |
| 61 | |
| 62 | int translate_linux_errno(int error) { |
| 63 | #if defined(__linux__) |
| 64 | UNUSED(initialize_translations); |
| 65 | return error; |
| 66 | #else |
| 67 | static std::unordered_map<int, int> translations = initialize_translations(); |
| 68 | auto it = translations.find(error); |
| 69 | if (it != translations.end()) { |
| 70 | return it->second; |
| 71 | } |
| 72 | fatal("received unexpected remote errno: %d", error); |
| 73 | #endif |
| 74 | } |