Index: wp-settings.php
===================================================================
--- wp-settings.php	(revision 8582)
+++ wp-settings.php	(working copy)
@@ -107,6 +107,33 @@
 if ( !defined('WP_CONTENT_DIR') )
 	define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' ); // no trailing slash, full paths only - WP_CONTENT_URL is defined further down
 
+if ( file_exists(ABSPATH . '.maintenance') ) {
+	if ( file_exists( WP_CONTENT_DIR . '/maintenance.php' ) ) {
+		require_once( WP_CONTENT_DIR . '/maintenance.php' );
+		die();
+	}
+
+	$protocol = $_SERVER["SERVER_PROTOCOL"];
+	if ( 'HTTP/1.1' != $protocol && 'HTTP/1.0' != $protocol )
+		$protocol = 'HTTP/1.0';
+	header( "$protocol 503 Service Unavailable", true, 503 );
+	header( 'Content-Type: text/html; charset=utf-8' );
+?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+	<title>Maintenance</title>
+
+</head>
+<body>
+	<h1>Briefly unavailable for scheduled maintenance. Check back in a minute.</h1>
+</body>
+</html>
+<?php
+die();
+}
+
 if ( !extension_loaded('mysql') && !file_exists(WP_CONTENT_DIR . '/db.php') )
 	die( /*WP_I18N_OLD_MYSQL*/'Your PHP installation appears to be missing the MySQL extension which is required by WordPress.'/*/WP_I18N_OLD_MYSQL*/ );
 
Index: wp-admin/includes/update-core.php
===================================================================
--- wp-admin/includes/update-core.php	(revision 0)
+++ wp-admin/includes/update-core.php	(revision 0)
@@ -0,0 +1,50 @@
+<?php
+
+function update_core($from, $to) {
+	global $wp_filesystem;
+
+	// Sanity check the unzipped distribution
+	apply_filters('update_feedback', __('Verifying the unpacked files'));
+	if ( !file_exists($from . '/wordpress/wp-settings.php') || !file_exists($from . '/wordpress/wp-admin/admin.php') ||
+		!file_exists($from . '/wordpress/wp-includes/functions.php') ) {
+		$wp_filesystem->delete($from, true);
+		return new WP_Error('insane_distro', __('The update could not be unpacked') );
+	}
+	
+	apply_filters('update_feedback', __('Installing the latest version'));
+
+	// Create maintenance file to signal that we are upgrading
+	$maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
+	$maintenance_file = $to . '.maintenance';
+	$wp_filesystem->delete($maintenance_file);
+	$wp_filesystem->put_contents($maintenance_file, $maintenance_string, 0644);
+
+	// Copy new versions of WP files into place.
+	$result = copy_dir($from . '/wordpress', $to);
+	if ( is_wp_error($result) ) {
+		$wp_filesystem->delete($maintenance_file);
+		//$wp_filesystem->delete($working_dir, true); //TODO: Uncomment? This DOES mean that the new files are available in the upgrade folder if it fails.
+		return $result;
+	}
+
+	// Might have to do upgrade in a separate step.
+	apply_filters('update_feedback', __('Upgrading database'));
+	// Get new db version
+	global $wp_db_version;
+	require (ABSPATH . WPINC . '/version.php');
+	// Upgrade db
+	define('WP_INSTALLING', true);
+	require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
+	wp_upgrade();
+
+	// Remove working directory
+	$wp_filesystem->delete($from, true);
+
+	// Remove maintenance file, we're done.
+	$wp_filesystem->delete($maintenance_file);
+
+	// Force refresh of update information
+	delete_option('update_core');
+}
+
+?>
\ No newline at end of file

Property changes on: wp-admin/includes/update-core.php
___________________________________________________________________
Name: svn:eol-style
   + native

Index: wp-admin/includes/file.php
===================================================================
--- wp-admin/includes/file.php	(revision 8582)
+++ wp-admin/includes/file.php	(working copy)
@@ -408,8 +408,10 @@
 				return new WP_Error('copy_failed', __('Could not copy file'), $to . $filename);
 			$wp_filesystem->chmod($to . $filename, 0644);
 		} elseif ( 'd' == $fileinfo['type'] ) {
-			if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
-				return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
+			if ( !$wp_filesystem->is_dir($to . $filename) ) {
+				if ( !$wp_filesystem->mkdir($to . $filename, 0755) )
+					return new WP_Error('mkdir_failed', __('Could not create directory'), $to . $filename);
+			}
 			$result = copy_dir($from . $filename, $to . $filename);
 			if ( is_wp_error($result) )
 				return $result;
Index: wp-admin/includes/update.php
===================================================================
--- wp-admin/includes/update.php	(revision 8582)
+++ wp-admin/includes/update.php	(working copy)
@@ -15,7 +15,7 @@
 
 	case 'upgrade' :
 		if ( current_user_can('manage_options') ) {
-			return sprintf( '| <strong>'.__( '<a href="%2$s">Get Version %3$s</a>' ).'</strong>', $GLOBALS['wp_version'], $cur->url, $cur->current );
+			return sprintf( '| <strong>'.__( '<a href="%1$s">Get Version %2$s</a>' ).'</strong>', wp_nonce_url('update.php?action=upgrade-core', 'upgrade-core'), $cur->current);
 			break;
 		}
 
@@ -188,4 +188,79 @@
 	return  $folder . '/' . $pluginfiles[0];
 }
 
