xapian-core  2.0.0
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, see
18  * <https://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef XAPIAN_INCLUDED_SAFEFCNTL_H
22 #define XAPIAN_INCLUDED_SAFEFCNTL_H
23 
24 #include <fcntl.h>
25 
26 #ifdef _AIX
27 
28 #include <stdarg.h>
29 
30 // On AIX, O_CLOEXEC may be a 64-bit constant which won't fit in "int flags".
31 // The solution is to call open64x() instead of open() when the flags don't fit
32 // in an int, which this overload achieves.
33 
34 inline int open(const char *filename, int64_t flags, ...) {
35  va_list ap;
36  va_start(ap, flags);
37  mode_t mode = 0;
38  if (flags & O_CREAT) {
39  mode = va_arg(ap, mode_t);
40  }
41  va_end(ap);
42  // open64x() takes a non-const path but is not documented as modifying it.
43  char* f = const_cast<char*>(filename);
44  return open64x(f, flags, mode, 0);
45 }
46 
47 #elif defined __cplusplus && defined open
48 
49 // On some versions of Solaris, fcntl.h pollutes the namespace by #define-ing
50 // "open" to "open64" when largefile support is enabled. This causes problems
51 // if you have a method called "open" (other symbols are also #define-d
52 // e.g. "creat" to "creat64", but only "open" is a problem for Xapian so
53 // that's the only one we currently fix).
54 
55 inline int fcntl_open_(const char *filename, int flags, mode_t mode) {
56  return open(filename, flags, mode);
57 }
58 
59 inline int fcntl_open_(const char *filename, int flags) {
60  return open(filename, flags);
61 }
62 
63 #undef open
64 
65 inline int open(const char *filename, int flags, mode_t mode) {
66  return fcntl_open_(filename, flags, mode);
67 }
68 
69 inline int open(const char *filename, int flags) {
70  return fcntl_open_(filename, flags);
71 }
72 
73 #endif
74 
75 // O_BINARY is only useful for platforms like Windows which distinguish between
76 // text and binary files, but it's cleaner to define it to 0 here for other
77 // platforms so we can avoid #ifdef where we need to use it in the code.
78 #ifndef __WIN32__
79 # ifndef O_BINARY
80 # define O_BINARY 0
81 # endif
82 #endif
83 
84 #ifndef O_CLOEXEC
85 # ifdef O_NOINHERIT
86 # define O_CLOEXEC O_NOINHERIT
87 # else
88 // If O_CLOEXEC isn't supported, we probably can't mark fds as close-on-exec.
89 # define O_CLOEXEC 0
90 # endif
91 #endif
92 
93 #endif /* XAPIAN_INCLUDED_SAFEFCNTL_H */
Database open(std::string_view host, unsigned int port, unsigned timeout=10000, unsigned connect_timeout=10000)
Construct a Database object for read-only access to a remote database accessed via a TCP connection.