Fix error creating directories during update when the app is installed to a drive other than C: on Windows.

FileUtils::dirname() did not include the drive letter under Windows and
the return value of dirname() was fed directly to mkpath().

When the drive letter is not specified, mkdir() assumes drive C: -
this either fails if the user does not have access rights to drive C:
or creates a directory in the wrong place.

This commit changes FileUtils::dirname() to include the drive letter
on Windows and adds a test.
This commit is contained in:
Robert Knight 2011-10-19 11:30:17 +01:00
parent f49c147c4f
commit 02fa638261
5 changed files with 43 additions and 2 deletions

View File

@ -335,7 +335,14 @@ std::string FileUtils::dirname(const char* path)
0 /* extension length */ 0 /* extension length */
); );
return std::string(dir); std::string result;
if (drive[0])
{
result += std::string(drive);
}
result += dir;
return result;
#endif #endif
} }

View File

@ -84,7 +84,9 @@ class FileUtils
/** Returns the file name part of a file path, including the extension. */ /** Returns the file name part of a file path, including the extension. */
static std::string fileName(const char* path); static std::string fileName(const char* path);
/** Returns the directory part of a file path. */ /** Returns the directory part of a file path.
* On Windows this includes the drive letter, if present in @p path.
*/
static std::string dirname(const char* path); static std::string dirname(const char* path);
/** Remove a directory and all of its contents. */ /** Remove a directory and all of its contents. */

View File

@ -36,6 +36,12 @@ add_executable(TestUpdaterOptions
target_link_libraries(TestUpdaterOptions target_link_libraries(TestUpdaterOptions
updatershared updatershared
) )
add_executable(TestFileUtils
TestFileUtils.cpp
)
target_link_libraries(TestFileUtils
updatershared
)
if (APPLE) if (APPLE)
set_target_properties(TestUpdateScript PROPERTIES LINK_FLAGS "-framework Security -framework Cocoa") set_target_properties(TestUpdateScript PROPERTIES LINK_FLAGS "-framework Security -framework Cocoa")

View File

@ -0,0 +1,19 @@
#include "TestFileUtils.h"
#include "FileUtils.h"
#include "TestUtils.h"
void TestFileUtils::testDirName()
{
#ifdef PLATFORM_WINDOWS
std::string dirName = FileUtils::dirname("E:/Some Dir/App.exe");
TEST_COMPARE(dirName,"E:/Some Dir/");
#endif
}
int main(int,char**)
{
TestList<TestFileUtils> tests;
tests.addTest(&TestFileUtils::testDirName);
return TestUtils::runTest(tests);
}

View File

@ -0,0 +1,7 @@
#pragma once
class TestFileUtils
{
public:
void testDirName();
};