Port: implement posix_fallocate wrapper
authorJérémie Galarneau <jeremie.galarneau@efficios.com>
Thu, 1 Oct 2015 19:53:47 +0000 (15:53 -0400)
committerMichael Jeanson <mjeanson@efficios.com>
Fri, 16 Oct 2015 19:40:30 +0000 (15:40 -0400)
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
configure.ac
formats/ctf/ctf.c
formats/ctf/writer/event-fields.c
include/Makefile.am
include/babeltrace/compat/fcntl.h [new file with mode: 0644]

index 75bc9af422e40e30ef3f5f95f1286c437690ce1c..c4faf6e58d6fbe4040f4d00a80c174f2ab752182 100644 (file)
@@ -133,6 +133,13 @@ AC_CHECK_LIB([c], [open_memstream],
 ]
 )
 
 ]
 )
 
+# Check for posix_fallocate
+AC_CHECK_LIB([c], [posix_fallocate],
+[
+       AC_DEFINE_UNQUOTED([BABELTRACE_HAVE_POSIX_FALLOCATE], 1, [Has posix_fallocate support.])
+]
+)
+
 AC_CHECK_LIB([popt], [poptGetContext], [],
         [AC_MSG_ERROR([Cannot find popt.])]
 )
 AC_CHECK_LIB([popt], [poptGetContext], [],
         [AC_MSG_ERROR([Cannot find popt.])]
 )
index 865e2c2e0e8e21ebddcce42c55db55f497e182c9..af1809efc8a0390986027bded924e6d2b3bb932e 100644 (file)
@@ -53,6 +53,7 @@
 #include "metadata/ctf-ast.h"
 #include "events-private.h"
 #include <babeltrace/compat/memstream.h>
 #include "metadata/ctf-ast.h"
 #include "events-private.h"
 #include <babeltrace/compat/memstream.h>
+#include <babeltrace/compat/fcntl.h>
 
 #define LOG2_CHAR_BIT  3
 
 
 #define LOG2_CHAR_BIT  3
 
@@ -906,7 +907,7 @@ void ctf_packet_seek(struct bt_stream_pos *stream_pos, size_t index, int whence)
                }
                pos->content_size = -1U;        /* Unknown at this point */
                pos->packet_size = WRITE_PACKET_LEN;
                }
                pos->content_size = -1U;        /* Unknown at this point */
                pos->packet_size = WRITE_PACKET_LEN;
-               off = posix_fallocate(pos->fd, pos->mmap_offset,
+               off = bt_posix_fallocate(pos->fd, pos->mmap_offset,
                                      pos->packet_size / CHAR_BIT);
                assert(off == 0);
                pos->offset = 0;
                                      pos->packet_size / CHAR_BIT);
                assert(off == 0);
                pos->offset = 0;
index 461888f9b09498e0ea558d9b39b2ccd3aa46ce7f..9eff2dca98b19e3cb8eaa995af7cf6be968038f9 100644 (file)
@@ -30,6 +30,7 @@
 #include <babeltrace/ctf-writer/event-fields-internal.h>
 #include <babeltrace/ctf-writer/event-types-internal.h>
 #include <babeltrace/compiler.h>
 #include <babeltrace/ctf-writer/event-fields-internal.h>
 #include <babeltrace/ctf-writer/event-types-internal.h>
 #include <babeltrace/compiler.h>
+#include <babeltrace/compat/fcntl.h>
 
 #define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
 
 
 #define PACKET_LEN_INCREMENT   (getpagesize() * 8 * CHAR_BIT)
 
@@ -1241,7 +1242,7 @@ int increase_packet_size(struct ctf_stream_pos *pos)
        }
 
        pos->packet_size += PACKET_LEN_INCREMENT;
        }
 
        pos->packet_size += PACKET_LEN_INCREMENT;
