diff --git a/lib_xua/host/xmosdfu/xmosdfu.cpp b/lib_xua/host/xmosdfu/xmosdfu.cpp index b9b0a952..5e5c6051 100644 --- a/lib_xua/host/xmosdfu/xmosdfu.cpp +++ b/lib_xua/host/xmosdfu/xmosdfu.cpp @@ -2,8 +2,17 @@ #include #include #include +#include #ifdef _WIN32 #include + +// Used for checking if a file is a directory +#ifndef S_ISDIR +#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR) +#endif +#define stat _stat +#define fstat _fstat + #else #include @@ -235,6 +244,7 @@ int write_dfu_image(char *file) unsigned int timeout = 0; unsigned char strIndex = 0; unsigned int dfuBlockCount = 0; + struct stat statbuf; inFile = fopen( file, "rb" ); if( inFile == NULL ) @@ -243,6 +253,19 @@ int write_dfu_image(char *file) return -1; } + /* Check if file is a directory */ + int status = fstat(fileno(inFile), &statbuf); + if (status != 0) + { + fprintf(stderr,"Error: Failed to get info on file.\n"); + return -1; + } + if ( S_ISDIR(statbuf.st_mode) ) + { + fprintf(stderr,"Error: Specified path is a directory.\n"); + return -1; + } + /* Discover the size of the image. */ if( 0 != fseek( inFile, 0, SEEK_END ) ) {