From 41f6f17d01562aa09bdbcc6b02241b62f1d06b75 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Fri, 19 Sep 2008 08:11:44 +0000
Subject: [PATCH] return 0 (meaning the file is not seekable, as the docs now
 explain) instead of 4KB for the files in sysfs under Linux (#9965)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@55726 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 interface/wx/file.h |  6 ++++++
 src/common/file.cpp | 15 +++++++++++++++
 2 files changed, 21 insertions(+)

diff --git a/interface/wx/file.h b/interface/wx/file.h
index db7cb2f8f3..6c0ababe84 100644
--- a/interface/wx/file.h
+++ b/interface/wx/file.h
@@ -82,6 +82,12 @@ public:
 
     /**
         Returns the length of the file.
+
+        This method may return wxInvalidOffset if the length couldn't be
+        determined or also 0 even for non-empty files if the file is not
+        seekable. In general, the only way to determine if the file for which
+        this function returns 0 is really empty or not is to try reading from
+        it.
     */
     wxFileOffset Length() const;
 
diff --git a/src/common/file.cpp b/src/common/file.cpp
index a6d96d6a8e..58e36f2218 100644
--- a/src/common/file.cpp
+++ b/src/common/file.cpp
@@ -393,6 +393,21 @@ wxFileOffset wxFile::Length() const
 {
     wxASSERT( IsOpened() );
 
+    // we use a special method for Linux systems where files in sysfs (i.e.
+    // those under /sys typically) return length of 4096 bytes even when
+    // they're much smaller -- this is a problem as it results in errors later
+    // when we try reading 4KB from them
+#ifdef __LINUX__
+    struct stat st;
+    if ( fstat(m_fd, &st) == 0 )
+    {
+        // returning 0 for the special files indicates to the caller that they
+        // are not seekable
+        return st.st_blocks ? st.st_size : 0;
+    }
+    //else: failed to stat, try the normal method
+#endif // __LINUX__
+
     wxFileOffset iRc = Tell();
     if ( iRc != wxInvalidOffset ) {
         // have to use const_cast :-(
-- 
2.47.2