Make WordPress Core

Changeset 35267


Ignore:
Timestamp:
10/19/2015 12:52:08 AM (11 years ago)
Author:
dd32
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/misc.php

    r34828 r35267  
    9797 * @since 1.5.0
    9898 *
    99  * @param string $filename
    100  * @param string $marker
    101  * @param array  $insertion
     99 * @param string       $filename  Filename to alter.
     100 * @param string       $marker    The marker to alter.
     101 * @param array|string $insertion The new content to insert.
    102102 * @return bool True on write success, false on failure.
    103103 */
    104104function insert_with_markers( $filename, $marker, $insertion ) {
    105         if ( ! is_writeable( $filename ) ) {
     105        if ( ! file_exists( $filename ) ) {
     106                if ( ! is_writable( dirname( $filename ) ) ) {
     107                        return false;
     108                }
     109                if ( ! touch( $filename ) ) {
     110                        return false;
     111                }
     112        } elseif ( ! is_writeable( $filename ) ) {
    106113                return false;
    107114        }
    108115
    109116        if ( ! is_array( $insertion ) ) {
    110                 $insertion = array( $insertion );
     117                $insertion = explode( "\n", $insertion );
    111118        }
    112119
     
    128135
    129136        // Split out the existing file into the preceeding lines, and those that appear after the marker
    130         $pre_lines = $post_lines = array();
     137        $pre_lines = $post_lines = $existing_lines = array();
    131138        $found_marker = $found_end_marker = false;
    132139        foreach ( $lines as $line ) {
     
    142149                } elseif ( $found_marker && $found_end_marker ) {
    143150                        $post_lines[] = $line;
    144                 }
     151                } else {
     152                        $existing_lines[] = $line;
     153                }
     154        }
     155
     156        // Check to see if there was a change
     157        if ( $existing_lines === $insertion ) {
     158                flock( $fp, LOCK_UN );
     159                fclose( $fp );
     160
     161                return true;
    145162        }
    146163
     
    158175        $bytes = fwrite( $fp, $new_file_data );
    159176        if ( $bytes ) {
    160                 ftruncate( $fp, $bytes );
    161         }
    162 
     177                ftruncate( $fp, ftell( $fp ) );
     178        }
     179        fflush( $fp );
    163180        flock( $fp, LOCK_UN );
    164181        fclose( $fp );
Note: See TracChangeset for help on using the changeset viewer.