Project

General

Profile

Download (19.1 KB) Statistics
| Branch: | Tag: | Revision:
1
/*
2
 * gitsync
3
 *
4
 * part of pfSense (https://www.pfsense.org)
5
 * Copyright (c) 2010-2012 Erik Fonnesbeck
6
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions are met:
11
 *
12
 * 1. Redistributions of source code must retain the above copyright notice,
13
 *    this list of conditions and the following disclaimer.
14
 *
15
 * 2. Redistributions in binary form must reproduce the above copyright
16
 *    notice, this list of conditions and the following disclaimer in
17
 *    the documentation and/or other materials provided with the
18
 *    distribution.
19
 *
20
 * 3. All advertising materials mentioning features or use of this software
21
 *    must display the following acknowledgment:
22
 *    "This product includes software developed by the pfSense Project
23
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
24
 *
25
 * 4. The names "pfSense" and "pfSense Project" must not be used to
26
 *    endorse or promote products derived from this software without
27
 *    prior written permission. For written permission, please contact
28
 *    coreteam@pfsense.org.
29
 *
30
 * 5. Products derived from this software may not be called "pfSense"
31
 *    nor may "pfSense" appear in their names without prior written
32
 *    permission of the Electric Sheep Fencing, LLC.
33
 *
34
 * 6. Redistributions of any form whatsoever must retain the following
35
 *    acknowledgment:
36
 *
37
 * "This product includes software developed by the pfSense Project
38
 * for use in the pfSense software distribution (http://www.pfsense.org/).
39
 *
40
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
41
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
44
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51
 * OF THE POSSIBILITY OF SUCH DAMAGE.
52
 */
53

    
54
require_once("globals.inc");
55
require_once("filter.inc");
56
require_once("shaper.inc");
57
require_once("rrd.inc");
58
require_once("pfsense-utils.inc");
59

    
60
$GIT_PKG = "git"; // Either "git" or the full package URL
61
$GIT_BIN= "/usr/local/bin/git";
62
$GIT_REPO = "git://github.com/pfsense/pfsense.git";
63
$DEFAULT_BRANCH = "RELENG_2_3";
64
$CODIR =  "/root/pfsense";
65
$GITSYNC_MERGE = "/root/.gitsync_merge";
66

    
67
/* NOTE: Set branches here */
68
$branches = array(
69
	"master" => "2.4 development branch",
70
	"RELENG_2_3" => "2.3 development branch",
71
	"build_commit" => "The commit originally used to build the image"
72
);
73

    
74
global $g;
75
global $argv;
76
global $command_split;
77

    
78
if (is_array($command_split)) {
79
	$temp_args = array_slice($command_split, 2);
80
} else {
81
	$temp_args = array_slice($argv, 3);
82
}
83

    
84
$valid_args = array(
85
	"--minimal" => "\tPerform a copy of only the updated files.\n" .
86
	    "\tNot recommended if the system has files modified by any method other\n" .
87
	    "\tthan gitsync.\n",
88
	"--diff" => "\tPerform a copy of only the different and missing files.\n" .
89
	    "\tRecommended for SSD if system has files modified by any method other\n" .
90
	    "\tthan gitsync.\n",
91
	"--show-files" => "\tShow the updated, different and missing files.\n" .
92
	    "\t(when used with --minimal and --diff options)\n",
93
	"--show-command" => "\tShow the constructed command.\n",
94
	"--dry-run" => "\tDry-run only.  No files copied.\n",
95
	"--help" => "\tDisplay this help list.\n"
96
	);
