X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=fd-cache%2Ffd-cache.c;h=fcd4f4c201b4bbc9620d1dfb392debd18d1661da;hb=68b66a256a54d32992dfefeaad11eea88b7df234;hp=c0b6859bb1077a93682be38cfb8241381e2a27f6;hpb=95dc9c849ed229971f663b54841d8729c7495f47;p=babeltrace.git diff --git a/fd-cache/fd-cache.c b/fd-cache/fd-cache.c index c0b6859b..fcd4f4c2 100644 --- a/fd-cache/fd-cache.c +++ b/fd-cache/fd-cache.c @@ -35,8 +35,8 @@ #include #include -#include -#include +#include +#include struct file_key { uint64_t dev; @@ -135,17 +135,22 @@ struct bt_fd_cache_handle *bt_fd_cache_get_handle(struct bt_fd_cache *fdc, 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. */ @@ -156,7 +161,7 @@ struct bt_fd_cache_handle *bt_fd_cache_get_handle(struct bt_fd_cache *fdc, if (!fd_internal) { 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; @@ -184,12 +189,22 @@ struct bt_fd_cache_handle *bt_fd_cache_get_handle(struct bt_fd_cache *fdc, 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: