Skip to content

Commit 193e060

Browse files
sdkrystianalandefreitas
authored andcommitted
chore: files::createDirectory
1 parent f63df18 commit 193e060

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

include/mrdox/Support/Path.hpp

+13-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ forEachFile(
5454

5555
explicit FileVisitor(Visitor& v)
5656
: visitor_(v)
57-
{
57+
{
5858
}
5959

6060
Error
@@ -240,6 +240,18 @@ std::string_view
240240
getSourceFilename(
241241
std::string_view pathName);
242242

243+
/** Create a directory.
244+
245+
Any missing parent directories will also be created.
246+
247+
@param pathName The absolute or relative path
248+
to create.
249+
*/
250+
MRDOX_DECL
251+
Error
252+
createDirectory(
253+
std::string_view pathName);
254+
243255
} // files
244256

245257
} // mrdox

src/lib/Support/Path.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,27 @@ getSourceFilename(
354354
return pathName;
355355
}
356356

357+
Error
358+
createDirectory(
359+
std::string_view pathName)
360+
{
361+
namespace fs = llvm::sys::fs;
362+
363+
auto kind = files::getFileType(pathName);
364+
if(kind.has_error())
365+
return kind.error();
366+
if(*kind == files::FileType::directory)
367+
return Error::success();
368+
else if(*kind != files::FileType::not_found)
369+
return formatError("creating the directory \"{}\""
370+
" would overwrite an existing file", pathName);
371+
372+
if(auto ec = fs::create_directories(pathName))
373+
return formatError("fs::create_directories(\"{}\") returned {}", pathName, ec);
374+
375+
return Error::success();
376+
}
377+
357378
} // files
358379

359380
} // mrdox

0 commit comments

Comments
 (0)