Determine file size only of regular files.

lcDiskfile::Open() uses fopen() to open a file. On Linux, this can open
a directory just fine (at least for reading). This is slightly problematic
when it is attempted to open the parts library $(sharedir)/library.bin
and the library is an unzipped directory hierarchy. It is first attempted
to open the path as a ZIP file. While opening the directory as a file is
successful, subsequent navigation in the open "file" fails.

Pretend that a directory is an empty file so that the ZIP file reader is
not tempted to navigate around in the "file" and so reports failure in a
deterministic manner.

We could have inserted the check for regular files in lcDiskFile::Open(),
but this burdens every file open request, which can happen thousands of
times when the parts library is extracted instead of in a ZIP file.
This commit is contained in:
Johannes Sixt 2017-03-15 21:49:48 +01:00
parent acd5a485f0
commit ba280bc1a7

View file

@ -2,6 +2,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/stat.h>
#include "lc_file.h" #include "lc_file.h"
// ============================================================================= // =============================================================================
@ -210,14 +211,11 @@ void lcDiskFile::SetLength(size_t NewLength)
size_t lcDiskFile::GetLength() const size_t lcDiskFile::GetLength() const
{ {
long Length, Current; struct stat st;
if (fstat(fileno(mFile), &st) < 0 || !S_ISREG(st.st_mode))
return 0;
Current = ftell(mFile); return st.st_size;
fseek(mFile, 0, SEEK_END);
Length = ftell(mFile);
fseek(mFile, Current, SEEK_SET);
return Length;
} }
void lcDiskFile::Flush() void lcDiskFile::Flush()