97
$args = array();
98
$arg_count = 0;
99
while (!empty($temp_args)) {
100
	$arg = array_shift($temp_args);
101
	if ($arg[0] == '-') {
102
		switch ($arg) {
103
			case "--help":
104
				echo "Usage: playback gitsync [options] [[repository] <branch>]\nOptions:\n";
105
				foreach ($valid_args as $arg_name => $arg_desc) {
106
					echo $arg_name . "\n" . $arg_desc;
107
				}
108
				exit;
109
			case "--upgrading":
110
				// Disables all interactive functions and neither PHP
111
				// nor the web GUI will be killed or restarted.
112
				$upgrading = true;
113
			case (isset($valid_args[$arg])):
114
				$args[$arg] = true;
115
				break;
116
			default:
117
				echo "Invalid option: {$arg}\nUse --help for usage information.\n";
118
				exit;
119
		}
120
	} else {
121
		$args[$arg_count++] = $arg;
122
	}
123
}
124

    
125
if (!function_exists('post_cvssync_commands')) {
126
function post_cvssync_commands() {
127
	echo "===> Removing FAST-CGI temporary files...\n";
128
	exec("find /tmp -name \"php-fastcgi.socket*\" -exec rm -rf {} \;");
129
	exec("find /tmp -name \"*.tmp\" -exec rm -rf {} \;");
130

    
131
	exec("rm -rf /tmp/xcache/* 2>/dev/null");
132

    
133
	echo "===> Upgrading configuration (if needed)...\n";
134
	convert_config();
135

    
136
	echo "===> Configuring filter...";
137
	exec("/etc/rc.filter_configure_sync");
138
	exec("pfctl -f /tmp/rules.debug");
139
	echo "\n";
140

    
141
	if (file_exists("/etc/rc.php_ini_setup")) {
142
		echo "===> Running /etc/rc.php_ini_setup...";
143
		exec("/etc/rc.php_ini_setup >/dev/null 2>&1");
144
		echo "\n";
145
	}
146

    
147
	/* lock down console if necessary */
148
	echo "===> Locking down the console if needed...\n";
149
	reload_ttys();
150

    
151
	echo "===> Signaling PHP and nginx restart...";
152
	$fd = fopen("/tmp/restart_nginx", "w");
153
	fwrite($fd, "#!/bin/sh\n");
154
	fwrite($fd, "sleep 5\n");
155
	fwrite($fd, "/usr/local/sbin/pfSctl -c 'service restart webgui'\n");
156
	fclose($fd);
157
	mwexec_bg("sh /tmp/restart_nginx");
158
	echo "\n";
159
}
160
}
161

    
162
if (!function_exists('isUrl')) {
163
function isUrl($url = "") {
164
	if ($url) {
165
		if (strstr($url, "rcs.pfsense.org") or
166
			strstr($url, "mainline") or
167
			strstr($url, ".git") or
168
			strstr($url, "git://")) {
169
			return true;
170
		}
171
	}
172
	return false;
173
}
174
}
175

    
176
if (!function_exists('run_cmds')) {
177
function run_cmds($cmds) {
178
	global $debug;
179
	foreach ($cmds as $cmd) {
180
		if ($debug) {
181
			echo "Running $cmd";
182
		}
183
		exec($cmd);
184
	}
185
}
186
}
187

    
188
unlink_if_exists("/tmp/config.cache");
189
conf_mount_rw();
190

    
191
if (!file_exists($GIT_BIN)) {
192
	require_once("pkg-utils.inc");
193

    
194
	echo "Cannot find git, installing...\n";
195
	if (!pkg_call('install -y -q git')) {
196
		echo "\nERROR: Unable to install git pkg.\n";
197
		return;
198
	}
199
}
200

    
201
# Remove mainline if exists (older)
202
if (is_dir("/root/pfsense/mainline")) {
203
	exec("rm -rf /root/pfsense/mainline");
204
}
205

    
206
# Remove RELENG_1_2 if exists (older)
207
if (is_dir("/root/pfsense/RELENG_1_2")) {
208
	exec("rm -rf /root/pfsense/RELENG_1_2");
209
}
210

    
211
# Remove HEAD if exists (older)
212
if (is_dir("/root/pfsense/HEAD")) {
213
	exec("rm -rf /root/pfsense/HEAD");
214
}
215

    
216
if (file_exists("/root/cvssync_backup.tgz")) {
217
	$backup_date = `ls -lah /root/cvssync_backup.tgz | awk '{ print $6,$7,$8 }'`;
218
	$tmp = array("RESTORE" => "Restores prior CVSSync backup data performed at {$backup_date}");
219
	$branches = array_merge($branches, $tmp);
220
}
221

    
222
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
223
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url", $output_str, $ret);
224
	if (is_array($output_str) && !empty($output_str[0])) {
225
		$GIT_REPO = $output_str[0];
226
	}
227
	unset($output_str);
228
}
229

    
230
if (!$args[0] && !$upgrading) {
231
	echo "\nCurrent repository is $GIT_REPO\n";
232
	echo "\nPlease select which branch you would like to sync against:\n\n";
233
	foreach ($branches as $branchname => $branchdesc) {
234
		echo "{$branchname} \t {$branchdesc}\n";
235
	}
236
	echo "\nOr alternatively you may enter a custom RCS branch URL (Git or HTTP).\n\n";
237
	$branch = readline("> ");
238
	echo "\n";
239
} else {
240
	$branch = $args[0];
241
}
242

    
243
if ($args[1] == "NOBACKUP") {
244
	$nobackup = true;
245
} else {
246
	$nobackup = false;
247
}
248

    
249
// If the repository has been fetched before, build a list of its branches.
250
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
251
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch -r", $branch_list, $ret);
252
	if ($ret == 0 && is_array($branch_list)) {
253
		foreach ($branch_list as $branch_item) {
254
			$branch_item = substr(strrchr($branch_item, "/"), 1);
255
			if (!isset($branches[$branch_item])) {
256
				$branches[$branch_item] = " ";
257
			}
258
		}
259
	}
