#include <unistd.h>
#include <glib.h>
-#include <babeltrace/assert-internal.h>
-#include <babeltrace/fd-cache-internal.h>
+#include <babeltrace2/assert-internal.h>
+#include <babeltrace2/fd-cache-internal.h>
struct file_key {
uint64_t dev;
struct fd_handle_internal *fd_internal = NULL;
struct stat statbuf;
struct file_key fk;
- int ret;
+ int ret, fd = -1;
ret = stat(path, &statbuf);
if (ret < 0) {
- BT_LOGE_ERRNO("Failed to stat file", ": path=%s", path);
+ /*
+ * This is not necessarily an error as we sometimes try to open
+ * files to see if they exist. Log the error as DEBUG severity
+ * level.
+ */
+ BT_LOGD_ERRNO("Failed to stat file", ": path=%s", path);
goto end;
}
/*
* Use the device number and inode number to uniquely identify a file.
- * Even if the file as the same path, it may have been replaced so we
+ * Even if the file has the same path, it may have been replaced so we
* must open a new FD for it. This replacement of file is more likely
* to happen with a lttng-live source component.
*/
fd_internal = g_hash_table_lookup(fdc->cache, &fk);
if (!fd_internal) {
- gboolean ret;
struct file_key *file_key;
- int fd = open(path, O_RDONLY);
+ fd = open(path, O_RDONLY);
if (fd < 0) {
BT_LOGE_ERRNO("Failed to open file", "path=%s", path);
goto error;
fd_internal->key = file_key;
/* Insert the newly created fd handle. */
- ret = g_hash_table_insert(fdc->cache, fd_internal->key,
- fd_internal);
- BT_ASSERT(ret);
+ g_hash_table_insert(fdc->cache, fd_internal->key, fd_internal);
}
- BT_ASSERT(fd_internal->ref_count >= 0);
-
fd_internal->ref_count++;
goto end;
error:
+ /*
+ * Close file descriptor if it was open() and we are currently on error
+ * path.
+ */
+ if (fd != -1) {
+ ret = close(fd);
+ if (ret) {
+ BT_LOGE_ERRNO("Failed to close file descriptor",
+ ": fd=%i, path=%s", fd, path);
+ }
+ }
+
fd_cache_handle_internal_destroy(fd_internal);
fd_internal = NULL;
end: