From: Jérémie Galarneau Date: Wed, 24 Apr 2019 19:33:45 +0000 (-0400) Subject: Add a copy method to lttng_directory_handle X-Git-Tag: v2.12.0-rc1~581 X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=578e21bdc951a54e34fe539b64e446557b703206 Add a copy method to lttng_directory_handle This method allows the duplication of a directory handle to existing storage. It duplicates the underlying directory file descriptor or path, depending on the configured implementation. Signed-off-by: Jérémie Galarneau --- diff --git a/src/common/compat/directory-handle.c b/src/common/compat/directory-handle.c index 781f2574b..562a79287 100644 --- a/src/common/compat/directory-handle.c +++ b/src/common/compat/directory-handle.c @@ -88,6 +88,24 @@ void lttng_directory_handle_fini(struct lttng_directory_handle *handle) } } +LTTNG_HIDDEN +int lttng_directory_handle_copy(const struct lttng_directory_handle *handle, + struct lttng_directory_handle *new_copy) +{ + int ret = 0; + + if (handle->dirfd == AT_FDCWD) { + new_copy->dirfd = handle->dirfd; + } else { + new_copy->dirfd = dup(handle->dirfd); + if (new_copy->dirfd == -1) { + PERROR("Failed to duplicate directory fd of directory handle"); + ret = -1; + } + } + return ret; +} + static int lttng_directory_handle_stat(const struct lttng_directory_handle *handle, const char *path, struct stat *st) @@ -225,6 +243,14 @@ void lttng_directory_handle_fini(struct lttng_directory_handle *handle) free(handle->base_path); } +LTTNG_HIDDEN +int lttng_directory_handle_copy(const struct lttng_directory_handle *handle, + struct lttng_directory_handle *new_copy) +{ + new_copy->base_path = strdup(handle->base_path); + return new_copy->base_path ? 0 : -1; +} + static int get_full_path(const struct lttng_directory_handle *handle, const char *subdirectory, char *fullpath, size_t size) diff --git a/src/common/compat/directory-handle.h b/src/common/compat/directory-handle.h index 7d2cab339..ceebc941e 100644 --- a/src/common/compat/directory-handle.h +++ b/src/common/compat/directory-handle.h @@ -55,6 +55,13 @@ LTTNG_HIDDEN int lttng_directory_handle_init_from_dirfd( struct lttng_directory_handle *handle, int dirfd); +/* + * Copy a directory handle. + */ +LTTNG_HIDDEN +int lttng_directory_handle_copy(const struct lttng_directory_handle *handle, + struct lttng_directory_handle *new_copy); + /* * Release the resources of a directory handle. */