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


git-svn-id: https://develop.svn.wordpress.org/trunk@35267 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Dion Hulse 2015-10-19 00:52:08 +00:00
parent 10322a38bf
commit 063d42a801
1 changed files with 25 additions and 8 deletions

View File

@ -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 );