Make idev, hlink and file_struct + strings use allocation
[rsync/rsync.git] / progress.c
CommitLineData
fcb69e5c 1/* -*- c-file-style: "linux" -*-
603e6b05
WD
2 *
3 * Copyright (C) 1996-2000 by Andrew Tridgell
fcb69e5c
MP
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
603e6b05 6 *
fcb69e5c
MP
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
603e6b05 11 *
fcb69e5c
MP
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
603e6b05 16 *
fcb69e5c
MP
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
b35d0d8e
MP
22#include "rsync.h"
23
fb6e0ea1 24extern struct stats stats;
5562deb1
WD
25extern int am_server;
26
b35d0d8e
MP
27static OFF_T last_ofs;
28static struct timeval print_time;
29static struct timeval start_time;
30static OFF_T start_ofs;
31
32static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
33{
603e6b05
WD
34 return (t2->tv_sec - t1->tv_sec) * 1000
35 + (t2->tv_usec - t1->tv_usec) / 1000;
b35d0d8e
MP
36}
37
38
39/**
40 * @param ofs Current position in file
41 * @param size Total size of file
42 * @param is_last True if this is the last time progress will be
43 * printed for this file, so we should output a newline. (Not
44 * necessarily the same as all bytes being received.)
45 **/
46static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
47 int is_last)
48{
fb6e0ea1 49 char eol[256];
603e6b05
WD
50 int pct = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
51 unsigned long diff = msdiff(&start_time, now);
52 double rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
53 const char *units;
54 /* If we've finished transferring this file, show the time taken;
55 * otherwise show expected time to complete. That's kind of
56 * inconsistent, but people can probably cope. Hopefully we'll
57 * get more consistent and complete progress reporting soon. --
58 * mbp */
59 double remain = is_last ? (double) diff / 1000.0
60 : rate ? (double) (size-ofs) / rate / 1000.0 : 0.0;
61 int remain_h, remain_m, remain_s;
b35d0d8e 62
603e6b05
WD
63 if (rate > 1024*1024) {
64 rate /= 1024.0 * 1024.0;
65 units = "GB/s";
66 } else if (rate > 1024) {
67 rate /= 1024.0;
68 units = "MB/s";
69 } else {
70 units = "kB/s";
71 }
72
73 remain_s = (int) remain % 60;
74 remain_m = (int) (remain / 60.0) % 60;
75 remain_h = (int) (remain / 3600.0);
b35d0d8e 76
fb6e0ea1
WD
77 if (is_last) {
78 snprintf(eol, sizeof eol, " (%d, %.1f%% of %d)\n",
79 stats.num_transferred_files,
3eaf615f
WD
80 (float)((stats.current_file_index+1) * 100)
81 / stats.num_files,
fb6e0ea1
WD
82 stats.num_files);
83 } else
84 strcpy(eol, "\r");
603e6b05
WD
85 rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
86 (double) ofs, pct, rate, units,
fb6e0ea1 87 remain_h, remain_m, remain_s, eol);
b35d0d8e
MP
88}
89
90void end_progress(OFF_T size)
91{
1c8162a9
WD
92 if (!am_server) {
93 struct timeval now;
94 gettimeofday(&now, NULL);
95 rprint_progress(size, size, &now, True);
b35d0d8e 96 }
603e6b05
WD
97 last_ofs = 0;
98 start_ofs = 0;
99 print_time.tv_sec = print_time.tv_usec = 0;
100 start_time.tv_sec = start_time.tv_usec = 0;
b35d0d8e
MP
101}
102
103void show_progress(OFF_T ofs, OFF_T size)
104{
1c8162a9 105 struct timeval now;
b35d0d8e 106
1c8162a9
WD
107 if (!start_time.tv_sec) {
108 gettimeofday(&now, NULL);
603e6b05 109 start_time.tv_sec = now.tv_sec;
1c8162a9 110 start_time.tv_usec = now.tv_usec;
603e6b05 111 start_ofs = ofs;
1c8162a9
WD
112 if (am_server)
113 return;
114 }
115 else {
116 if (am_server)
117 return;
118 gettimeofday(&now, NULL);
119 }
b35d0d8e 120
603e6b05 121 if (ofs > last_ofs + 1000 && msdiff(&print_time, &now) > 250) {
1c8162a9
WD
122 rprint_progress(ofs, size, &now, False);
123 last_ofs = ofs;
603e6b05 124 print_time.tv_sec = now.tv_sec;
1c8162a9 125 print_time.tv_usec = now.tv_usec;
b35d0d8e
MP
126 }
127}