Skip to content

Commit

Permalink
Error message and fail for realpath (#8706)
Browse files Browse the repository at this point in the history
  • Loading branch information
AnHeuermann committed Mar 15, 2022
1 parent c9ba3f4 commit 842f9ac
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
18 changes: 17 additions & 1 deletion OMCompiler/Compiler/Util/System.mo
Expand Up @@ -40,6 +40,7 @@ encapsulated package System

protected
import Autoconf;
import Error;

public function trim
"removes chars in charsToRemove from begin and end of inString"
Expand Down Expand Up @@ -172,6 +173,8 @@ public function tolower
end tolower;

public function strtok
"Break string into a series of tokens using the delimiter token.
See strtok from C standard."
input String string;
input String token;
output list<String> strings;
Expand Down Expand Up @@ -1082,9 +1085,22 @@ public function numBits
end numBits;

public function realpath
"Return the canonicalized absolute pathname"
input String path;
output String fullpath;
external "C" fullpath = System_realpath(path) annotation(Library = {"omcruntime"});
protected
function system_realpath
input String path;
output String fullpath;
external "C" fullpath = System_realpath(path) annotation(Library = {"omcruntime"});
end system_realpath;
algorithm
try
fullpath := system_realpath(path);
else
Error.addInternalError(getInstanceName() + " failed", sourceInfo());
fail();
end try;
end realpath;

public function getSimulationHelpText
Expand Down
30 changes: 30 additions & 0 deletions OMCompiler/Compiler/runtime/System_omc.c
Expand Up @@ -786,6 +786,36 @@ extern const char* System_realpath(const char *path)
#else
char buf[PATH_MAX];
if (realpath(path, buf) == NULL) {
fprintf(stderr, "System_realpath failed.\n");
switch (errno)
{
case EACCES:
fprintf(stderr, "Read or search permission was denied for a component of the path prefix.\n");
break;
case EINVAL:
fprintf(stderr, "path is NULL.\n");
break;
case EIO:
fprintf(stderr, "An I/O error occurred while reading from the filesystem.\n");
break;
case ELOOP:
fprintf(stderr, "Too many symbolic links were encountered in translating the pathname.\n");
break;
case ENAMETOOLONG:
fprintf(stderr, "A component of a pathname exceeded %u characters, or an entire pathname exceeded %u characters.\n", NAME_MAX, PATH_MAX);
break;
case ENOENT:
fprintf(stderr, "The named file does not exist.\n");
break;
case ENOMEM:
fprintf(stderr, "Out of memory.\n");
break;
case ENOTDIR:
fprintf(stderr, "A component of the path prefix is not a directory.\n");
break;
default:
break;
}
MMC_THROW();
}
return omc_alloc_interface.malloc_strdup(buf);
Expand Down

0 comments on commit 842f9ac

Please sign in to comment.