blob: 44e23531f11d2074b2c6f82a6f3925c20fe8f8aa [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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 Nienhuys36fbe632015-04-21 13:58:08 +000015#include "src/main/native/unix_jni.h"
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016
17#include <string.h>
18#include <stdlib.h>
19#include <sys/stat.h>
20#include <sys/xattr.h>
21
22#include <string>
23
24std::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 Chodorowe3285f42015-05-26 17:14:51 +000034int portable_fstatat(
35 int dirfd, char *name, portable_stat_struct *statbuf, int flags) {
Kristina Chodorow73ad1482015-05-07 17:56:30 +000036 return fstatat64(dirfd, name, statbuf, flags);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010037}
38
Kristina Chodorowe3285f42015-05-26 17:14:51 +000039int StatSeconds(const portable_stat_struct &statbuf, StatTimes t) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010040 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 Chodorowe3285f42015-05-26 17:14:51 +000053int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t) {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010054 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
67ssize_t portable_getxattr(const char *path, const char *name, void *value,
68 size_t size) {
69 return ::getxattr(path, name, value, size);
70}
71
72ssize_t portable_lgetxattr(const char *path, const char *name, void *value,
73 size_t size) {
74 return ::lgetxattr(path, name, value, size);
75}