260
}
261

    
262
$found = false;
263
foreach ($branches as $branchname => $branchdesc) {
264
	if ($branchname == $branch) {
265
		$found = true;
266
		break;
267
	}
268
}
269
if (!$found) {
270
	if (isURL($branch) && !$upgrading) {
271
		if ($args[1]) {
272
			$GIT_REPO = $branch;
273
			$branch = $args[1];
274
			$found = true;
275
		} else {
276
			echo "\n";
277
			echo "NOTE: $branch was not found.\n\n";
278
			$command = readline("Is this a custom GIT URL? [y]? ");
279
			if (strtolower($command) == "y" or $command == "") {
280
				$GIT_REPO = $branch;
281
				$command = readline("Checkout which branch [${DEFAULT_BRANCH}]? ");
282
				if ($command == "") {
283
					$branch = $DEFAULT_BRANCH;
284
				}
285
				if ($command) {
286
					$branch = $command;
287
				}
288
				$found = true;
289
			}
290
		}
291
	}
292
	if (!$found) {
293
		echo "\nNo valid branch found.  Exiting.\n\n";
294
		conf_mount_ro();
295
		exit;
296
	}
297
}
298

    
299
$merge_repos = array();
300
if (file_exists($GITSYNC_MERGE)) {
301
	$gitsync_merges = file($GITSYNC_MERGE, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
302
	if (!empty($gitsync_merges) && is_array($gitsync_merges)) {
303
		echo "\n===> Automatic merge list read from ${GITSYNC_MERGE}\n";
304
		foreach ($gitsync_merges as $merge_line_num => $merge_line) {
305
			$merge_comments = explode("#", trim($merge_line));
306
			if (empty($merge_comments[0])) {
307
				continue;
308
			}
309

    
310
			$merge_line = explode(" ", trim($merge_comments[0]));
311
			if (count($merge_line) != 2 || empty($merge_line[0]) || empty($merge_line[1])) {
312
				echo "\nLine " . ($merge_line_num + 1) . " does not have the correct parameter count or has improper spacing.\n";
313
				echo "Expected parameters:  repository_url branch\n";
314
				echo "Line read:  " . implode(" ", $merge_line) . "\n\n";
315
				echo "Aborting automatic merge.\n\n";
316
				$merge_repos = array();
317
				break;
318
			}
319
			$merge_repos[] = array('repo' => $merge_line[0], 'branch' => $merge_line[1]);
320
		}
321
	}
322
}
323
if (!$args[0] && !$upgrading) {
324
	do {
325
		echo "\nAdd a custom RCS branch URL (Git or HTTP) to merge in or press enter if done.\n\n";
326
		$merge_repo = readline("> ");
327
		if (!empty($merge_repo)) {
328
			$merge_branch = readline("Merge which branch [${DEFAULT_BRANCH}]? ");
329
			if ($merge_branch == "") {
330
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $DEFAULT_BRANCH);
331
			} else if ($merge_branch) {
332
				$merge_repos[] = array('repo' => $merge_repo, 'branch' => $merge_branch);
333
			}
334
		}
335
	} while (!empty($merge_repo));
336
}
337

    
338
if ($branch == "RESTORE" && $g['platform'] == $g['product_name']) {
339
	if (!file_exists("/root/cvssync_backup.tgz")) {
340
		echo "Sorry, we could not find a previous CVSSync backup file.\n";
341
		conf_mount_ro();
342
		exit();
343
	}
344
	echo "===> Restoring previous CVSSync backup... Please wait...\n";
345
	exec("tar Uxpf /root/cvssync_backup.tgz -C /");
346
	post_cvssync_commands();
347
	conf_mount_ro();
348
	exit();
349
} else {
350
	$nobackup = true; // do not backup embedded
351
}
352

    
353
if ($nobackup == false) {
354
	echo "===> Backing up current pfSense information...\n";
355
	echo "===> Please wait... ";
356
	exec("tar czPf /root/cvssync_backup.tgz --exclude /root --exclude /dev --exclude /tmp --exclude /var/run --exclude /var/empty /");
357
	$size = filesize("/root/cvssync_backup.tgz");
358
	echo "{$size} bytes.\n\n";
359
	sleep(5);
360
}
361

    
362
echo "===> Checking out $branch\n";
363

    
364
// Git commands for resetting to the specified branch
365
if ($branch == "build_commit") {
366
	$git_cmd = array(
367
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " 2>/dev/null",
368
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
369
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg(trim(file_get_contents("/etc/version.lastcommit")))
370
	);
371
} else {
372
	$git_cmd = array(
373
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} branch " . escapeshellarg($branch) . " " . escapeshellarg("origin/{$branch}") . " 2>/dev/null",
374
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} checkout -f " . escapeshellarg($branch) . " 2>/dev/null",
375
		"cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard " . escapeshellarg("origin/{$branch}")
