0

I am trying to find ways to manage system storage efficiently.

One thing I've noticed while using MS OneDrive is that whenever I click "Free up space" on its context menu, it converts the "Storage on disk" to 0 bytes while its File size remains the same. I want to mimic what is happening over there.

Then I researched to see if I can partially modify its attribute (allocation size) through SetFileInformationByHandle using FileAllocationInfo as its parameter. It succeeded but both of its file size and allocation size became 0.

Test code as follows:

    WCHAR wcsDebug[2048];

    // testfile is 1224 bytes
    HANDLE hFile = CreateFile(_TEXT("C:\\test\\testfile.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile != INVALID_HANDLE_VALUE)
    {
        FILE_ALLOCATION_INFO allocInfo;
        ZeroMemory(&allocInfo, sizeof(FILE_ALLOCATION_INFO));
        
        BOOL fResult = SetFileInformationByHandle(hFile,
            FileAllocationInfo,
            &allocInfo,
            sizeof(FILE_ALLOCATION_INFO));

        // after the execution, the size becomes 0
        if (fResult)
        {
            swprintf_s(wcsDebug, _countof(wcsDebug), L"SetFileInfomationByHandle() Success.");
            OutputDebugString(wcsDebug);
        }
        else
        {
            swprintf_s(wcsDebug, _countof(wcsDebug), L"SetFileInfomationByHandle() Failed. Err(%08X)", GetLastError());
            OutputDebugString(wcsDebug);
        }
    }

Any more ideas on developing the thoughts?

vaska11
  • 380
  • 2
  • 13
  • If i'm not mistaken, OneDrive and similar services are utilizing [ProjFS](https://learn.microsoft.com/en-us/windows/win32/projfs/projected-file-system). So it just reports arbitrary file parameters. – user7860670 Mar 14 '22 at 06:56
  • @user7860670 are you saying those properties do not represent its real properties? – vaska11 Mar 14 '22 at 07:10
  • I'm not sure, but i strongly suspect so. – user7860670 Mar 14 '22 at 07:22
  • 3
    This question seems to be the result of a fundamental misunderstanding on how things work. Changing the *"Storage on disk"* property **changes the actual file**. You've essentially purged the entire file contents. If you want to allow the local filesystem to act as a cache for cloud-stored files, use the [Cloud Filter API](https://learn.microsoft.com/en-us/windows/win32/cfapi/cloud-filter-reference) (which is how OneDrive is implemented). Likely, this is a duplicate of [this Q&A](https://stackoverflow.com/q/68137435/1889329). – IInspectable Mar 14 '22 at 07:28
  • lets `GetFileInformationByHandle` before and after your `SetFileInformationByHandle` and print some information to compare them. – long.kl Mar 14 '22 at 07:35
  • *"I want to mimic what is happening over there."* -- so you want to take a local file -- a file that exists only on your local drive -- and remove it from your local drive without removing it from cloud storage? Ermm... maybe you should write out real technical specs instead of using vague terms like "mimic". – JaMiT Mar 14 '22 at 07:46
  • @IInspectable You're right. I think I got the whole concept wrong. I will look into the APIs you've linked. – vaska11 Mar 14 '22 at 07:50
  • To "free up space" all you have to do is clear the `FILE_ATTRIBUTE_PINNED` attribute and set the `FILE_ATTRIBUTE_UNPINNED` attribute. – Jonathan Potter Mar 14 '22 at 10:29

0 Answers0