From ebd0404825a8b8b7d5701933f0acfcd3a8b534a7 Mon Sep 17 00:00:00 2001 From: Michael Jeanson Date: Wed, 9 Nov 2016 14:35:33 -0500 Subject: [PATCH] Port: dirfd is not portable, replace it MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Michael Jeanson Signed-off-by: Jérémie Galarneau --- cli/babeltrace-log.c | 33 +++++++------ configure.ac | 8 --- include/Makefile.am | 1 - include/babeltrace/compat/dirent-internal.h | 49 ------------------- include/babeltrace/compat/fcntl-internal.h | 40 --------------- .../babeltrace/ctf-writer/writer-internal.h | 1 - lib/ctf-ir/stream.c | 17 +++++-- lib/ctf-writer/writer.c | 22 ++++----- tests/lib/common.c | 2 +- tests/lib/test_ctf_writer.c | 1 - 10 files changed, 40 insertions(+), 134 deletions(-) delete mode 100644 include/babeltrace/compat/dirent-internal.h diff --git a/cli/babeltrace-log.c b/cli/babeltrace-log.c index a2d4884c..040af759 100644 --- a/cli/babeltrace-log.c +++ b/cli/babeltrace-log.c @@ -30,7 +30,7 @@ #include #include #include -#include +#include #include #include #include @@ -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) diff --git a/configure.ac b/configure.ac index b9d5e621..9051405d 100644 --- a/configure.ac +++ b/configure.ac @@ -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.])] ) diff --git a/include/Makefile.am b/include/Makefile.am index 3bd6a25a..0859427f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -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 index 5373c667..00000000 --- a/include/babeltrace/compat/dirent-internal.h +++ /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 - * - * 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 - -#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 */ diff --git a/include/babeltrace/compat/fcntl-internal.h b/include/babeltrace/compat/fcntl-internal.h index 2040bec0..f0894e73 100644 --- a/include/babeltrace/compat/fcntl-internal.h +++ b/include/babeltrace/compat/fcntl-internal.h @@ -232,44 +232,4 @@ end: } #endif /* #else #ifdef BABELTRACE_HAVE_POSIX_FALLOCATE */ - -#ifdef BABELTRACE_HAVE_FACCESSAT - -#include -#include - -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 -#include - -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 */ diff --git a/include/babeltrace/ctf-writer/writer-internal.h b/include/babeltrace/ctf-writer/writer-internal.h index 3fa03b59..17d4af7d 100644 --- a/include/babeltrace/ctf-writer/writer-internal.h +++ b/include/babeltrace/ctf-writer/writer-internal.h @@ -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; }; diff --git a/lib/ctf-ir/stream.c b/lib/ctf-ir/stream.c index cc4d9795..8ba0977c 100644 --- a/lib/ctf-ir/stream.c +++ b/lib/ctf-ir/stream.c @@ -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); diff --git a/lib/ctf-writer/writer.c b/lib/ctf-writer/writer.c index a5efe477..2978cdfb 100644 --- a/lib/ctf-writer/writer.c +++ b/lib/ctf-writer/writer.c @@ -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"); diff --git a/tests/lib/common.c b/tests/lib/common.c index a6812b9f..08bc1185 100644 --- a/tests/lib/common.c +++ b/tests/lib/common.c @@ -21,7 +21,7 @@ #include #include -#include +#include #include #include diff --git a/tests/lib/test_ctf_writer.c b/tests/lib/test_ctf_writer.c index e5a7af37..060b68ab 100644 --- a/tests/lib/test_ctf_writer.c +++ b/tests/lib/test_ctf_writer.c @@ -42,7 +42,6 @@ #include #include #include -#include #include "tap/tap.h" #include #include -- 2.34.1