< prev index next >
src/jdk.jpackage/windows/native/libjpackage/FileUtils.cpp
Print this page
@@ -60,11 +60,12 @@
return GetFileAttributes(filePath.c_str()) != INVALID_FILE_ATTRIBUTES;
}
namespace {
bool isDirectoryAttrs(const DWORD attrs) {
- return attrs != INVALID_FILE_ATTRIBUTES && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
+ return attrs != INVALID_FILE_ATTRIBUTES
+ && (attrs & FILE_ATTRIBUTE_DIRECTORY) != 0;
}
} // namespace
bool isDirectory(const tstring &filePath) {
return isDirectoryAttrs(GetFileAttributes(filePath.c_str()));
@@ -75,12 +76,11 @@
return false;
}
return FALSE == PathIsDirectoryEmpty(dirPath.c_str());
}
-tstring dirname(const tstring &path)
-{
+tstring dirname(const tstring &path) {
tstring::size_type pos = path.find_last_of(_T("\\/"));
if (pos != tstring::npos) {
pos = path.find_last_not_of(_T("\\/"), pos); // skip trailing slashes
}
return pos == tstring::npos ? tstring() : path.substr(0, pos + 1);
@@ -147,24 +147,26 @@
}
namespace {
bool createNewFile(const tstring& path) {
- HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);
- // if the file exists => h == INVALID_HANDLE_VALUE & GetLastError returns ERROR_FILE_EXISTS
+ HANDLE h = CreateFile(path.c_str(), GENERIC_WRITE, 0, NULL, CREATE_NEW,
+ FILE_ATTRIBUTE_NORMAL, NULL);
+ // if the file exists => h == INVALID_HANDLE_VALUE & GetLastError
+ // returns ERROR_FILE_EXISTS
if (h != INVALID_HANDLE_VALUE) {
CloseHandle(h);
LOG_TRACE(tstrings::any() << "Created [" << path << "] file");
return true;
}
return false;
}
} // namespace
-tstring createTempFile(const tstring &prefix, const tstring &suffix, const tstring &path)
-{
+tstring createTempFile(const tstring &prefix, const tstring &suffix,
+ const tstring &path) {
const tstring invalidChars = reservedFilenameChars();
if (prefix.find_first_of(invalidChars) != tstring::npos) {
JP_THROW(tstrings::any() << "Illegal characters in prefix=" << prefix);
}
@@ -175,11 +177,12 @@
int rnd = (int)GetTickCount();
// do no more than 100 attempts
for (int i=0; i<100; i++) {
- const tstring filePath = mkpath() << path << (prefix + (tstrings::any() << (rnd + i)).tstr() + suffix);
+ const tstring filePath = mkpath() << path << (prefix
+ + (tstrings::any() << (rnd + i)).tstr() + suffix);
if (createNewFile(filePath)) {
return filePath;
}
}
@@ -187,11 +190,12 @@
JP_THROW(tstrings::any() << "createTempFile(" << prefix << ", "
<< suffix << ", "
<< path << ") failed");
}
-tstring createTempDirectory(const tstring &prefix, const tstring &suffix, const tstring &basedir) {
+tstring createTempDirectory(const tstring &prefix, const tstring &suffix,
+ const tstring &basedir) {
const tstring filePath = createTempFile(prefix, suffix, basedir);
// delete the file and create directory with the same name
deleteFile(filePath);
createDirectory(filePath);
return filePath;
@@ -206,22 +210,23 @@
suffix(prototype), dirname(prototype));
}
namespace {
-void createDir(const tstring path, LPSECURITY_ATTRIBUTES saAttr, tstring_array* createdDirs=0) {
+void createDir(const tstring path, LPSECURITY_ATTRIBUTES saAttr,
+ tstring_array* createdDirs=0) {
if (CreateDirectory(path.c_str(), saAttr)) {
LOG_TRACE(tstrings::any() << "Created [" << path << "] directory");
if (createdDirs) {
createdDirs->push_back(removeTrailingSlash(path));
}
} else {
const DWORD createDirectoryErr = GetLastError();
// if saAttr is specified, fail even if the directory exists
if (saAttr != NULL || !isDirectory(path)) {
- JP_THROW(SysError(tstrings::any() << "CreateDirectory(" << path << ") failed",
- CreateDirectory, createDirectoryErr));
+ JP_THROW(SysError(tstrings::any() << "CreateDirectory("
+ << path << ") failed", CreateDirectory, createDirectoryErr));
}
}
}
}
@@ -236,13 +241,15 @@
pos = dirPath.find_first_of(_T("\\/"), pos + 1);
}
}
-void copyFile(const tstring& fromPath, const tstring& toPath, bool failIfExists) {
+void copyFile(const tstring& fromPath, const tstring& toPath,
+ bool failIfExists) {
createDirectory(dirname(toPath));
- if (!CopyFile(fromPath.c_str(), toPath.c_str(), (failIfExists ? TRUE : FALSE))) {
+ if (!CopyFile(fromPath.c_str(), toPath.c_str(),
+ (failIfExists ? TRUE : FALSE))) {
JP_THROW(SysError(tstrings::any()
<< "CopyFile(" << fromPath << ", " << toPath << ", "
<< failIfExists << ") failed", CopyFile));
}
LOG_TRACE(tstrings::any() << "Copied [" << fromPath << "] file to ["
@@ -256,12 +263,11 @@
DWORD flags) {
const bool isDir = isDirectory(fromPath);
if (!MoveFileEx(fromPath.c_str(), toPath.empty() ? NULL : toPath.c_str(),
flags)) {
JP_THROW(SysError(tstrings::any() << "MoveFileEx(" << fromPath
- << ", " << toPath << ", " << flags << ") failed",
- MoveFileEx));
+ << ", " << toPath << ", " << flags << ") failed", MoveFileEx));
}
const bool onReboot = 0 != (flags & MOVEFILE_DELAY_UNTIL_REBOOT);
const LPCTSTR label = isDir ? _T("folder") : _T("file");
@@ -289,12 +295,12 @@
}
} // namespace
-void moveFile(const tstring& fromPath, const tstring& toPath, bool failIfExists)
-{
+void moveFile(const tstring& fromPath, const tstring& toPath,
+ bool failIfExists) {
createDirectory(dirname(toPath));
DWORD flags = MOVEFILE_COPY_ALLOWED;
if (!failIfExists) {
flags |= MOVEFILE_REPLACE_EXISTING;
@@ -524,14 +530,17 @@
{
const tstring searchString = combinePath(dirPath, _T("*"));
WIN32_FIND_DATA findData;
UniqueFindFileHandle h(FindFirstFile(searchString.c_str(), &findData));
if (h.get() == INVALID_HANDLE_VALUE) {
- // GetLastError() == ERROR_FILE_NOT_FOUND is OK - no files in the directory
- // ERROR_PATH_NOT_FOUND is returned if the parent directory does not exist
+ // GetLastError() == ERROR_FILE_NOT_FOUND is OK
+ // - no files in the directory
+ // ERROR_PATH_NOT_FOUND is returned
+ // if the parent directory does not exist
if (GetLastError() != ERROR_FILE_NOT_FOUND) {
- JP_THROW(SysError(tstrings::any() << "FindFirstFile(" << dirPath << ") failed", FindFirstFile));
+ JP_THROW(SysError(tstrings::any() << "FindFirstFile("
+ << dirPath << ") failed", FindFirstFile));
}
return;
}
do {
@@ -548,11 +557,12 @@
}
} while (FindNextFile(h.get(), &findData));
// expect GetLastError() == ERROR_NO_MORE_FILES
if (GetLastError() != ERROR_NO_MORE_FILES) {
- JP_THROW(SysError(tstrings::any() << "FindNextFile(" << dirPath << ") failed", FindNextFile));
+ JP_THROW(SysError(tstrings::any() << "FindNextFile("
+ << dirPath << ") failed", FindNextFile));
}
}
tstring replaceSuffix(const tstring& path, const tstring& newSuffix) {
@@ -654,24 +664,21 @@
return *this;
}
Deleter& Deleter::appendEmptyDirectory(const tstring& path) {
- paths.push_back(std::make_pair(path,
- DeleterFunctor::EmptyDirectory));
+ paths.push_back(std::make_pair(path, DeleterFunctor::EmptyDirectory));
return *this;
}
Deleter& Deleter::appendAllFilesInDirectory(const tstring& path) {
- paths.push_back(std::make_pair(path,
- DeleterFunctor::FilesInDirectory));
+ paths.push_back(std::make_pair(path, DeleterFunctor::FilesInDirectory));
return *this;
}
Deleter& Deleter::appendRecursiveDirectory(const tstring& path) {
- paths.push_back(std::make_pair(path,
- DeleterFunctor::RecursiveDirectory));
+ paths.push_back(std::make_pair(path, DeleterFunctor::RecursiveDirectory));
return *this;
}
FileWriter::FileWriter(const tstring& path): dstPath(path) {
< prev index next >