376
	);
377
}
378

    
379
// Git 'er done!
380
if (is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO")) {
381
	echo "===> Fetching updates...\n";
382
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} config remote.origin.url " . escapeshellarg($GIT_REPO));
383
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} fetch");
384
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} clean -f -x -d");
385
	run_cmds($git_cmd);
386
} else {
387
	exec("mkdir -p $CODIR/pfSenseGITREPO");
388
	echo "Executing cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone $GIT_REPO pfSenseGITREPO\n";
389
	exec("cd $CODIR/pfSenseGITREPO && {$GIT_BIN} clone " . escapeshellarg($GIT_REPO) . " pfSenseGITREPO");
390
	if (is_dir("$CODIR/pfSenseGITREPO/pfSense")) {
391
		exec("mv $CODIR/pfSenseGITREPO/pfSense $CODIR/pfSenseGITREPO/pfSenseGITREPO");
392
	}
393
	if (is_dir("$CODIR/pfSenseGITREPO/mainline")) {
394
		exec("mv $CODIR/pfSenseGITREPO/mainline $CODIR/pfSenseGITREPO/pfSenseGITREPO");
395
	}
396
	run_cmds($git_cmd);
397
}
398

    
399
foreach ($merge_repos as $merge_repo) {
400
	echo "===> Merging branch {$merge_repo['branch']} from {$merge_repo['repo']}\n";
401
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} pull " . escapeshellarg($merge_repo['repo']) . " " . escapeshellarg($merge_repo['branch']), $output_str, $ret);
402
	unset($output_str);
403
	if ($ret <> 0) {
404
		echo "\nMerge failed.  Aborting sync.\n\n";
405
		run_cmds($git_cmd);
406
		conf_mount_ro();
407
		exit;
408
	}
409
}
410

    
411
$updated_files_array[1] = array();
412
if (isset($args["--minimal"])) {
413
	if (file_exists("/etc/version.gitsync")) {
414
		$old_revision = trim(file_get_contents("/etc/version.gitsync"));
415
	} else if (file_exists("/etc/version.lastcommit")) {
416
		$old_revision = trim(file_get_contents("/etc/version.lastcommit"));
417
	}
418

    
419
	# Find updated files.
420
	$updated_files = shell_exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} diff --name-only --relative=src " . escapeshellarg($old_revision));
421

    
422
	# Get the path of each updated file.
423
	preg_match_all('@(?:^(.+?)$)@sim', $updated_files, $updated_files_array, PREG_PATTERN_ORDER);
424

    
425
	if(isset($args["--show-files"])) {
426
		echo "===> Updated Files: \n";
427
		print_r($updated_files_array[1]);
428
	}
429
}
430

    
431
// Save new commit ID for later minimal file copies
432
if(!isset($args["--dry-run"])) {
433
	exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} rev-parse -q --verify HEAD > /etc/version.gitsync");
434
}
435

    
436
// Remove files that we do not want to overwrite the system with
437
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/crontab");
438
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/master.passwd");
439
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/passwd");
440
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
441
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/ttys");
442
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/group");
443
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/fstab");
444
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/platform");
445
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/device.hints");
446
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.conf");
447
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/boot/loader.rc");
448
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/etc/syslog.conf");
449
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.shrc");
450
@unlink("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/root/.tcshrc");
451
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/conf*");
452
exec("rm -rf {$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/cf 2>/dev/null");
453
@chmod("{$CODIR}/pfSenseGITREPO/pfSenseGITREPO/src/tmp", 01777);
454

    
455
$different_files_array[1] = array();
456
$missing_files_array[1] = array();
457
if(isset($args["--diff"])) {
458
	# Find different and missing files.
459
	$different_missing_files = shell_exec("/usr/bin/diff -qr / $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/ | /usr/bin/grep -E '^(Files .*? and $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/.*? differ)|(Only in $CODIR/pfSenseGITREPO/pfSenseGITREPO/src/)'");
460

    
461
	# Get the path of each different or missing file.
462
	preg_match_all('@(?:^Files .*? and '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?) differ.*?$)@sim', $different_missing_files, $different_files_array, PREG_PATTERN_ORDER);