-       ret = posix_fallocate(pos->fd, pos->mmap_offset,
+       ret = bt_posix_fallocate(pos->fd, pos->mmap_offset,
                pos->packet_size / CHAR_BIT);
        if (ret) {
                goto end;
                pos->packet_size / CHAR_BIT);
        if (ret) {
                goto end;
index 9109642dcda8cb037c87640eccca7796c36436ce..d183dbe0b521be3544105d4bd1caacfa47a2c269 100644 (file)
@@ -54,5 +54,6 @@ noinst_HEADERS = \
        babeltrace/compat/utc.h \
        babeltrace/compat/limits.h \
        babeltrace/compat/send.h \
        babeltrace/compat/utc.h \
        babeltrace/compat/limits.h \
        babeltrace/compat/send.h \
+       babeltrace/compat/fcntl.h \
        babeltrace/endian.h \
        babeltrace/mmap-align.h
        babeltrace/endian.h \
        babeltrace/mmap-align.h
diff --git a/include/babeltrace/compat/fcntl.h b/include/babeltrace/compat/fcntl.h
new file mode 100644 (file)
index 0000000..59060ff
--- /dev/null
@@ -0,0 +1,143 @@
+#ifndef _BABELTRACE_COMPAT_FCNTL_H
+#define _BABELTRACE_COMPAT_FCNTL_H
+
+/*
+ * babeltrace/compat/fcntl.h
+ *
+ * Copyright 2015 (c) - Jérémie Galarneau <jeremie.galarneau@efficios.com>
+ *
+ * fcntl compatibility layer.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include <config.h>
+
+#ifdef BABELTRACE_HAVE_POSIX_FALLOCATE
+
+#include <fcntl.h>
+
+static inline
+int bt_posix_fallocate(int fd, off_t offset, off_t len)
+{
+       return posix_fallocate(fd, offset, len);
+}
+
+#else /* #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
+
+#include <sys/types.h>
+#include <unistd.h>
+#include <string.h>
+
+#define BABELTRACE_FALLOCATE_BUFLEN    256
+
+#ifndef min_t
+#define min_t(type, a, b)      \
+       ((type) (a) < (type) (b) ? (type) (a) : (type) (b))
+#endif
+
+static inline
+int bt_posix_fallocate(int fd, off_t offset, off_t len)
+{
+       int ret = 0;
+       ssize_t copy_len;
+       char buf[BABELTRACE_FALLOCATE_BUFLEN];
+       off_t i, file_pos, orig_end_offset, range_end;
+
+       if (offset < 0 || len < 0) {
+               ret = EINVAL;
+               goto end;
+       }
+
+       range_end = offset + len;
+       if (range_end < 0) {
+               ret = EFBIG;
+               goto end;
+       }
+
+       file_pos = lseek(fd, 0, SEEK_CUR);
+       if (file_pos < 0) {
+               ret = errno;
+               goto end;
+       }
+
+       orig_end_offset = lseek(fd, 0, SEEK_END);
+       if (orig_end_offset < 0) {
+               ret = errno;
+               goto end;
+       }
+
+       /* Seek back to original position. */
+       ret = lseek(fd, file_pos, SEEK_SET);
+       if (ret) {
+               ret = errno;
+               goto end;
+       }
+
+       /*
+        * The file may not need to grow, but we want to ensure the
+        * space has actually been reserved by the file system. First, copy
+        * the "existing" region of the file, then grow the file if needed.
+        */
+       for (i = file_pos; i < min_t(off_t, range_end, orig_end_offset);
+                       i += copy_len) {
+               ssize_t copy_ret;
+
+               copy_len = min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN,
+                               min_t(off_t, range_end - i,
+                                       orig_end_offset - i));
+               copy_ret = pread(fd, &buf, copy_len, i);
+               if (copy_ret < copy_len) {
+                       /*
+                        * The caller must handle any EINTR.
+                        * POSIX_FALLOCATE(3) does not mention EINTR.
+                        * However, glibc does forward to fallocate()
+                        * directly on Linux, which may be interrupted.
+                        */
+                       ret = errno;
+                       goto end;
+               }
+
+               copy_ret = pwrite(fd, &buf, copy_len, i);
+               if (copy_ret < copy_len) {
+                       /* Same caveat as noted at pread() */
+                       ret = errno;
+                       goto end;
+               }
+       }
+
+       /* Grow file, as necessary. */
+       memset(&buf, 0, BABELTRACE_FALLOCATE_BUFLEN);
+       for (i = orig_end_offset; i < range_end; i += copy_len) {
+               ssize_t write_ret;
+
+               copy_len = min_t(size_t, BABELTRACE_FALLOCATE_BUFLEN,
+                               range_end - i);
+               write_ret = pwrite(fd, &buf, copy_len, i);
+               if (write_ret < copy_len) {
+                       ret = errno;
+                       goto end;
+               }
+       }
+end:
+       return ret;
+}
+#endif /* #else #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
+
+#endif /* _BABELTRACE_COMPAT_FCNTL_H */
This page took 0.028884 seconds and 4 git commands to generate.