1*6a54128fSAndroid Build Coastguard Worker /*
2*6a54128fSAndroid Build Coastguard Worker * progress.c - Numeric progress meter
3*6a54128fSAndroid Build Coastguard Worker *
4*6a54128fSAndroid Build Coastguard Worker * Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
5*6a54128fSAndroid Build Coastguard Worker * 2003, 2004, 2005 by Theodore Ts'o.
6*6a54128fSAndroid Build Coastguard Worker *
7*6a54128fSAndroid Build Coastguard Worker * %Begin-Header%
8*6a54128fSAndroid Build Coastguard Worker * This file may be redistributed under the terms of the GNU Public
9*6a54128fSAndroid Build Coastguard Worker * License.
10*6a54128fSAndroid Build Coastguard Worker * %End-Header%
11*6a54128fSAndroid Build Coastguard Worker */
12*6a54128fSAndroid Build Coastguard Worker
13*6a54128fSAndroid Build Coastguard Worker #include "config.h"
14*6a54128fSAndroid Build Coastguard Worker #include "ext2fs.h"
15*6a54128fSAndroid Build Coastguard Worker #include "ext2fsP.h"
16*6a54128fSAndroid Build Coastguard Worker
17*6a54128fSAndroid Build Coastguard Worker #include <time.h>
18*6a54128fSAndroid Build Coastguard Worker
19*6a54128fSAndroid Build Coastguard Worker static char spaces[80], backspaces[80];
20*6a54128fSAndroid Build Coastguard Worker static time_t last_update;
21*6a54128fSAndroid Build Coastguard Worker
22*6a54128fSAndroid Build Coastguard Worker struct ext2fs_progress_ops ext2fs_numeric_progress_ops = {
23*6a54128fSAndroid Build Coastguard Worker .init = ext2fs_numeric_progress_init,
24*6a54128fSAndroid Build Coastguard Worker .update = ext2fs_numeric_progress_update,
25*6a54128fSAndroid Build Coastguard Worker .close = ext2fs_numeric_progress_close,
26*6a54128fSAndroid Build Coastguard Worker };
27*6a54128fSAndroid Build Coastguard Worker
int_log10(unsigned int arg)28*6a54128fSAndroid Build Coastguard Worker static int int_log10(unsigned int arg)
29*6a54128fSAndroid Build Coastguard Worker {
30*6a54128fSAndroid Build Coastguard Worker int l;
31*6a54128fSAndroid Build Coastguard Worker
32*6a54128fSAndroid Build Coastguard Worker for (l=0; arg ; l++)
33*6a54128fSAndroid Build Coastguard Worker arg = arg / 10;
34*6a54128fSAndroid Build Coastguard Worker return l;
35*6a54128fSAndroid Build Coastguard Worker }
36*6a54128fSAndroid Build Coastguard Worker
ext2fs_numeric_progress_init(ext2_filsys fs,struct ext2fs_numeric_progress_struct * progress,const char * label,__u64 max)37*6a54128fSAndroid Build Coastguard Worker void ext2fs_numeric_progress_init(ext2_filsys fs,
38*6a54128fSAndroid Build Coastguard Worker struct ext2fs_numeric_progress_struct * progress,
39*6a54128fSAndroid Build Coastguard Worker const char *label, __u64 max)
40*6a54128fSAndroid Build Coastguard Worker {
41*6a54128fSAndroid Build Coastguard Worker /*
42*6a54128fSAndroid Build Coastguard Worker * The PRINT_PROGRESS flag turns on or off ALL
43*6a54128fSAndroid Build Coastguard Worker * progress-related messages, whereas the SKIP_PROGRESS
44*6a54128fSAndroid Build Coastguard Worker * environment variable prints the start and end messages but
45*6a54128fSAndroid Build Coastguard Worker * not the numeric countdown in the middle.
46*6a54128fSAndroid Build Coastguard Worker */
47*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
48*6a54128fSAndroid Build Coastguard Worker return;
49*6a54128fSAndroid Build Coastguard Worker
50*6a54128fSAndroid Build Coastguard Worker memset(spaces, ' ', sizeof(spaces)-1);
51*6a54128fSAndroid Build Coastguard Worker spaces[sizeof(spaces)-1] = 0;
52*6a54128fSAndroid Build Coastguard Worker memset(backspaces, '\b', sizeof(backspaces)-1);
53*6a54128fSAndroid Build Coastguard Worker backspaces[sizeof(backspaces)-1] = 0;
54*6a54128fSAndroid Build Coastguard Worker
55*6a54128fSAndroid Build Coastguard Worker memset(progress, 0, sizeof(*progress));
56*6a54128fSAndroid Build Coastguard Worker if (getenv("E2FSPROGS_SKIP_PROGRESS"))
57*6a54128fSAndroid Build Coastguard Worker progress->skip_progress++;
58*6a54128fSAndroid Build Coastguard Worker
59*6a54128fSAndroid Build Coastguard Worker
60*6a54128fSAndroid Build Coastguard Worker /*
61*6a54128fSAndroid Build Coastguard Worker * Figure out how many digits we need
62*6a54128fSAndroid Build Coastguard Worker */
63*6a54128fSAndroid Build Coastguard Worker progress->max = max;
64*6a54128fSAndroid Build Coastguard Worker progress->log_max = int_log10(max);
65*6a54128fSAndroid Build Coastguard Worker
66*6a54128fSAndroid Build Coastguard Worker if (label) {
67*6a54128fSAndroid Build Coastguard Worker fputs(label, stdout);
68*6a54128fSAndroid Build Coastguard Worker fflush(stdout);
69*6a54128fSAndroid Build Coastguard Worker }
70*6a54128fSAndroid Build Coastguard Worker last_update = 0;
71*6a54128fSAndroid Build Coastguard Worker }
72*6a54128fSAndroid Build Coastguard Worker
ext2fs_numeric_progress_update(ext2_filsys fs,struct ext2fs_numeric_progress_struct * progress,__u64 val)73*6a54128fSAndroid Build Coastguard Worker void ext2fs_numeric_progress_update(ext2_filsys fs,
74*6a54128fSAndroid Build Coastguard Worker struct ext2fs_numeric_progress_struct * progress,
75*6a54128fSAndroid Build Coastguard Worker __u64 val)
76*6a54128fSAndroid Build Coastguard Worker {
77*6a54128fSAndroid Build Coastguard Worker time_t now;
78*6a54128fSAndroid Build Coastguard Worker
79*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
80*6a54128fSAndroid Build Coastguard Worker return;
81*6a54128fSAndroid Build Coastguard Worker if (progress->skip_progress)
82*6a54128fSAndroid Build Coastguard Worker return;
83*6a54128fSAndroid Build Coastguard Worker now = time(0);
84*6a54128fSAndroid Build Coastguard Worker if (now == last_update)
85*6a54128fSAndroid Build Coastguard Worker return;
86*6a54128fSAndroid Build Coastguard Worker last_update = now;
87*6a54128fSAndroid Build Coastguard Worker
88*6a54128fSAndroid Build Coastguard Worker printf("%*llu/%*llu", progress->log_max, (unsigned long long) val,
89*6a54128fSAndroid Build Coastguard Worker progress->log_max, (unsigned long long) progress->max);
90*6a54128fSAndroid Build Coastguard Worker fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
91*6a54128fSAndroid Build Coastguard Worker }
92*6a54128fSAndroid Build Coastguard Worker
ext2fs_numeric_progress_close(ext2_filsys fs,struct ext2fs_numeric_progress_struct * progress,const char * message)93*6a54128fSAndroid Build Coastguard Worker void ext2fs_numeric_progress_close(ext2_filsys fs,
94*6a54128fSAndroid Build Coastguard Worker struct ext2fs_numeric_progress_struct * progress,
95*6a54128fSAndroid Build Coastguard Worker const char *message)
96*6a54128fSAndroid Build Coastguard Worker {
97*6a54128fSAndroid Build Coastguard Worker if (!(fs->flags & EXT2_FLAG_PRINT_PROGRESS))
98*6a54128fSAndroid Build Coastguard Worker return;
99*6a54128fSAndroid Build Coastguard Worker fprintf(stdout, "%.*s", (2*progress->log_max)+1, spaces);
100*6a54128fSAndroid Build Coastguard Worker fprintf(stdout, "%.*s", (2*progress->log_max)+1, backspaces);
101*6a54128fSAndroid Build Coastguard Worker if (message)
102*6a54128fSAndroid Build Coastguard Worker fputs(message, stdout);
103*6a54128fSAndroid Build Coastguard Worker }
104