blob: 3b60e297bf1ad94c878e3f5536955e9012f02ecf [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Doug Rabson709bc612015-08-25 14:12:00 +00002//
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 Sikora6ebc7db2016-04-12 09:45:12 +000025#include <sys/sysctl.h>
26#include <sys/types.h>
Doug Rabson709bc612015-08-25 14:12:00 +000027
28#include <string>
29
Doug Rabson709bc612015-08-25 14:12:00 +000030using std::string;
31
32// See unix_jni.h.
33string 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
42int portable_fstatat(int dirfd, char *name, portable_stat_struct *statbuf,
43 int flags) {
44 return fstatat(dirfd, name, statbuf, flags);
45}
46
47int 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
60int 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
73ssize_t portable_getxattr(const char *path, const char *name, void *value,
John Caterb3729d82018-02-22 11:56:42 -080074 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 Rabson709bc612015-08-25 14:12:00 +000079}
80
81ssize_t portable_lgetxattr(const char *path, const char *name, void *value,
John Caterb3729d82018-02-22 11:56:42 -080082 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 Rabson709bc612015-08-25 14:12:00 +000087}
Yue Gana6ae3e72016-04-05 09:00:22 +000088
89int portable_sysctlbyname(const char *name_chars, long *mibp, size_t *sizep) {
90 return sysctlbyname(name_chars, mibp, sizep, NULL, 0);
91}