Skip to content

Commit

Permalink
Simplify SystemImpl to use omc_stat (#10907)
Browse files Browse the repository at this point in the history
* Simplify system file operations on Windows
* refactor SystemImpl__regularFileExists
  • Loading branch information
AnHeuermann committed Jul 3, 2023
1 parent 91dba94 commit a8e92e8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 55 deletions.
78 changes: 25 additions & 53 deletions OMCompiler/Compiler/runtime/systemimpl.c
Expand Up @@ -328,39 +328,26 @@ extern char* SystemImpl__pwd(void)
#endif
}

extern int SystemImpl__regularFileExists(const char* str)
/**
* @brief Check if regular file exists.
*
* Use stat and check mode of file.
*
* @param filename Multibyte string with file name.
* @return int Return 1 if it exists, 0 otherwise.
*/
extern int SystemImpl__regularFileExists(const char* filename)
{
#if defined(__MINGW32__) || defined(_MSC_VER)
WIN32_FIND_DATAW FileData;
HANDLE sh;

wchar_t* unicodeFilename = omc_multibyte_to_wchar_str(str);
sh = FindFirstFileW(unicodeFilename, &FileData);
free(unicodeFilename);
omc_stat_t buf;
int res = omc_stat(filename, &buf);

if (sh == INVALID_HANDLE_VALUE) {
if (strlen(str) >= MAXPATHLEN)
{
const char *c_tokens[1]={str};
c_add_message(NULL,85, /* error opening file */
ErrorType_scripting,
ErrorLevel_error,
gettext("Error opening file: %s."),
c_tokens,
1);
}
if(res != 0) {
return 0;
}
FindClose(sh);
return ((FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0);
#else
omc_stat_t buf;
/* adrpo: TODO: check if str leads to a path > PATH_MAX, maybe use realpath impl. from below */
if (omc_stat(str, &buf)) return 0;
return (buf.st_mode & S_IFREG) != 0;
#endif
}


extern int SystemImpl__regularFileWritable(const char* str)
{
FILE *f;
Expand Down Expand Up @@ -929,37 +916,22 @@ extern double SystemImpl__time(void)
return (double)cl / (double)CLOCKS_PER_SEC;
}

extern int SystemImpl__directoryExists(const char *str)
/**
* @brief Check if regular directory exists.
*
* Use stat and check mode of directory.
*
* @param dirname Multibyte string with directory name.
* @return int Return 1 if it exists, 0 otherwise.
*/
extern int SystemImpl__directoryExists(const char *dirname)
{
/* if the string is NULL return 0 */
if (!str) return 0;
#if defined(__MINGW32__) || defined(_MSC_VER)
WIN32_FIND_DATAW FileData;
HANDLE sh;
char* path = strdup(str);
int last = strlen(path)-1;
/* adrpo: RTFM! the path cannot end in a slash??!! https://msdn.microsoft.com/en-us/library/windows/desktop/aa364418(v=vs.85).aspx */
while (last > 0 && (path[last] == '\\' || path[last] == '/'))
last--;

path[last + 1] = '\0';

wchar_t* unicodePath = omc_multibyte_to_wchar_str(path);
sh = FindFirstFileW(unicodePath, &FileData);
free(unicodePath);
free(path);

if (sh == INVALID_HANDLE_VALUE)
return 0;

FindClose(sh);
return (FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
#else
if (!dirname) return 0;
omc_stat_t buf;
if (omc_stat(str, &buf))
if (omc_stat(dirname, &buf)) {
return 0;
}
return (buf.st_mode & S_IFDIR) != 0;
#endif
}

extern int SystemImpl__createDirectory(const char *str)
Expand Down
4 changes: 2 additions & 2 deletions OMCompiler/Compiler/runtime/systemimpl.h
Expand Up @@ -90,7 +90,7 @@ extern int SystemImpl__setLinker(const char *str);
extern int SystemImpl__setCFlags(const char *str);
extern int SystemImpl__setLDFlags(const char *str);
extern char* SystemImpl__pwd(void);
extern int SystemImpl__regularFileExists(const char* str);
extern int SystemImpl__regularFileExists(const char* filename);
extern int SystemImpl__removeFile(const char* filename);
extern int SystemImpl__rename(const char *source, const char *dest);
extern const char* SystemImpl__basename(const char *str);
Expand All @@ -103,7 +103,7 @@ extern void SystemImpl__plotCallBack(threadData_t *threadData, int externalWindo
const char* y2, const char* curveWidth, const char* curveStyle, const char* legendPosition, const char* footer,
const char* autoScale, const char* variables);
extern double SystemImpl__time(void);
extern int SystemImpl__directoryExists(const char* str);
extern int SystemImpl__directoryExists(const char *dirname);
extern int SystemImpl__copyFile(const char* str_1, const char* str_2);
extern int SystemImpl__createDirectory(const char *str);
extern int SystemImpl__removeDirectory(const char *str);
Expand Down

0 comments on commit a8e92e8

Please sign in to comment.