common: Move size format helper to utils and use it in idevicebackup tools
diff --git a/common/utils.c b/common/utils.c
index f95ecfd..4a45d95 100644
--- a/common/utils.c
+++ b/common/utils.c
@@ -139,6 +139,28 @@
return out;
}
+char *string_format_size(uint64_t size)
+{
+ char buf[80];
+ double sz;
+ if (size >= 1000000000000LL) {
+ sz = ((double)size / 1000000000000.0f);
+ sprintf(buf, "%0.1f TB", sz);
+ } else if (size >= 1000000000LL) {
+ sz = ((double)size / 1000000000.0f);
+ sprintf(buf, "%0.1f GB", sz);
+ } else if (size >= 1000000LL) {
+ sz = ((double)size / 1000000.0f);
+ sprintf(buf, "%0.1f MB", sz);
+ } else if (size >= 1000LL) {
+ sz = ((double)size / 1000.0f);
+ sprintf(buf, "%0.1f KB", sz);
+ } else {
+ sprintf(buf, "%d Bytes", (int)size);
+ }
+ return strdup(buf);
+}
+
char *string_toupper(char* str)
{
char *res = strdup(str);
diff --git a/common/utils.h b/common/utils.h
index 5cd4a53..97d3748 100644
--- a/common/utils.h
+++ b/common/utils.h
@@ -38,6 +38,7 @@
#endif
char *string_concat(const char *str, ...);
char *string_build_path(const char *elem, ...);
+char *string_format_size(uint64_t size);
char *string_toupper(char *str);
char *generate_uuid();
diff --git a/tools/idevicebackup.c b/tools/idevicebackup.c
index 8260de6..195f837 100644
--- a/tools/idevicebackup.c
+++ b/tools/idevicebackup.c
@@ -224,25 +224,6 @@
}
}
-static char* format_size_for_display(uint64_t size)
-{
- char buf[32];
- double sz;
- if (size >= 1000000000LL) {
- sz = ((double)size / 1000000000.0f);
- sprintf(buf, "%0.1f GB", sz);
- } else if (size >= 1000000LL) {
- sz = ((double)size / 1000000.0f);
- sprintf(buf, "%0.1f MB", sz);
- } else if (size >= 1000LL) {
- sz = ((double)size / 1000.0f);
- sprintf(buf, "%0.1f kB", sz);
- } else {
- sprintf(buf, "%d Bytes", (int)size);
- }
- return strdup(buf);
-}
-
static plist_t mobilebackup_factory_info_plist_new(const char* udid)
{
/* gather data from lockdown */
@@ -1037,7 +1018,7 @@
node = plist_dict_get_item(node_tmp, "BackupTotalSizeKey");
if (node) {
plist_get_uint_val(node, &backup_total_size);
- format_size = format_size_for_display(backup_total_size);
+ format_size = string_format_size(backup_total_size);
printf("Backup data requires %s on the disk.\n", format_size);
free(format_size);
}
@@ -1067,15 +1048,15 @@
plist_get_uint_val(node, &file_size);
backup_real_size += file_size;
- format_size = format_size_for_display(backup_real_size);
+ format_size = string_format_size(backup_real_size);
printf("(%s", format_size);
free(format_size);
- format_size = format_size_for_display(backup_total_size);
+ format_size = string_format_size(backup_total_size);
printf("/%s): ", format_size);
free(format_size);
- format_size = format_size_for_display(file_size);
+ format_size = string_format_size(file_size);
printf("Receiving file %s (%s)... \n", filename_source, format_size);
free(format_size);
diff --git a/tools/idevicebackup2.c b/tools/idevicebackup2.c
index 4fe5751..a43cbda 100644
--- a/tools/idevicebackup2.c
+++ b/tools/idevicebackup2.c
@@ -189,25 +189,6 @@
return res;
}
-static char* format_size_for_display(uint64_t size)
-{
- char buf[32];
- double sz;
- if (size >= 1000000000LL) {
- sz = ((double)size / 1000000000.0f);
- sprintf(buf, "%0.1f GB", sz);
- } else if (size >= 1000000LL) {
- sz = ((double)size / 1000000.0f);
- sprintf(buf, "%0.1f MB", sz);
- } else if (size >= 1000LL) {
- sz = ((double)size / 1000.0f);
- sprintf(buf, "%0.1f kB", sz);
- } else {
- sprintf(buf, "%d Bytes", (int)size);
- }
- return strdup(buf);
-}
-
static plist_t mobilebackup_factory_info_plist_new(const char* udid, lockdownd_client_t lockdown, afc_client_t afc)
{
/* gather data from lockdown */
@@ -404,10 +385,10 @@
print_progress_real((double)progress, 0);
- format_size = format_size_for_display(current);
+ format_size = string_format_size(current);
PRINT_VERBOSE(1, " (%s", format_size);
free(format_size);
- format_size = format_size_for_display(total);
+ format_size = string_format_size(total);
PRINT_VERBOSE(1, "/%s) ", format_size);
free(format_size);
@@ -543,7 +524,7 @@
total = fst.st_size;
- char *format_size = format_size_for_display(total);
+ char *format_size = string_format_size(total);
PRINT_VERBOSE(1, "Sending '%s' (%s)\n", path, format_size);
free(format_size);