1 #include "../burp.h"
2 #include "../action.h"
3 #include "../asfd.h"
4 #include "../async.h"
5 #include "../cntr.h"
6 #include "../conf.h"
7 #include "../handy.h"
8 #include "../log.h"
9 #include "backup_phase1.h"
10 #include "cvss.h"
11 #include "protocol1/backup_phase2.h"
12 #include "protocol2/backup_phase2.h"
13 #include "backup.h"
14 
15 #ifdef HAVE_WIN32
set_priority(int priority,const char * str)16 static void set_priority(int priority, const char *str)
17 {
18 	if(SetThreadPriority(GetCurrentThread(), priority))
19 		logp("Set %s\n", str);
20 	else
21 		logp("Failed to set %s\n", str);
22 }
23 
set_low_priority(void)24 static void set_low_priority(void)
25 {
26 	// Run timed backups with lower priority. I found that this has to be
27 	// done after the snapshot, or the snapshot never finishes. At least, I
28 	// waited 30 minutes with nothing happening.
29 #if defined(B_VSS_XP) || defined(B_VSS_W2K3)
30 	set_priority(THREAD_PRIORITY_LOWEST,
31 		"thread_priority_lowest");
32 #else
33 	set_priority(THREAD_MODE_BACKGROUND_BEGIN,
34 		"thread_mode_background_begin");
35 #endif
36 }
37 
unset_low_priority(void)38 static void unset_low_priority(void)
39 {
40 	set_priority(THREAD_MODE_BACKGROUND_END,
41 		"thread_mode_background_end");
42 }
43 #endif
44 
45 // Return 0 for OK, -1 for error.
do_backup_client(struct asfd * asfd,struct conf ** confs,enum action action,int resume)46 int do_backup_client(struct asfd *asfd, struct conf **confs, enum action action,
47 	int resume)
48 {
49 	int ret=-1;
50 	int breaking=get_int(confs[OPT_BREAKPOINT]);
51 
52 	if(action==ACTION_ESTIMATE)
53 		logp("do estimate client\n");
54 	else
55 	{
56 		logp("do backup client\n");
57 		if(get_protocol(confs)==PROTO_1)
58 			logp("Using librsync hash %s\n",
59 			  rshash_to_str(get_e_rshash(confs[OPT_RSHASH])));
60 	}
61 
62 #ifdef HAVE_WIN32
63 	win32_enable_backup_privileges();
64 #ifdef WIN32_VSS
65 	if(win32_start_vss(asfd, confs))
66 	{
67 		log_and_send(asfd, "Problem with VSS\n");
68 		return ret;
69 	}
70 #endif
71 	if(action==ACTION_BACKUP_TIMED) set_low_priority();
72 #endif
73 
74 	// Scan the file system and send the results to the server.
75 	// Skip phase1 if the server wanted to resume.
76 	if(!resume)
77 	{
78 		if(breaking==1)
79 		{
80 			breakpoint(breaking, __func__);
81 			goto end;
82 		}
83 		if(backup_phase1_client(asfd, confs))
84 			goto end;
85 	}
86 
87 	switch(action)
88 	{
89 		case ACTION_DIFF:
90 		case ACTION_DIFF_LONG:
91 			ret=1;
92 			goto end;
93 		case ACTION_ESTIMATE:
94 			cntr_print(get_cntr(confs), ACTION_ESTIMATE);
95 			break;
96 		default:
97 			// Now, the server will be telling us what data we need
98 			// to send.
99 			if(breaking==2)
100 			{
101 				breakpoint(breaking, __func__);
102 				goto end;
103 			}
104 
105 			if(get_protocol(confs)==PROTO_1)
106 				ret=backup_phase2_client_protocol1(asfd,
107 					confs, resume);
108 			else
109 				ret=backup_phase2_client_protocol2(asfd,
110 					confs, resume);
111 			if(ret) goto end;
112 			break;
113 	}
114 
115 	ret=0;
116 end:
117 #if defined(HAVE_WIN32)
118 	if(action==ACTION_BACKUP_TIMED) unset_low_priority();
119 #if defined(WIN32_VSS)
120 	win32_stop_vss();
121 #endif
122 #endif
123 	return ret;
124 }
125