public/wordpress/wp-content/plugins/backupbuddy/classes/core.php
/* truncate_file_beginning()
	 *
	 * Truncates a file from the BEGINNING. Eg for trimming logs while leaving the most recent (at the bottom) content.
	 *
	 */
truncate_file_beginning()
Truncates a file from the BEGINNING. Eg for trimming logs while leaving the most recent (at the bottom) content.
truncate_file_beginning () Truncates a file from the BEGINNING . Eg for trimming logs while leaving the most recent ( at the bottom ) content .
public static function truncate_file_beginning($filename,$maxfilesize,$keepPercent = 50 ){
		@clearstatcache( true, $filename );
		if ( false === ( $size = @filesize( $filename ) ) ) {
			return; 
		}
		if ($size<$maxfilesize) return;
		pb_backupbuddy::status( 'details', 'Truncating LARGE file `' . $filename . '` of size `' . pb_backupbuddy::$format->file_size( $size ) . '` exceeding threshold, only keeping newest ' . $keepPercent . '%.' );
		$maxfilesize=$maxfilesize * ( $keepPercent / 100 ); 
		$fh=fopen($filename,"r+");
		if ( false === ( $start = @ftell($fh) ) ) {
			return; 
		}
		fseek( $fh, -$maxfilesize, SEEK_END ); 
		$start = fgets( $fh ); 
		$length = ( (int)$size - (int)$start );
		if ( $length < 1 ) {
			return false;
		}
		$contents = fread( $fh, $length );
		$first_newline = strpos( $contents, "\n" );
		if ( false === $first_newline ) {
			$first_newline = 0; 
		} else {
			$first_newline = $first_newline + 1; 
		}
		$contents = substr( $contents, $first_newline ); 
		ftruncate( $fh, 0 ); 
		fseek( $fh, 0 );
		fwrite( $fh, $contents );
		fclose($fh);
	}
public static function truncate_file_beginning ( $ filename , $ maxfilesize , $ keepPercent = 50 ) { @ clearstatcache ( true , $ filename ) ; if ( false === ( $ size = @ filesize ( $ filename ) ) ) { return ; } if ( $ size < $ maxfilesize ) return ; pb_backupbuddy :: status ( 'details' , 'Truncating LARGE file `' . $ filename . '` of size `' . pb_backupbuddy :: $ format -> file_size ( $ size ) . '` exceeding threshold, only keeping newest ' . $ keepPercent . '%.' ) ; $ maxfilesize = $ maxfilesize * ( $ keepPercent / 100 ) ; $ fh = fopen ( $ filename , " r+ " ) ; if ( false === ( $ start = @ ftell ( $ fh ) ) ) { return ; } fseek ( $ fh , - $ maxfilesize , SEEK_END ) ; $ start = fgets ( $ fh ) ; $ length = ( ( int ) $ size - ( int ) $ start ) ; if ( $ length < 1 ) { return false ; } $ contents = fread ( $ fh , $ length ) ; $ first_newline = strpos ( $ contents , " \n " ) ; if ( false === $ first_newline ) { $ first_newline = 0 ; } else { $ first_newline = $ first_newline + 1 ; } $ contents = substr ( $ contents , $ first_newline ) ; ftruncate ( $ fh , 0 ) ; fseek ( $ fh , 0 ) ; fwrite ( $ fh , $ contents ) ; fclose ( $ fh ) ; }
truncate_file_beginning()
 Truncates a file from the BEGINNING.
truncate_file_beginning () Truncates a file from the BEGINNING .
// File did not exist/cannot access. // keep newest part up to cetain percent. // File did not exist/cannot access. // Seek to middle from the end. // Get start position. // Catch instances where fread length parameter was coming in less than 0. // Read in file from middle point. // Find first newline & cut off everything before it so we make sure to start at a fresh line (no half lines as beginning). // No newline. // After new line. // Cut out up to the first newline (just after). // Write new file contents over existing file data. // Erase current contents. // Cleanup.