[patch] On Unix, FileOpen works on directories, which causes weird TFileStream.Size, TMemoryStream.LoadFromFile behaviour
Original Reporter info from Mantis: Michalis @michaliskambi
-
Reporter name: Michalis Kamburelis
Original Reporter info from Mantis: Michalis @michaliskambi
- Reporter name: Michalis Kamburelis
Description:
On Linux, opening a directory with TFileStream works. What's worse, TFileStream.Size is then a huge number 9223372036854775807 (at least on 64-bit system). One bad effect of this is that TMemoryStream.LoadFromFile fails with confusing EOutOfMemory exception. Tested with FPC 3.0.0 and latest 3.1.1.
On Windows, opening a directory fails with EFOpenError, and this seems more sane.
So I would propose to change FileOpen (used by TFileStream.Create) in rtl/unix/sysutils.pp to check whether the file is a directory, and then return feInvalidHandle in this case. Although various Unix documentation calls "directory" a "special kind of file", you cannot really read the directory contents using TFileStream, or interpret it's size in any cross-platform useful manner.
This change will be
- compatible with what happens on Windows,
- and make TFileStream.Create and TMemoryStream.LoadFromFile fail with EFOpenError, which seems more reasonable than current behavior.
Attaching a patch that does exactly that.
Mantis conversion info:
- Mantis ID: 30766
- OS: Debian GNU/Linux
- OS Build: (testing)
- Platform: x86-64
- Version: 3.1.1
- Fixed in version: 3.1.1
- Fixed in revision: 35063 (#d05175a1)
- Target version: 3.2.0