From fbc12fd9ec43142d2b20d060b92ddfbc4f860175 Mon Sep 17 00:00:00 2001 From: David Sterba Date: Wed, 4 Nov 2020 23:26:43 +0100 Subject: [PATCH] btrfs-progs: move path_cat_out helpers to path-utils The path-util.[ch] is the right place, keep the send-utils.h prototypes as it's part of libbtrfs headers. Signed-off-by: David Sterba --- common/path-utils.c | 37 +++++++++++++++++++++++++++++++++++++ common/path-utils.h | 2 ++ common/send-utils.c | 37 ------------------------------------- 3 files changed, 39 insertions(+), 37 deletions(-) diff --git a/common/path-utils.c b/common/path-utils.c index 175da572..480f41b9 100644 --- a/common/path-utils.c +++ b/common/path-utils.c @@ -393,6 +393,43 @@ int arg_copy_path(char *dest, const char *src, int destlen) return 0; } +int path_cat_out(char *out, const char *p1, const char *p2) +{ + int p1_len = strlen(p1); + int p2_len = strlen(p2); + + if (p1_len + p2_len + 2 >= PATH_MAX) + return -ENAMETOOLONG; + + if (p1_len && p1[p1_len - 1] == '/') + p1_len--; + if (p2_len && p2[p2_len - 1] == '/') + p2_len--; + sprintf(out, "%.*s/%.*s", p1_len, p1, p2_len, p2); + + return 0; +} + +int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3) +{ + int p1_len = strlen(p1); + int p2_len = strlen(p2); + int p3_len = strlen(p3); + + if (p1_len + p2_len + p3_len + 3 >= PATH_MAX) + return -ENAMETOOLONG; + + if (p1_len && p1[p1_len - 1] == '/') + p1_len--; + if (p2_len && p2[p2_len - 1] == '/') + p2_len--; + if (p3_len && p3[p3_len - 1] == '/') + p3_len--; + sprintf(out, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3); + + return 0; +} + /* Subvolume helper functions */ /* * test if name is a correct subvolume name diff --git a/common/path-utils.h b/common/path-utils.h index 0cabcb7d..d1b5e4e5 100644 --- a/common/path-utils.h +++ b/common/path-utils.h @@ -23,6 +23,8 @@ char *canonicalize_dm_name(const char *ptname); char *canonicalize_path(const char *path); int arg_copy_path(char *dest, const char *src, int destlen); +int path_cat_out(char *out, const char *p1, const char *p2); +int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3); char *__strncpy_null(char *dest, const char *src, size_t n); /* Helper to always get proper size of the destination string */ diff --git a/common/send-utils.c b/common/send-utils.c index 58eca58f..6778e2c6 100644 --- a/common/send-utils.c +++ b/common/send-utils.c @@ -749,40 +749,3 @@ void subvol_uuid_search_finit(struct subvol_uuid_search *s) { } #endif - -int path_cat_out(char *out, const char *p1, const char *p2) -{ - int p1_len = strlen(p1); - int p2_len = strlen(p2); - - if (p1_len + p2_len + 2 >= PATH_MAX) - return -ENAMETOOLONG; - - if (p1_len && p1[p1_len - 1] == '/') - p1_len--; - if (p2_len && p2[p2_len - 1] == '/') - p2_len--; - sprintf(out, "%.*s/%.*s", p1_len, p1, p2_len, p2); - - return 0; -} - -int path_cat3_out(char *out, const char *p1, const char *p2, const char *p3) -{ - int p1_len = strlen(p1); - int p2_len = strlen(p2); - int p3_len = strlen(p3); - - if (p1_len + p2_len + p3_len + 3 >= PATH_MAX) - return -ENAMETOOLONG; - - if (p1_len && p1[p1_len - 1] == '/') - p1_len--; - if (p2_len && p2[p2_len - 1] == '/') - p2_len--; - if (p3_len && p3[p3_len - 1] == '/') - p3_len--; - sprintf(out, "%.*s/%.*s/%.*s", p1_len, p1, p2_len, p2, p3_len, p3); - - return 0; -}