Revert "ci: skip "lib/test-fork-safe-execvpe.sh" on Alpine Linux"
[libnbd.git] / copy / progress.c
blobcc0910d676f658e9e0a3c6436390e1e0687abd1b
1 /* NBD client library in userspace.
2 * Copyright Red Hat
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
19 #include <config.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdint.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <unistd.h>
28 #include <pthread.h>
30 #include <libnbd.h>
32 #include "array-size.h"
34 #include "nbdcopy.h"
36 /* Display the progress bar. */
37 static void
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;
48 char msg[80];
49 size_t n, i;
51 if (frac < 0) frac = 0; else if (frac > 1) frac = 1;
53 if (frac == 1) {
54 snprintf (msg, sizeof msg,
55 "%s 100%% [****************************************]\n",
56 spinner_100);
57 progress = false; /* Don't print any more progress bar messages. */
58 } else {
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)
64 msg[n+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. */
75 static void
76 do_progress_bar_fd (off_t pos, int64_t size)
78 double frac = (double) pos / size;
79 char msg[80];
81 if (frac < 0) frac = 0; else if (frac > 1) frac = 1;
82 if (frac == 1)
83 snprintf (msg, sizeof msg, "100/100\n");
84 else
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
93 void
94 progress_bar (off_t pos, int64_t size)
96 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
98 if (!progress)
99 return;
100 if (size == 0)
101 return;
103 pthread_mutex_lock (&lock);
104 if (progress_fd == -1)
105 do_progress_bar (pos, size);
106 else
107 do_progress_bar_fd (pos, size);
108 pthread_mutex_unlock (&lock);