Skip to content

Commit

Permalink
Fix CloudMirror placeholders #164
Browse files Browse the repository at this point in the history
RelativeFileName must be a file name, not a relative path.

Other fixes:
* Use std::wstring::size() instead of calling wcslen.
* Use c_str() instead of data() if you want a null-terminated read-only C-style string.
* Fix typos in wprintf format strings.
  • Loading branch information
Windows classic samples committed Dec 24, 2020
1 parent 340d6c8 commit fe45388
Showing 1 changed file with 13 additions and 14 deletions.
27 changes: 13 additions & 14 deletions Samples/CloudMirror/CloudMirror/Placeholders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,12 @@ void Placeholders::Create(
sourceSubDir.push_back(L'\\');
}

std::wstring fileName(sourcePath);
fileName.append(sourceSubDir);
fileName.append(L"*");
std::wstring filePattern = sourcePath + sourceSubDir + L"*";
std::wstring fullDestPath = std::wstring(destPath) + L'\\' + sourceSubDir;

hFileHandle =
FindFirstFileEx(
fileName.data(),
filePattern.c_str(),
FindExInfoStandard,
&findData,
FindExSearchNameMatch,
Expand All @@ -73,10 +72,10 @@ void Placeholders::Create(
std::wstring relativeName(sourceSubDir);
relativeName.append(findData.cFileName);

cloudEntry.FileIdentity = relativeName.data();
cloudEntry.FileIdentityLength = (USHORT)(wcslen(relativeName.data()) + 1) * sizeof(WCHAR);
cloudEntry.FileIdentity = relativeName.c_str();
cloudEntry.FileIdentityLength = (DWORD)((relativeName.size() + 1) * sizeof(WCHAR));

cloudEntry.RelativeFileName = relativeName.data();
cloudEntry.RelativeFileName = findData.cFileName;
cloudEntry.Flags = CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC;
cloudEntry.FsMetadata.FileSize.QuadPart = ((ULONGLONG)findData.nFileSizeHigh << 32) + findData.nFileSizeLow;
cloudEntry.FsMetadata.BasicInfo.FileAttributes = findData.dwFileAttributes;
Expand All @@ -93,14 +92,14 @@ void Placeholders::Create(

try
{
wprintf(L"Creating placeholder for %sx\n", relativeName.data());
winrt::check_hresult(CfCreatePlaceholders(destPath, &cloudEntry, 1, CF_CREATE_FLAG_NONE, NULL));
wprintf(L"Creating placeholder for %s\n", relativeName.c_str());
winrt::check_hresult(CfCreatePlaceholders(fullDestPath.c_str(), &cloudEntry, 1, CF_CREATE_FLAG_NONE, NULL));
}
catch (...)
{
// winrt::to_hresult() will eat the exception if it is a result of winrt::check_hresult,
// otherwise the exception will get rethrown and this method will crash out as it should
wprintf(L"Failed to create placeholder for %s with %08x\n", relativeName.data(), static_cast<HRESULT>(winrt::to_hresult()));
wprintf(L"Failed to create placeholder for %s with %08x\n", relativeName.c_str(), static_cast<HRESULT>(winrt::to_hresult()));
// Eating it here lets other files still get a chance. Not worth crashing the sample, but
// certainly noteworthy for production code
continue;
Expand All @@ -114,19 +113,19 @@ void Placeholders::Create(
// This icon is just for the sample. You should provide your own branded icon here
prop.IconResource(L"shell32.dll,-44");

wprintf(L"Applying custom state for %sx\n", relativeName.data());
Utilities::ApplyCustomStateToPlaceholderFile(destPath, relativeName.data(), prop);
wprintf(L"Applying custom state for %s\n", relativeName.c_str());
Utilities::ApplyCustomStateToPlaceholderFile(destPath, relativeName.c_str(), prop);

if ((findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0)
{
Create(sourcePath.c_str(), relativeName.data(), destPath);
Create(sourcePath.c_str(), relativeName.c_str(), destPath);
}
}
catch (...)
{
// winrt::to_hresult() will eat the exception if it is a result of winrt::check_hresult,
// otherwise the exception will get rethrown and this method will crash out as it should
wprintf(L"Failed to set custom state on %s with %08x\n", relativeName.data(), static_cast<HRESULT>(winrt::to_hresult()));
wprintf(L"Failed to set custom state on %s with %08x\n", relativeName.c_str(), static_cast<HRESULT>(winrt::to_hresult()));
// Eating it here lets other files still get a chance. Not worth crashing the sample, but
// certainly noteworthy for production code
}
Expand Down

0 comments on commit fe45388

Please sign in to comment.