1 #ifndef _BABELTRACE_COMPAT_FCNTL_H
2 #define _BABELTRACE_COMPAT_FCNTL_H
5 * babeltrace/compat/fcntl.h
7 * Copyright 2015 (c) - Jérémie Galarneau <jeremie.galarneau@efficios.com>
9 * fcntl compatibility layer.
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE
37 int bt_posix_fallocate(int fd
, off_t offset
, off_t len
)
39 return posix_fallocate(fd
, offset
, len
);
42 #else /* #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
44 #include <sys/types.h>
48 #define BABELTRACE_FALLOCATE_BUFLEN 256
51 #define min_t(type, a, b) \
52 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
56 int bt_posix_fallocate(int fd
, off_t offset
, off_t len
)
60 char buf
[BABELTRACE_FALLOCATE_BUFLEN
];
61 off_t i
, file_pos
, orig_end_offset
, range_end
;
63 if (offset
< 0 || len
< 0) {
68 range_end
= offset
+ len
;
74 file_pos
= lseek(fd
, 0, SEEK_CUR
);
80 orig_end_offset
= lseek(fd
, 0, SEEK_END
);
81 if (orig_end_offset
< 0) {
86 /* Seek back to original position. */
87 ret
= lseek(fd
, file_pos
, SEEK_SET
);
94 * The file may not need to grow, but we want to ensure the
95 * space has actually been reserved by the file system. First, copy
96 * the "existing" region of the file, then grow the file if needed.
98 for (i
= file_pos
; i
< min_t(off_t
, range_end
, orig_end_offset
);
102 copy_len
= min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN
,
103 min_t(off_t
, range_end
- i
,
104 orig_end_offset
- i
));
105 copy_ret
= pread(fd
, &buf
, copy_len
, i
);
106 if (copy_ret
< copy_len
) {
108 * The caller must handle any EINTR.
109 * POSIX_FALLOCATE(3) does not mention EINTR.
110 * However, glibc does forward to fallocate()
111 * directly on Linux, which may be interrupted.
117 copy_ret
= pwrite(fd
, &buf
, copy_len
, i
);
118 if (copy_ret
< copy_len
) {
119 /* Same caveat as noted at pread() */
125 /* Grow file, as necessary. */
126 memset(&buf
, 0, BABELTRACE_FALLOCATE_BUFLEN
);
127 for (i
= orig_end_offset
; i
< range_end
; i
+= copy_len
) {
130 copy_len
= min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN
,
132 write_ret
= pwrite(fd
, &buf
, copy_len
, i
);
133 if (write_ret
< copy_len
) {
141 #endif /* #else #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
144 #ifdef BABELTRACE_HAVE_FACCESSAT
150 int bt_faccessat(int dirfd
, const char *dirname
,
151 const char *pathname
, int mode
, int flags
)
153 return faccessat(dirfd
, pathname
, mode
, flags
);
156 #else /* #ifdef BABELTRACE_HAVE_FACCESSAT */
162 int bt_faccessat(int dirfd
, const char *dirname
,
163 const char *pathname
, int mode
, int flags
)
165 char cpath
[PATH_MAX
];
171 /* Includes middle / and final \0. */
172 if (strlen(dirname
) + strlen(pathname
) + 2 > PATH_MAX
) {
175 strcpy(cpath
, dirname
);
177 strcat(cpath
, pathname
);
178 return access(cpath
, mode
);
181 #endif /* #else #ifdef BABELTRACE_HAVE_FACCESSAT */
183 #endif /* _BABELTRACE_COMPAT_FCNTL_H */