1c2c66affSColin Finck /* Copyright (c) Mark Harmstone 2016-17
2c2c66affSColin Finck  *
3c2c66affSColin Finck  * This file is part of WinBtrfs.
4c2c66affSColin Finck  *
5c2c66affSColin Finck  * WinBtrfs is free software: you can redistribute it and/or modify
6c2c66affSColin Finck  * it under the terms of the GNU Lesser General Public Licence as published by
7c2c66affSColin Finck  * the Free Software Foundation, either version 3 of the Licence, or
8c2c66affSColin Finck  * (at your option) any later version.
9c2c66affSColin Finck  *
10c2c66affSColin Finck  * WinBtrfs is distributed in the hope that it will be useful,
11c2c66affSColin Finck  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12c2c66affSColin Finck  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13c2c66affSColin Finck  * GNU Lesser General Public Licence for more details.
14c2c66affSColin Finck  *
15c2c66affSColin Finck  * You should have received a copy of the GNU Lesser General Public Licence
16c2c66affSColin Finck  * along with WinBtrfs.  If not, see <http://www.gnu.org/licenses/>. */
17c2c66affSColin Finck 
18c2c66affSColin Finck #include "shellext.h"
19c2c66affSColin Finck #ifndef __REACTOS__
20c2c66affSColin Finck #include <windows.h>
21c2c66affSColin Finck #include <winternl.h>
22c2c66affSColin Finck #else
23c2c66affSColin Finck #define WIN32_NO_STATUS
24c2c66affSColin Finck #include <windef.h>
25c2c66affSColin Finck #include <winbase.h>
26c2c66affSColin Finck #include <ndk/iofuncs.h>
27c2c66affSColin Finck #endif
28c2c66affSColin Finck #include "iconoverlay.h"
29c2c66affSColin Finck #ifndef __REACTOS__
30c2c66affSColin Finck #include "../btrfsioctl.h"
31c2c66affSColin Finck #else
32c2c66affSColin Finck #include "btrfsioctl.h"
33c2c66affSColin Finck #endif
34c2c66affSColin Finck 
QueryInterface(REFIID riid,void ** ppObj)35c2c66affSColin Finck HRESULT __stdcall BtrfsIconOverlay::QueryInterface(REFIID riid, void **ppObj) {
36c2c66affSColin Finck     if (riid == IID_IUnknown || riid == IID_IShellIconOverlayIdentifier) {
37c2c66affSColin Finck         *ppObj = static_cast<IShellIconOverlayIdentifier*>(this);
38c2c66affSColin Finck         AddRef();
39c2c66affSColin Finck         return S_OK;
40c2c66affSColin Finck     }
41c2c66affSColin Finck 
427b718d36SPierre Schweitzer     *ppObj = nullptr;
43c2c66affSColin Finck     return E_NOINTERFACE;
44c2c66affSColin Finck }
45c2c66affSColin Finck 
GetOverlayInfo(PWSTR pwszIconFile,int cchMax,int * pIndex,DWORD * pdwFlags)46*3cfd8ab7SVincent Franchomme HRESULT __stdcall BtrfsIconOverlay::GetOverlayInfo(PWSTR pwszIconFile, int cchMax, int* pIndex, DWORD* pdwFlags) noexcept {
477b718d36SPierre Schweitzer     if (GetModuleFileNameW(module, pwszIconFile, cchMax) == 0)
487b718d36SPierre Schweitzer         return E_FAIL;
49c2c66affSColin Finck 
507b718d36SPierre Schweitzer     if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
517b718d36SPierre Schweitzer         return E_FAIL;
52c2c66affSColin Finck 
53c2c66affSColin Finck     if (!pIndex)
54c2c66affSColin Finck         return E_INVALIDARG;
55c2c66affSColin Finck 
56c2c66affSColin Finck     if (!pdwFlags)
57c2c66affSColin Finck         return E_INVALIDARG;
58c2c66affSColin Finck 
59c2c66affSColin Finck     *pIndex = 0;
60c2c66affSColin Finck     *pdwFlags = ISIOI_ICONFILE | ISIOI_ICONINDEX;
61c2c66affSColin Finck 
62c2c66affSColin Finck     return S_OK;
63c2c66affSColin Finck }
64c2c66affSColin Finck 
GetPriority(int * pPriority)65*3cfd8ab7SVincent Franchomme HRESULT __stdcall BtrfsIconOverlay::GetPriority(int *pPriority) noexcept {
66c2c66affSColin Finck     if (!pPriority)
67c2c66affSColin Finck         return E_INVALIDARG;
68c2c66affSColin Finck 
69c2c66affSColin Finck     *pPriority = 0;
70c2c66affSColin Finck 
71c2c66affSColin Finck     return S_OK;
72c2c66affSColin Finck }
73c2c66affSColin Finck 
IsMemberOf(PCWSTR pwszPath,DWORD dwAttrib)74*3cfd8ab7SVincent Franchomme HRESULT __stdcall BtrfsIconOverlay::IsMemberOf(PCWSTR pwszPath, DWORD dwAttrib) noexcept {
757b718d36SPierre Schweitzer     win_handle h;
76c2c66affSColin Finck     NTSTATUS Status;
77c2c66affSColin Finck     IO_STATUS_BLOCK iosb;
78c2c66affSColin Finck     btrfs_get_file_ids bgfi;
79c2c66affSColin Finck 
807b718d36SPierre Schweitzer     h = CreateFileW(pwszPath, 0, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OPEN_REPARSE_POINT, nullptr);
81c2c66affSColin Finck 
82c2c66affSColin Finck     if (h == INVALID_HANDLE_VALUE)
83c2c66affSColin Finck         return S_FALSE;
84c2c66affSColin Finck 
857b718d36SPierre Schweitzer     Status = NtFsControlFile(h, nullptr, nullptr, nullptr, &iosb, FSCTL_BTRFS_GET_FILE_IDS, nullptr, 0, &bgfi, sizeof(btrfs_get_file_ids));
86c2c66affSColin Finck 
877b718d36SPierre Schweitzer     if (!NT_SUCCESS(Status))
88c2c66affSColin Finck         return S_FALSE;
89c2c66affSColin Finck 
90c2c66affSColin Finck     return (bgfi.inode == 0x100 && !bgfi.top) ? S_OK : S_FALSE;
91c2c66affSColin Finck }
92