This is an automated email from Gerrit.

"Daniel Anselmi <danse...@gmx.ch>" just uploaded a new patch set to Gerrit, 
which you can find at https://review.openocd.org/c/openocd/+/7388

-- gerrit

commit 65ffb8b27a94814f24b499d6a54d3e7792b3a6a3
Author: Daniel Anselmi <danse...@gmx.ch>
Date:   Wed Nov 30 23:04:51 2022 +0100

    pld: move file sanity checks to pld.c
    
    Change-Id: Id64b1165b25a03634949ac22b8af16eb0e24c1fa
    Signed-off-by: Daniel Anselmi <danse...@gmx.ch>

diff --git a/src/pld/efinix.c b/src/pld/efinix.c
index 7e16d1c4aa..2a94f6451e 100644
--- a/src/pld/efinix.c
+++ b/src/pld/efinix.c
@@ -9,9 +9,7 @@
 #include "config.h"
 #endif
 
-#include <sys/stat.h>
 #include <jtag/jtag.h>
-
 #include "pld.h"
 #include "raw_bit.h"
 
@@ -91,26 +89,9 @@ static int trion_read_bit_file(struct raw_bit_file 
*bit_file, const char *filena
 
 static int trion_read_file(struct raw_bit_file *bit_file, const char *filename)
 {
-       struct stat input_stat;
-
        if (!filename || !bit_file)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (stat(filename, &input_stat) == -1) {
-               LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (S_ISDIR(input_stat.st_mode)) {
-               LOG_ERROR("%s is a directory", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (input_stat.st_size == 0) {
-               LOG_ERROR("Empty file %s", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
        /* check if binary .bin or ascii .bit/.hex */
        const char *file_ending_pos = strrchr(filename, '.');
        if (!file_ending_pos) {
@@ -125,7 +106,7 @@ static int trion_read_file(struct raw_bit_file *bit_file, 
const char *filename)
                return trion_read_bit_file(bit_file, filename);
        }
 
-       LOG_ERROR("Unable to detect filetype");
+       LOG_ERROR("Unable to detect file-type, expecting .bin or .bit or .hex 
file");
        return ERROR_PLD_FILE_LOAD_FAILED;
 }
 
diff --git a/src/pld/gowin.c b/src/pld/gowin.c
index 550f897058..840d192d8d 100644
--- a/src/pld/gowin.c
+++ b/src/pld/gowin.c
@@ -11,7 +11,6 @@
 
 #include <jtag/jtag.h>
 #include <jtag/adapter.h>
-#include <sys/stat.h>
 #include "pld.h"
 #include "raw_bit.h"
 
@@ -177,23 +176,6 @@ static int gowin_read_file(struct gowin_bit_file *bit_file,
        if (!filename || !bit_file)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       struct stat input_stat;
-       if (stat(filename, &input_stat) == -1) {
-               LOG_ERROR("couldn't stat() %s: %s", filename,
-                                       strerror(errno));
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (S_ISDIR(input_stat.st_mode)) {
-               LOG_ERROR("%s is a directory", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (input_stat.st_size == 0) {
-               LOG_ERROR("Empty file %s", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
        const char *file_suffix_pos = strrchr(filename, '.');
        if (!file_suffix_pos) {
                LOG_ERROR("Unable to detect filename suffix");
diff --git a/src/pld/intel.c b/src/pld/intel.c
index e9b3176eb9..3778e2f3ac 100644
--- a/src/pld/intel.c
+++ b/src/pld/intel.c
@@ -11,7 +11,6 @@
 
 #include <jtag/jtag.h>
 #include <jtag/adapter.h>
-#include <sys/stat.h>
 #include <helper/system.h>
 #include <helper/log.h>
 
@@ -185,26 +184,9 @@ static int intel_check_config(struct intel_pld_device 
*intel_info)
 
 static int intel_read_file(struct raw_bit_file *bit_file, const char *filename)
 {
-       struct stat input_stat;
-
        if (!filename || !bit_file)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (stat(filename, &input_stat) == -1) {
-               LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (S_ISDIR(input_stat.st_mode)) {
-               LOG_ERROR("%s is a directory", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (input_stat.st_size == 0) {
-               LOG_ERROR("Empty file %s", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
        /* check if binary .bin or ascii .bit/.hex */
        const char *file_ending_pos = strrchr(filename, '.');
        if (!file_ending_pos) {
@@ -215,7 +197,7 @@ static int intel_read_file(struct raw_bit_file *bit_file, 
const char *filename)
        if (strcasecmp(file_ending_pos, ".rbf") == 0)
                return cpld_read_raw_bit_file(bit_file, filename);
 
-       LOG_ERROR("Unable to detect filetype");
+       LOG_ERROR("File-type not supported, expecting .rbf file");
        return ERROR_PLD_FILE_LOAD_FAILED;
 }
 
diff --git a/src/pld/lattice_bit.c b/src/pld/lattice_bit.c
index 0d4795c65e..4af6c03930 100644
--- a/src/pld/lattice_bit.c
+++ b/src/pld/lattice_bit.c
@@ -11,7 +11,6 @@
 #include "lattice_bit.h"
 #include "raw_bit.h"
 #include "pld.h"
-#include <sys/stat.h>
 #include <helper/system.h>
 #include <helper/log.h>
 #include <helper/binarybuffer.h>
@@ -91,26 +90,9 @@ static int lattice_read_bit_file(struct lattice_bit_file 
*bit_file, const char *
 
 int lattice_read_file(struct lattice_bit_file *bit_file, const char *filename, 
enum bit_file_version version)
 {
-       struct stat input_stat;
-
        if (!filename || !bit_file)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (stat(filename, &input_stat) == -1) {
-               LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (S_ISDIR(input_stat.st_mode)) {
-               LOG_ERROR("%s is a directory", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (input_stat.st_size == 0) {
-               LOG_ERROR("Empty file %s", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
        /* check if binary .bin or ascii .bit/.hex */
        const char *file_suffix_pos = strrchr(filename, '.');
        if (!file_suffix_pos) {
@@ -121,6 +103,6 @@ int lattice_read_file(struct lattice_bit_file *bit_file, 
const char *filename, e
        if (strcasecmp(file_suffix_pos, ".bit") == 0)
                return lattice_read_bit_file(bit_file, filename, version);
 
-       LOG_ERROR("Filetype not supported");
+       LOG_ERROR("File-type not supported, expecting .bit file");
        return ERROR_PLD_FILE_LOAD_FAILED;
 }
diff --git a/src/pld/pld.c b/src/pld/pld.c
index 5c2bb91d78..c55aa97cf6 100644
--- a/src/pld/pld.c
+++ b/src/pld/pld.c
@@ -10,6 +10,7 @@
 #endif
 
 #include "pld.h"
+#include <sys/stat.h>
 #include <helper/log.h>
 #include <helper/replacements.h>
 #include <helper/time_support.h>
@@ -146,6 +147,22 @@ COMMAND_HANDLER(handle_pld_load_command)
                return ERROR_OK;
        }
 
+       struct stat input_stat;
+       if (stat(CMD_ARGV[1], &input_stat) == -1) {
+               LOG_ERROR("couldn't stat() %s: %s", CMD_ARGV[1], 
strerror(errno));
+               return ERROR_PLD_FILE_LOAD_FAILED;
+       }
+
+       if (S_ISDIR(input_stat.st_mode)) {
+               LOG_ERROR("%s is a directory", CMD_ARGV[1]);
+               return ERROR_PLD_FILE_LOAD_FAILED;
+       }
+
+       if (input_stat.st_size == 0) {
+               LOG_ERROR("Empty file %s", CMD_ARGV[1]);
+               return ERROR_PLD_FILE_LOAD_FAILED;
+       }
+
        retval = p->driver->load(p, CMD_ARGV[1]);
        if (retval != ERROR_OK) {
                command_print(CMD, "failed loading file %s to pld device %u",
diff --git a/src/pld/raw_bit.c b/src/pld/raw_bit.c
index f9defe9d2b..0c3b92e7e9 100644
--- a/src/pld/raw_bit.c
+++ b/src/pld/raw_bit.c
@@ -12,7 +12,6 @@
 #include "raw_bit.h"
 #include "pld.h"
 
-#include <sys/stat.h>
 #include <helper/system.h>
 #include <helper/log.h>
 
diff --git a/src/pld/xilinx_bit.c b/src/pld/xilinx_bit.c
index 792b3375b6..e4cc52ef97 100644
--- a/src/pld/xilinx_bit.c
+++ b/src/pld/xilinx_bit.c
@@ -13,7 +13,6 @@
 #include "pld.h"
 #include <helper/log.h>
 
-#include <sys/stat.h>
 #include <helper/system.h>
 
 static int read_section(FILE *input_file, int length_size, char section,
@@ -60,27 +59,11 @@ static int read_section(FILE *input_file, int length_size, 
char section,
 int xilinx_read_bit_file(struct xilinx_bit_file *bit_file, const char 
*filename)
 {
        FILE *input_file;
-       struct stat input_stat;
        int read_count;
 
        if (!filename || !bit_file)
                return ERROR_COMMAND_SYNTAX_ERROR;
 
-       if (stat(filename, &input_stat) == -1) {
-               LOG_ERROR("couldn't stat() %s: %s", filename, strerror(errno));
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (S_ISDIR(input_stat.st_mode)) {
-               LOG_ERROR("%s is a directory", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
-       if (input_stat.st_size == 0) {
-               LOG_ERROR("Empty file %s", filename);
-               return ERROR_PLD_FILE_LOAD_FAILED;
-       }
-
        input_file = fopen(filename, "rb");
        if (!input_file) {
                LOG_ERROR("couldn't open %s: %s", filename, strerror(errno));

-- 

Reply via email to