Commit 31b8b25a authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

cdiGetFiletype: check whether file is empty or a directory.

parent d1c0158c
...@@ -36,11 +36,13 @@ extern "C" { ...@@ -36,11 +36,13 @@ extern "C" {
#define CDI_EEOF -1 // The end of file was encountered #define CDI_EEOF -1 // The end of file was encountered
#define CDI_ESYSTEM -10 // Operating system error #define CDI_ESYSTEM -10 // Operating system error
#define CDI_EINVAL -20 // Invalid argument #define CDI_EINVAL -20 // Invalid argument
#define CDI_EUFTYPE -21 // Unsupported file type #define CDI_EISDIR -21 // Is a directory
#define CDI_ELIBNAVAIL -22 // xxx library not available #define CDI_EISEMPTY -22 // Is empty
#define CDI_EUFSTRUCT -23 // Unsupported file structure #define CDI_EUFTYPE -23 // Unsupported file type
#define CDI_EUNC4 -24 // Unsupported NetCDF4 structure #define CDI_ELIBNAVAIL -24 // xxx library not available
#define CDI_EDIMSIZE -25 // Invalid dimension size #define CDI_EUFSTRUCT -25 // Unsupported file structure
#define CDI_EUNC4 -26 // Unsupported NetCDF4 structure
#define CDI_EDIMSIZE -27 // Invalid dimension size
#define CDI_ELIMIT -99 // Internal limits exceeded #define CDI_ELIMIT -99 // Internal limits exceeded
/* File types */ /* File types */
......
#if defined (HAVE_CONFIG_H) #if defined (HAVE_CONFIG_H)
# include "config.h" #include "config.h"
#endif #endif
#include <stdio.h> #include <stdio.h>
...@@ -10,6 +10,8 @@ ...@@ -10,6 +10,8 @@
const char *cdiStringError(int cdiErrno) const char *cdiStringError(int cdiErrno)
{ {
static const char UnknownError[] = "Unknown Error"; static const char UnknownError[] = "Unknown Error";
static const char _EISDIR[] = "Is a directory";
static const char _EISEMPTY[] = "File is empty";
static const char _EUFTYPE[] = "Unsupported file type"; static const char _EUFTYPE[] = "Unsupported file type";
static const char _ELIBNAVAIL[] = "Unsupported file type (library support not compiled in)"; static const char _ELIBNAVAIL[] = "Unsupported file type (library support not compiled in)";
static const char _EUFSTRUCT[] = "Unsupported file structure"; static const char _EUFSTRUCT[] = "Unsupported file structure";
...@@ -24,6 +26,8 @@ const char *cdiStringError(int cdiErrno) ...@@ -24,6 +26,8 @@ const char *cdiStringError(int cdiErrno)
if ( cp == NULL ) break; if ( cp == NULL ) break;
return cp; return cp;
} }
case CDI_EISDIR: return _EISDIR;
case CDI_EISEMPTY: return _EISEMPTY;
case CDI_EUFTYPE: return _EUFTYPE; case CDI_EUFTYPE: return _EUFTYPE;
case CDI_ELIBNAVAIL: return _ELIBNAVAIL; case CDI_ELIBNAVAIL: return _ELIBNAVAIL;
case CDI_EUFSTRUCT: return _EUFSTRUCT; case CDI_EUFSTRUCT: return _EUFSTRUCT;
......
...@@ -6,6 +6,7 @@ ...@@ -6,6 +6,7 @@
#define _XOPEN_SOURCE 600 #define _XOPEN_SOURCE 600
#endif #endif
#include <sys/stat.h> // struct stat
#include <ctype.h> #include <ctype.h>
#include "binary.h" #include "binary.h"
...@@ -78,7 +79,6 @@ int cdiGetFiletype(const char *filename, int *byteorder) ...@@ -78,7 +79,6 @@ int cdiGetFiletype(const char *filename, int *byteorder)
int swap = 0; int swap = 0;
int version; int version;
long recpos; long recpos;
char buffer[8];
int fileID = fileOpen(filename, "r"); int fileID = fileOpen(filename, "r");
...@@ -90,7 +90,18 @@ int cdiGetFiletype(const char *filename, int *byteorder) ...@@ -90,7 +90,18 @@ int cdiGetFiletype(const char *filename, int *byteorder)
return CDI_ESYSTEM; return CDI_ESYSTEM;
} }
if ( fileRead(fileID, buffer, 8) != 8 ) return CDI_EUFTYPE; char buffer[8];
if ( fileRead(fileID, buffer, 8) != 8 )
{
struct stat buf;
if ( stat(filename, &buf) == 0 )
{
if ( buf.st_size == 0 ) return CDI_EISEMPTY;
if ( buf.st_mode&S_IFDIR ) return CDI_EISDIR;
}
return CDI_EUFTYPE;
}
fileRewind(fileID); fileRewind(fileID);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment