2 * UNIXFS - Shell namespace extension for the unix filesystem
4 * Copyright (C) 2005 Michael Jung
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
49 #include "wine/debug.h"
51 #include "shell32_main.h"
52 #include "shellfolder.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(shell
);
59 const GUID CLSID_UnixFolder
= {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
60 const GUID CLSID_UnixDosFolder
= {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
62 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
63 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
65 /* FileStruct reserves one byte for szNames, thus we don't have to
66 * alloc a byte for the terminating '\0' of 'name'. Two of the
67 * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
68 * type field. One is for FileStruct's szNames field, to terminate
69 * the alternate DOS name, which we don't use here.
71 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
72 (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char))
73 #define NAME_LEN_FROM_LPSHITEMID(s) \
74 (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char))
76 #define PATHMODE_UNIX 0
77 #define PATHMODE_DOS 1
79 /* UnixFolder object layout and typedef.
81 typedef struct _UnixFolder
{
82 const IShellFolder2Vtbl
*lpIShellFolder2Vtbl
;
83 const IPersistFolder3Vtbl
*lpIPersistFolder3Vtbl
;
84 const ISFHelperVtbl
*lpISFHelperVtbl
;
87 LPITEMIDLIST m_pidlLocation
;
92 /******************************************************************************
93 * UNIXFS_is_rooted_at_desktop [Internal]
95 * Checks if the unixfs namespace extension is rooted at desktop level.
98 * TRUE, if unixfs is rooted at desktop level
101 BOOL
UNIXFS_is_rooted_at_desktop(void) {
103 WCHAR
*pwszCLSID_UnixDosFolder
, wszRootedAtDesktop
[69 + CHARS_IN_GUID
] = {
104 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
105 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
106 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
107 'N','a','m','e','S','p','a','c','e','\\',0 };
109 if (FAILED(StringFromCLSID(&CLSID_UnixDosFolder
, &pwszCLSID_UnixDosFolder
)))
112 lstrcatW(wszRootedAtDesktop
, pwszCLSID_UnixDosFolder
);
113 CoTaskMemFree(pwszCLSID_UnixDosFolder
);
115 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE
, wszRootedAtDesktop
, 0, KEY_READ
, &hKey
) != ERROR_SUCCESS
)
122 /******************************************************************************
123 * UNIXFS_is_pidl_of_type [INTERNAL]
125 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
128 * pIDL [I] The ITEMIDLIST to be checked.
129 * fFilter [I] Shell condition flags, which specify the filter.
132 * TRUE, if pIDL is accepted by fFilter
135 static inline BOOL
UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL
, SHCONTF fFilter
) {
136 LPPIDLDATA pIDLData
= _ILGetDataPointer(pIDL
);
137 if (!(fFilter
& SHCONTF_INCLUDEHIDDEN
) && pIDLData
&&
138 (pIDLData
->u
.file
.uFileAttribs
& FILE_ATTRIBUTE_HIDDEN
))
142 if (_ILIsFolder(pIDL
) && (fFilter
& SHCONTF_FOLDERS
)) return TRUE
;
143 if (_ILIsValue(pIDL
) && (fFilter
& SHCONTF_NONFOLDERS
)) return TRUE
;
147 /******************************************************************************
148 * UNIXFS_is_dos_device [Internal]
150 * Determines if a unix directory corresponds to any dos device.
153 * statPath [I] The stat struct of the directory, as returned by stat(2).
156 * TRUE, if statPath corresponds to any dos drive letter
159 static BOOL
UNIXFS_is_dos_device(const struct stat
*statPath
) {
160 struct stat statDrive
;
163 WCHAR wszDosDevice
[4] = { 'A', ':', '\\', 0 };
165 for (dwDriveMap
= GetLogicalDrives(); dwDriveMap
; dwDriveMap
>>= 1, wszDosDevice
[0]++) {
166 if (!(dwDriveMap
& 0x1)) continue;
167 pszDrivePath
= wine_get_unix_file_name(wszDosDevice
);
168 if (pszDrivePath
&& !stat(pszDrivePath
, &statDrive
)) {
169 HeapFree(GetProcessHeap(), 0, pszDrivePath
);
170 if ((statPath
->st_dev
== statDrive
.st_dev
) && (statPath
->st_ino
== statDrive
.st_ino
))
177 /******************************************************************************
178 * UNIXFS_get_unix_path [Internal]
180 * Convert an absolute dos path to an absolute canonicalized unix path.
181 * Evaluate "/.", "/.." and symbolic links.
184 * pszDosPath [I] An absolute dos path
185 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
189 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
191 static BOOL
UNIXFS_get_unix_path(LPCWSTR pszDosPath
, char *pszCanonicalPath
)
193 char *pPathTail
, *pElement
, *pCanonicalTail
, szPath
[FILENAME_MAX
], *pszUnixPath
;
194 struct stat fileStat
;
196 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath
), pszCanonicalPath
);
198 if (!pszDosPath
|| pszDosPath
[1] != ':')
201 pszUnixPath
= wine_get_unix_file_name(pszDosPath
);
202 if (!pszUnixPath
) return FALSE
;
203 strcpy(szPath
, pszUnixPath
);
204 HeapFree(GetProcessHeap(), 0, pszUnixPath
);
206 /* pCanonicalTail always points to the end of the canonical path constructed
207 * thus far. pPathTail points to the still to be processed part of the input
208 * path. pElement points to the path element currently investigated.
210 *pszCanonicalPath
= '\0';
211 pCanonicalTail
= pszCanonicalPath
;
218 pElement
= pPathTail
;
219 pPathTail
= strchr(pPathTail
+1, '/');
220 if (!pPathTail
) /* Last path element may not be terminated by '/'. */
221 pPathTail
= pElement
+ strlen(pElement
);
222 /* Temporarily terminate the current path element. Will be restored later. */
226 /* Skip "/." path elements */
227 if (!strcmp("/.", pElement
)) {
232 /* Remove last element in canonical path for "/.." elements, then skip. */
233 if (!strcmp("/..", pElement
)) {
234 char *pTemp
= strrchr(pszCanonicalPath
, '/');
236 pCanonicalTail
= pTemp
;
237 *pCanonicalTail
= '\0';
242 /* lstat returns zero on success. */
243 if (lstat(szPath
, &fileStat
))
246 if (S_ISLNK(fileStat
.st_mode
)) {
247 char szSymlink
[FILENAME_MAX
];
248 int cLinkLen
, cTailLen
;
250 /* Avoid infinite loop for recursive links. */
254 cLinkLen
= readlink(szPath
, szSymlink
, FILENAME_MAX
);
259 cTailLen
= strlen(pPathTail
);
261 if (szSymlink
[0] == '/') {
262 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
263 if (cLinkLen
+ cTailLen
+ 1 > FILENAME_MAX
)
266 /* Avoid double slashes. */
267 if (szSymlink
[cLinkLen
-1] == '/' && pPathTail
[0] == '/') {
268 szSymlink
[cLinkLen
-1] = '\0';
272 memcpy(szSymlink
+ cLinkLen
, pPathTail
, cTailLen
+ 1);
273 memcpy(szPath
, szSymlink
, cLinkLen
+ cTailLen
+ 1);
274 *pszCanonicalPath
= '\0';
275 pCanonicalTail
= pszCanonicalPath
;
278 /* Relative link. Expand into szPath and continue. */
279 char szTemp
[FILENAME_MAX
];
280 int cTailLen
= strlen(pPathTail
);
282 if (pElement
- szPath
+ 1 + cLinkLen
+ cTailLen
+ 1 > FILENAME_MAX
)
285 memcpy(szTemp
, pPathTail
, cTailLen
+ 1);
286 memcpy(pElement
+ 1, szSymlink
, cLinkLen
);
287 memcpy(pElement
+ 1 + cLinkLen
, szTemp
, cTailLen
+ 1);
288 pPathTail
= pElement
;
291 /* Regular directory or file. Copy to canonical path */
292 if (pCanonicalTail
- pszCanonicalPath
+ pPathTail
- pElement
+ 1 > FILENAME_MAX
)
295 memcpy(pCanonicalTail
, pElement
, pPathTail
- pElement
+ 1);
296 pCanonicalTail
+= pPathTail
- pElement
;
299 } while (pPathTail
[0] == '/');
301 TRACE("--> %s\n", debugstr_a(pszCanonicalPath
));
306 /******************************************************************************
307 * UNIXFS_build_shitemid [Internal]
309 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
313 * pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
314 * pIDL [O] SHITEMID will be constructed here.
317 * Success: A pointer to the terminating '\0' character of path.
321 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
322 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
325 static char* UNIXFS_build_shitemid(char *pszUnixPath
, void *pIDL
) {
329 struct stat fileStat
;
333 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath
), pIDL
);
335 /* Compute the SHITEMID's length and wipe it. */
336 pszComponent
= strrchr(pszUnixPath
, '/') + 1;
337 cComponentLen
= strlen(pszComponent
);
338 memset(pIDL
, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen
));
339 ((LPSHITEMID
)pIDL
)->cb
= SHITEMID_LEN_FROM_NAME_LEN(cComponentLen
) ;
341 /* We are only interested in regular files and directories. */
342 if (stat(pszUnixPath
, &fileStat
)) return NULL
;
343 if (!S_ISDIR(fileStat
.st_mode
) && !S_ISREG(fileStat
.st_mode
)) return NULL
;
345 /* Set shell32's standard SHITEMID data fields. */
346 pIDLData
= _ILGetDataPointer((LPCITEMIDLIST
)pIDL
);
347 pIDLData
->type
= S_ISDIR(fileStat
.st_mode
) ? PT_FOLDER
: PT_VALUE
;
348 pIDLData
->u
.file
.dwFileSize
= (DWORD
)fileStat
.st_size
;
349 RtlSecondsSince1970ToTime( fileStat
.st_mtime
, &time
);
350 fileTime
.dwLowDateTime
= time
.u
.LowPart
;
351 fileTime
.dwHighDateTime
= time
.u
.HighPart
;
352 FileTimeToDosDateTime(&fileTime
, &pIDLData
->u
.file
.uFileDate
, &pIDLData
->u
.file
.uFileTime
);
353 pIDLData
->u
.file
.uFileAttribs
= 0;
354 if (S_ISDIR(fileStat
.st_mode
)) pIDLData
->u
.file
.uFileAttribs
|= FILE_ATTRIBUTE_DIRECTORY
;
355 if (pszComponent
[0] == '.') pIDLData
->u
.file
.uFileAttribs
|= FILE_ATTRIBUTE_HIDDEN
;
356 memcpy(pIDLData
->u
.file
.szNames
, pszComponent
, cComponentLen
);
358 return pszComponent
+ cComponentLen
;
361 /******************************************************************************
362 * UNIXFS_path_to_pidl [Internal]
365 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
366 * path [I] An absolute unix or dos path or a path relativ to pUnixFolder
367 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
371 * Failure: FALSE, invalid params or out of memory
374 * pUnixFolder also carries the information if the path is expected to be unix or dos.
376 static BOOL
UNIXFS_path_to_pidl(UnixFolder
*pUnixFolder
, const WCHAR
*path
, LPITEMIDLIST
*ppidl
) {
378 int cSubDirs
, cPidlLen
, cPathLen
;
379 char *pSlash
, szCompletePath
[FILENAME_MAX
], *pNextPathElement
;
381 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder
, debugstr_w(path
), ppidl
);
386 /* Build an absolute path and let pNextPathElement point to the interesting
387 * relative sub-path. We need the absolute path to call 'stat', but the pidl
388 * will only contain the relative part.
390 if ((pUnixFolder
->m_dwPathMode
== PATHMODE_DOS
) && (path
[1] == ':'))
392 /* Absolute dos path. Convert to unix */
393 if (!UNIXFS_get_unix_path(path
, szCompletePath
))
395 pNextPathElement
= szCompletePath
;
397 else if ((pUnixFolder
->m_dwPathMode
== PATHMODE_UNIX
) && (path
[0] == '/'))
399 /* Absolute unix path. Just convert to ANSI. */
400 WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, szCompletePath
, FILENAME_MAX
, NULL
, NULL
);
401 pNextPathElement
= szCompletePath
;
405 /* Relative dos or unix path. Concat with this folder's path */
406 int cBasePathLen
= strlen(pUnixFolder
->m_pszPath
);
407 memcpy(szCompletePath
, pUnixFolder
->m_pszPath
, cBasePathLen
);
408 WideCharToMultiByte(CP_UNIXCP
, 0, path
, -1, szCompletePath
+ cBasePathLen
,
409 FILENAME_MAX
- cBasePathLen
, NULL
, NULL
);
410 pNextPathElement
= szCompletePath
+ cBasePathLen
- 1;
412 /* If in dos mode, replace '\' with '/' */
413 if (pUnixFolder
->m_dwPathMode
== PATHMODE_DOS
) {
414 char *pBackslash
= strchr(pNextPathElement
, '\\');
417 pBackslash
= strchr(pBackslash
, '\\');
422 /* Special case for the root folder. */
423 if (!strcmp(szCompletePath
, "/")) {
424 *ppidl
= pidl
= (LPITEMIDLIST
)SHAlloc(sizeof(USHORT
));
425 if (!pidl
) return FALSE
;
426 pidl
->mkid
.cb
= 0; /* Terminate the ITEMIDLIST */
430 /* Remove trailing slash, if present */
431 cPathLen
= strlen(szCompletePath
);
432 if (szCompletePath
[cPathLen
-1] == '/')
433 szCompletePath
[cPathLen
-1] = '\0';
435 if ((szCompletePath
[0] != '/') || (pNextPathElement
[0] != '/')) {
436 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath
, pNextPathElement
);
440 /* At this point, we have an absolute unix path in szCompletePath
441 * and the relative portion of it in pNextPathElement. Both starting with '/'
442 * and _not_ terminated by a '/'. */
443 TRACE("complete path: %s, relative path: %s\n", szCompletePath
, pNextPathElement
);
445 /* Count the number of sub-directories in the path */
447 pSlash
= pNextPathElement
;
450 pSlash
= strchr(pSlash
+1, '/');
453 /* Allocate enough memory to hold the path. The -cSubDirs is for the '/'
454 * characters, which are not stored in the ITEMIDLIST. */
455 cPidlLen
= strlen(pNextPathElement
) - cSubDirs
+ cSubDirs
* SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT
);
456 *ppidl
= pidl
= (LPITEMIDLIST
)SHAlloc(cPidlLen
);
457 if (!pidl
) return FALSE
;
459 /* Concatenate the SHITEMIDs of the sub-directories. */
460 while (*pNextPathElement
) {
461 pSlash
= strchr(pNextPathElement
+1, '/');
462 if (pSlash
) *pSlash
= '\0';
463 pNextPathElement
= UNIXFS_build_shitemid(szCompletePath
, pidl
);
464 if (pSlash
) *pSlash
= '/';
466 if (!pNextPathElement
) {
470 pidl
= ILGetNext(pidl
);
472 pidl
->mkid
.cb
= 0; /* Terminate the ITEMIDLIST */
474 if ((int)pidl
-(int)*ppidl
+sizeof(USHORT
) != cPidlLen
) /* We've corrupted the heap :( */
475 ERR("Computed length of pidl incorrect. Please report.\n");
480 /******************************************************************************
483 * Class whose heap based instances represent unix filesystem directories.
486 static void UnixFolder_Destroy(UnixFolder
*pUnixFolder
) {
487 TRACE("(pUnixFolder=%p)\n", pUnixFolder
);
489 SHFree(pUnixFolder
->m_pszPath
);
490 ILFree(pUnixFolder
->m_pidlLocation
);
494 static HRESULT WINAPI
UnixFolder_IShellFolder2_QueryInterface(IShellFolder2
*iface
, REFIID riid
,
497 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
499 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface
, riid
, ppv
);
501 if (!ppv
) return E_INVALIDARG
;
503 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IShellFolder
, riid
) ||
504 IsEqualIID(&IID_IShellFolder2
, riid
))
506 *ppv
= &This
->lpIShellFolder2Vtbl
;
507 } else if (IsEqualIID(&IID_IPersistFolder3
, riid
) || IsEqualIID(&IID_IPersistFolder2
, riid
) ||
508 IsEqualIID(&IID_IPersistFolder
, riid
) || IsEqualIID(&IID_IPersist
, riid
))
510 *ppv
= &This
->lpIPersistFolder3Vtbl
;
511 } else if (IsEqualIID(&IID_ISFHelper
, riid
)) {
512 *ppv
= &This
->lpISFHelperVtbl
;
515 return E_NOINTERFACE
;
518 IUnknown_AddRef((IUnknown
*)*ppv
);
522 static ULONG WINAPI
UnixFolder_IShellFolder2_AddRef(IShellFolder2
*iface
) {
523 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
525 TRACE("(iface=%p)\n", iface
);
527 return InterlockedIncrement(&This
->m_cRef
);
530 static ULONG WINAPI
UnixFolder_IShellFolder2_Release(IShellFolder2
*iface
) {
531 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
534 TRACE("(iface=%p)\n", iface
);
536 cRef
= InterlockedDecrement(&This
->m_cRef
);
539 UnixFolder_Destroy(This
);
544 static HRESULT WINAPI
UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2
* iface
, HWND hwndOwner
,
545 LPBC pbcReserved
, LPOLESTR lpszDisplayName
, ULONG
* pchEaten
, LPITEMIDLIST
* ppidl
,
546 ULONG
* pdwAttributes
)
548 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
551 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
552 "pdwAttributes=%p) stub\n", iface
, hwndOwner
, pbcReserved
, debugstr_w(lpszDisplayName
),
553 pchEaten
, ppidl
, pdwAttributes
);
555 result
= UNIXFS_path_to_pidl(This
, lpszDisplayName
, ppidl
);
556 if (result
&& pdwAttributes
&& *pdwAttributes
)
558 IShellFolder
*pParentSF
;
559 LPCITEMIDLIST pidlLast
;
562 hr
= SHBindToParent(*ppidl
, &IID_IShellFolder
, (LPVOID
*)&pParentSF
, &pidlLast
);
563 if (FAILED(hr
)) return E_FAIL
;
564 IShellFolder_GetAttributesOf(pParentSF
, 1, &pidlLast
, pdwAttributes
);
565 IShellFolder_Release(pParentSF
);
568 if (!result
) TRACE("FAILED!\n");
569 return result
? S_OK
: E_FAIL
;
572 static IUnknown
*UnixSubFolderIterator_Constructor(UnixFolder
*pUnixFolder
, SHCONTF fFilter
);
574 static HRESULT WINAPI
UnixFolder_IShellFolder2_EnumObjects(IShellFolder2
* iface
, HWND hwndOwner
,
575 SHCONTF grfFlags
, IEnumIDList
** ppEnumIDList
)
577 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
578 IUnknown
*newIterator
;
581 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n",
582 iface
, hwndOwner
, grfFlags
, ppEnumIDList
);
584 newIterator
= UnixSubFolderIterator_Constructor(This
, grfFlags
);
585 hr
= IUnknown_QueryInterface(newIterator
, &IID_IEnumIDList
, (void**)ppEnumIDList
);
586 IUnknown_Release(newIterator
);
591 static HRESULT WINAPI
UnixFolder_IShellFolder2_BindToObject(IShellFolder2
* iface
, LPCITEMIDLIST pidl
,
592 LPBC pbcReserved
, REFIID riid
, void** ppvOut
)
594 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
595 IPersistFolder3
*persistFolder
;
596 LPITEMIDLIST pidlSubFolder
;
599 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
600 iface
, pidl
, pbcReserved
, riid
, ppvOut
);
602 if (!pidl
|| !pidl
->mkid
.cb
)
605 if (This
->m_dwPathMode
== PATHMODE_DOS
)
606 hr
= UnixDosFolder_Constructor(NULL
, &IID_IPersistFolder3
, (void**)&persistFolder
);
608 hr
= UnixFolder_Constructor(NULL
, &IID_IPersistFolder3
, (void**)&persistFolder
);
610 if (!SUCCEEDED(hr
)) return hr
;
611 hr
= IPersistFolder_QueryInterface(persistFolder
, riid
, (void**)ppvOut
);
613 pidlSubFolder
= ILCombine(This
->m_pidlLocation
, pidl
);
614 IPersistFolder3_Initialize(persistFolder
, pidlSubFolder
);
615 IPersistFolder3_Release(persistFolder
);
616 ILFree(pidlSubFolder
);
621 static HRESULT WINAPI
UnixFolder_IShellFolder2_BindToStorage(IShellFolder2
* This
, LPCITEMIDLIST pidl
,
622 LPBC pbcReserved
, REFIID riid
, void** ppvObj
)
628 static HRESULT WINAPI
UnixFolder_IShellFolder2_CompareIDs(IShellFolder2
* iface
, LPARAM lParam
,
629 LPCITEMIDLIST pidl1
, LPCITEMIDLIST pidl2
)
631 BOOL isEmpty1
, isEmpty2
;
633 LPITEMIDLIST firstpidl
;
637 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface
, lParam
, pidl1
, pidl2
);
639 isEmpty1
= !pidl1
|| !pidl1
->mkid
.cb
;
640 isEmpty2
= !pidl2
|| !pidl2
->mkid
.cb
;
642 if (isEmpty1
&& isEmpty2
)
643 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, 0);
645 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)-1);
647 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)1);
649 if (_ILIsFolder(pidl1
) && !_ILIsFolder(pidl2
))
650 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)-1);
651 if (!_ILIsFolder(pidl1
) && _ILIsFolder(pidl2
))
652 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)1);
654 compare
= CompareStringA(LOCALE_USER_DEFAULT
, NORM_IGNORECASE
,
655 _ILGetTextPointer(pidl1
), NAME_LEN_FROM_LPSHITEMID(pidl1
),
656 _ILGetTextPointer(pidl2
), NAME_LEN_FROM_LPSHITEMID(pidl2
));
658 if ((compare
== CSTR_LESS_THAN
) || (compare
== CSTR_GREATER_THAN
))
659 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)((compare
== CSTR_LESS_THAN
)?-1:1));
661 if (pidl1
->mkid
.cb
< pidl2
->mkid
.cb
)
662 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)-1);
663 else if (pidl1
->mkid
.cb
> pidl2
->mkid
.cb
)
664 return MAKE_HRESULT(SEVERITY_SUCCESS
, 0, (WORD
)1);
666 firstpidl
= ILCloneFirst(pidl1
);
667 pidl1
= ILGetNext(pidl1
);
668 pidl2
= ILGetNext(pidl2
);
670 hr
= IShellFolder2_BindToObject(iface
, firstpidl
, NULL
, &IID_IShellFolder
, (LPVOID
*)&psf
);
672 hr
= IShellFolder_CompareIDs(psf
, lParam
, pidl1
, pidl2
);
673 IShellFolder2_Release(psf
);
680 static HRESULT WINAPI
UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2
* iface
, HWND hwndOwner
,
681 REFIID riid
, void** ppv
)
683 HRESULT hr
= E_INVALIDARG
;
685 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface
, hwndOwner
, riid
, ppv
);
687 if (!ppv
) return E_INVALIDARG
;
690 if (IsEqualIID(&IID_IShellView
, riid
)) {
691 LPSHELLVIEW pShellView
;
693 pShellView
= IShellView_Constructor((IShellFolder
*)iface
);
695 hr
= IShellView_QueryInterface(pShellView
, riid
, ppv
);
696 IShellView_Release(pShellView
);
703 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2
* iface
, UINT cidl
,
704 LPCITEMIDLIST
* apidl
, SFGAOF
* rgfInOut
)
706 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
709 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface
, cidl
, apidl
, rgfInOut
);
711 if (!rgfInOut
|| (cidl
&& !apidl
))
715 *rgfInOut
&= This
->m_dwAttributes
;
717 char szAbsolutePath
[FILENAME_MAX
], *pszRelativePath
;
720 *rgfInOut
&= SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_CANRENAME
|
722 lstrcpyA(szAbsolutePath
, This
->m_pszPath
);
723 pszRelativePath
= szAbsolutePath
+ lstrlenA(szAbsolutePath
);
724 for (i
=0; i
<cidl
; i
++) {
725 if ((*rgfInOut
& SFGAO_FILESYSTEM
) && !(This
->m_dwAttributes
& SFGAO_FILESYSTEM
)) {
726 struct stat fileStat
;
727 char *pszName
= _ILGetTextPointer(apidl
[i
]);
728 if (!pszName
) return E_INVALIDARG
;
729 lstrcpyA(pszRelativePath
, pszName
);
730 if (stat(szAbsolutePath
, &fileStat
) || !UNIXFS_is_dos_device(&fileStat
))
731 *rgfInOut
&= ~SFGAO_FILESYSTEM
;
733 if (!_ILIsFolder(apidl
[i
]))
734 *rgfInOut
&= ~(SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
|SFGAO_FILESYSANCESTOR
);
741 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2
* iface
, HWND hwndOwner
,
742 UINT cidl
, LPCITEMIDLIST
* apidl
, REFIID riid
, UINT
* prgfInOut
, void** ppvOut
)
744 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
746 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
747 iface
, hwndOwner
, cidl
, apidl
, debugstr_guid(riid
), prgfInOut
, ppvOut
);
749 if (IsEqualIID(&IID_IContextMenu
, riid
)) {
750 *ppvOut
= ISvItemCm_Constructor((IShellFolder
*)iface
, This
->m_pidlLocation
, apidl
, cidl
);
752 } else if (IsEqualIID(&IID_IDataObject
, riid
)) {
753 *ppvOut
= IDataObject_Constructor(hwndOwner
, This
->m_pidlLocation
, apidl
, cidl
);
755 } else if (IsEqualIID(&IID_IExtractIconA
, riid
)) {
757 if (cidl
!= 1) return E_FAIL
;
758 pidl
= ILCombine(This
->m_pidlLocation
, apidl
[0]);
759 *ppvOut
= (LPVOID
)IExtractIconA_Constructor(pidl
);
762 } else if (IsEqualIID(&IID_IExtractIconW
, riid
)) {
764 if (cidl
!= 1) return E_FAIL
;
765 pidl
= ILCombine(This
->m_pidlLocation
, apidl
[0]);
766 *ppvOut
= (LPVOID
)IExtractIconW_Constructor(pidl
);
769 } else if (IsEqualIID(&IID_IDropTarget
, riid
)) {
770 FIXME("IDropTarget\n");
772 } else if (IsEqualIID(&IID_IShellLinkW
, riid
)) {
773 FIXME("IShellLinkW\n");
775 } else if (IsEqualIID(&IID_IShellLinkA
, riid
)) {
776 FIXME("IShellLinkA\n");
779 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid
));
780 return E_NOINTERFACE
;
784 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2
* iface
,
785 LPCITEMIDLIST pidl
, SHGDNF uFlags
, STRRET
* lpName
)
787 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
790 TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface
, pidl
, uFlags
, lpName
);
792 if ((GET_SHGDN_FOR(uFlags
) & SHGDN_FORPARSING
) &&
793 (GET_SHGDN_RELATION(uFlags
) != SHGDN_INFOLDER
))
795 if (!pidl
->mkid
.cb
) {
796 lpName
->uType
= STRRET_CSTR
;
797 strcpy(lpName
->u
.cStr
, This
->m_pszPath
);
798 if (This
->m_dwPathMode
== PATHMODE_DOS
) {
800 GetFullPathNameA(lpName
->u
.cStr
, MAX_PATH
, path
, NULL
);
801 PathRemoveBackslashA(path
);
802 strcpy(lpName
->u
.cStr
, path
);
805 IShellFolder
*pSubFolder
;
808 hr
= IShellFolder_BindToObject(iface
, pidl
, NULL
, &IID_IShellFolder
, (void**)&pSubFolder
);
809 if (!SUCCEEDED(hr
)) return hr
;
811 hr
= IShellFolder_GetDisplayNameOf(pSubFolder
, (LPITEMIDLIST
)&emptyIDL
, uFlags
, lpName
);
812 IShellFolder_Release(pSubFolder
);
815 char *pszFileName
= _ILGetTextPointer(pidl
);
816 lpName
->uType
= STRRET_CSTR
;
817 strcpy(lpName
->u
.cStr
, pszFileName
? pszFileName
: "");
820 /* If in dos mode, do some post-processing on the path.
821 * (e.g. remove filename extension, if uFlags & SHGDN_FOREDITING)
823 if (SUCCEEDED(hr
) && This
->m_dwPathMode
== PATHMODE_DOS
&& !_ILIsFolder(pidl
))
824 SHELL_FS_ProcessDisplayFilename(lpName
->u
.cStr
, uFlags
);
826 TRACE("--> %s\n", lpName
->u
.cStr
);
831 static HRESULT WINAPI
UnixFolder_IShellFolder2_SetNameOf(IShellFolder2
* iface
, HWND hwnd
,
832 LPCITEMIDLIST pidl
, LPCOLESTR lpszName
, SHGDNF uFlags
, LPITEMIDLIST
* ppidlOut
)
834 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
836 char szSrc
[FILENAME_MAX
], szDest
[FILENAME_MAX
], szDosDest
[MAX_PATH
];
837 WCHAR wszDosDest
[MAX_PATH
];
838 int cBasePathLen
= lstrlenA(This
->m_pszPath
);
839 struct stat statDest
;
840 LPITEMIDLIST pidlSrc
, pidlDest
;
842 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
843 iface
, hwnd
, pidl
, debugstr_w(lpszName
), uFlags
, ppidlOut
);
845 /* pidl has to contain a single non-empty SHITEMID */
846 if (_ILIsDesktop(pidl
) || !_ILIsPidlSimple(pidl
) || !_ILGetTextPointer(pidl
))
852 /* build source path */
853 memcpy(szSrc
, This
->m_pszPath
, cBasePathLen
);
854 lstrcpyA(szSrc
+cBasePathLen
, _ILGetTextPointer(pidl
));
856 /* build destination path */
857 if (uFlags
& SHGDN_FORPARSING
) { /* absolute path in lpszName */
858 WideCharToMultiByte(CP_UNIXCP
, 0, lpszName
, -1, szDest
, FILENAME_MAX
, NULL
, NULL
);
860 WCHAR wszSrcRelative
[MAX_PATH
];
861 memcpy(szDest
, This
->m_pszPath
, cBasePathLen
);
862 WideCharToMultiByte(CP_UNIXCP
, 0, lpszName
, -1, szDest
+cBasePathLen
,
863 FILENAME_MAX
-cBasePathLen
, NULL
, NULL
);
865 /* uFlags is SHGDN_FOREDITING of SHGDN_FORADDRESSBAR. If the filename's
866 * extension is hidden to the user, we have to append it. */
867 if (_ILSimpleGetTextW(pidl
, wszSrcRelative
, MAX_PATH
) &&
868 SHELL_FS_HideExtension(wszSrcRelative
))
870 char *pszExt
= PathFindExtensionA(_ILGetTextPointer(pidl
));
871 lstrcatA(szDest
, pszExt
);
875 TRACE("src=%s dest=%s\n", szSrc
, szDest
);
877 /* Fail, if destination does already exist */
878 if (!stat(szDest
, &statDest
))
881 /* Rename the file */
882 if (rename(szSrc
, szDest
))
885 /* Build a pidl for the path of the renamed file */
886 if (!GetFullPathNameA(szDest
, MAX_PATH
, szDosDest
, NULL
) ||
887 !MultiByteToWideChar(CP_UNIXCP
, 0, szDosDest
, -1, wszDosDest
, MAX_PATH
) ||
888 !UNIXFS_path_to_pidl(This
, wszDosDest
, &pidlDest
))
890 rename(szDest
, szSrc
); /* Undo the renaming */
894 /* Inform the shell */
895 pidlSrc
= ILCombine(This
->m_pidlLocation
, pidl
);
896 if (_ILIsFolder(ILFindLastID(pidlDest
)))
897 SHChangeNotify(SHCNE_RENAMEFOLDER
, SHCNF_IDLIST
, pidlSrc
, pidlDest
);
899 SHChangeNotify(SHCNE_RENAMEITEM
, SHCNF_IDLIST
, pidlSrc
, pidlDest
);
904 _ILCreateFromPathW(wszDosDest
, ppidlOut
);
909 static HRESULT WINAPI
UnixFolder_IShellFolder2_EnumSearches(IShellFolder2
* iface
,
910 IEnumExtraSearch
**ppEnum
)
916 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2
* iface
,
917 DWORD dwReserved
, ULONG
*pSort
, ULONG
*pDisplay
)
923 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2
* iface
,
924 UINT iColumn
, SHCOLSTATEF
*pcsFlags
)
930 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2
* iface
,
937 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2
* iface
,
938 LPCITEMIDLIST pidl
, const SHCOLUMNID
*pscid
, VARIANT
*pv
)
944 #define SHELLVIEWCOLUMNS 7
946 static HRESULT WINAPI
UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2
* iface
,
947 LPCITEMIDLIST pidl
, UINT iColumn
, SHELLDETAILS
*psd
)
949 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IShellFolder2
, iface
);
951 struct passwd
*pPasswd
;
952 struct group
*pGroup
;
953 static const shvheader SFHeader
[SHELLVIEWCOLUMNS
] = {
954 {IDS_SHV_COLUMN1
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 15},
955 {IDS_SHV_COLUMN2
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 10},
956 {IDS_SHV_COLUMN3
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 10},
957 {IDS_SHV_COLUMN4
, SHCOLSTATE_TYPE_DATE
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 12},
958 {IDS_SHV_COLUMN5
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 9},
959 {IDS_SHV_COLUMN10
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 7},
960 {IDS_SHV_COLUMN11
, SHCOLSTATE_TYPE_STR
| SHCOLSTATE_ONBYDEFAULT
, LVCFMT_RIGHT
, 7}
963 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface
, pidl
, iColumn
, psd
);
965 if (!psd
|| iColumn
>= SHELLVIEWCOLUMNS
)
969 psd
->fmt
= SFHeader
[iColumn
].fmt
;
970 psd
->cxChar
= SFHeader
[iColumn
].cxChar
;
971 psd
->str
.uType
= STRRET_CSTR
;
972 LoadStringA(shell32_hInstance
, SFHeader
[iColumn
].colnameid
, psd
->str
.u
.cStr
, MAX_PATH
);
975 struct stat statItem
;
976 if (iColumn
== 4 || iColumn
== 5 || iColumn
== 6) {
977 char szPath
[FILENAME_MAX
], *pszFile
= _ILGetTextPointer(pidl
);
980 lstrcpyA(szPath
, This
->m_pszPath
);
981 lstrcatA(szPath
, pszFile
);
982 if (stat(szPath
, &statItem
))
985 psd
->str
.u
.cStr
[0] = '\0';
986 psd
->str
.uType
= STRRET_CSTR
;
989 hr
= IShellFolder2_GetDisplayNameOf(iface
, pidl
, SHGDN_NORMAL
|SHGDN_INFOLDER
, &psd
->str
);
992 _ILGetFileSize(pidl
, psd
->str
.u
.cStr
, MAX_PATH
);
995 _ILGetFileType (pidl
, psd
->str
.u
.cStr
, MAX_PATH
);
998 _ILGetFileDate(pidl
, psd
->str
.u
.cStr
, MAX_PATH
);
1001 psd
->str
.u
.cStr
[0] = S_ISDIR(statItem
.st_mode
) ? 'd' : '-';
1002 psd
->str
.u
.cStr
[1] = (statItem
.st_mode
& S_IRUSR
) ? 'r' : '-';
1003 psd
->str
.u
.cStr
[2] = (statItem
.st_mode
& S_IWUSR
) ? 'w' : '-';
1004 psd
->str
.u
.cStr
[3] = (statItem
.st_mode
& S_IXUSR
) ? 'x' : '-';
1005 psd
->str
.u
.cStr
[4] = (statItem
.st_mode
& S_IRGRP
) ? 'r' : '-';
1006 psd
->str
.u
.cStr
[5] = (statItem
.st_mode
& S_IWGRP
) ? 'w' : '-';
1007 psd
->str
.u
.cStr
[6] = (statItem
.st_mode
& S_IXGRP
) ? 'x' : '-';
1008 psd
->str
.u
.cStr
[7] = (statItem
.st_mode
& S_IROTH
) ? 'r' : '-';
1009 psd
->str
.u
.cStr
[8] = (statItem
.st_mode
& S_IWOTH
) ? 'w' : '-';
1010 psd
->str
.u
.cStr
[9] = (statItem
.st_mode
& S_IXOTH
) ? 'x' : '-';
1011 psd
->str
.u
.cStr
[10] = '\0';
1014 pPasswd
= getpwuid(statItem
.st_uid
);
1015 if (pPasswd
) strcpy(psd
->str
.u
.cStr
, pPasswd
->pw_name
);
1018 pGroup
= getgrgid(statItem
.st_gid
);
1019 if (pGroup
) strcpy(psd
->str
.u
.cStr
, pGroup
->gr_name
);
1027 static HRESULT WINAPI
UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2
* iface
, UINT iColumn
,
1034 /* VTable for UnixFolder's IShellFolder2 interface.
1036 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl
= {
1037 UnixFolder_IShellFolder2_QueryInterface
,
1038 UnixFolder_IShellFolder2_AddRef
,
1039 UnixFolder_IShellFolder2_Release
,
1040 UnixFolder_IShellFolder2_ParseDisplayName
,
1041 UnixFolder_IShellFolder2_EnumObjects
,
1042 UnixFolder_IShellFolder2_BindToObject
,
1043 UnixFolder_IShellFolder2_BindToStorage
,
1044 UnixFolder_IShellFolder2_CompareIDs
,
1045 UnixFolder_IShellFolder2_CreateViewObject
,
1046 UnixFolder_IShellFolder2_GetAttributesOf
,
1047 UnixFolder_IShellFolder2_GetUIObjectOf
,
1048 UnixFolder_IShellFolder2_GetDisplayNameOf
,
1049 UnixFolder_IShellFolder2_SetNameOf
,
1050 UnixFolder_IShellFolder2_GetDefaultSearchGUID
,
1051 UnixFolder_IShellFolder2_EnumSearches
,
1052 UnixFolder_IShellFolder2_GetDefaultColumn
,
1053 UnixFolder_IShellFolder2_GetDefaultColumnState
,
1054 UnixFolder_IShellFolder2_GetDetailsEx
,
1055 UnixFolder_IShellFolder2_GetDetailsOf
,
1056 UnixFolder_IShellFolder2_MapColumnToSCID
1059 static HRESULT WINAPI
UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3
* This
, REFIID riid
,
1062 return UnixFolder_IShellFolder2_QueryInterface(
1063 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, IPersistFolder3
, This
), riid
, ppvObject
);
1066 static ULONG WINAPI
UnixFolder_IPersistFolder3_AddRef(IPersistFolder3
* This
)
1068 return UnixFolder_IShellFolder2_AddRef(
1069 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, IPersistFolder3
, This
));
1072 static ULONG WINAPI
UnixFolder_IPersistFolder3_Release(IPersistFolder3
* This
)
1074 return UnixFolder_IShellFolder2_Release(
1075 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, IPersistFolder3
, This
));
1078 static HRESULT WINAPI
UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3
* This
, CLSID
* pClassID
)
1084 static HRESULT WINAPI
UnixFolder_IPersistFolder3_Initialize(IPersistFolder3
* iface
, LPCITEMIDLIST pidl
)
1086 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IPersistFolder3
, iface
);
1087 struct stat statPrefix
;
1088 LPCITEMIDLIST current
= pidl
, root
;
1090 char *pNextDir
, szBasePath
[FILENAME_MAX
] = "/";
1092 TRACE("(iface=%p, pidl=%p)\n", iface
, pidl
);
1094 /* Find the UnixFolderClass root */
1095 while (current
->mkid
.cb
) {
1096 if (_ILIsSpecialFolder(current
) &&
1097 (IsEqualIID(&CLSID_UnixFolder
, _ILGetGUIDPointer(current
)) ||
1098 IsEqualIID(&CLSID_UnixDosFolder
, _ILGetGUIDPointer(current
))))
1102 current
= ILGetNext(current
);
1105 if (current
&& current
->mkid
.cb
) {
1106 root
= current
= ILGetNext(current
);
1107 dwPathLen
= 2; /* For the '/' prefix and the terminating '\0' */
1108 } else if (_ILIsDesktop(pidl
) || _ILIsValue(pidl
) || _ILIsFolder(pidl
)) {
1109 /* Path rooted at Desktop */
1110 WCHAR wszDesktopPath
[MAX_PATH
];
1111 if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath
, CSIDL_DESKTOP
, FALSE
)))
1113 PathAddBackslashW(wszDesktopPath
);
1114 if (!UNIXFS_get_unix_path(wszDesktopPath
, szBasePath
))
1116 dwPathLen
= strlen(szBasePath
) + 1;
1117 root
= current
= pidl
;
1119 ERR("Unknown pidl type!\n");
1121 return E_INVALIDARG
;
1124 /* Determine the path's length bytes */
1125 while (current
&& current
->mkid
.cb
) {
1126 dwPathLen
+= NAME_LEN_FROM_LPSHITEMID(current
) + 1; /* For the '/' */
1127 current
= ILGetNext(current
);
1130 /* Build the path */
1131 This
->m_dwAttributes
= SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_CANRENAME
;
1132 This
->m_pidlLocation
= ILClone(pidl
);
1133 This
->m_pszPath
= pNextDir
= SHAlloc(dwPathLen
);
1134 if (!This
->m_pszPath
|| !This
->m_pidlLocation
) {
1135 WARN("SHAlloc failed!\n");
1139 strcpy(pNextDir
, szBasePath
);
1140 pNextDir
+= strlen(szBasePath
);
1141 if (This
->m_dwPathMode
== PATHMODE_UNIX
)
1142 This
->m_dwAttributes
|= SFGAO_FILESYSTEM
;
1143 if (!(This
->m_dwAttributes
& SFGAO_FILESYSTEM
)) {
1145 if (!stat(This
->m_pszPath
, &statPrefix
) && UNIXFS_is_dos_device(&statPrefix
))
1146 This
->m_dwAttributes
|= SFGAO_FILESYSTEM
;
1148 while (current
&& current
->mkid
.cb
) {
1149 memcpy(pNextDir
, _ILGetTextPointer(current
), NAME_LEN_FROM_LPSHITEMID(current
));
1150 pNextDir
+= NAME_LEN_FROM_LPSHITEMID(current
);
1151 if (!(This
->m_dwAttributes
& SFGAO_FILESYSTEM
)) {
1153 if (!stat(This
->m_pszPath
, &statPrefix
) && UNIXFS_is_dos_device(&statPrefix
))
1154 This
->m_dwAttributes
|= SFGAO_FILESYSTEM
;
1157 current
= ILGetNext(current
);
1164 static HRESULT WINAPI
UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3
* iface
, LPITEMIDLIST
* ppidl
)
1166 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IPersistFolder3
, iface
);
1168 TRACE ("(iface=%p, ppidl=%p)\n", iface
, ppidl
);
1172 *ppidl
= ILClone (This
->m_pidlLocation
);
1176 static HRESULT WINAPI
UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3
*iface
, IBindCtx
*pbc
,
1177 LPCITEMIDLIST pidlRoot
, const PERSIST_FOLDER_TARGET_INFO
*ppfti
)
1179 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, IPersistFolder3
, iface
);
1180 WCHAR wszTargetDosPath
[MAX_PATH
];
1181 char szTargetPath
[FILENAME_MAX
] = "";
1183 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface
, pbc
, pidlRoot
, ppfti
);
1185 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1187 return IPersistFolder3_Initialize(iface
, pidlRoot
);
1189 if (ppfti
->csidl
!= -1) {
1190 if (SUCCEEDED(SHGetFolderPathW(0, ppfti
->csidl
, NULL
, 0, wszTargetDosPath
))) {
1191 UNIXFS_get_unix_path(wszTargetDosPath
, szTargetPath
);
1193 } else if (*ppfti
->szTargetParsingName
) {
1194 UNIXFS_get_unix_path(ppfti
->szTargetParsingName
, szTargetPath
);
1195 } else if (ppfti
->pidlTargetFolder
) {
1196 if (SHGetPathFromIDListW(ppfti
->pidlTargetFolder
, wszTargetDosPath
)) {
1197 UNIXFS_get_unix_path(wszTargetDosPath
, szTargetPath
);
1201 This
->m_pszPath
= SHAlloc(lstrlenA(szTargetPath
)+1);
1202 if (!This
->m_pszPath
)
1204 lstrcpyA(This
->m_pszPath
, szTargetPath
);
1205 This
->m_pidlLocation
= ILClone(pidlRoot
);
1206 This
->m_dwAttributes
= (ppfti
->dwAttributes
== -1) ? ppfti
->dwAttributes
:
1207 SFGAO_FOLDER
|SFGAO_HASSUBFOLDER
|SFGAO_FILESYSANCESTOR
|SFGAO_CANRENAME
|SFGAO_FILESYSTEM
;
1212 static HRESULT WINAPI
UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3
*iface
,
1213 PERSIST_FOLDER_TARGET_INFO
*ppfti
)
1215 FIXME("(iface=%p, ppfti=%p) stub\n", iface
, ppfti
);
1219 /* VTable for UnixFolder's IPersistFolder interface.
1221 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl
= {
1222 UnixFolder_IPersistFolder3_QueryInterface
,
1223 UnixFolder_IPersistFolder3_AddRef
,
1224 UnixFolder_IPersistFolder3_Release
,
1225 UnixFolder_IPersistFolder3_GetClassID
,
1226 UnixFolder_IPersistFolder3_Initialize
,
1227 UnixFolder_IPersistFolder3_GetCurFolder
,
1228 UnixFolder_IPersistFolder3_InitializeEx
,
1229 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1232 static HRESULT WINAPI
UnixFolder_ISFHelper_QueryInterface(ISFHelper
* iface
, REFIID riid
,
1235 return UnixFolder_IShellFolder2_QueryInterface(
1236 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, ISFHelper
, iface
), riid
, ppvObject
);
1239 static ULONG WINAPI
UnixFolder_ISFHelper_AddRef(ISFHelper
* iface
)
1241 return UnixFolder_IShellFolder2_AddRef(
1242 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, ISFHelper
, iface
));
1245 static ULONG WINAPI
UnixFolder_ISFHelper_Release(ISFHelper
* iface
)
1247 return UnixFolder_IShellFolder2_Release(
1248 (IShellFolder2
*)ADJUST_THIS(UnixFolder
, ISFHelper
, iface
));
1251 static HRESULT WINAPI
UnixFolder_ISFHelper_GetUniqueName(ISFHelper
* iface
, LPSTR lpName
, UINT uLen
)
1253 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, ISFHelper
, iface
);
1256 LPITEMIDLIST pidlElem
;
1259 static const char szNewFolder
[] = "New Folder";
1261 TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface
, lpName
, uLen
);
1263 if (uLen
< sizeof(szNewFolder
)+3)
1264 return E_INVALIDARG
;
1266 hr
= IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2
, This
), 0,
1267 SHCONTF_FOLDERS
|SHCONTF_NONFOLDERS
|SHCONTF_INCLUDEHIDDEN
, &pEnum
);
1268 if (SUCCEEDED(hr
)) {
1269 lstrcpyA(lpName
, szNewFolder
);
1270 IEnumIDList_Reset(pEnum
);
1272 while ((IEnumIDList_Next(pEnum
, 1, &pidlElem
, &dwFetched
) == S_OK
) && (dwFetched
== 1)) {
1273 if (!strcasecmp(_ILGetTextPointer(pidlElem
), lpName
)) {
1274 IEnumIDList_Reset(pEnum
);
1275 sprintf(lpName
, "%s %d", szNewFolder
, i
++);
1282 IEnumIDList_Release(pEnum
);
1287 static HRESULT WINAPI
UnixFolder_ISFHelper_AddFolder(ISFHelper
* iface
, HWND hwnd
, LPCSTR pszName
,
1288 LPITEMIDLIST
* ppidlOut
)
1290 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, ISFHelper
, iface
);
1291 char szNewDir
[FILENAME_MAX
];
1293 TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface
, hwnd
, pszName
, ppidlOut
);
1298 lstrcpyA(szNewDir
, This
->m_pszPath
);
1299 lstrcatA(szNewDir
, pszName
);
1301 if (mkdir(szNewDir
, 0755)) {
1302 char szMessage
[256 + FILENAME_MAX
];
1303 char szCaption
[256];
1305 LoadStringA(shell32_hInstance
, IDS_CREATEFOLDER_DENIED
, szCaption
, sizeof(szCaption
));
1306 sprintf(szMessage
, szCaption
, szNewDir
);
1307 LoadStringA(shell32_hInstance
, IDS_CREATEFOLDER_CAPTION
, szCaption
, sizeof(szCaption
));
1308 MessageBoxA(hwnd
, szMessage
, szCaption
, MB_OK
| MB_ICONEXCLAMATION
);
1312 LPITEMIDLIST pidlRelative
;
1313 WCHAR wszName
[MAX_PATH
];
1315 /* Inform the shell */
1316 MultiByteToWideChar(CP_UNIXCP
, 0, pszName
, -1, wszName
, MAX_PATH
);
1317 if (UNIXFS_path_to_pidl(This
, wszName
, &pidlRelative
)) {
1318 LPITEMIDLIST pidlAbsolute
= ILCombine(This
->m_pidlLocation
, pidlRelative
);
1320 *ppidlOut
= pidlRelative
;
1322 ILFree(pidlRelative
);
1323 SHChangeNotify(SHCNE_MKDIR
, SHCNF_IDLIST
, pidlAbsolute
, NULL
);
1324 ILFree(pidlAbsolute
);
1330 static HRESULT WINAPI
UnixFolder_ISFHelper_DeleteItems(ISFHelper
* iface
, UINT cidl
,
1331 LPCITEMIDLIST
* apidl
)
1333 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, ISFHelper
, iface
);
1334 char szAbsolute
[FILENAME_MAX
], *pszRelative
;
1335 LPITEMIDLIST pidlAbsolute
;
1339 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface
, cidl
, apidl
);
1341 lstrcpyA(szAbsolute
, This
->m_pszPath
);
1342 pszRelative
= szAbsolute
+ lstrlenA(szAbsolute
);
1344 for (i
=0; i
<cidl
&& SUCCEEDED(hr
); i
++) {
1345 lstrcpyA(pszRelative
, _ILGetTextPointer(apidl
[i
]));
1346 pidlAbsolute
= ILCombine(This
->m_pidlLocation
, apidl
[i
]);
1347 if (_ILIsFolder(apidl
[i
])) {
1348 if (rmdir(szAbsolute
)) {
1351 SHChangeNotify(SHCNE_RMDIR
, SHCNF_IDLIST
, pidlAbsolute
, NULL
);
1353 } else if (_ILIsValue(apidl
[i
])) {
1354 if (unlink(szAbsolute
)) {
1357 SHChangeNotify(SHCNE_DELETE
, SHCNF_IDLIST
, pidlAbsolute
, NULL
);
1360 ILFree(pidlAbsolute
);
1366 static HRESULT WINAPI
UnixFolder_ISFHelper_CopyItems(ISFHelper
* iface
, IShellFolder
*psfFrom
,
1367 UINT cidl
, LPCITEMIDLIST
*apidl
)
1369 UnixFolder
*This
= ADJUST_THIS(UnixFolder
, ISFHelper
, iface
);
1373 char szAbsoluteDst
[FILENAME_MAX
], *pszRelativeDst
;
1375 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface
, psfFrom
, cidl
, apidl
);
1377 if (!psfFrom
|| !cidl
|| !apidl
)
1378 return E_INVALIDARG
;
1380 /* All source items have to be filesystem items. */
1381 dwAttributes
= SFGAO_FILESYSTEM
;
1382 hr
= IShellFolder_GetAttributesOf(psfFrom
, cidl
, apidl
, &dwAttributes
);
1383 if (FAILED(hr
) || !(dwAttributes
& SFGAO_FILESYSTEM
))
1384 return E_INVALIDARG
;
1386 lstrcpyA(szAbsoluteDst
, This
->m_pszPath
);
1387 pszRelativeDst
= szAbsoluteDst
+ strlen(szAbsoluteDst
);
1389 for (i
=0; i
<cidl
; i
++) {
1390 WCHAR wszSrc
[MAX_PATH
];
1391 char szSrc
[FILENAME_MAX
];
1394 /* Build the unix path of the current source item. */
1395 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom
, apidl
[i
], SHGDN_FORPARSING
, &strret
)))
1397 if (FAILED(StrRetToBufW(&strret
, apidl
[i
], wszSrc
, MAX_PATH
)))
1399 if (!UNIXFS_get_unix_path(wszSrc
, szSrc
))
1402 /* Build the unix path of the current destination item */
1403 lstrcpyA(pszRelativeDst
, _ILGetTextPointer(apidl
[i
]));
1405 FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc
, szAbsoluteDst
);
1410 /* VTable for UnixFolder's ISFHelper interface
1412 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl
= {
1413 UnixFolder_ISFHelper_QueryInterface
,
1414 UnixFolder_ISFHelper_AddRef
,
1415 UnixFolder_ISFHelper_Release
,
1416 UnixFolder_ISFHelper_GetUniqueName
,
1417 UnixFolder_ISFHelper_AddFolder
,
1418 UnixFolder_ISFHelper_DeleteItems
,
1419 UnixFolder_ISFHelper_CopyItems
1422 /******************************************************************************
1423 * Unix[Dos]Folder_Constructor [Internal]
1426 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
1427 * riid [I] Interface asked for by the client.
1428 * ppv [O] Pointer to an riid interface to the UnixFolder object.
1431 * Those are the only functions exported from shfldr_unixfs.c. They are called from
1432 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1433 * compatible signature.
1435 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1436 * means that paths are converted from dos to unix and back at the interfaces.
1438 static HRESULT
CreateUnixFolder(IUnknown
*pUnkOuter
, REFIID riid
, LPVOID
*ppv
, DWORD dwPathMode
) {
1439 HRESULT hr
= E_FAIL
;
1440 UnixFolder
*pUnixFolder
= SHAlloc((ULONG
)sizeof(UnixFolder
));
1443 pUnixFolder
->lpIShellFolder2Vtbl
= &UnixFolder_IShellFolder2_Vtbl
;
1444 pUnixFolder
->lpIPersistFolder3Vtbl
= &UnixFolder_IPersistFolder3_Vtbl
;
1445 pUnixFolder
->lpISFHelperVtbl
= &UnixFolder_ISFHelper_Vtbl
;
1446 pUnixFolder
->m_cRef
= 0;
1447 pUnixFolder
->m_pszPath
= NULL
;
1448 pUnixFolder
->m_dwPathMode
= dwPathMode
;
1450 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2
, pUnixFolder
));
1451 hr
= UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2
, pUnixFolder
), riid
, ppv
);
1452 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2
, pUnixFolder
));
1457 HRESULT WINAPI
UnixFolder_Constructor(IUnknown
*pUnkOuter
, REFIID riid
, LPVOID
*ppv
) {
1458 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter
, riid
, ppv
);
1459 return CreateUnixFolder(pUnkOuter
, riid
, ppv
, PATHMODE_UNIX
);
1462 HRESULT WINAPI
UnixDosFolder_Constructor(IUnknown
*pUnkOuter
, REFIID riid
, LPVOID
*ppv
) {
1463 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter
, riid
, ppv
);
1464 return CreateUnixFolder(pUnkOuter
, riid
, ppv
, PATHMODE_DOS
);
1467 /******************************************************************************
1468 * UnixSubFolderIterator
1470 * Class whose heap based objects represent iterators over the sub-directories
1471 * of a given UnixFolder object.
1474 /* UnixSubFolderIterator object layout and typedef.
1476 typedef struct _UnixSubFolderIterator
{
1477 const IEnumIDListVtbl
*lpIEnumIDListVtbl
;
1481 char m_szFolder
[FILENAME_MAX
];
1482 } UnixSubFolderIterator
;
1484 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator
*iterator
) {
1485 TRACE("(iterator=%p)\n", iterator
);
1487 if (iterator
->m_dirFolder
)
1488 closedir(iterator
->m_dirFolder
);
1492 static HRESULT WINAPI
UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList
* iface
,
1493 REFIID riid
, void** ppv
)
1495 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface
, riid
, ppv
);
1497 if (!ppv
) return E_INVALIDARG
;
1499 if (IsEqualIID(&IID_IUnknown
, riid
) || IsEqualIID(&IID_IEnumIDList
, riid
)) {
1503 return E_NOINTERFACE
;
1506 IEnumIDList_AddRef(iface
);
1510 static ULONG WINAPI
UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList
* iface
)
1512 UnixSubFolderIterator
*This
= ADJUST_THIS(UnixSubFolderIterator
, IEnumIDList
, iface
);
1514 TRACE("(iface=%p)\n", iface
);
1516 return InterlockedIncrement(&This
->m_cRef
);
1519 static ULONG WINAPI
UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList
* iface
)
1521 UnixSubFolderIterator
*This
= ADJUST_THIS(UnixSubFolderIterator
, IEnumIDList
, iface
);
1524 TRACE("(iface=%p)\n", iface
);
1526 cRef
= InterlockedDecrement(&This
->m_cRef
);
1529 UnixSubFolderIterator_Destroy(This
);
1534 static HRESULT WINAPI
UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList
* iface
, ULONG celt
,
1535 LPITEMIDLIST
* rgelt
, ULONG
* pceltFetched
)
1537 UnixSubFolderIterator
*This
= ADJUST_THIS(UnixSubFolderIterator
, IEnumIDList
, iface
);
1540 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
1541 if (This
->m_dirFolder
) {
1542 char *pszRelativePath
= This
->m_szFolder
+ lstrlenA(This
->m_szFolder
);
1543 struct dirent
*pDirEntry
;
1546 pDirEntry
= readdir(This
->m_dirFolder
);
1547 if (!pDirEntry
) break; /* No more entries */
1548 if (!strcmp(pDirEntry
->d_name
, ".") || !strcmp(pDirEntry
->d_name
, "..")) continue;
1550 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
1551 * and see if it passes the filter.
1553 lstrcpyA(pszRelativePath
, pDirEntry
->d_name
);
1554 rgelt
[i
] = (LPITEMIDLIST
)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(lstrlenA(pszRelativePath
))+sizeof(USHORT
));
1555 if (!UNIXFS_build_shitemid(This
->m_szFolder
, rgelt
[i
]) ||
1556 !UNIXFS_is_pidl_of_type(rgelt
[i
], This
->m_fFilter
))
1561 memset(((PBYTE
)rgelt
[i
])+rgelt
[i
]->mkid
.cb
, 0, sizeof(USHORT
));
1564 *pszRelativePath
= '\0'; /* Restore the original path in This->m_szFolder. */
1570 return (i
== 0) ? S_FALSE
: S_OK
;
1573 static HRESULT WINAPI
UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList
* iface
, ULONG celt
)
1575 LPITEMIDLIST
*apidl
;
1579 TRACE("(iface=%p, celt=%ld)\n", iface
, celt
);
1581 /* Call IEnumIDList::Next and delete the resulting pidls. */
1582 apidl
= (LPITEMIDLIST
*)SHAlloc(celt
* sizeof(LPITEMIDLIST
));
1583 hr
= IEnumIDList_Next(iface
, celt
, apidl
, &cFetched
);
1586 SHFree(apidl
[cFetched
]);
1592 static HRESULT WINAPI
UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList
* iface
)
1594 UnixSubFolderIterator
*This
= ADJUST_THIS(UnixSubFolderIterator
, IEnumIDList
, iface
);
1596 TRACE("(iface=%p)\n", iface
);
1598 if (This
->m_dirFolder
)
1599 rewinddir(This
->m_dirFolder
);
1604 static HRESULT WINAPI
UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList
* This
,
1605 IEnumIDList
** ppenum
)
1611 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1613 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl
= {
1614 UnixSubFolderIterator_IEnumIDList_QueryInterface
,
1615 UnixSubFolderIterator_IEnumIDList_AddRef
,
1616 UnixSubFolderIterator_IEnumIDList_Release
,
1617 UnixSubFolderIterator_IEnumIDList_Next
,
1618 UnixSubFolderIterator_IEnumIDList_Skip
,
1619 UnixSubFolderIterator_IEnumIDList_Reset
,
1620 UnixSubFolderIterator_IEnumIDList_Clone
1623 static IUnknown
*UnixSubFolderIterator_Constructor(UnixFolder
*pUnixFolder
, SHCONTF fFilter
) {
1624 UnixSubFolderIterator
*iterator
;
1626 TRACE("(pUnixFolder=%p)\n", pUnixFolder
);
1628 iterator
= SHAlloc((ULONG
)sizeof(UnixSubFolderIterator
));
1629 iterator
->lpIEnumIDListVtbl
= &UnixSubFolderIterator_IEnumIDList_Vtbl
;
1630 iterator
->m_cRef
= 0;
1631 iterator
->m_fFilter
= fFilter
;
1632 iterator
->m_dirFolder
= opendir(pUnixFolder
->m_pszPath
);
1633 lstrcpyA(iterator
->m_szFolder
, pUnixFolder
->m_pszPath
);
1635 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList
*)iterator
);
1637 return (IUnknown
*)iterator
;