1 /* NBD client library in userspace.
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "array-size.h"
36 /* Display the progress bar. */
38 do_progress_bar (off_t pos
, int64_t size
)
40 /* Note the spinner is covered with the cursor which usually makes
41 * it appear inverse video.
43 static const char *spinner
[] = { "▝", "▐", "▗", "▃", "▖", "▍", "▘", "▀" };
44 static const char *spinner_100
= "█";
45 static int spinpos
= 0;
47 double frac
= (double) pos
/ size
;
51 if (frac
< 0) frac
= 0; else if (frac
> 1) frac
= 1;
54 snprintf (msg
, sizeof msg
,
55 "%s 100%% [****************************************]\n",
57 progress
= false; /* Don't print any more progress bar messages. */
59 snprintf (msg
, sizeof msg
,
60 "%s %3d%% [----------------------------------------]\r",
61 spinner
[spinpos
], (int)(100*frac
));
62 n
= strcspn (msg
, "-");
63 for (i
= 0; i
< 40*frac
; ++i
)
65 spinpos
= (spinpos
+1) % ARRAY_SIZE (spinner
);
68 #pragma GCC diagnostic push
69 #pragma GCC diagnostic ignored "-Wunused-result"
70 write (fileno (stderr
), msg
, strlen (msg
));
71 #pragma GCC diagnostic pop
74 /* Machine-readable progress bar used with --progress-fd. */
76 do_progress_bar_fd (off_t pos
, int64_t size
)
78 double frac
= (double) pos
/ size
;
81 if (frac
< 0) frac
= 0; else if (frac
> 1) frac
= 1;
83 snprintf (msg
, sizeof msg
, "100/100\n");
85 snprintf (msg
, sizeof msg
, "%d/100\n", (int)(100*frac
));
87 #pragma GCC diagnostic push
88 #pragma GCC diagnostic ignored "-Wunused-result"
89 write (progress_fd
, msg
, strlen (msg
));
90 #pragma GCC diagnostic pop
94 progress_bar (off_t pos
, int64_t size
)
96 static pthread_mutex_t lock
= PTHREAD_MUTEX_INITIALIZER
;
103 pthread_mutex_lock (&lock
);
104 if (progress_fd
== -1)
105 do_progress_bar (pos
, size
);
107 do_progress_bar_fd (pos
, size
);
108 pthread_mutex_unlock (&lock
);