// Build in an x64 Native Tools Command Prompt for Visual Studio: // cl /nologo /std:c++17 /EHsc /W4 repro.cpp /Fe:repro.exe // Run: repro.exe // The directory must not exist. Only synthetic files created here are cleaned up. #define WIN32_LEAN_AND_MEAN #define NOMINMAX #include #include #include #include #include #include #include #include #include #include #include #include #include #pragma comment(lib, "CldApi.lib") namespace fs = std::filesystem; using Bytes = std::vector; bool cleanupOk = true; void check(HRESULT hr, const char* operation) { if (FAILED(hr)) { std::cerr << operation << " failed: 0x" << std::hex << static_cast(hr) << std::dec << '\n'; throw std::runtime_error(operation); } } HRESULT inspect(const fs::path& path, const Bytes& expected) { HANDLE file = CreateFileW(path.c_str(), 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); if (file == INVALID_HANDLE_VALUE) return HRESULT_FROM_WIN32(GetLastError()); alignas(CF_PLACEHOLDER_BASIC_INFO) std::array buffer{}; DWORD returned = 0; HRESULT hr = CfGetPlaceholderInfo(file, CF_PLACEHOLDER_INFO_BASIC, buffer.data(), static_cast(buffer.size()), &returned); CloseHandle(file); if (FAILED(hr)) return hr; const auto* info = reinterpret_cast(buffer.data()); constexpr auto offset = offsetof(CF_PLACEHOLDER_BASIC_INFO, FileIdentity); if (returned < offset || info->FileIdentityLength != expected.size() || expected.size() > returned - offset || std::memcmp(info->FileIdentity, expected.data(), expected.size()) != 0) return E_UNEXPECTED; return S_OK; } CF_PLACEHOLDER_CREATE_INFO descriptor(const wchar_t* name, const Bytes& identity) { CF_PLACEHOLDER_CREATE_INFO item{}; item.RelativeFileName = name; item.FsMetadata.BasicInfo.FileAttributes = FILE_ATTRIBUTE_NORMAL; item.FsMetadata.FileSize.QuadPart = 4096; item.FileIdentity = identity.data(); item.FileIdentityLength = static_cast(identity.size()); item.Flags = CF_PLACEHOLDER_CREATE_FLAG_MARK_IN_SYNC; item.Result = S_FALSE; return item; } struct Root { fs::path path; CF_CONNECTION_KEY key{}; bool registered = false; bool connected = false; explicit Root(fs::path value) : path(std::move(value)) { if (!CreateDirectoryW(path.c_str(), nullptr)) check(HRESULT_FROM_WIN32(GetLastError()), "CreateDirectory"); } void connect() { CF_SYNC_REGISTRATION registration{}; registration.StructSize = sizeof(registration); registration.ProviderName = L"CFAPI batch metadata bug reproduction"; registration.ProviderVersion = L"1.0"; CF_SYNC_POLICIES policies{}; policies.StructSize = sizeof(policies); policies.Hydration.Primary = CF_HYDRATION_POLICY_PARTIAL; policies.Population.Primary = CF_POPULATION_POLICY_FULL; check(CfRegisterSyncRoot(path.c_str(), ®istration, &policies, CF_REGISTER_FLAG_DISABLE_ON_DEMAND_POPULATION_ON_ROOT | CF_REGISTER_FLAG_MARK_IN_SYNC_ON_ROOT), "CfRegisterSyncRoot"); registered = true; const CF_CALLBACK_REGISTRATION callbacks[] = { CF_CALLBACK_REGISTRATION_END }; check(CfConnectSyncRoot(path.c_str(), callbacks, nullptr, CF_CONNECT_FLAG_NONE, &key), "CfConnectSyncRoot"); connected = true; } ~Root() { // Rebuild only these two disposable FILES if the experiment corrupted them. // This is cleanup, not a recovery procedure for user files or directories. const Bytes cleanupIdentity = {'c','l','e','a','n','u','p'}; for (auto name : {L"a.bin", L"b.bin"}) { const fs::path file = path / name; HANDLE h = CreateFileW(file.c_str(), 0, 7, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr); DWORD error = h == INVALID_HANDLE_VALUE ? GetLastError() : ERROR_SUCCESS; if (h != INVALID_HANDLE_VALUE) CloseHandle(h); if (error == ERROR_CLOUD_FILE_METADATA_CORRUPT && registered) { auto item = descriptor(name, cleanupIdentity); item.FsMetadata.FileSize.QuadPart = 0; item.Flags |= CF_PLACEHOLDER_CREATE_FLAG_SUPERSEDE; HRESULT hr = CfCreatePlaceholders(path.c_str(), &item, 1, CF_CREATE_FLAG_NONE, nullptr); if (FAILED(hr) || FAILED(item.Result)) cleanupOk = false; } } if (connected && FAILED(CfDisconnectSyncRoot(key))) cleanupOk = false; if (registered && FAILED(CfUnregisterSyncRoot(path.c_str()))) cleanupOk = false; for (auto name : {L"a.bin", L"b.bin"}) { if (!DeleteFileW((path / name).c_str()) && GetLastError() != ERROR_FILE_NOT_FOUND) cleanupOk = false; } if (!RemoveDirectoryW(path.c_str())) cleanupOk = false; } }; int runCase(const fs::path& parent, const wchar_t* label, bool single, bool reverse) { Root root(parent / label); root.connect(); Bytes repeated(512, 1), random(512); std::uint32_t state = 0x12345678; for (auto& byte : random) { state ^= state << 13; state ^= state >> 17; state ^= state << 5; byte = static_cast(state); } // Both blobs and names remain alive through every synchronous call. std::array entries = { descriptor(L"a.bin", repeated), descriptor(L"b.bin", random) }; if (reverse) std::swap(entries[0], entries[1]); std::wcout << L"CASE " << label << L"\n"; for (DWORD index = 0; index < entries.size(); index += single ? 1 : 2) { DWORD processed = 0; HRESULT hr = CfCreatePlaceholders(root.path.c_str(), entries.data() + index, single ? 1 : 2, CF_CREATE_FLAG_NONE, &processed); std::wcout << L" CfCreatePlaceholders hr=0x" << std::hex << std::setw(8) << std::setfill(L'0') << static_cast(hr) << std::dec << L" processed=" << processed << L"\n"; check(hr, "CfCreatePlaceholders"); } for (const auto& entry : entries) { std::wcout << L" " << entry.RelativeFileName << L" Result=0x" << std::hex << std::setw(8) << static_cast(entry.Result) << std::dec << L"\n"; check(entry.Result, "Per-item creation result"); } int corrupt = 0; for (const auto& item : {std::make_pair(L"a.bin", &repeated), std::make_pair(L"b.bin", &random)}) { HRESULT hr = inspect(root.path / item.first, *item.second); std::wcout << L" " << item.first << L" metadata=0x" << std::hex << std::setw(8) << static_cast(hr) << std::dec << L"\n"; if (hr == HRESULT_FROM_WIN32(ERROR_CLOUD_FILE_METADATA_CORRUPT)) ++corrupt; else check(hr, "Metadata inspection / identity comparison"); } return corrupt; } int wmain(int argc, wchar_t** argv) { if (argc != 2) { std::wcerr << L"Usage: repro.exe \nThe directory must not already exist.\n"; return 2; } fs::path parent; bool owned = false; int result = 1; try { parent = fs::weakly_canonical(fs::absolute(argv[1])); if (!CreateDirectoryW(parent.c_str(), nullptr)) check(HRESULT_FROM_WIN32(GetLastError()), "Create new test parent (must not exist)"); owned = true; CF_PLATFORM_INFO platform{}; check(CfGetPlatformInfo(&platform), "CfGetPlatformInfo"); std::wcout << L"CFAPI " << platform.BuildNumber << L'.' << platform.RevisionNumber << L"\n"; int batch = runCase(parent, L"batch", false, false); int single = runCase(parent, L"single", true, false); int reverse = runCase(parent, L"reverse", false, true); bool reproduced = batch == 1 && single == 0 && reverse == 0; std::wcout << L"REPRODUCED=" << (reproduced ? L"yes" : L"no") << L"\n"; result = 0; // 0 means all experiments completed, not that Windows passed. } catch (const std::exception& error) { std::cerr << "Experiment stopped: " << error.what() << '\n'; } if (owned && !RemoveDirectoryW(parent.c_str())) cleanupOk = false; std::wcout << L"CLEANUP=" << (cleanupOk ? L"OK" : L"INCOMPLETE (inspect the test directory)") << L"\n"; return cleanupOk ? result : 1; }