From: Simon Marchi Date: Tue, 5 Dec 2023 03:25:35 +0000 (+0000) Subject: src.ctf.fs: make ctf_fs_file::fp a FileUP X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=commitdiff_plain;h=85a25425dc0e75ed5fc2e38d93ef56fbe7156252 src.ctf.fs: make ctf_fs_file::fp a FileUP Change-Id: I7c0467a20942cf107d8f10951c8a393e3da7c608 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/8264 Reviewed-by: Philippe Proulx Reviewed-on: https://review.lttng.org/c/babeltrace/+/12302 Tested-by: jenkins --- diff --git a/src/plugins/ctf/fs-src/data-stream-file.cpp b/src/plugins/ctf/fs-src/data-stream-file.cpp index d05a04a3..dd725ee7 100644 --- a/src/plugins/ctf/fs-src/data-stream-file.cpp +++ b/src/plugins/ctf/fs-src/data-stream-file.cpp @@ -116,7 +116,7 @@ static enum ctf_msg_iter_medium_status ds_file_mmap(struct ctf_fs_ds_file *ds_fi BT_ASSERT(ds_file->mmap_len > 0); ds_file->mmap_addr = - bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp), + bt_mmap(ds_file->mmap_len, PROT_READ, MAP_PRIVATE, fileno(ds_file->file->fp.get()), ds_file->mmap_offset_in_file, static_cast(ds_file->logger.level())); if (ds_file->mmap_addr == MAP_FAILED) { BT_CPPLOGE_SPEC(ds_file->logger, diff --git a/src/plugins/ctf/fs-src/file.cpp b/src/plugins/ctf/fs-src/file.cpp index d125789e..33399d94 100644 --- a/src/plugins/ctf/fs-src/file.cpp +++ b/src/plugins/ctf/fs-src/file.cpp @@ -18,16 +18,6 @@ void ctf_fs_file_destroy(struct ctf_fs_file *file) return; } - if (file->fp) { - BT_CPPLOGD_SPEC(file->logger, "Closing file \"{}\" ({})", - file->path ? file->path->str : NULL, fmt::ptr(file->fp)); - - if (fclose(file->fp)) { - BT_CPPLOGE_SPEC(file->logger, "Cannot close file \"{}\": {}", - file->path ? file->path->str : "NULL", strerror(errno)); - } - } - if (file->path) { g_string_free(file->path, TRUE); } @@ -64,7 +54,7 @@ int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) struct stat stat; BT_CPPLOGI_SPEC(file->logger, "Opening file \"{}\" with mode \"{}\"", file->path->str, mode); - file->fp = fopen(file->path->str, mode); + file->fp.reset(fopen(file->path->str, mode)); if (!file->fp) { BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot open file", ": path={}, mode={}", file->path->str, mode); @@ -73,7 +63,7 @@ int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) BT_CPPLOGI_SPEC(file->logger, "Opened file: {}", fmt::ptr(file->fp)); - if (fstat(fileno(file->fp), &stat)) { + if (fstat(fileno(file->fp.get()), &stat)) { BT_CPPLOGE_ERRNO_APPEND_CAUSE_SPEC(file->logger, "Cannot get file information", ": path={}", file->path->str); goto error; @@ -86,13 +76,6 @@ int ctf_fs_file_open(struct ctf_fs_file *file, const char *mode) error: ret = -1; - if (file->fp) { - if (fclose(file->fp)) { - BT_CPPLOGE_SPEC(file->logger, "Cannot close file \"{}\": {}", file->path->str, - strerror(errno)); - } - } - end: return ret; } diff --git a/src/plugins/ctf/fs-src/file.hpp b/src/plugins/ctf/fs-src/file.hpp index 607c1a94..33fa10d8 100644 --- a/src/plugins/ctf/fs-src/file.hpp +++ b/src/plugins/ctf/fs-src/file.hpp @@ -11,6 +11,7 @@ #include +#include "cpp-common/bt2c/libc-up.hpp" #include "cpp-common/bt2c/logging.hpp" struct ctf_fs_file_deleter @@ -32,8 +33,7 @@ struct ctf_fs_file /* Owned by this */ GString *path = nullptr; - /* Owned by this */ - FILE *fp = nullptr; + bt2c::FileUP fp; off_t size = 0; }; diff --git a/src/plugins/ctf/fs-src/metadata.cpp b/src/plugins/ctf/fs-src/metadata.cpp index 4957bbf9..842b3eb7 100644 --- a/src/plugins/ctf/fs-src/metadata.cpp +++ b/src/plugins/ctf/fs-src/metadata.cpp @@ -83,7 +83,8 @@ int ctf_fs_metadata_set_trace_class(bt_self_component *self_comp, struct ctf_fs_ goto end; } - ret = ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder.get(), file->fp); + ret = + ctf_metadata_decoder_append_content(ctf_fs_trace->metadata->decoder.get(), file->fp.get()); if (ret) { BT_CPPLOGE_SPEC(ctf_fs_trace->logger, "Cannot update metadata decoder's content."); goto end;