|
Packit Service |
360c39 |
/**
|
|
Packit Service |
360c39 |
* Progress bar to give updates for operations in gfs2-utils.
|
|
Packit Service |
360c39 |
* Adapted from the simple progress bar in e2fsprogs progress.c
|
|
Packit Service |
360c39 |
*
|
|
Packit Service |
360c39 |
*/
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
#include <stdio.h>
|
|
Packit Service |
360c39 |
#include <string.h>
|
|
Packit Service |
360c39 |
#include <inttypes.h>
|
|
Packit Service |
360c39 |
#include <unistd.h>
|
|
Packit Service |
360c39 |
#include <time.h>
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
#include "progress.h"
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
static char spaces[44], backspaces[44];
|
|
Packit Service |
360c39 |
static time_t last_update;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
static int number_of_digits(int value)
|
|
Packit Service |
360c39 |
{
|
|
Packit Service |
360c39 |
int digits = 0;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
do {
|
|
Packit Service |
360c39 |
value /= 10;
|
|
Packit Service |
360c39 |
digits++;
|
|
Packit Service |
360c39 |
} while (value != 0);
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
return digits;
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
void gfs2_progress_init(struct gfs2_progress_bar *progress, uint64_t max, const char *message, int quiet)
|
|
Packit Service |
360c39 |
{
|
|
Packit Service |
360c39 |
/**
|
|
Packit Service |
360c39 |
* NOTE:
|
|
Packit Service |
360c39 |
*
|
|
Packit Service |
360c39 |
* Default operation is to output the progress indication
|
|
Packit Service |
360c39 |
* in full. Although we will honor the quiet flag in the
|
|
Packit Service |
360c39 |
* application, if this is set we skip progress bar any
|
|
Packit Service |
360c39 |
* update operations and output.
|
|
Packit Service |
360c39 |
*
|
|
Packit Service |
360c39 |
*/
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
memset(spaces, ' ', sizeof(spaces)-1);
|
|
Packit Service |
360c39 |
spaces[sizeof(spaces)-1] = 0;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
memset(backspaces, '\b', sizeof(backspaces)-1);
|
|
Packit Service |
360c39 |
backspaces[sizeof(backspaces)-1] = 0;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
memset(progress, 0, sizeof(*progress));
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
if (quiet) {
|
|
Packit Service |
360c39 |
progress->skip_progress++;
|
|
Packit Service |
360c39 |
return;
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
progress->max = max;
|
|
Packit Service |
360c39 |
progress->max_digits = number_of_digits(max);
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
if (message) {
|
|
Packit Service |
360c39 |
fputs(message, stdout);
|
|
Packit Service |
360c39 |
fflush(stdout);
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
last_update = 0;
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
void gfs2_progress_update(struct gfs2_progress_bar *progress, uint64_t value)
|
|
Packit Service |
360c39 |
{
|
|
Packit Service |
360c39 |
time_t current_time;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
if (progress->skip_progress || (!isatty(STDOUT_FILENO)))
|
|
Packit Service |
360c39 |
return;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
current_time = time(0);
|
|
Packit Service |
360c39 |
if (current_time == last_update)
|
|
Packit Service |
360c39 |
return;
|
|
Packit Service |
360c39 |
last_update = current_time;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
printf("[%*"PRIu64"/%*"PRIu64"]", progress->max_digits, value,
|
|
Packit Service |
360c39 |
progress->max_digits, progress->max);
|
|
Packit Service |
360c39 |
fflush(stdout);
|
|
Packit Service |
360c39 |
fprintf(stdout, "%.*s", (2 * progress->max_digits) + 3, backspaces);
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
void gfs2_progress_close(struct gfs2_progress_bar *progress, const char *message)
|
|
Packit Service |
360c39 |
{
|
|
Packit Service |
360c39 |
if (progress->skip_progress)
|
|
Packit Service |
360c39 |
return;
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
if (isatty(STDOUT_FILENO)) {
|
|
Packit Service |
360c39 |
fprintf(stdout, "%.*s", (2 * progress->max_digits) + 3, spaces);
|
|
Packit Service |
360c39 |
fprintf(stdout, "%.*s", (2 * progress->max_digits) + 3, backspaces);
|
|
Packit Service |
360c39 |
}
|
|
Packit Service |
360c39 |
|
|
Packit Service |
360c39 |
if (message)
|
|
Packit Service |
360c39 |
fputs(message, stdout);
|
|
Packit Service |
360c39 |
}
|