mirror of https://github.com/ecmwf/eccodes.git
Tools: check if input paths are directories
This commit is contained in:
parent
bd430b71f8
commit
bbe5e29b71
|
@ -43,6 +43,19 @@ char get_dir_separator_char(void)
|
||||||
return DIR_SEPARATOR_CHAR;
|
return DIR_SEPARATOR_CHAR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Return 1 if the filepath is a directory, 0 otherwise */
|
||||||
|
int path_is_directory(const char* filename)
|
||||||
|
{
|
||||||
|
struct stat s;
|
||||||
|
int stat_val = stat(filename, &s);
|
||||||
|
if ( stat_val == 0 ) {
|
||||||
|
if (S_ISDIR(s.st_mode)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
char* codes_getenv(const char* name)
|
char* codes_getenv(const char* name)
|
||||||
{
|
{
|
||||||
/* Look for the new ecCodes environment variable names */
|
/* Look for the new ecCodes environment variable names */
|
||||||
|
|
|
@ -1464,6 +1464,7 @@ grib_expression *grib_arguments_get_expression(grib_handle *h, grib_arguments *a
|
||||||
|
|
||||||
/* codes_util.c */
|
/* codes_util.c */
|
||||||
char get_dir_separator_char(void);
|
char get_dir_separator_char(void);
|
||||||
|
int path_is_directory(const char* filename);
|
||||||
char *codes_getenv(const char *name);
|
char *codes_getenv(const char *name);
|
||||||
|
|
||||||
/* grib_util.c */
|
/* grib_util.c */
|
||||||
|
|
|
@ -301,6 +301,9 @@ int grib_tool_init(grib_runtime_options* options)
|
||||||
context->blacklist=blacklist;
|
context->blacklist=blacklist;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Check 1st file is not a directory */
|
||||||
|
exit_if_input_is_directory(grib_tool_name, options->infile_extra->name);
|
||||||
|
|
||||||
if (grib_options_on("r")) {
|
if (grib_options_on("r")) {
|
||||||
char* filename[1];
|
char* filename[1];
|
||||||
filename[0]=options->infile_extra->name;
|
filename[0]=options->infile_extra->name;
|
||||||
|
@ -369,11 +372,9 @@ int grib_tool_init(grib_runtime_options* options)
|
||||||
{
|
{
|
||||||
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
||||||
/* with the same name as first file in that directory */
|
/* with the same name as first file in that directory */
|
||||||
struct stat s;
|
|
||||||
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
||||||
if (infile) {
|
if (infile) {
|
||||||
int stat_val = stat(infile->name, &s);
|
if (path_is_directory(infile->name)) {
|
||||||
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
|
|
||||||
/* Take the filename of the 1st file and append to dir */
|
/* Take the filename of the 1st file and append to dir */
|
||||||
char bufr[2048] = {0,};
|
char bufr[2048] = {0,};
|
||||||
/* options->infile_extra->name is the 1st file */
|
/* options->infile_extra->name is the 1st file */
|
||||||
|
|
|
@ -162,7 +162,6 @@ int main(int argc,char* argv[])
|
||||||
FILE* infh = NULL;
|
FILE* infh = NULL;
|
||||||
char* filename;
|
char* filename;
|
||||||
int i, status=0;
|
int i, status=0;
|
||||||
struct stat s;
|
|
||||||
int err=0;
|
int err=0;
|
||||||
unsigned long count=0;
|
unsigned long count=0;
|
||||||
|
|
||||||
|
@ -176,11 +175,9 @@ int main(int argc,char* argv[])
|
||||||
}
|
}
|
||||||
|
|
||||||
filename=argv[i];
|
filename=argv[i];
|
||||||
if (stat(filename, &s)==0) {
|
if (path_is_directory(filename)) {
|
||||||
if (S_ISDIR(s.st_mode)) {
|
fprintf(stderr, "ERROR: %s: Is a directory\n", filename);
|
||||||
fprintf(stderr, "ERROR: %s: Is a directory\n", filename);
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
infh=fopen(filename,"rb");
|
infh=fopen(filename,"rb");
|
||||||
if (!infh) {
|
if (!infh) {
|
||||||
|
|
|
@ -66,18 +66,6 @@ static int count_messages(FILE* in, int message_type, unsigned long *count)
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int is_a_directory(const char* filename)
|
|
||||||
{
|
|
||||||
struct stat s;
|
|
||||||
int stat_val = stat(filename, &s);
|
|
||||||
if ( stat_val == 0 ) {
|
|
||||||
if (S_ISDIR(s.st_mode)) {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc,char* argv[])
|
int main(int argc,char* argv[])
|
||||||
{
|
{
|
||||||
FILE* infh = NULL;
|
FILE* infh = NULL;
|
||||||
|
@ -105,7 +93,7 @@ int main(int argc,char* argv[])
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
filename=argv[i];
|
filename=argv[i];
|
||||||
if (is_a_directory(filename)) {
|
if (path_is_directory(filename)) {
|
||||||
fprintf(stderr, "%s: ERROR: \"%s\": Is a directory\n", tool_name, filename);
|
fprintf(stderr, "%s: ERROR: \"%s\": Is a directory\n", tool_name, filename);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
@ -111,7 +111,6 @@ int main(int argc,char* argv[])
|
||||||
FILE* infh = NULL;
|
FILE* infh = NULL;
|
||||||
char* filename;
|
char* filename;
|
||||||
int i, status=0;
|
int i, status=0;
|
||||||
struct stat s;
|
|
||||||
int err=0,nchunks=0;
|
int err=0,nchunks=0;
|
||||||
unsigned long count=0;
|
unsigned long count=0;
|
||||||
|
|
||||||
|
@ -133,11 +132,9 @@ int main(int argc,char* argv[])
|
||||||
|
|
||||||
i++;
|
i++;
|
||||||
filename=argv[i];
|
filename=argv[i];
|
||||||
if (stat(filename, &s)==0) {
|
if (path_is_directory(filename)) {
|
||||||
if (S_ISDIR(s.st_mode)) {
|
fprintf(stderr, "ERROR: %s: Is a directory\n", filename);
|
||||||
fprintf(stderr, "ERROR: %s: Is a directory\n", filename);
|
return 1;
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
infh=fopen(filename,"rb");
|
infh=fopen(filename,"rb");
|
||||||
if (!infh) {
|
if (!infh) {
|
||||||
|
|
|
@ -308,11 +308,9 @@ int grib_tool_init(grib_runtime_options* options)
|
||||||
{
|
{
|
||||||
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
||||||
/* with the same name as first file in that directory */
|
/* with the same name as first file in that directory */
|
||||||
struct stat s;
|
|
||||||
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
||||||
if (infile) {
|
if (infile) {
|
||||||
int stat_val = stat(infile->name, &s);
|
if (path_is_directory(infile->name)) {
|
||||||
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
|
|
||||||
/* Take the filename of the 1st file and append to dir */
|
/* Take the filename of the 1st file and append to dir */
|
||||||
char bufr[2048] = {0,};
|
char bufr[2048] = {0,};
|
||||||
/* options->infile_extra->name is the 1st file */
|
/* options->infile_extra->name is the 1st file */
|
||||||
|
|
|
@ -1272,13 +1272,9 @@ void grib_tools_write_message(grib_runtime_options* options, grib_handle* h)
|
||||||
}
|
}
|
||||||
int exit_if_input_is_directory(const char* tool_name, const char* filename)
|
int exit_if_input_is_directory(const char* tool_name, const char* filename)
|
||||||
{
|
{
|
||||||
struct stat s;
|
if ( path_is_directory(filename) ) {
|
||||||
int stat_val = stat(filename, &s);
|
fprintf(stderr, "%s: ERROR: \"%s\": Is a directory\n", tool_name, filename);
|
||||||
if ( stat_val == 0 ) {
|
exit(1);
|
||||||
if (S_ISDIR(s.st_mode)) {
|
|
||||||
fprintf(stderr, "%s: ERROR: \"%s\": Is a directory\n", tool_name, filename);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -228,11 +228,9 @@ int grib_tool_init(grib_runtime_options* options)
|
||||||
{
|
{
|
||||||
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
||||||
/* with the same name as first file in that directory */
|
/* with the same name as first file in that directory */
|
||||||
struct stat s;
|
|
||||||
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
||||||
if (infile) {
|
if (infile) {
|
||||||
int stat_val = stat(infile->name, &s);
|
if (path_is_directory(infile->name)) {
|
||||||
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
|
|
||||||
/* Take the filename of the 1st file and append to dir */
|
/* Take the filename of the 1st file and append to dir */
|
||||||
char bufr[2048] = {0,};
|
char bufr[2048] = {0,};
|
||||||
/* options->infile_extra->name is the 1st file */
|
/* options->infile_extra->name is the 1st file */
|
||||||
|
|
|
@ -313,11 +313,9 @@ int grib_tool_init(grib_runtime_options* options)
|
||||||
{
|
{
|
||||||
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
/* Check for 2nd file being a directory. If so, we assume user is comparing to a file */
|
||||||
/* with the same name as first file in that directory */
|
/* with the same name as first file in that directory */
|
||||||
struct stat s;
|
|
||||||
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
grib_tools_file* infile = options->infile; /* the 2nd file in comparison */
|
||||||
if (infile) {
|
if (infile) {
|
||||||
int stat_val = stat(infile->name, &s);
|
if (path_is_directory(infile->name)) {
|
||||||
if ( stat_val == 0 && S_ISDIR(s.st_mode)) {
|
|
||||||
/* Take the filename of the 1st file and append to dir */
|
/* Take the filename of the 1st file and append to dir */
|
||||||
char bufr[2048] = {0,};
|
char bufr[2048] = {0,};
|
||||||
/* options->infile_extra->name is the 1st file */
|
/* options->infile_extra->name is the 1st file */
|
||||||
|
|
Loading…
Reference in New Issue