+function wp_update_core($feedback = '') {
+	global $wp_filesystem;
+
+	if ( !empty($feedback) )
+		add_filter('update_feedback', $feedback);
+
+	// Is an update available?
+	$current = get_option( 'update_core' );
+	//if ( !isset( $current->response ) || $current->response != 'upgrade' )
+	//	return new WP_Error('up_to_date', __('WordPress is at the latest version.'));
+
+	// Is a filesystem accessor setup?
+	if ( ! $wp_filesystem || ! is_object($wp_filesystem) )
+		WP_Filesystem();
+
+	if ( ! is_object($wp_filesystem) )
+		return new WP_Error('fs_unavailable', __('Could not access filesystem.'));
+
+	if ( $wp_filesystem->errors->get_error_code() )
+		return new WP_Error('fs_error', __('Filesystem error'), $wp_filesystem->errors);
+
+	// Get the base WP folder
+	$wp_dir = $wp_filesystem->abspath();
+	if ( empty($wp_dir) )
+		return new WP_Error('fs_no_wp_dir', __('Unable to locate WordPress directory.'));
+
+	// And the same for the Content directory.
+	$content_dir = $wp_filesystem->wp_content_dir();
+	if( empty($content_dir) )
+		return new WP_Error('fs_no_content_dir', __('Unable to locate WordPress Content directory (wp-content).'));
+	
+	$wp_dir = trailingslashit( $wp_dir );
+	$content_dir = trailingslashit( $content_dir );
+
+	// Get the URL to the zip file
+	//$package = "http://wordpress.org/wordpress-{$current->current}.zip";
+	//$package = "http://wordpress.org/wordpress-2.6.zip";
+	$package = "http://localhost/wordpress.zip";
+
+	// Download the package
+	apply_filters('update_feedback', sprintf(__('Downloading update from %s'), $package));
+	$download_file = download_url($package);
+
+	if ( is_wp_error($download_file) )
+		return new WP_Error('download_failed', __('Download failed.'), $download_file->get_error_message());
+
+	$working_dir = $content_dir . 'upgrade/core';
+
+	// Clean up working directory
+	if ( $wp_filesystem->is_dir($working_dir) )
+		$wp_filesystem->delete($working_dir, true);
+
+	apply_filters('update_feedback', __('Unpacking the update'));
+	// Unzip package to working directory
+	$result = unzip_file($download_file, $working_dir);
+
+	// Once extracted, delete the package
+	unlink($download_file);
+
+	if ( is_wp_error($result) ) {
+		$wp_filesystem->delete($working_dir, true);
+		return $result;
+	}
+
+	// Copy update-core.php from the new version into place.
+	/* if ( !$wp_filesystem->copy($working_dir . '/wordpress/wp-admin/includes/update-core.php', $wp_dir . 'wp-admin/includes/update-core.php', true) ) {
+		$wp_filesystem->delete($working_dir, true);
+		return $result;
+	} */
+
+	require(ABSPATH . 'wp-admin/includes/update-core.php');
+
+	return update_core($working_dir, $wp_dir);
+}
+
 ?>
Index: wp-admin/update.php
===================================================================
--- wp-admin/update.php	(revision 8582)
+++ wp-admin/update.php	(working copy)
@@ -44,6 +44,39 @@
 	echo '</div>';
 }
 
+function do_core_upgrade() {
+	global $wp_filesystem;
+
+	$url = wp_nonce_url("update.php?action=upgrade-core", "upgrade-core");
+	if ( false === ($credentials = request_filesystem_credentials($url)) )
+		return;
+
+	if ( ! WP_Filesystem($credentials) ) {
+		request_filesystem_credentials($url, '', true); //Failed to connect, Error and request again
+		return;
+	}
+
+	echo '<div class="wrap">';
+	echo '<h2>' . __('Upgrade WordPress') . '</h2>';
+	if ( $wp_filesystem->errors->get_error_code() ) {
+		foreach ( $wp_filesystem->errors->get_error_messages() as $message )
+			show_message($message);
+		echo '</div>';
+		return;
+	}
+
+	$result = wp_update_core('show_message');
+
+	if ( is_wp_error($result) ) {
+		show_message($result);
+		if ('up_to_date' != $result->get_error_code() )
+			show_message( __('Installation Failed') );
+	} else {
+		show_message( __('WordPress upgraded successfully') );	
+	}
+	echo '</div>';
+}
+
 if ( isset($_GET['action']) ) {
 	$plugin = isset($_GET['plugin']) ? trim($_GET['plugin']) : '';
 
@@ -84,6 +117,13 @@
 			include(WP_PLUGIN_DIR . '/' . $plugin);
 		}
 		echo "</body></html>";
+	} elseif ( 'upgrade-core' == $_GET['action'] ) {
+		//check_admin_referer('upgrade-core');
+		$title = __('Upgrade WordPress');
+		$parent_file = 'index.php';
+		require_once('admin-header.php');
+		do_core_upgrade();
+		include('admin-footer.php');
 	}
 }
 
