Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Doug Rabson | 709bc61 | 2015-08-25 14:12:00 +0000 | [diff] [blame] | 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 | #include "src/main/native/unix_jni.h" |
| 16 | |
| 17 | #include <assert.h> |
| 18 | #include <errno.h> |
| 19 | #include <fcntl.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | #include <sys/extattr.h> |
| 23 | #include <sys/param.h> |
| 24 | #include <sys/stat.h> |
Piotr Sikora | 6ebc7db | 2016-04-12 09:45:12 +0000 | [diff] [blame] | 25 | #include <sys/sysctl.h> |
| 26 | #include <sys/types.h> |
Doug Rabson | 709bc61 | 2015-08-25 14:12:00 +0000 | [diff] [blame] | 27 | |
| 28 | #include <string> |
| 29 | |
Doug Rabson | 709bc61 | 2015-08-25 14:12:00 +0000 | [diff] [blame] | 30 | using std::string; |
| 31 | |
| 32 | // See unix_jni.h. |
| 33 | string ErrorMessage(int error_number) { |
| 34 | char buf[1024] = ""; |
| 35 | if (strerror_r(error_number, buf, sizeof buf) < 0) { |
| 36 | snprintf(buf, sizeof buf, "strerror_r(%d): errno %d", error_number, errno); |
| 37 | } |
| 38 | |
| 39 | return string(buf); |
| 40 | } |
| 41 | |
| 42 | int portable_fstatat(int dirfd, char *name, portable_stat_struct *statbuf, |
| 43 | int flags) { |
| 44 | return fstatat(dirfd, name, statbuf, flags); |
| 45 | } |
| 46 | |
| 47 | int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) { |
| 48 | switch (t) { |
| 49 | case STAT_ATIME: |
| 50 | return statbuf.st_atime; |
| 51 | case STAT_CTIME: |
| 52 | return statbuf.st_ctime; |
| 53 | case STAT_MTIME: |
| 54 | return statbuf.st_mtime; |
| 55 | default: |
| 56 | CHECK(false); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) { |
| 61 | switch (t) { |
| 62 | case STAT_ATIME: |
| 63 | return statbuf.st_atimespec.tv_nsec; |
| 64 | case STAT_CTIME: |
| 65 | return statbuf.st_ctimespec.tv_nsec; |
| 66 | case STAT_MTIME: |
| 67 | return statbuf.st_mtimespec.tv_nsec; |
| 68 | default: |
| 69 | CHECK(false); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | ssize_t portable_getxattr(const char *path, const char *name, void *value, |
John Cater | b3729d8 | 2018-02-22 11:56:42 -0800 | [diff] [blame] | 74 | size_t size, bool *attr_not_found) { |
| 75 | ssize_t result = |
| 76 | extattr_get_file(path, EXTATTR_NAMESPACE_SYSTEM, name, value, size); |
| 77 | *attr_not_found = (errno == ENOATTR); |
| 78 | return result; |
Doug Rabson | 709bc61 | 2015-08-25 14:12:00 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | ssize_t portable_lgetxattr(const char *path, const char *name, void *value, |
John Cater | b3729d8 | 2018-02-22 11:56:42 -0800 | [diff] [blame] | 82 | size_t size, bool *attr_not_found) { |
| 83 | ssize_t result = |
| 84 | extattr_get_link(path, EXTATTR_NAMESPACE_SYSTEM, name, value, size); |
| 85 | *attr_not_found = (errno == ENOATTR); |
| 86 | return result; |
Doug Rabson | 709bc61 | 2015-08-25 14:12:00 +0000 | [diff] [blame] | 87 | } |
Yue Gan | a6ae3e7 | 2016-04-05 09:00:22 +0000 | [diff] [blame] | 88 | |
| 89 | int portable_sysctlbyname(const char *name_chars, long *mibp, size_t *sizep) { |
| 90 | return sysctlbyname(name_chars, mibp, sizep, NULL, 0); |
| 91 | } |