Make writing to prefs/abook/calendar files more reliable by handling it
authorkink <kink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 15 Mar 2004 10:10:00 +0000 (10:10 +0000)
committerkink <kink@7612ce4b-ef26-0410-bec9-ea0150e637f0>
Mon, 15 Mar 2004 10:10:00 +0000 (10:10 +0000)
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
functions/abook_local_file.php
functions/file_prefs.php
functions/strings.php
plugins/calendar/calendar_data.php

index 05185373e2f6e6255646b04021ab2a172489324a..a4ae1e53feb956a2d586ebf7a993c83bb423b480 100644 (file)
--- 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
 --------------------
index b9f93a78dbdda097990c8940b8833d0f5507ab2f..72c7932b018c0b6b217726c2df87fdbcca769488 100644 (file)
@@ -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 */
index 692b6c0411bfdaa0ef7b28cf48d1b883904ca39d..e4128326f0cac98a183061acd01421b56800cf90 100644 (file)
@@ -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;
     }
index 01454742ee109a5458f277fae3ba9b3e0fb07227..e295c4c910581e81d4f37e7384ebfed934bccb33 100644 (file)
@@ -541,5 +541,21 @@ function sm_print_r() {
     print '</pre>';
 }
 
+/**
+ * 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();
 ?>
index 8d6566808e3ef967ae16e64a9160d7f8477618fe..73dc8608fefb5e56879c702f150f2fe2c58f17ae 100644 (file)
@@ -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);
+               }
             }
 
         }