Skip to content

Commit

Permalink
System.removeDirectory removing dead links (#9479)
Browse files Browse the repository at this point in the history
- Handling of symbolic links to SystemImpl__removeDirectoryItem
  - omc_lstat added
    - using lstat on Unix and omc_stat on Windows
  - Error message if omc_stat and omc_lstat fails
  • Loading branch information
AnHeuermann committed Oct 6, 2022
1 parent 5b7da20 commit 14823c5
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
14 changes: 14 additions & 0 deletions OMCompiler/Compiler/runtime/systemimpl.c
Expand Up @@ -1069,6 +1069,20 @@ static int SystemImpl__removeDirectoryItem(const char *path)
r2 = omc_unlink(buf);
}
}
else if(omc_lstat(buf, &statbuf) == 0)
{
// Dead link, that isn't pointing to a file/directory any more
r2 = omc_unlink(buf);
}
else {
const char *c_tokens[1]={buf};
c_add_message(NULL,85,
ErrorType_scripting,
ErrorLevel_error,
gettext("Failed to remove %s"),
c_tokens,
1);
}
}
retval = r2;
}
Expand Down
27 changes: 26 additions & 1 deletion OMCompiler/SimulationRuntime/c/util/omc_file.c
Expand Up @@ -137,7 +137,6 @@ size_t omc_fread(void *buffer, size_t size, size_t count, FILE *stream, int allo
}



#if defined(__MINGW32__) || defined(_MSC_VER)
/**
* @brief File attributes
Expand Down Expand Up @@ -171,6 +170,32 @@ int omc_stat(const char *filename, omc_stat_t* statbuf)
}
#endif


#if defined(__MINGW32__) || defined(_MSC_VER)
/**
* @brief File attributes
*
* Using (long) unicode absolute path and `_wstat` on Windows.
* Using `stat` on Unix.
*
* @param filename File name.
* @param statbuf Pointer to stat structure.
* @return int 0 on success, -1 on error.
*/
int omc_lstat(const char *filename, omc_stat_t* statbuf)
{
return omc_stat(filename, statbuf);
}
#else /* unix */
int omc_lstat(const char *filename, omc_stat_t* statbuf)
{
int res;
res = lstat(filename, statbuf);
return res;
}
#endif


/**
* @brief Unlink file.
*
Expand Down
1 change: 1 addition & 0 deletions OMCompiler/SimulationRuntime/c/util/omc_file.h
Expand Up @@ -78,6 +78,7 @@ typedef struct _stat omc_stat_t;
typedef struct stat omc_stat_t;
#endif
int omc_stat(const char *filename, omc_stat_t *statbuf);
int omc_lstat(const char *filename, omc_stat_t *statbuf);

int omc_unlink(const char *filename);

Expand Down

0 comments on commit 14823c5

Please sign in to comment.