X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/blobdiff_plain/0479eb76015913a40a2d0423bdd6cfeec4a3a3e3..6d301fa3de32d35cd88b113aef720676a4fbc573:/util.c?ds=sidebyside diff --git a/util.c b/util.c index 4b6c11fd..ea247751 100644 --- a/util.c +++ b/util.c @@ -22,12 +22,13 @@ #include "rsync.h" #include "ifuncs.h" +#include "itypes.h" +#include "inums.h" extern int dry_run; extern int module_id; extern int modify_window; extern int relative_paths; -extern int human_readable; extern int preserve_xattrs; extern char *module_dir; extern unsigned int module_dirlen; @@ -1024,6 +1025,34 @@ int change_dir(const char *dir, int set_path_only) return 1; } +/* This will make a relative path absolute and clean it up via clean_fname(). + * Returns the string, which might be newly allocated, or NULL on error. */ +char *normalize_path(char *path, BOOL force_newbuf, unsigned int *len_ptr) +{ + unsigned int len; + + if (*path != '/') { /* Make path absolute. */ + int len = strlen(path); + if (curr_dir_len + 1 + len >= sizeof curr_dir) + return NULL; + curr_dir[curr_dir_len] = '/'; + memcpy(curr_dir + curr_dir_len + 1, path, len + 1); + if (!(path = strdup(curr_dir))) + out_of_memory("normalize_path"); + curr_dir[curr_dir_len] = '\0'; + } else if (force_newbuf) { + if (!(path = strdup(path))) + out_of_memory("normalize_path"); + } + + len = clean_fname(path, CFN_COLLAPSE_DOT_DOT_DIRS | CFN_DROP_TRAILING_DOT_DIR); + + if (len_ptr) + *len_ptr = len; + + return path; +} + /** * Return a quoted string with the full pathname of the indicated filename. * The string " (in MODNAME)" may also be appended. The returned pointer @@ -1192,21 +1221,6 @@ int unsafe_symlink(const char *dest, const char *src) return (depth < 0); } -/* Return the double number as a string. If the --human-readable option was - * specified, we may output the number in K, M, or G units. We use a buffer - * from big_num() to return our result. */ -char *human_dnum(double dnum, int decimal_digits) -{ - char *buf = big_num(dnum, human_readable); - int len = strlen(buf); - if (isDigit(buf + len - 1)) { - /* There's extra room in buf prior to the start of the num. */ - buf -= decimal_digits + 1; - snprintf(buf, len + decimal_digits + 2, "%.*f", decimal_digits, dnum); - } - return buf; -} - /* Return the date and time as a string. Some callers tweak returned buf. */ char *timestring(time_t t) { @@ -1545,7 +1559,7 @@ void *expand_item_list(item_list *lp, size_t item_size, new_ptr = _realloc_array(lp->items, item_size, new_size); if (DEBUG_GTE(FLIST, 3)) { rprintf(FINFO, "[%s] expand %s to %s bytes, did%s move\n", - who_am_i(), desc, big_num(new_size * item_size, 0), + who_am_i(), desc, big_num(new_size * item_size), new_ptr == lp->items ? " not" : ""); } if (!new_ptr)