Added the new --protocol option.
[rsync.git] / progress.c
blob083b6df279e000518796d14a0700f78311207e9e
1 /* -*- c-file-style: "linux" -*-
2 *
3 * Copyright (C) 1996-2000 by Andrew Tridgell
4 * Copyright (C) Paul Mackerras 1996
5 * Copyright (C) 2001, 2002 by Martin Pool <mbp@samba.org>
6 *
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.
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.
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.
22 #include "rsync.h"
24 static OFF_T last_ofs;
25 static struct timeval print_time;
26 static struct timeval start_time;
27 static OFF_T start_ofs;
29 static unsigned long msdiff(struct timeval *t1, struct timeval *t2)
31 return (t2->tv_sec - t1->tv_sec) * 1000
32 + (t2->tv_usec - t1->tv_usec) / 1000;
36 /**
37 * @param ofs Current position in file
38 * @param size Total size of file
39 * @param is_last True if this is the last time progress will be
40 * printed for this file, so we should output a newline. (Not
41 * necessarily the same as all bytes being received.)
42 **/
43 static void rprint_progress(OFF_T ofs, OFF_T size, struct timeval *now,
44 int is_last)
46 int pct = (ofs == size) ? 100 : (int)((100.0*ofs)/size);
47 unsigned long diff = msdiff(&start_time, now);
48 double rate = diff ? (double) (ofs-start_ofs) * 1000.0 / diff / 1024.0 : 0;
49 const char *units;
50 /* If we've finished transferring this file, show the time taken;
51 * otherwise show expected time to complete. That's kind of
52 * inconsistent, but people can probably cope. Hopefully we'll
53 * get more consistent and complete progress reporting soon. --
54 * mbp */
55 double remain = is_last
56 ? (double) diff / 1000.0
57 : rate ? (double) (size-ofs) / rate / 1000.0 : 0.0;
58 int remain_h, remain_m, remain_s;
60 if (rate > 1024*1024) {
61 rate /= 1024.0 * 1024.0;
62 units = "GB/s";
63 } else if (rate > 1024) {
64 rate /= 1024.0;
65 units = "MB/s";
66 } else {
67 units = "kB/s";
70 remain_s = (int) remain % 60;
71 remain_m = (int) (remain / 60.0) % 60;
72 remain_h = (int) (remain / 3600.0);
74 rprintf(FINFO, "%12.0f %3d%% %7.2f%s %4d:%02d:%02d%s",
75 (double) ofs, pct, rate, units,
76 remain_h, remain_m, remain_s,
77 is_last ? "\n" : "\r");
80 void end_progress(OFF_T size)
82 extern int am_server;
84 if (!am_server) {
85 struct timeval now;
86 gettimeofday(&now, NULL);
87 rprint_progress(size, size, &now, True);
89 last_ofs = 0;
90 start_ofs = 0;
91 print_time.tv_sec = print_time.tv_usec = 0;
92 start_time.tv_sec = start_time.tv_usec = 0;
95 void show_progress(OFF_T ofs, OFF_T size)
97 extern int am_server;
98 struct timeval now;
100 if (!start_time.tv_sec) {
101 gettimeofday(&now, NULL);
102 start_time.tv_sec = now.tv_sec;
103 start_time.tv_usec = now.tv_usec;
104 start_ofs = ofs;
105 if (am_server)
106 return;
108 else {
109 if (am_server)
110 return;
111 gettimeofday(&now, NULL);
114 if (ofs > last_ofs + 1000
115 && msdiff(&print_time, &now) > 250) {
116 rprint_progress(ofs, size, &now, False);
117 last_ofs = ofs;
118 print_time.tv_sec = now.tv_sec;
119 print_time.tv_usec = now.tv_usec;