@@ -46,6 +46,22 @@
#define STRIPE_LEN (64 * 1024)
#define EXT2_IMAGE_SUBVOL_OBJECTID BTRFS_FIRST_FREE_OBJECTID
+#define PROGRESS_BAR_LENGTH 48
+
+typedef struct {
+ __u32 inoinuse;
+ __u32 inocopied;
+ __u32 progressunit;
+ __u32 halfprogunit;
+ __u32 progressperc;
+ int istty;
+ int progress;
+ int flagprint;
+ float perc;
+ char msg[128];
+ char bar[256];
+} progress_bar;
+
/*
* Open Ext2fs in readonly mode, read block allocation bitmap and
* inode bitmap into memory.
@@ -1119,6 +1135,78 @@ fail:
ret = -1;
return ret;
}
+
+/*
+ * init values of progress bar.
+ */
+static void init_progress_bar(ext2_filsys ext2_fs, progress_bar *pb)
+{
+ int i;
+ sprintf(pb->msg, "Creating btrfs metadata");
+
+ if (isatty(fileno(stdout)) != 0)
+ pb->istty = 1;
+ else
+ pb->istty = 0;
+ pb->inoinuse = ext2_fs->super->s_inodes_count
+ - ext2_fs->super->s_free_inodes_count;
+ pb->inocopied = 0;
+ pb->progressunit = pb->inoinuse / PROGRESS_BAR_LENGTH;
+ pb->halfprogunit = (int)(pb->progressunit/2);
+ pb->progressperc = pb->inoinuse / 100;
+ pb->progress = 0;
+ pb->flagprint = 0;
+ pb->perc = 0;
+ pb->bar[0] = '[';
+ for (i = 1; i < PROGRESS_BAR_LENGTH+1; i++) pb->bar[i] = ' ';
+ pb->bar[PROGRESS_BAR_LENGTH+1] = ']';
+ pb->bar[PROGRESS_BAR_LENGTH+2] = '\0';
+ if (pb->istty) {
+ printf("\e[?25l"); // hide the cursor
+ printf("\r%s %s %3d%% ", pb->msg, pb->bar, (int)pb->perc);
+ fflush(stdout);
+ }
+}
+
+/*
+ * show progress in bar.
+ */
+static void update_progress_bar(progress_bar *pb)
+{
+ pb->flagprint = 0;
+ if (pb->inocopied % pb->progressunit == 0) {
+ pb->flagprint = 1;
+ pb->progress += 1;
+ if (pb->progress < PROGRESS_BAR_LENGTH+1) pb->bar[pb->progress] = '=';
+ } else if (pb->inocopied % pb->progressunit == pb->halfprogunit) {
+ pb->flagprint = 1;
+ if (pb->progress+1 < PROGRESS_BAR_LENGTH+1) pb->bar[pb->progress+1] = '-';
+ }
+ if (pb->inocopied % pb->progressperc == 0) {
+ pb->flagprint = 1;
+ pb->perc = ((float)pb->inocopied / (float)pb->inoinuse) * 100;
+ }
+ if (pb->flagprint) {
+ printf("\r%s %s %3d%%", pb->msg, pb->bar, (int)pb->perc);
+ fflush(stdout);
+ }
+}
+
+/*
+ * show process is complete in progress bar.
+ */
+static void close_progress_bar(progress_bar *pb)
+{
+ pb->progress++;
+ for (; pb->progress < PROGRESS_BAR_LENGTH+1; pb->progress++)
pb->bar[pb->progress] = '=';
+ if (pb->istty) {
+ printf("\r%s %s 100%%\n", pb->msg, pb->bar);
+ printf("\e[?25h"); // show the cursor
+ } else
+ printf("%s %s 100%%\n", pb->msg, pb->bar);
+ fflush(stdout);
+}
+
/*
* scan ext2's inode bitmap and copy all used inodes.