• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2014, The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef _FCNTL_PORTABLE_H_
18 #define _FCNTL_PORTABLE_H_
19 #endif
20 
21 #include <portability.h>
22 #include <fcntl.h>
23 #include <stdarg.h>
24 
25 #define O_DIRECTORY_PORTABLE  040000
26 #define O_NOFOLLOW_PORTABLE   0100000
27 #define O_DIRECT_PORTABLE     0200000
28 #define O_LARGEFILE_PORTABLE  0400000
29 
flags_p2n(int p_flags)30 static int flags_p2n(int p_flags)
31 {
32   int machine_flags = p_flags;
33   if (p_flags & O_DIRECTORY_PORTABLE) {
34     machine_flags ^= O_DIRECTORY_PORTABLE;
35     machine_flags |= O_DIRECTORY;
36   }
37   if (p_flags & O_NOFOLLOW_PORTABLE) {
38     machine_flags ^= O_NOFOLLOW_PORTABLE;
39     machine_flags |= O_NOFOLLOW;
40   }
41   if (p_flags & O_DIRECT_PORTABLE) {
42     machine_flags ^= O_DIRECT_PORTABLE;
43     machine_flags |= O_DIRECT;
44   }
45   if (p_flags & O_LARGEFILE_PORTABLE) {
46     machine_flags ^= O_LARGEFILE_PORTABLE;
47     machine_flags |= O_LARGEFILE;
48   }
49 
50   return machine_flags;
51 }
52 
53 #define FLAGS_VAARGS_TRANSLATE \
54   flags = flags_p2n(flags); \
55   mode_t mode = 0; \
56   if ((flags & O_CREAT) != 0) { \
57     va_list args; \
58     va_start(args, flags); \
59     mode = (mode_t) va_arg(args, int); \
60     va_end(args);\
61   }
62 
63 
WRAP(openat)64 int WRAP(openat)(int fd, const char* path, int flags, ...)
65 {
66   FLAGS_VAARGS_TRANSLATE
67   return REAL(openat)(fd, path, flags, mode);
68 }
69 
WRAP(openat64)70 int WRAP(openat64)(int fd, const char* path, int flags, ...)
71 {
72   FLAGS_VAARGS_TRANSLATE
73   return REAL(openat64)(fd, path, flags, mode);
74 }
75 
WRAP(open)76 int WRAP(open)(const char* path, int flags, ...)
77 {
78   FLAGS_VAARGS_TRANSLATE
79   return REAL(open)(path, flags, mode);
80 }
81 
WRAP(open64)82 int WRAP(open64)(const char* path, int flags, ...)
83 {
84   FLAGS_VAARGS_TRANSLATE
85   return REAL(open64)(path, flags, mode);
86 }
87