xapian-core  1.4.25
safefcntl.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2012,2018 Olly Betts
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19  * USA
20  */
21 
22 #ifndef XAPIAN_INCLUDED_SAFEFCNTL_H
23 #define XAPIAN_INCLUDED_SAFEFCNTL_H
24 
25 #include <fcntl.h>
26 
27 #ifdef _AIX
28 
29 #include <stdarg.h>
30 
31 // On AIX, O_CLOEXEC may be a 64-bit constant which won't fit in "int flags".
32 // The solution is to call open64x() instead of open() when the flags don't fit
33 // in an int, which this overload achieves.
34 
35 inline int open(const char *filename, int64_t flags, ...) {
36  va_list ap;
37  va_start(ap, flags);
38  mode_t mode = 0;
39  if (flags & O_CREAT) {
40  mode = va_arg(ap, mode_t);
41  }
42  va_end(ap);
43  // open64x() takes a non-const path but is not documented as modifying it.
44  char* f = const_cast<char*>(filename);
45  return open64x(f, flags, mode, 0);
46 }
47 
48 #elif defined __cplusplus && defined open
49 
50 // On some versions of Solaris, fcntl.h pollutes the namespace by #define-ing
51 // "open" to "open64" when largefile support is enabled. This causes problems
52 // if you have a method called "open" (other symbols are also #define-d
53 // e.g. "creat" to "creat64", but only "open" is a problem for Xapian so
54 // that's the only one we currently fix).
55 
56 inline int fcntl_open_(const char *filename, int flags, mode_t mode) {
57  return open(filename, flags, mode);
58 }
59 
60 inline int fcntl_open_(const char *filename, int flags) {
61  return open(filename, flags);
62 }
63 
64 #undef open
65 
66 inline int open(const char *filename, int flags, mode_t mode) {
67  return fcntl_open_(filename, flags, mode);
68 }
69 
70 inline int open(const char *filename, int flags) {
71  return fcntl_open_(filename, flags);
72 }
73 
74 #endif
75 
76 // O_BINARY is only useful for platforms like Windows which distinguish between
77 // text and binary files, but it's cleaner to define it to 0 here for other
78 // platforms so we can avoid #ifdef where we need to use it in the code.
79 #ifndef __WIN32__
80 # ifndef O_BINARY
81 # define O_BINARY 0
82 # endif
83 #endif
84 
85 #ifndef O_CLOEXEC
86 # ifdef O_NOINHERIT
87 # define O_CLOEXEC O_NOINHERIT
88 # else
89 // If O_CLOEXEC isn't supported, we probably can't mark fds as close-on-exec.
90 # define O_CLOEXEC 0
91 # endif
92 #endif
93 
94 #endif /* XAPIAN_INCLUDED_SAFEFCNTL_H */
WritableDatabase open()
Construct a WritableDatabase object for a new, empty InMemory database.
Definition: dbfactory.h:104