Skip to content

Commit

Permalink
Pathname bug in ecl_sum
Browse files Browse the repository at this point in the history
The ecl_sum_set_case() function was confused if a directory BASE existed along
with the BASE.xxxx files which belonged to a simulation. Fixed by using new
purely string based path inspection functions.
  • Loading branch information
joakim-hove committed Oct 5, 2018
1 parent afebb5a commit 77ca7d5
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
32 changes: 21 additions & 11 deletions lib/ecl/ecl_sum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#include <ert/ecl/ecl_sum_data.hpp>
#include <ert/ecl/smspec_node.hpp>

#include "detail/util/path.hpp"

/**
The ECLIPSE summary data is organised in a header file (.SMSPEC)
Expand Down Expand Up @@ -131,22 +132,31 @@ void ecl_sum_set_case( ecl_sum_type * ecl_sum , const char * input_arg) {
free( ecl_sum->base );
free( ecl_sum->ext );
{
char *path , *base, *ext;
std::string path = ecl::util::path::dirname(input_arg);
std::string base = ecl::util::path::basename(input_arg);
std::string ext = ecl::util::path::extension(input_arg);

util_alloc_file_components( input_arg, &path , &base , &ext);
ecl_sum->ecl_case = util_alloc_filename(path.c_str(), base.c_str(), NULL);
if (path.size())
ecl_sum->path = util_alloc_string_copy( path.c_str() );
else
ecl_sum->path = NULL;

if (base.size())
ecl_sum->base = util_alloc_string_copy( base.c_str() );
else
ecl_sum->base = NULL;

ecl_sum->ecl_case = util_alloc_filename( path, base, NULL );
ecl_sum->path = util_alloc_string_copy( path );
ecl_sum->base = util_alloc_string_copy( base );
ecl_sum->ext = util_alloc_string_copy( ext );
if (path != NULL)
ecl_sum->abs_path = util_alloc_abs_path( path );
if (ext.size())
ecl_sum->ext = util_alloc_string_copy( ext.c_str() );
else
ecl_sum->ext = NULL;

if (path.size() > 0)
ecl_sum->abs_path = util_alloc_abs_path( path.c_str() );
else
ecl_sum->abs_path = util_alloc_cwd();

free( base );
free( path );
free( ext );
}
}

Expand Down
26 changes: 20 additions & 6 deletions lib/util/path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ namespace ecl {

namespace path {

/*
The bizarre dive down into c_str() is to avoid inadvertendtly
introducing a symbol which is creates a ABI compatibility issue between
the libstdc++ librararies. A bug in the devtoolset compiler?
*/

std::string dirname(const std::string& fname) {
size_t last_slash = fname.rfind(UTIL_PATH_SEP_CHAR);
if (last_slash == std::string::npos)
Expand All @@ -39,7 +45,6 @@ namespace ecl {
return fname.substr(0, last_slash);
}


std::string basename(const std::string& fname) {
size_t end_pos = fname.rfind('.');
size_t offset = fname.rfind(UTIL_PATH_SEP_CHAR);
Expand All @@ -48,19 +53,28 @@ namespace ecl {
else
offset += 1;

if (end_pos == std::string::npos)
return fname.substr(offset);
else
return fname.substr(offset, end_pos - offset);
{
const char * c_str = fname.c_str();
if (end_pos == std::string::npos || end_pos < offset)
return util_alloc_string_copy( &c_str[offset] );

return util_alloc_substring_copy(c_str, offset, end_pos - offset);
}
}


std::string extension(const std::string& fname) {
size_t end_pos = fname.rfind('.');
size_t last_slash = fname.rfind(UTIL_PATH_SEP_CHAR);
if (end_pos == std::string::npos)
return "";

return fname.substr(end_pos + 1);
if (last_slash == std::string::npos || end_pos > last_slash) {
const char * c_str = fname.c_str();
return util_alloc_substring_copy( c_str, end_pos + 1, fname.size() - end_pos - 1);
}

return "";
}

}
Expand Down
3 changes: 3 additions & 0 deletions lib/util/tests/ert_util_split_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,7 @@ int main(int argc , char ** argv) {
test_assert_std_string_equal( std::string("file") , path::basename("/tmp/user.ext/file.ext"));
test_assert_std_string_equal( std::string("ext"), path::extension("/tmp/user.ext/file.ext"));

test_assert_std_string_equal( std::string("/tmp/user.ext"), path::dirname("/tmp/user.ext/"));
test_assert_std_string_equal( std::string("") , path::basename("/tmp/user.ext/"));
test_assert_std_string_equal( std::string(""), path::extension("/tmp/user.ext/"));
}
9 changes: 9 additions & 0 deletions python/tests/ecl_tests/test_sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,3 +576,12 @@ def test_write_not_implemented(self):
self.assertFalse(case.can_write())
with self.assertRaises(NotImplementedError):
case.fwrite( )



def test_directory_conflict(self):
with TestAreaContext("dir_conflict"):
case = create_case("UNITS")
case.fwrite()
os.mkdir("UNITS")
case2 = EclSum("./UNITS")

0 comments on commit 77ca7d5

Please sign in to comment.