Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Check return value of fclose()
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Alan Jenkins  
View profile  
 More options Feb 19 2010, 10:50 am
From: Alan Jenkins <sourcejedi.l...@googlemail.com>
Date: Fri, 19 Feb 2010 15:50:09 +0000
Local: Fri, Feb 19 2010 10:50 am
Subject: [PATCH 1/2] Check return value of fclose()
man close(2)
...
Not checking the return value of close() is a common but nevertheless
serious programming error.  It is quite possible that errors on a
previous write(2) operation are first reported at the final close().
Not checking the return value when closing the file may lead to
silent loss of data.  This can especially be observed with NFS and
with disk quota.

---
 .../src/unix/filesystem/ZLUnixFileOutputStream.cpp |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)

diff --git a/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp b/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
index 7370dfb..99b6a15 100644
--- a/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
+++ b/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
@@ -61,7 +61,8 @@ void ZLUnixFileOutputStream::write(const std::string &str) {

 void ZLUnixFileOutputStream::close() {
        if (myFile != 0) {
-               ::fclose(myFile);
+               if (::fclose(myFile))
+                       myHasErrors = true;
                myFile = 0;
                if (!myHasErrors) {
                        rename(myTemporaryName.c_str(), myName.c_str());
--
1.6.3.3


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "Write file data with fsync() before replacing existing files" by Alan Jenkins
Alan Jenkins  
View profile  
 More options Feb 19 2010, 10:51 am
From: Alan Jenkins <sourcejedi.l...@googlemail.com>
Date: Fri, 19 Feb 2010 15:51:28 +0000
Local: Fri, Feb 19 2010 10:51 am
Subject: [PATCH 2/2] Write file data with fsync() before replacing existing files
SQLLite files are already updated using fsync().  This change
will make ZLUnixFileOutputStream safer in case of power failure
on certain filesystems.

More specifically, this change make it safer for me to run
FBReader on my e-book reader using UBIFS.  Without this, there
is a risk that config files will be truncated to 0 bytes if the
battery runs out soon after changing a config option.

See <http://www.linux-mtd.infradead.org/faq/ubifs.html#L_atomic_change>

"Changing a file atomically means changing its contents in a way that
unclean reboots could not lead to any corruption or inconsistency in
the file. The only reliable way to do this in UBIFS (and in most of
other file-systems, e.g. JFFS2 or ext3) is the following:

    * make a copy of the file;
    * change the copy;
    * synchronize the copy (see here);
    * re-name the copy to the file (using the rename() libc function
      or the mv utility).

Note, if a power-cut happens during the re-naming, the original file
will be intact because the re-name operation is atomic. This is a POSIX
requirement and UBIFS satisfies it.

Often applications do not do the third step - synchronizing the copy.
Although this is generally an application bug, the ext4 file-system has
a hack which makes sure the data of the copy hits the disk before the
re-name meta-data, which "fixes" buggy applications. However, UBIFS does
not have this feature [...]"

---
 .../src/unix/filesystem/ZLUnixFileOutputStream.cpp |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)

diff --git a/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp b/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
index 99b6a15..40fa692 100644
--- a/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
+++ b/zlibrary/core/src/unix/filesystem/ZLUnixFileOutputStream.cpp
@@ -61,6 +61,8 @@ void ZLUnixFileOutputStream::write(const std::string &str) {

 void ZLUnixFileOutputStream::close() {
        if (myFile != 0) {
+               if (::fsync(fileno(myFile)))
+                       myHasErrors = true;
                if (::fclose(myFile))
                        myHasErrors = true;
                myFile = 0;
--
1.6.3.3


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »