Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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 | |
Han-Wen Nienhuys | 36fbe63 | 2015-04-21 13:58:08 +0000 | [diff] [blame] | 15 | #include "src/main/native/unix_jni.h" |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | |
| 17 | #include <string.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <sys/stat.h> |
| 20 | #include <sys/xattr.h> |
| 21 | |
| 22 | #include <string> |
| 23 | |
| 24 | std::string ErrorMessage(int error_number) { |
| 25 | char buf[1024] = ""; |
| 26 | |
| 27 | // In its infinite wisdom, GNU libc defines strerror_r with extended |
| 28 | // functionality which is not compatible with not the |
| 29 | // SUSv3-conformant one which returns an error code; see DESCRIPTION |
| 30 | // at strerror(1). |
| 31 | return std::string(strerror_r(error_number, buf, sizeof buf)); |
| 32 | } |
| 33 | |
Kristina Chodorow | e3285f4 | 2015-05-26 17:14:51 +0000 | [diff] [blame] | 34 | int portable_fstatat( |
| 35 | int dirfd, char *name, portable_stat_struct *statbuf, int flags) { |
Kristina Chodorow | 73ad148 | 2015-05-07 17:56:30 +0000 | [diff] [blame] | 36 | return fstatat64(dirfd, name, statbuf, flags); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | } |
| 38 | |
Kristina Chodorow | e3285f4 | 2015-05-26 17:14:51 +0000 | [diff] [blame] | 39 | int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 40 | switch (t) { |
| 41 | case STAT_ATIME: |
| 42 | return statbuf.st_atim.tv_sec; |
| 43 | case STAT_CTIME: |
| 44 | return statbuf.st_ctim.tv_sec; |
| 45 | case STAT_MTIME: |
| 46 | return statbuf.st_mtim.tv_sec; |
| 47 | default: |
| 48 | CHECK(false); |
| 49 | } |
| 50 | return 0; |
| 51 | } |
| 52 | |
Kristina Chodorow | e3285f4 | 2015-05-26 17:14:51 +0000 | [diff] [blame] | 53 | int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | switch (t) { |
| 55 | case STAT_ATIME: |
| 56 | return statbuf.st_atim.tv_nsec; |
| 57 | case STAT_CTIME: |
| 58 | return statbuf.st_ctim.tv_nsec; |
| 59 | case STAT_MTIME: |
| 60 | return statbuf.st_mtim.tv_nsec; |
| 61 | default: |
| 62 | CHECK(false); |
| 63 | } |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | ssize_t portable_getxattr(const char *path, const char *name, void *value, |
| 68 | size_t size) { |
| 69 | return ::getxattr(path, name, value, size); |
| 70 | } |
| 71 | |
| 72 | ssize_t portable_lgetxattr(const char *path, const char *name, void *value, |
| 73 | size_t size) { |
| 74 | return ::lgetxattr(path, name, value, size); |
| 75 | } |