| 1807 | } |
| 1808 | |
| 1809 | bool FileSystem::CreateDirectoryPath(const char* Path, bool Recursive, Error* error) |
| 1810 | { |
| 1811 | const std::wstring wpath = GetWin32Path(Path); |
| 1812 | |
| 1813 | // has a path |
| 1814 | if (wpath.empty()) [[unlikely]] |
| 1815 | { |
| 1816 | Error::SetStringView(error, "Path is empty."); |
| 1817 | return false; |
| 1818 | } |
| 1819 | |
| 1820 | // try just flat-out, might work if there's no other segments that have to be made |
| 1821 | if (CreateDirectoryW(wpath.c_str(), nullptr)) |
| 1822 | return true; |
| 1823 | |
| 1824 | // check error |
| 1825 | DWORD lastError = GetLastError(); |
| 1826 | if (lastError == ERROR_ALREADY_EXISTS) |
| 1827 | { |
| 1828 | // check the attributes |
| 1829 | const u32 Attributes = GetFileAttributesW(wpath.c_str()); |
| 1830 | if (Attributes != INVALID_FILE_ATTRIBUTES && Attributes & FILE_ATTRIBUTE_DIRECTORY) |
| 1831 | return true; |
| 1832 | } |
| 1833 | |
| 1834 | if (!Recursive) |
| 1835 | { |
| 1836 | Error::SetWin32(error, "CreateDirectoryW() failed: ", lastError); |
| 1837 | return false; |
| 1838 | } |
| 1839 | |
| 1840 | // check error |
| 1841 | if (lastError == ERROR_PATH_NOT_FOUND) |
| 1842 | { |
| 1843 | // part of the path does not exist, so we'll create the parent folders, then |
| 1844 | // the full path again. |
| 1845 | const size_t pathLength = wpath.size(); |
| 1846 | std::wstring tempPath; |
| 1847 | tempPath.reserve(pathLength); |
| 1848 | |
| 1849 | // for absolute paths, we need to skip over the path root |
| 1850 | size_t rootLength = 0; |
| 1851 | if (Path::IsAbsolute(Path)) |
| 1852 | { |
| 1853 | const wchar_t* root_start = wpath.c_str(); |
| 1854 | wchar_t* root_end; |
| 1855 | const HRESULT hr = PathCchSkipRoot(const_cast<wchar_t*>(root_start), &root_end); |
| 1856 | if (FAILED(hr)) |
| 1857 | { |
| 1858 | Error::SetHResult(error, "PathCchSkipRoot() failed: ", hr); |
| 1859 | return false; |
| 1860 | } |
| 1861 | rootLength = static_cast<size_t>(root_end - root_start); |
| 1862 | |
| 1863 | // copy path root |
| 1864 | tempPath.append(wpath, 0, rootLength); |
| 1865 | } |
| 1866 |
nothing calls this directly
no test coverage detected