From 85258bb914e8079c93e7b81453516f135efb27cf Mon Sep 17 00:00:00 2001 From: Dion Hulse Date: Mon, 19 Oct 2015 00:53:24 +0000 Subject: [PATCH] In `insert_with_markers()` restore the 4.3 behaviour of creating the file if it doesn't exist. This change also makes it bail early (without writing) if the markers content is the same as the existing, and uses `ftell()` rather than `$bytes` for the location to truncate the file to - based on the file pointer being at the end of the written stream. Props willmot tigertech kevinatelement See #31767 Built from https://develop.svn.wordpress.org/trunk@35267 git-svn-id: http://core.svn.wordpress.org/trunk@35233 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/includes/misc.php | 33 +++++++++++++++++++++++++-------- wp-includes/version.php | 2 +- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/wp-admin/includes/misc.php b/wp-admin/includes/misc.php index 776f04c968..d776681ccc 100644 --- a/wp-admin/includes/misc.php +++ b/wp-admin/includes/misc.php @@ -96,18 +96,25 @@ function extract_from_markers( $filename, $marker ) { * * @since 1.5.0 * - * @param string $filename - * @param string $marker - * @param array $insertion + * @param string $filename Filename to alter. + * @param string $marker The marker to alter. + * @param array|string $insertion The new content to insert. * @return bool True on write success, false on failure. */ function insert_with_markers( $filename, $marker, $insertion ) { - if ( ! is_writeable( $filename ) ) { + if ( ! file_exists( $filename ) ) { + if ( ! is_writable( dirname( $filename ) ) ) { + return false; + } + if ( ! touch( $filename ) ) { + return false; + } + } elseif ( ! is_writeable( $filename ) ) { return false; } if ( ! is_array( $insertion ) ) { - $insertion = array( $insertion ); + $insertion = explode( "\n", $insertion ); } $start_marker = "# BEGIN {$marker}"; @@ -127,7 +134,7 @@ function insert_with_markers( $filename, $marker, $insertion ) { } // Split out the existing file into the preceeding lines, and those that appear after the marker - $pre_lines = $post_lines = array(); + $pre_lines = $post_lines = $existing_lines = array(); $found_marker = $found_end_marker = false; foreach ( $lines as $line ) { if ( ! $found_marker && false !== strpos( $line, $start_marker ) ) { @@ -141,9 +148,19 @@ function insert_with_markers( $filename, $marker, $insertion ) { $pre_lines[] = $line; } elseif ( $found_marker && $found_end_marker ) { $post_lines[] = $line; + } else { + $existing_lines[] = $line; } } + // Check to see if there was a change + if ( $existing_lines === $insertion ) { + flock( $fp, LOCK_UN ); + fclose( $fp ); + + return true; + } + // Generate the new file data $new_file_data = implode( "\n", array_merge( $pre_lines, @@ -157,9 +174,9 @@ function insert_with_markers( $filename, $marker, $insertion ) { fseek( $fp, 0 ); $bytes = fwrite( $fp, $new_file_data ); if ( $bytes ) { - ftruncate( $fp, $bytes ); + ftruncate( $fp, ftell( $fp ) ); } - + fflush( $fp ); flock( $fp, LOCK_UN ); fclose( $fp ); diff --git a/wp-includes/version.php b/wp-includes/version.php index 0d2a08d698..4987c8a9c3 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.4-alpha-35266'; +$wp_version = '4.4-alpha-35267'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.