463
	preg_match_all('@(?:^Only in '.$CODIR.'/pfSenseGITREPO/pfSenseGITREPO/src/(.+?)$)@sim', $different_missing_files, $missing_files_array, PREG_PATTERN_ORDER);
464

    
465
	# Deal with diff's output format of missing files (path: missing_file).
466
	foreach ($missing_files_array[1] as $key => $file) {
467

    
468
		# Most of the time there will be only the one ': ' injected by diff output.  So global replace with dir delimiter (/) is fine.
469
		$tmp = str_replace(": ", "/", $file, $count);
470
		if ($count == 1)
471
			$file = ltrim($tmp, "/");
472

    
473
		# For the very rare case a path component (dir or file) contains ': ' as well, then need to find and replace only the ': ' injected by diff output.
474
		else {
475
			$tmp = $file;
476
			do {
477
				$pos = strrpos($tmp, ": ");
478
				if ($pos) {
479
					$tmp = substr($tmp, 0, $pos);
480
					$res = is_dir("$CODIR/pfSenseGITREPO/pfSenseGITREPO/src/$tmp/");
481
				}
482
			} while (!$res && $pos);
483

    
484
			if ($res)
485
				$file = ltrim($tmp . "/" . substr($file, $pos+2), "/");
486
		}
487

    
488
		$missing_files_array[1][$key] = $file;
489
	}
490

    
491
	if(isset($args["--show-files"])) {
492
		echo "===> Different Files: \n";
493
		print_r($different_files_array[1]);
494
		echo "===> Missing Files: \n";
495
		print_r($missing_files_array[1]);
496
	}
497
}
498

    
499
# Files to be copied.
500
if(isset($args["--minimal"]) || isset($args["--diff"])) {
501
	$files_to_copy_array = array_merge($updated_files_array[1], $different_files_array[1], $missing_files_array[1]);
502
	$files_to_copy_array = array_unique($files_to_copy_array);
503

    
504
	unset($updated_files_array, $different_files_array, $missing_files_array);
505

    
506
	# Convert the list from an array to a space separated quoted string.  Quoted for white space file name support.
507
	if (count($files_to_copy_array) > 0) {	# Leave the string empty/unset if there is nothing to copy.
508
		$files_to_copy = '"' . implode('" "', $files_to_copy_array) . '"';
509
	}
510
	$qty_files_to_copy = count($files_to_copy_array);
511
	unset($files_to_copy_array);
512
} else {
513
	$files_to_copy = '.';
514
	$qty_files_to_copy = chr(8);
515
}
516

    
517
$tar_options = '-C ./src';
518

    
519
echo "===> Installing $qty_files_to_copy new files...\n";
520

    
521
if ($g['platform'] == $g['product_name']) {
522
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$tar_options} {$files_to_copy} | (cd / ; tar -Uxpf -)";
523
} else {
524
	$command = "cd $CODIR/pfSenseGITREPO/pfSenseGITREPO ; tar -cpf - {$tar_options} {$files_to_copy} | (cd / ; tar -xpf -) 2>/dev/null";
525
}
526

    
527
if (!empty($files_to_copy)) {
528
	if(isset($args["--show-command"])) {
529
		echo "===> Command: \n$command\n";
530
	}
531
	if(!isset($args["--dry-run"])) {
532
		exec($command);
533
	}
534
} else {
535
	echo "Already up-to-date.\n";
536
	$upgrading = true;
537
}
538

    
539
// Reset the repository to restore the deleted files
540
exec("cd $CODIR/pfSenseGITREPO/pfSenseGITREPO && {$GIT_BIN} reset --hard >/dev/null 2>/dev/null");
541

    
542
// Remove obsolete files
543
$files_to_remove = file("/etc/pfSense.obsoletedfiles");
544
foreach ($files_to_remove as $file_to_remove) {
545
	if (file_exists($file_to_remove)) {
546
		exec("/bin/rm -f $file_to_remove");
547
	}
548
}
549

    
550
if (!$upgrading) {
551
	post_cvssync_commands();
552
}
553

    
554
echo "===> Checkout complete.\n";
555
echo "\n";
556
if (!$upgrading) {
557
	echo "Your system is now sync'd and PHP and nginx will be restarted in 5 seconds.\n\n";
558
} else {
559
	echo "Your system is now sync'd.\n\n";
560
}
561

    
562
conf_mount_ro();
(12-12/21)