From 3ecad5e6d4b425f935cbb53d460cd627a9db2f17 Mon Sep 17 00:00:00 2001 From: kink Date: Mon, 15 Mar 2004 10:10:00 +0000 Subject: [PATCH] Make writing to prefs/abook/calendar files more reliable by handling it the right way when writing fails (error in stead of truncating file). git-svn-id: https://svn.code.sf.net/p/squirrelmail/code/trunk/squirrelmail@6799 7612ce4b-ef26-0410-bec9-ea0150e637f0 --- ChangeLog | 2 ++ functions/abook_local_file.php | 18 ++++++++++-------- functions/file_prefs.php | 4 ++-- functions/strings.php | 16 ++++++++++++++++ plugins/calendar/calendar_data.php | 6 ++++-- 5 files changed, 34 insertions(+), 12 deletions(-) diff --git a/ChangeLog b/ChangeLog index 05185373..a4ae1e53 100644 --- a/ChangeLog +++ b/ChangeLog @@ -25,6 +25,8 @@ Version 1.5.1 -- CVS - Fix test for LOGINDISABLED, should only test when the auth_mech actually is 'login'. - Update required PHP version to 4.1.0, and remove PHP 4.0.x legacy code. + - Make writing of preferences, abook, calendars fail better when disk full + (#915527). Version 1.5.0 -------------------- diff --git a/functions/abook_local_file.php b/functions/abook_local_file.php index b9f93a78..72c7932b 100644 --- a/functions/abook_local_file.php +++ b/functions/abook_local_file.php @@ -151,8 +151,8 @@ class abook_local_file extends addressbook_backend { for($j = 0, $cnt_part=count($rows[$i]) ; $j < $cnt_part ; $j++) { $rows[$i][$j] = $this->quotevalue($rows[$i][$j]); } - $tmpwrite = @fwrite($newfh, join('|', $rows[$i]) . "\n"); - if ($tmpwrite == -1) { + $tmpwrite = sq_fwrite($newfh, join('|', $rows[$i]) . "\n"); + if ($tmpwrite === FALSE) { return $this->set_error($this->filename . '.tmp:' . _("Write failed")); } } @@ -286,17 +286,19 @@ class abook_local_file extends addressbook_backend { } /* Write */ - $r = fwrite($this->filehandle, $data); + $r = sq_fwrite($this->filehandle, $data); /* Unlock file */ $this->unlock(); - /* Test write result and exit if OK */ - if($r > 0) return true; + /* Test write result */ + if($r === FALSE) { + /* Fail */ + $this->set_error(_("Write to addressbook failed")); + return FALSE; + } - /* Fail */ - $this->set_error(_("Write to addressbook failed")); - return false; + return TRUE; } /* Delete address */ diff --git a/functions/file_prefs.php b/functions/file_prefs.php index 692b6c04..e4128326 100644 --- a/functions/file_prefs.php +++ b/functions/file_prefs.php @@ -120,7 +120,7 @@ function savePrefValues($data_dir, $username) { } foreach ($prefs_cache as $Key => $Value) { if (isset($Value)) { - if ( @fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) { + if ( sq_fwrite($file, $Key . '=' . $Value . "\n") === FALSE ) { logout_error( sprintf( _("Preference file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp') ); exit; } @@ -227,7 +227,7 @@ function setSig($data_dir, $username, $number, $value) { logout_error( sprintf( _("Signature file, %s, could not be opened. Contact your system administrator to resolve this issue."), $filename . '.tmp') ); exit; } - if ( @fwrite($file, $value) === FALSE ) { + if ( sq_fwrite($file, $value) === FALSE ) { logout_error( sprintf( _("Signature file, %s, could not be written. Contact your system administrator to resolve this issue.") , $filename . '.tmp')); exit; } diff --git a/functions/strings.php b/functions/strings.php index 01454742..e295c4c9 100644 --- a/functions/strings.php +++ b/functions/strings.php @@ -541,5 +541,21 @@ function sm_print_r() { print ''; } +/** + * version of fwrite which checks for failure + */ +function sq_fwrite($fp, $string) { + // write to file + $count = @fwrite($fp,$string); + // the number of bytes written should be the length of the string + if($count != strlen($string)) { + return FALSE; + } + + return $count; +} + + + $PHP_SELF = php_self(); ?> diff --git a/plugins/calendar/calendar_data.php b/plugins/calendar/calendar_data.php index 8d656680..73dc8608 100644 --- a/plugins/calendar/calendar_data.php +++ b/plugins/calendar/calendar_data.php @@ -57,7 +57,7 @@ function readcalendardata() { //makes events persistant function writecalendardata() { - global $calendardata, $username, $data_dir, $year; + global $calendardata, $username, $data_dir, $year, $color; $filetmp = getHashedFile($username, $data_dir, "$username.$year.cal.tmp"); $filename = getHashedFile($username, $data_dir, "$username.$year.cal"); @@ -67,7 +67,9 @@ function writecalendardata() { while ( $calbar = each ($calfoo['value'])) { $calfoobar = $calendardata[$calfoo['key']][$calbar['key']]; $calstr = "$calfoo[key]|$calbar[key]|$calfoobar[length]|$calfoobar[priority]|$calfoobar[title]|$calfoobar[message]|$calfoobar[reminder]\n"; - fwrite($fp, $calstr, 4096); + if(sq_fwrite($fp, $calstr, 4096) === FALSE) { + error_box(_("Could not write calendar file %s", "$username.$year.cal.tmp"), $color); + } } } -- 2.25.1