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
30 #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE
35 int bt_posix_fallocate(int fd
, off_t offset
, off_t len
)
37 return posix_fallocate(fd
, offset
, len
);
40 #else /* #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
42 #include <sys/types.h>
46 #define BABELTRACE_FALLOCATE_BUFLEN 256
49 #define min_t(type, a, b) \
50 ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
54 int bt_posix_fallocate(int fd
, off_t offset
, off_t len
)
58 char buf
[BABELTRACE_FALLOCATE_BUFLEN
];
59 off_t i
, file_pos
, orig_end_offset
, range_end
;
61 if (offset
< 0 || len
< 0) {
66 range_end
= offset
+ len
;
72 file_pos
= lseek(fd
, 0, SEEK_CUR
);
78 orig_end_offset
= lseek(fd
, 0, SEEK_END
);
79 if (orig_end_offset
< 0) {
84 /* Seek back to original position. */
85 ret
= lseek(fd
, file_pos
, SEEK_SET
);
92 * The file may not need to grow, but we want to ensure the
93 * space has actually been reserved by the file system. First, copy
94 * the "existing" region of the file, then grow the file if needed.
96 for (i
= file_pos
; i
< min_t(off_t
, range_end
, orig_end_offset
);
100 copy_len
= min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN
,
101 min_t(off_t
, range_end
- i
,
102 orig_end_offset
- i
));
103 copy_ret
= pread(fd
, &buf
, copy_len
, i
);
104 if (copy_ret
< copy_len
) {
106 * The caller must handle any EINTR.
107 * POSIX_FALLOCATE(3) does not mention EINTR.
108 * However, glibc does forward to fallocate()
109 * directly on Linux, which may be interrupted.
115 copy_ret
= pwrite(fd
, &buf
, copy_len
, i
);
116 if (copy_ret
< copy_len
) {
117 /* Same caveat as noted at pread() */
123 /* Grow file, as necessary. */
124 memset(&buf
, 0, BABELTRACE_FALLOCATE_BUFLEN
);
125 for (i
= orig_end_offset
; i
< range_end
; i
+= copy_len
) {
128 copy_len
= min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN
,
130 write_ret
= pwrite(fd
, &buf
, copy_len
, i
);
131 if (write_ret
< copy_len
) {
139 #endif /* #else #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
142 #ifdef BABELTRACE_HAVE_FACCESSAT
148 int bt_faccessat(int dirfd
, const char *dirname
,
149 const char *pathname
, int mode
, int flags
)
151 return faccessat(dirfd
, pathname
, mode
, flags
);
154 #else /* #ifdef BABELTRACE_HAVE_FACCESSAT */
160 int bt_faccessat(int dirfd
, const char *dirname
,
161 const char *pathname
, int mode
, int flags
)
163 char cpath
[PATH_MAX
];
169 /* Includes middle / and final \0. */
170 if (strlen(dirname
) + strlen(pathname
) + 2 > PATH_MAX
) {
173 strcpy(cpath
, dirname
);
175 strcat(cpath
, pathname
);
176 return access(cpath
, mode
);
179 #endif /* #else #ifdef BABELTRACE_HAVE_FACCESSAT */
181 #endif /* _BABELTRACE_COMPAT_FCNTL_H */