Port: dirfd is not portable, replace it
authorMichael Jeanson <mjeanson@efficios.com>
Wed, 9 Nov 2016 19:35:33 +0000 (14:35 -0500)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Wed, 5 Jul 2017 19:40:23 +0000 (15:40 -0400)
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
cli/babeltrace-log.c
configure.ac
include/Makefile.am
include/babeltrace/compat/dirent-internal.h [deleted file]
include/babeltrace/compat/fcntl-internal.h
include/babeltrace/ctf-writer/writer-internal.h
lib/ctf-ir/stream.c
lib/ctf-writer/writer.c
tests/lib/common.c
tests/lib/test_ctf_writer.c

index a2d4884c825bc64919456285ef621173b23ce668..040af75947f6a63dcf3f7a078b46dc365e09c264 100644 (file)
@@ -30,7 +30,7 @@
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <babeltrace/compat/mman-internal.h>
-#include <babeltrace/compat/dirent-internal.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdint.h>
@@ -388,8 +388,8 @@ int main(int argc, char **argv)
 {
        int fd, metadata_fd, ret;
        DIR *dir;
-       int dir_fd;
        FILE *metadata_fp;
+       char *file_path;
 
        ret = parse_args(argc, argv);
        if (ret) {
@@ -414,23 +414,30 @@ int main(int argc, char **argv)
                perror("opendir");
                goto error_rmdir;
        }
-       dir_fd = bt_dirfd(dir);
-       if (dir_fd < 0) {
-               perror("dirfd");
+
+       file_path = g_build_filename(s_outputname, "datastream", NULL);
+       if (file_path == NULL) {
+               perror("g_build_filename");
                goto error_closedir;
        }
-
-       fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
+       fd = open(file_path, O_RDWR|O_CREAT,
                    S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
+       g_free(file_path);
        if (fd < 0) {
-               perror("openat");
-               goto error_closedirfd;
+               perror("open");
+               goto error_closedir;
        }
 
-       metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT,
+       file_path = g_build_filename(s_outputname, "metadata", NULL);
+       if (file_path == NULL) {
+               perror("g_build_filename");
+               goto error_closedatastream;
+       }
+       metadata_fd = open(file_path, O_RDWR|O_CREAT,
                             S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
+       g_free(file_path);
        if (metadata_fd < 0) {
-               perror("openat");
+               perror("open");
                goto error_closedatastream;
        }
        metadata_fp = fdopen(metadata_fd, "w");
@@ -457,10 +464,6 @@ error_closedatastream:
        ret = close(fd);
        if (ret)
                perror("close");
-error_closedirfd:
-       ret = close(dir_fd);
-       if (ret)
-               perror("close");
 error_closedir:
        ret = closedir(dir);
        if (ret)
index b9d5e621ac53cc4b2f5065405343bc371cda58d1..9051405dc2b63668523d18551aa1d8b35e9475d7 100644 (file)
@@ -124,7 +124,6 @@ AC_FUNC_REALLOC
 AC_FUNC_STRERROR_R
 AC_CHECK_FUNCS([ \
        atexit \
-       dirfd \
        dup2 \
        ftruncate \
        gethostbyname \
@@ -234,13 +233,6 @@ AC_CHECK_LIB([c], [posix_fallocate],
 ]
 )
 
-# Check for faccessat
-AC_CHECK_LIB([c], [faccessat],
-[
-       AC_DEFINE_UNQUOTED([BABELTRACE_HAVE_FACCESSAT], 1, [Has faccessat support.])
-]
-)
-
 AC_CHECK_LIB([popt], [poptGetContext], [],
         [AC_MSG_ERROR([Cannot find popt.])]
 )
index 3bd6a25a0c01476a6ac93a449f133af8e018b436..0859427fd0fad5ce290294af536137ce7f6ead7c 100644 (file)
@@ -76,7 +76,6 @@ noinst_HEADERS = \
        babeltrace/babeltrace-internal.h \
        babeltrace/bitfield-internal.h \
        babeltrace/common-internal.h \
-       babeltrace/compat/dirent-internal.h \
        babeltrace/compat/fcntl-internal.h \
        babeltrace/compat/glib-internal.h \
        babeltrace/compat/limits-internal.h \
diff --git a/include/babeltrace/compat/dirent-internal.h b/include/babeltrace/compat/dirent-internal.h
deleted file mode 100644 (file)
index 5373c66..0000000
+++ /dev/null
@@ -1,49 +0,0 @@
-#ifndef _BABELTRACE_COMPAT_DIRENT_H
-#define _BABELTRACE_COMPAT_DIRENT_H
-
-/*
- * babeltrace/compat/dirent.h
- *
- * Copyright (C) 2015 Michael Jeanson <mjeanson@efficios.com>
- *
- * 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 <dirent.h>
-
-#ifdef HAVE_DIRFD
-static inline
-int bt_dirfd(DIR *dir) {
-       return dirfd(dir);
-}
-#else
-# ifndef __XOPEN_OR_POSIX
-static inline
-int bt_dirfd(DIR *dir) {
-       return dir->dd_fd;
-}
-# else
-static inline
-int bt_dirfd(DIR *dir) {
-       return dir->d_fd;
-}
-# endif
-#endif
-
-#endif /* _BABELTRACE_COMPAT_DIRENT_H */
index 2040bec0e84b5d4fc049ba57e2035960150b8ccc..f0894e739f63b59af2672fce366a220c9f96781d 100644 (file)
@@ -232,44 +232,4 @@ end:
 }
 #endif /* #else #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */
 
-
-#ifdef BABELTRACE_HAVE_FACCESSAT
-
-#include <fcntl.h>
-#include <unistd.h>
-
-static inline
-int bt_faccessat(int dirfd, const char *dirname,
-               const char *pathname, int mode, int flags)
-{
-       return faccessat(dirfd, pathname, mode, flags);
-}
-
-#else /* #ifdef BABELTRACE_HAVE_FACCESSAT */
-
-#include <string.h>
-#include <unistd.h>
-
-static inline
-int bt_faccessat(int dirfd, const char *dirname,
-               const char *pathname, int mode, int flags)
-{
-       char cpath[PATH_MAX];
-
-       if (flags != 0) {
-               errno = EINVAL;
-               return -1;
-       }
-       /* Includes middle / and final \0. */
-       if (strlen(dirname) + strlen(pathname) + 2 > PATH_MAX) {
-               return -1;
-       }
-       strcpy(cpath, dirname);
-       strcat(cpath, "/");
-       strcat(cpath, pathname);
-       return access(cpath, mode);
-}
-
-#endif /* #else #ifdef BABELTRACE_HAVE_FACCESSAT */
-
 #endif /* _BABELTRACE_COMPAT_FCNTL_H */
index 3fa03b5953cc33bcdd5145ffe5edeb3fcbfe2a0e..17d4af7d99c9a41b0eadb98d2aef1f6a21282fce 100644 (file)
@@ -40,7 +40,6 @@ struct bt_ctf_writer {
        int frozen; /* Protects attributes that can't be changed mid-trace */
        struct bt_ctf_trace *trace;
        GString *path;
-       int trace_dir_fd;
        int metadata_fd;
 };
 
index cc4d9795064951b529fefe8b89c87c9c6fe4206c..8ba0977c13bef601458dc296b13809805b4032c3 100644 (file)
@@ -668,6 +668,7 @@ int create_stream_file(struct bt_ctf_writer *writer,
 {
        int fd;
        GString *filename = g_string_new(stream->stream_class->name->str);
+       char *file_path;
 
        BT_LOGD("Creating stream file: writer-addr=%p, stream-addr=%p, "
                "stream-name=\"%s\", stream-class-addr=%p, stream-class-name=\"%s\"",
@@ -692,21 +693,27 @@ int create_stream_file(struct bt_ctf_writer *writer,
        }
 
        g_string_append_printf(filename, "_%" PRId64, stream->id);
-       fd = openat(writer->trace_dir_fd, filename->str,
+       file_path = g_build_filename(writer->path->str, filename->str, NULL);
+       if (file_path == NULL) {
+               fd = -1;
+               goto end;
+       }
+       fd = open(file_path,
                O_RDWR | O_CREAT | O_TRUNC,
                S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       g_free(file_path);
        if (fd < 0) {
                BT_LOGW("Failed to open stream file for writing: %s: "
-                       "writer-trace-dir-fd=%d, filename=\"%s\", "
+                       "filename=\"%s\", "
                        "ret=%d, errno=%d", strerror(errno),
-                       writer->trace_dir_fd, filename->str, fd, errno);
+                       filename->str, fd, errno);
                goto end;
        }
 
        BT_LOGD("Created stream file for writing: "
-               "stream-addr=%p, stream-name=\"%s\", writer-trace-dir-fd=%d, "
+               "stream-addr=%p, stream-name=\"%s\", "
                "filename=\"%s\", fd=%d", stream, bt_ctf_stream_get_name(stream),
-               writer->trace_dir_fd, filename->str, fd);
+               filename->str, fd);
 
 end:
        g_string_free(filename, TRUE);
index a5efe4775c37468b296dfb990f78e39fb6f46834..2978cdfb78a02affa2f7b0e32193eb44225d10cd 100644 (file)
@@ -109,6 +109,7 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
        int ret;
        struct bt_ctf_writer *writer = NULL;
        unsigned char uuid[16];
+       char *metadata_path = NULL;
 
        if (!path) {
                goto error;
@@ -119,6 +120,8 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error;
        }
 
+       metadata_path = g_build_filename(path, "metadata", NULL);
+
        bt_object_init(writer, bt_ctf_writer_destroy);
        writer->path = g_string_new(path);
        if (!writer->path) {
@@ -161,22 +164,21 @@ struct bt_ctf_writer *bt_ctf_writer_create(const char *path)
                goto error_destroy;
        }
 
-       writer->trace_dir_fd = open(path, O_RDONLY, S_IRWXU | S_IRWXG);
-       if (writer->trace_dir_fd < 0) {
+       writer->metadata_fd = open(metadata_path,
+               O_WRONLY | O_CREAT | O_TRUNC,
+               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
+       if (writer->metadata_fd < 0) {
                perror("open");
                goto error_destroy;
        }
 
-       writer->metadata_fd = openat(writer->trace_dir_fd, "metadata",
-               O_WRONLY | O_CREAT | O_TRUNC,
-               S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
-
+       g_free(metadata_path);
        return writer;
 
 error_destroy:
-       unlinkat(writer->trace_dir_fd, "metadata", 0);
        BT_PUT(writer);
 error:
+       g_free(metadata_path);
        return writer;
 }
 
@@ -190,12 +192,6 @@ void bt_ctf_writer_destroy(struct bt_object *obj)
                g_string_free(writer->path, TRUE);
        }
 
-       if (writer->trace_dir_fd > 0) {
-               if (close(writer->trace_dir_fd)) {
-                       perror("close");
-               }
-       }
-
        if (writer->metadata_fd > 0) {
                if (close(writer->metadata_fd)) {
                        perror("close");
index a6812b9ffff2d2200a8bd48a68e1aadf200a9cc1..08bc1185bebddca93cfefa4c37b4d26aa1f40b74 100644 (file)
@@ -21,7 +21,7 @@
 
 #include <unistd.h>
 #include <stdio.h>
-#include <babeltrace/compat/dirent-internal.h>
+#include <dirent.h>
 #include <babeltrace/compat/limits-internal.h>
 #include <sys/stat.h>
 
index e5a7af3775aa11ca7a7a0563db330b6ed572943a..060b68ab61f0ef740a9022ed8c80b88ecfa69f81 100644 (file)
@@ -42,7 +42,6 @@
 #include <assert.h>
 #include <sys/wait.h>
 #include <fcntl.h>
-#include <babeltrace/compat/dirent-internal.h>
 #include "tap/tap.h"
 #include <math.h>
 #include <float.h>
This page took 0.030832 seconds and 4 git commands to generate.