Project

General

Profile

« Previous | Next » 

Revision eab543ed

Added by Ermal LUÇI over 14 years ago

Ticket #1017. Move the fwrite and fd_log initializing to a function. Name the function pkg_debug to properly show what is its purpose and also make it write something only when $debug is set.

View differences:

etc/inc/pkg-utils.inc
49 49
require_once("pfsense-utils.inc");
50 50
require_once("globals.inc");
51 51

  
52

  
53
/* set up logging if needed */
54
if (!$fd_log) {
55
	if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w"))
56
		update_output_window("Warning, could not open log for writing.");
57
}
58

  
59 52
if(!function_exists("update_status")) {
60 53
	function update_status($status) {
61 54
		echo $status . "\n";
......
67 60
	}
68 61
}
69 62

  
63
if (!function_exists("pkg_debug")) {
64
	/* set up logging if needed */
65
	function pkg_debug($msg) {
66
		global $g, $debug, $fd_log;
67

  
68
		if (!$debug)
69
			return;
70

  
71
		if (!$fd_log) {
72
			if (!$fd_log = fopen("{$g['tmp_path']}/pkg_mgr_{$package}.log", "w"))
73
				update_output_window("Warning, could not open log for writing.");
74
		}
75
		@fwrite($fd_log, $msg);
76
	}
77
}
78

  
70 79
$vardb = "/var/db/pkg";
71 80
safe_mkdir($vardb);
81
$g['platform'] = trim(file_get_contents("/etc/platform"));
72 82

  
73 83
conf_mount_rw();
74
$g['platform'] = trim(file_get_contents("/etc/platform"));
75 84
if(!is_dir("/usr/local/pkg") or !is_dir("/usr/local/pkg/pf")) {
76 85
	safe_mkdir("/usr/local/pkg");
77 86
	safe_mkdir("/usr/local/pkg/pf");	
78 87
}
79 88
conf_mount_ro();
80 89

  
81
$version = split("-", trim(file_get_contents("/etc/version")));
82
$ver = split("\.", $version[0]);
83
$g['version'] = intval($ver[1]);
84

  
85 90
/****f* pkg-utils/remove_package
86 91
 * NAME
87 92
 *   remove_package - Removes package from FreeBSD if it exists
......
453 458
			} else {
454 459
				//$dependlevel++;
455 460
				$static_output .= "\n" . str_repeat(" ", $dependlevel * 2) . $working_depend[1] . " ";
456
				@fwrite($fd_log, $working_depend[1] . "\n");
461
				pkg_debug($working_depend[1] . "\n");
457 462
			}
458 463
		}
459 464
	}
460 465
	$pkgaddout = "";
461 466
	exec("/usr/sbin/pkg_add -fv {$fetchto} 2>&1", $pkgaddout);
462
	@fwrite($fd_log, $pkgname . " " . print_r($pkgaddout, true) . "\n");
467
	pkg_debug($pkgname . " " . print_r($pkgaddout, true) . "\n");
463 468

  
464 469
	return true;
465 470
}
......
481 486
			return -1;
482 487
		}
483 488
	}
484
	@fwrite($fd_log, "Beginning package installation.\n");
489
	pkg_debug("Beginning package installation.\n");
485 490
	log_error('Beginning package installation for ' . $pkg_info['name'] . '.');
486 491
	$static_output .= "Beginning package installation for " . $pkg_info['name'] . "...";
487 492
	update_status($static_output);
......
489 494
	if($pkg_info['config_file'] != "") {
490 495
		$static_output .= "Downloading package configuration file... ";
491 496
		update_output_window($static_output);
492
		@fwrite($fd_log, "Downloading package configuration file...\n");
497
		pkg_debug("Downloading package configuration file...\n");
493 498
		$fetchto = substr(strrchr($pkg_info['config_file'], '/'), 1);
494 499
		download_file_with_progress_bar($pkg_info['config_file'], '/usr/local/pkg/' . $fetchto);
495 500
		if(!file_exists('/usr/local/pkg/' . $fetchto)) {
496
			@fwrite($fd_log, "ERROR! Unable to fetch package configuration file. Aborting installation.\n");
501
			pkg_debug("ERROR! Unable to fetch package configuration file. Aborting installation.\n");
497 502
			if($pkg_interface == "console")
498 503
				print "\nERROR! Unable to fetch package configuration file. Aborting package installation.\n";
499 504
			else {
......
609 614
			if($pkg_installed == true) {
610 615
				$static_output .= "done.\n";
611 616
				update_output_window($static_output);
612
				@fwrite($fd_log, "pkg_add successfully completed.\n");
617
				pkg_debug("pkg_add successfully completed.\n");
613 618
			} else {
614 619
				$static_output .= "of {$pkg_name} failed!\n\nInstallation aborted.";
615 620
				update_output_window($static_output);
616
				@fwrite($fd_log, "Package WAS NOT installed properly.\n");
621
				pkg_debug("Package WAS NOT installed properly.\n");
617 622
				if($pkg_interface <> "console") {
618 623
					echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
619 624
					echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
......
673 678
					return false;
674 679
				}
675 680
				if(stristr($filename, ".tgz") <> "") {
676
					@fwrite($fd_log, "Extracting tarball to -C for " . $filename . "...\n");
681
					pkg_debug("Extracting tarball to -C for " . $filename . "...\n");
677 682
					$tarout = "";
678 683
					exec("/usr/bin/tar xvzf " . $prefix . $filename . " -C / 2>&1", $tarout);
679
					@fwrite($fd_log, print_r($tarout, true) . "\n");
684
					pkg_debug(print_r($tarout, true) . "\n");
680 685
				}
681 686
				if($pkg_chmod <> "") {
682
					@fwrite($fd_log, "Changing file mode to {$pkg_chmod} for {$prefix}{$filename}\n");
687
					pkg_debug("Changing file mode to {$pkg_chmod} for {$prefix}{$filename}\n");
683 688
					@chmod($prefix . $filename, $pkg_chmod);
684 689
					system("/bin/chmod {$pkg_chmod} {$prefix}{$filename}");
685 690
				}
......
697 702
		if($pkg_config['include_file'] <> "") {
698 703
			$static_output .= "Loading package instructions...\n";
699 704
			update_output_window($static_output);
700
			@fwrite($fd_log, "require_once('{$pkg_config['include_file']}')\n");
705
			pkg_debug("require_once('{$pkg_config['include_file']}')\n");
701 706
			if (file_exists($pkg_config['include_file']))
702 707
				require_once($pkg_config['include_file']);
703 708
			else {
......
779 784
	} else {
780 785
		$static_output .= "Loading package configuration... failed!\n\nInstallation aborted.";
781 786
		update_output_window($static_output);
782
		@fwrite($fd_log, "Unable to load package configuration. Installation aborted.\n");
787
		pkg_debug("Unable to load package configuration. Installation aborted.\n");
783 788
		if($pkg_interface <> "console") {
784 789
			echo "\n<script language=\"JavaScript\">document.progressbar.style.visibility='hidden';</script>";
785 790
			echo "\n<script language=\"JavaScript\">document.progholder.style.visibility='hidden';</script>";
......
792 797
	if($pkg_info['logging']) {
793 798
		mwexec("/usr/sbin/fifolog_create -s 32768 {$g['varlog_path']}/{$pkg_info['logging']['logfilename']}");
794 799
		@chmod($g['varlog_path'] . '/' . $pkg_info['logging']['logfilename'], 0600);
795
		@fwrite($fd_log, "Adding text to file /etc/syslog.conf\n");
800
		pkg_debug("Adding text to file /etc/syslog.conf\n");
796 801
		if(is_process_running("syslogd"))
797 802
			mwexec("killall syslogd");
798 803
		system_syslogd_start();
......
846 851
		conf_mount_ro();
847 852
		return;
848 853
	}
849
	@fwrite($fd_log, "Removing {$pkg} package... ");
854
	pkg_debug("Removing {$pkg} package... ");
850 855
	$static_output .= "Removing {$pkg} components...\n";
851 856
	update_output_window($static_output);
852 857
	/* parse package configuration */
......
922 927
		if($pkg_config['include_file'] <> "") {
923 928
			$static_output .= "Loading package instructions...\n";
924 929
			update_output_window($static_output);
925
			@fwrite($fd_log, "require_once(\"{$pkg_config['include_file']}\")\n");
930
			pkg_debug("require_once(\"{$pkg_config['include_file']}\")\n");
926 931
			if (file_exists($pkg_config['include_file']))
927 932
				require_once($pkg_config['include_file']);
928 933
			else {
......
974 979
		if($pkg_config['include_file'] <> "") {
975 980
                        $static_output .= "\tRemoving package instructions...";
976 981
                        update_output_window($static_output);
977
                        @fwrite($fd_log, "Remove '{$pkg_config['include_file']}'\n");
982
                        pkg_debug("Remove '{$pkg_config['include_file']}'\n");
978 983
                        unlink_if_exists("/usr/local/pkg/" . $pkg_config['include_file']);
979 984
			$static_output .= "done.\n";
980 985
                        update_output_window($static_output);

Also available in: Unified diff