diff --git a/reactos/include/rosrtl/path.h b/reactos/include/rosrtl/path.h new file mode 100644 index 00000000000..4e71704c86f --- /dev/null +++ b/reactos/include/rosrtl/path.h @@ -0,0 +1,21 @@ +/* + */ + +#ifndef ROSRTL_PATH_H__ +#define ROSRTL_PATH_H__ + +#ifdef __cplusplus +extern "C" +{ +#endif + +BOOL STDCALL MakeSureDirectoryPathExistsExA(LPCSTR DirPath, BOOL FileAtEnd); +BOOL STDCALL MakeSureDirectoryPathExistsExW(LPCWSTR DirPath, BOOL FileAtEnd); + +#ifdef __cplusplus +} +#endif + +#endif + +/* EOF */ diff --git a/reactos/lib/rosrtl/file/path.c b/reactos/lib/rosrtl/file/path.c new file mode 100644 index 00000000000..595f9ee0f59 --- /dev/null +++ b/reactos/lib/rosrtl/file/path.c @@ -0,0 +1,100 @@ +#include +#include + + +/*********************************************************************** + * MakeSureDirectoryPathExistsExA + * + * If a dir is at the end and the path ends with a backslash, FileAtEnd + * is ignored. If the path doesn't end with a backslash, FileAtEnd is + * used to determine if the last part of the path is a file name or a + * directory. + * + * Path may be absolute or relative to current dir. + */ +BOOL STDCALL MakeSureDirectoryPathExistsExA(LPCSTR DirPath, BOOL FileAtEnd) +{ + char Path[MAX_PATH]; + char *SlashPos = Path; + char Slash; + BOOL bRes; + + strcpy(Path, DirPath); + + while((SlashPos=strpbrk(SlashPos+1,"\\/"))) + { + Slash = *SlashPos; + *SlashPos = 0; + + bRes = CreateDirectoryA(Path, NULL); + if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS) + { + return FALSE; + } + + *SlashPos = Slash; + + if (*(SlashPos+1) == 0) return TRUE; + } + + if (!FileAtEnd) + { + bRes = CreateDirectoryA(Path, NULL); + if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS) + { + return FALSE; + } + } + + return TRUE; +} + + + + +/*********************************************************************** + * MakeSureDirectoryPathExistsExW + * + * If a dir is at the end and the path ends with a backslash, FileAtEnd + * is ignored. If the path doesn't end with a backslash, FileAtEnd is + * used to determine if the last part of the path is a file name or a + * directory. + * + * Path may be absolute or relative to current dir. + */ +BOOL STDCALL MakeSureDirectoryPathExistsExW(LPCWSTR DirPath, BOOL FileAtEnd) +{ + WCHAR Path[MAX_PATH]; + WCHAR *SlashPos = Path; + WCHAR Slash; + BOOL bRes; + + wcscpy(Path, DirPath); + + while((SlashPos=wcspbrk(SlashPos+1,L"\\/"))) + { + Slash = *SlashPos; + *SlashPos = 0; + + bRes = CreateDirectoryW(Path, NULL); + if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS) + { + return FALSE; + } + + *SlashPos = Slash; + + if (*(SlashPos+1) == 0) return TRUE; + } + + if (!FileAtEnd) + { + bRes = CreateDirectoryW(Path, NULL); + if (bRes == FALSE && GetLastError() != ERROR_ALREADY_EXISTS) + { + return FALSE; + } + } + + return TRUE; +} diff --git a/reactos/lib/rosrtl/makefile b/reactos/lib/rosrtl/makefile index 42fd621ab52..6c0f6acbffc 100644 --- a/reactos/lib/rosrtl/makefile +++ b/reactos/lib/rosrtl/makefile @@ -30,7 +30,8 @@ MISC_OBJECTS = \ misc/qsort.o FILE_OBJECTS = \ - file/sparse.o + file/sparse.o \ + file/path.o RECMUTEX_OBJECTS = recmutex/recmutex.o