aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/native/unix_jni.h
blob: 426e205c08e8149c8610f13c6f11a9e6d26c1e0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2014 Google Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// INTERNAL header file for use by C++ code in this package.

#ifndef BAZEL_SRC_MAIN_NATIVE_UNIX_JNI_H__
#define BAZEL_SRC_MAIN_NATIVE_UNIX_JNI_H__

#include <jni.h>
#include <sys/stat.h>

#include <string>

#define CHECK(condition) \
    do { \
      if (!(condition)) { \
        fprintf(stderr, "%s:%d: check failed: %s\n", \
                __FILE__, __LINE__, #condition); \
        abort(); \
      } \
    } while (0)

#if defined(__APPLE__) || defined(__FreeBSD__)
// stat64 is deprecated on OS X/BSD.
typedef struct stat portable_stat_struct;
#define portable_stat ::stat
#define portable_lstat ::lstat
#else
typedef struct stat64 portable_stat_struct;
#define portable_stat ::stat64
#define portable_lstat ::lstat64
#endif

#if defined(__FreeBSD__)
#define ENODATA ENOATTR
#endif

// Posts a JNI exception to the current thread with the specified
// message; the exception's class is determined by the specified UNIX
// error number.  See package-info.html for details.
extern void PostException(JNIEnv *env, int error_number,
                          const std::string &message);

// Like PostException, but the exception message includes both the
// specified filename and the standard UNIX error message for the
// error number.
// (Consistent with errors generated by java.io package.)
extern void PostFileException(JNIEnv *env, int error_number,
                              const char *filename);

// Returns the standard error message for a given UNIX error number.
extern std::string ErrorMessage(int error_number);

// Runs fstatat(2), if available, or sets errno to ENOSYS if not.
int portable_fstatat(int dirfd, char *name, portable_stat_struct *statbuf,
                     int flags);

// Encoding for different timestamps in a struct stat{}.
enum StatTimes {
  STAT_ATIME,  // access
  STAT_MTIME,  // modification
  STAT_CTIME,  // status change
};

// Returns seconds from a stat buffer.
int StatSeconds(const portable_stat_struct &statbuf, StatTimes t);

// Returns nanoseconds from a stat buffer.
int StatNanoSeconds(const portable_stat_struct &statbuf, StatTimes t);

// Runs getxattr(2), if available. If not, sets errno to ENOSYS.
ssize_t portable_getxattr(const char *path, const char *name, void *value,
                          size_t size);

// Run lgetxattr(2), if available. If not, sets errno to ENOSYS.
ssize_t portable_lgetxattr(const char *path, const char *name, void *value,
                           size_t size);

#endif  // BAZEL_SRC_MAIN_NATIVE_UNIX_JNI_H__