1 //*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
22 #include "../my_config.h"
23 
24 extern "C"
25 {
26 #if HAVE_ERRNO_H
27 #include <errno.h>
28 #endif
29 
30 #if HAVE_STRING_H
31 #include <string.h>
32 #endif
33 }
34 
35 #include <string>
36 
37 #include "statistics.hpp"
38 #include "tools.hpp"
39 
40 using namespace std;
41 
42 namespace libdar
43 {
44 
clear()45     void statistics::clear()
46     {
47 	if(locking)
48 	{
49 	    LOCK_IN;
50 	    treated = hard_links = skipped = ignored = tooold = errored = deleted = ea_treated = byte_amount = fsa_treated = 0;
51 	    LOCK_OUT;
52 	}
53 	else
54 	    treated = hard_links = skipped = ignored = tooold = errored = deleted = ea_treated = byte_amount = fsa_treated = 0;
55     }
56 
total() const57     infinint statistics::total() const
58     {
59 	infinint ret;
60 
61 	if(locking)
62 	{
63 	    LOCK_IN_CONST;
64 	    ret = treated+skipped+ignored+tooold+errored+deleted;
65 		// hard_link are also counted in other counters
66 	    LOCK_OUT_CONST;
67 	}
68 	else
69 	    ret = treated+skipped+ignored+tooold+errored+deleted;
70 
71 	return ret;
72     }
73 
init(bool lock)74     void statistics::init(bool lock)
75     {
76 	locking = lock;
77 
78 #if MUTEX_WORKS
79 	if(locking)
80 	    if(pthread_mutex_init(&lock_mutex, nullptr) < 0)
81 		throw Erange("statistics::statistics", string(dar_gettext("Error while initializing \"mutex\" for class \"statistics\": ")) + tools_strerror_r(errno));
82 #else
83 	if(locking)
84 	    throw Ecompilation("Thread support not activated, cannot use statistics object with lock activated");
85 #endif
86 	if(locking)
87 	{
88 	    increment = & statistics::increment_locked;
89 	    add_to = & statistics::add_to_locked;
90 	    returned = & statistics::returned_locked;
91 	    decrement = & statistics::decrement_locked;
92 	    set_to = & statistics::set_to_locked;
93 	    sub_from = & statistics::sub_from_locked;
94 	}
95 	else
96 	{
97 	    increment = & statistics::increment_unlocked;
98 	    add_to = & statistics::add_to_unlocked;
99 	    returned = & statistics::returned_unlocked;
100 	    decrement = & statistics::decrement_unlocked;
101 	    set_to = & statistics::set_to_unlocked;
102 	    sub_from = & statistics::sub_from_unlocked;
103 	}
104     }
105 
106 
detruit()107     void statistics::detruit()
108     {
109 #if MUTEX_WORKS
110 	if(locking)
111 	    pthread_mutex_destroy(&lock_mutex);
112 #endif
113     }
114 
copy_from(const statistics & ref)115     void statistics::copy_from(const statistics & ref)
116     {
117 	init(ref.locking);
118 
119 	treated = ref.treated;
120 	hard_links = ref.hard_links;
121 	skipped = ref.skipped;
122 	ignored = ref.ignored;
123 	tooold = ref.tooold;
124 	errored = ref.errored;
125 	deleted = ref.deleted;
126 	ea_treated = ref.ea_treated;
127 	byte_amount = ref.byte_amount;
128 	fsa_treated = ref.fsa_treated;
129     }
130 
dump(user_interaction & dialog) const131     void statistics::dump(user_interaction & dialog) const
132     {
133 	dialog.printf("--------- Statistics DUMP ----------");
134 	dialog.printf("locking = %c", locking ? 'y' : 'n');
135 	dialog.printf("treated = %i", &treated);
136 	dialog.printf("hard_links = %i", &hard_links);
137 	dialog.printf("skipped = %i", &skipped);
138 	dialog.printf("ignored = %i", &ignored);
139 	dialog.printf("tooold = %i", &tooold);
140 	dialog.printf("errored = %i", &errored);
141 	dialog.printf("deleted = %i", &deleted);
142 	dialog.printf("ea_treated = %i", &ea_treated);
143 	dialog.printf("byte_amount = %i", &byte_amount);
144 	dialog.printf("fsa_treated = %i", &fsa_treated);
145 	dialog.printf("------------------------------------");
146     }
147 
148 
149 } // end of namespace
150