Re-added fix for very small files.

This commit is contained in:
Ward Fisher 2013-05-22 18:56:52 +00:00
parent 2c63696151
commit 1df00fa579

View File

@ -127,16 +127,32 @@ NC_check_file_type(const char *path, int use_parallel, void *mpi_info,
{
FILE *fp;
int i;
#ifdef HAVE_SYS_STAT_H
struct stat st;
#endif
if(path == NULL || strlen(path)==0)
return NC_EINVAL;
if (!(fp = fopen(path, "r")))
return errno;
#ifdef HAVE_SYS_STAT_H
/* The file must be at least MAGIC_NUMBER_LEN in size,
or otherwise the following fread will exhibit unexpected
behavior. */
if(!(fstat(fileno(fp),&st) == 0))
return errno;
if(st.st_size < MAGIC_NUMBER_LEN)
return NC_ENOTNC;
#endif
i = fread(magic, MAGIC_NUMBER_LEN, 1, fp);
fclose(fp);
if(i == 0 && errno == 22) //if file size < 4, Windows fread returns 0, errno 22.
return NC_ENOTNC;
if(i == 0)
return NC_ENOTNC;
if(i != 1)
return errno;
}