Unicodify wineesd.
[wine] / dlls / shell32 / shfldr_unixfs.c
1 /*
2  * UNIXFS - Shell namespace extension for the unix filesystem
3  *
4  * Copyright (C) 2005 Michael Jung
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdio.h>
25 #include <stdarg.h>
26 #include <limits.h>
27 #include <dirent.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_PWD_H
35 # include <pwd.h>
36 #endif
37 #include <grp.h>
38 #include <limits.h>
39
40 #define COBJMACROS
41 #define NONAMELESSUNION
42 #define NONAMELESSSTRUCT
43
44 #include "windef.h"
45 #include "winbase.h"
46 #include "winuser.h"
47 #include "objbase.h"
48 #include "winreg.h"
49 #include "shlwapi.h"
50 #include "winternl.h"
51 #include "wine/debug.h"
52
53 #include "shell32_main.h"
54 #include "shellfolder.h"
55 #include "shfldr.h"
56 #include "shresdef.h"
57 #include "pidl.h"
58
59 WINE_DEFAULT_DEBUG_CHANNEL(shell);
60
61 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
62 const GUID CLSID_UnixDosFolder = {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
63
64 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
65 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
66
67 /* FileStruct reserves one byte for szNames, thus we don't have to 
68  * alloc a byte for the terminating '\0' of 'name'. Two of the
69  * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
70  * type field. One is for FileStruct's szNames field, to terminate
71  * the alternate DOS name, which we don't use here.
72  */
73 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
74     (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char))
75 #define NAME_LEN_FROM_LPSHITEMID(s) \
76     (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char))
77
78 #define PATHMODE_UNIX 0
79 #define PATHMODE_DOS  1
80
81 /* UnixFolder object layout and typedef.
82  */
83 typedef struct _UnixFolder {
84     const IShellFolder2Vtbl  *lpIShellFolder2Vtbl;
85     const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
86     const ISFHelperVtbl *lpISFHelperVtbl;
87     LONG m_cRef;
88     CHAR *m_pszPath;
89     LPITEMIDLIST m_pidlLocation;
90     DWORD m_dwPathMode;
91     DWORD m_dwAttributes;
92 } UnixFolder;
93
94 /******************************************************************************
95  * UNIXFS_is_rooted_at_desktop [Internal]
96  *
97  * Checks if the unixfs namespace extension is rooted at desktop level.
98  *
99  * RETURNS
100  *  TRUE, if unixfs is rooted at desktop level
101  *  FALSE, if not.
102  */
103 BOOL UNIXFS_is_rooted_at_desktop(void) {
104     HKEY hKey;
105     WCHAR *pwszCLSID_UnixDosFolder, wszRootedAtDesktop[69 + CHARS_IN_GUID] = { 
106         'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
107         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
108         'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
109         'N','a','m','e','S','p','a','c','e','\\',0 }; 
110
111     if (FAILED(StringFromCLSID(&CLSID_UnixDosFolder, &pwszCLSID_UnixDosFolder)))
112         return FALSE;
113     
114     lstrcatW(wszRootedAtDesktop, pwszCLSID_UnixDosFolder);
115     CoTaskMemFree(pwszCLSID_UnixDosFolder);
116     
117     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
118         return FALSE;
119         
120     RegCloseKey(hKey);
121     return TRUE;
122 }
123
124 /******************************************************************************
125  * UNIXFS_is_pidl_of_type [INTERNAL]
126  *
127  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
128  *
129  * PARAMS
130  *  pIDL    [I] The ITEMIDLIST to be checked.
131  *  fFilter [I] Shell condition flags, which specify the filter.
132  *
133  * RETURNS
134  *  TRUE, if pIDL is accepted by fFilter
135  *  FALSE, otherwise
136  */
137 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
138     LPPIDLDATA pIDLData = _ILGetDataPointer(pIDL);
139     if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData && 
140         (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN)) 
141     {
142         return FALSE;
143     }
144     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
145     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
146     return FALSE;
147 }
148
149 /******************************************************************************
150  * UNIXFS_is_dos_device [Internal]
151  *
152  * Determines if a unix directory corresponds to any dos device.
153  *
154  * PARAMS
155  *  statPath [I] The stat struct of the directory, as returned by stat(2).
156  *
157  * RETURNS
158  *  TRUE, if statPath corresponds to any dos drive letter
159  *  FALSE, otherwise
160  */
161 static BOOL UNIXFS_is_dos_device(const struct stat *statPath) {
162     struct stat statDrive;
163     char *pszDrivePath;
164     DWORD dwDriveMap;
165     WCHAR wszDosDevice[4] = { 'A', ':', '\\', 0 };
166
167     for (dwDriveMap = GetLogicalDrives(); dwDriveMap; dwDriveMap >>= 1, wszDosDevice[0]++) {
168         if (!(dwDriveMap & 0x1)) continue;
169         pszDrivePath = wine_get_unix_file_name(wszDosDevice);
170         if (pszDrivePath && !stat(pszDrivePath, &statDrive)) {
171             HeapFree(GetProcessHeap(), 0, pszDrivePath);
172             if ((statPath->st_dev == statDrive.st_dev) && (statPath->st_ino == statDrive.st_ino))
173                 return TRUE;
174         }
175     }
176     return FALSE;
177 }
178
179 /******************************************************************************
180  * UNIXFS_get_unix_path [Internal]
181  *
182  * Convert an absolute dos path to an absolute canonicalized unix path.
183  * Evaluate "/.", "/.." and symbolic links.
184  *
185  * PARAMS
186  *  pszDosPath       [I] An absolute dos path
187  *  pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
188  *
189  * RETURNS
190  *  Success, TRUE
191  *  Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
192  */
193 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
194 {
195     char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
196     struct stat fileStat;
197     
198     TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
199     
200     if (!pszDosPath || pszDosPath[1] != ':')
201         return FALSE;
202
203     pszUnixPath = wine_get_unix_file_name(pszDosPath);
204     if (!pszUnixPath) return FALSE;   
205     strcpy(szPath, pszUnixPath);
206     HeapFree(GetProcessHeap(), 0, pszUnixPath);
207    
208     /* pCanonicalTail always points to the end of the canonical path constructed
209      * thus far. pPathTail points to the still to be processed part of the input
210      * path. pElement points to the path element currently investigated.
211      */
212     *pszCanonicalPath = '\0';
213     pCanonicalTail = pszCanonicalPath;
214     pPathTail = szPath;
215
216     do {
217         char cTemp;
218         int cLinks = 0;
219             
220         pElement = pPathTail;
221         pPathTail = strchr(pPathTail+1, '/');
222         if (!pPathTail) /* Last path element may not be terminated by '/'. */ 
223             pPathTail = pElement + strlen(pElement);
224         /* Temporarily terminate the current path element. Will be restored later. */
225         cTemp = *pPathTail;
226         *pPathTail = '\0';
227
228         /* Skip "/." path elements */
229         if (!strcmp("/.", pElement)) {
230             *pPathTail = cTemp;
231             continue;
232         }
233
234         /* Remove last element in canonical path for "/.." elements, then skip. */
235         if (!strcmp("/..", pElement)) {
236             char *pTemp = strrchr(pszCanonicalPath, '/');
237             if (pTemp)
238                 pCanonicalTail = pTemp;
239             *pCanonicalTail = '\0';
240             *pPathTail = cTemp;
241             continue;
242         }
243        
244         /* lstat returns zero on success. */
245         if (lstat(szPath, &fileStat)) 
246             return FALSE;
247
248         if (S_ISLNK(fileStat.st_mode)) {
249             char szSymlink[FILENAME_MAX];
250             int cLinkLen, cTailLen;
251           
252             /* Avoid infinite loop for recursive links. */
253             if (++cLinks > 64) 
254                 return FALSE;
255             
256             cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
257             if (cLinkLen < 0) 
258                 return FALSE;
259
260             *pPathTail = cTemp;
261             cTailLen = strlen(pPathTail);
262            
263             if (szSymlink[0] == '/') {
264                 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
265                 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
266                     return FALSE;
267                     
268                 /* Avoid double slashes. */
269                 if (szSymlink[cLinkLen-1] == '/' && pPathTail[0] == '/') {
270                     szSymlink[cLinkLen-1] = '\0';
271                     cLinkLen--;
272                 }
273                 
274                 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
275                 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
276                 *pszCanonicalPath = '\0';
277                 pCanonicalTail = pszCanonicalPath;
278                 pPathTail = szPath;
279             } else {
280                 /* Relative link. Expand into szPath and continue. */
281                 char szTemp[FILENAME_MAX];
282                 int cTailLen = strlen(pPathTail);
283
284                 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
285                     return FALSE;
286
287                 memcpy(szTemp, pPathTail, cTailLen + 1);
288                 memcpy(pElement + 1, szSymlink, cLinkLen);
289                 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
290                 pPathTail = pElement;
291             }
292         } else {
293             /* Regular directory or file. Copy to canonical path */
294             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
295                 return FALSE;
296                 
297             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
298             pCanonicalTail += pPathTail - pElement;
299             *pPathTail = cTemp;
300         }
301     } while (pPathTail[0] == '/');
302    
303     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
304     
305     return TRUE;
306 }
307
308 /******************************************************************************
309  * UNIXFS_build_shitemid [Internal]
310  *
311  * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into 
312  * buffer 'pIDL'.
313  *
314  * PARAMS
315  *  pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
316  *  pIDL        [O] SHITEMID will be constructed here.
317  *
318  * RETURNS
319  *  Success: A pointer to the terminating '\0' character of path.
320  *  Failure: NULL
321  *
322  * NOTES
323  *  Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
324  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
325  *  a 0 USHORT value.
326  */
327 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
328     LARGE_INTEGER time;
329     FILETIME fileTime;
330     LPPIDLDATA pIDLData;
331     struct stat fileStat;
332     char *pszComponent;
333     int cComponentLen;
334
335     TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
336
337     /* Compute the SHITEMID's length and wipe it. */
338     pszComponent = strrchr(pszUnixPath, '/') + 1;
339     cComponentLen = strlen(pszComponent);
340     memset(pIDL, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen));
341     ((LPSHITEMID)pIDL)->cb = SHITEMID_LEN_FROM_NAME_LEN(cComponentLen) ;
342    
343     /* We are only interested in regular files and directories. */
344     if (stat(pszUnixPath, &fileStat)) return NULL;
345     if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
346     
347     /* Set shell32's standard SHITEMID data fields. */
348     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
349     pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
350     pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
351     RtlSecondsSince1970ToTime( fileStat.st_mtime, &time );
352     fileTime.dwLowDateTime = time.u.LowPart;
353     fileTime.dwHighDateTime = time.u.HighPart;
354     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
355     pIDLData->u.file.uFileAttribs = 0;
356     if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
357     if (pszComponent[0] == '.') pIDLData->u.file.uFileAttribs |=  FILE_ATTRIBUTE_HIDDEN;
358     memcpy(pIDLData->u.file.szNames, pszComponent, cComponentLen);
359
360     return pszComponent + cComponentLen;
361 }
362
363 /******************************************************************************
364  * UNIXFS_path_to_pidl [Internal]
365  *
366  * PARAMS
367  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
368  *  path        [I] An absolute unix or dos path or a path relativ to pUnixFolder
369  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
370  *  
371  * RETURNS
372  *  Success: TRUE
373  *  Failure: FALSE, invalid params or out of memory
374  *
375  * NOTES
376  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
377  */
378 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
379     LPITEMIDLIST pidl;
380     int cSubDirs, cPidlLen, cPathLen;
381     char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
382
383     TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
384    
385     if (!ppidl || !path)
386         return FALSE;
387
388     /* Build an absolute path and let pNextPathElement point to the interesting 
389      * relative sub-path. We need the absolute path to call 'stat', but the pidl
390      * will only contain the relative part.
391      */
392     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
393     {
394         /* Absolute dos path. Convert to unix */
395         if (!UNIXFS_get_unix_path(path, szCompletePath))
396             return FALSE;
397         pNextPathElement = szCompletePath;
398     } 
399     else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/')) 
400     {
401         /* Absolute unix path. Just convert to ANSI. */
402         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL); 
403         pNextPathElement = szCompletePath;
404     } 
405     else 
406     {
407         /* Relative dos or unix path. Concat with this folder's path */
408         int cBasePathLen = strlen(pUnixFolder->m_pszPath);
409         memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
410         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen, 
411                             FILENAME_MAX - cBasePathLen, NULL, NULL);
412         pNextPathElement = szCompletePath + cBasePathLen - 1;
413         
414         /* If in dos mode, replace '\' with '/' */
415         if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
416             char *pBackslash = strchr(pNextPathElement, '\\');
417             while (pBackslash) {
418                 *pBackslash = '/';
419                 pBackslash = strchr(pBackslash, '\\');
420             }
421         }
422     }
423
424     /* Special case for the root folder. */
425     if (!strcmp(szCompletePath, "/")) {
426         *ppidl = pidl = (LPITEMIDLIST)SHAlloc(sizeof(USHORT));
427         if (!pidl) return FALSE;
428         pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
429         return TRUE;
430     }
431     
432     /* Remove trailing slash, if present */
433     cPathLen = strlen(szCompletePath);
434     if (szCompletePath[cPathLen-1] == '/') 
435         szCompletePath[cPathLen-1] = '\0';
436
437     if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
438         ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
439         return FALSE;
440     }
441     
442     /* At this point, we have an absolute unix path in szCompletePath 
443      * and the relative portion of it in pNextPathElement. Both starting with '/'
444      * and _not_ terminated by a '/'. */
445     TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
446     
447     /* Count the number of sub-directories in the path */
448     cSubDirs = 0;
449     pSlash = pNextPathElement;
450     while (pSlash) {
451         cSubDirs++;
452         pSlash = strchr(pSlash+1, '/');
453     }
454   
455     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
456      * characters, which are not stored in the ITEMIDLIST. */
457     cPidlLen = strlen(pNextPathElement) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
458     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
459     if (!pidl) return FALSE;
460
461     /* Concatenate the SHITEMIDs of the sub-directories. */
462     while (*pNextPathElement) {
463         pSlash = strchr(pNextPathElement+1, '/');
464         if (pSlash) *pSlash = '\0';
465         pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
466         if (pSlash) *pSlash = '/';
467             
468         if (!pNextPathElement) {
469             SHFree(pidl);
470             return FALSE;
471         }
472         pidl = ILGetNext(pidl);
473     }
474     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
475
476     if ((int)pidl-(int)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */ 
477         ERR("Computed length of pidl incorrect. Please report.\n");
478     
479     return TRUE;
480 }
481
482 /******************************************************************************
483  * UnixFolder
484  *
485  * Class whose heap based instances represent unix filesystem directories.
486  */
487
488 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
489     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
490     
491     SHFree(pUnixFolder->m_pszPath);
492     ILFree(pUnixFolder->m_pidlLocation);
493     SHFree(pUnixFolder);
494 }
495
496 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
497     void **ppv) 
498 {
499     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
500         
501     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
502     
503     if (!ppv) return E_INVALIDARG;
504     
505     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
506         IsEqualIID(&IID_IShellFolder2, riid)) 
507     {
508         *ppv = &This->lpIShellFolder2Vtbl;
509     } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) || 
510                IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) 
511     {
512         *ppv = &This->lpIPersistFolder3Vtbl;
513     } else if (IsEqualIID(&IID_ISFHelper, riid)) {
514         *ppv = &This->lpISFHelperVtbl;
515     } else {
516         *ppv = NULL;
517         return E_NOINTERFACE;
518     }
519
520     IUnknown_AddRef((IUnknown*)*ppv);
521     return S_OK;
522 }
523
524 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
525     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
526
527     TRACE("(iface=%p)\n", iface);
528
529     return InterlockedIncrement(&This->m_cRef);
530 }
531
532 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
533     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
534     ULONG cRef;
535     
536     TRACE("(iface=%p)\n", iface);
537
538     cRef = InterlockedDecrement(&This->m_cRef);
539     
540     if (!cRef) 
541         UnixFolder_Destroy(This);
542
543     return cRef;
544 }
545
546 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
547     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
548     ULONG* pdwAttributes)
549 {
550     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
551     BOOL result;
552
553     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
554           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
555           pchEaten, ppidl, pdwAttributes);
556
557     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
558     if (result && pdwAttributes && *pdwAttributes)
559     {
560         IShellFolder *pParentSF;
561         LPCITEMIDLIST pidlLast;
562         HRESULT hr;
563         
564         hr = SHBindToParent(*ppidl, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
565         if (FAILED(hr)) return E_FAIL;
566         IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
567         IShellFolder_Release(pParentSF);
568     }
569
570     if (!result) TRACE("FAILED!\n");
571     return result ? S_OK : E_FAIL;
572 }
573
574 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
575
576 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
577     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
578 {
579     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
580     IUnknown *newIterator;
581     HRESULT hr;
582     
583     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
584             iface, hwndOwner, grfFlags, ppEnumIDList);
585
586     newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
587     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
588     IUnknown_Release(newIterator);
589     
590     return hr;
591 }
592
593 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
594     LPBC pbcReserved, REFIID riid, void** ppvOut)
595 {
596     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
597     IPersistFolder3 *persistFolder;
598     LPITEMIDLIST pidlSubFolder;
599     HRESULT hr;
600         
601     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
602             iface, pidl, pbcReserved, riid, ppvOut);
603
604     if (!pidl || !pidl->mkid.cb)
605         return E_INVALIDARG;
606     
607     if (This->m_dwPathMode == PATHMODE_DOS)
608         hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
609     else
610         hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
611         
612     if (!SUCCEEDED(hr)) return hr;
613     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
614     
615     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
616     IPersistFolder3_Initialize(persistFolder, pidlSubFolder);
617     IPersistFolder3_Release(persistFolder);
618     ILFree(pidlSubFolder);
619
620     return hr;
621 }
622
623 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
624     LPBC pbcReserved, REFIID riid, void** ppvObj)
625 {
626     TRACE("stub\n");
627     return E_NOTIMPL;
628 }
629
630 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
631     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
632 {
633     BOOL isEmpty1, isEmpty2;
634     HRESULT hr = E_FAIL;
635     LPITEMIDLIST firstpidl;
636     IShellFolder2 *psf;
637     int compare;
638
639     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
640     
641     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
642     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
643
644     if (isEmpty1 && isEmpty2) 
645         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
646     else if (isEmpty1)
647         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
648     else if (isEmpty2)
649         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
650
651     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
652         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
653     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
654         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
655
656     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
657                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
658                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
659     
660     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
661         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
662
663     if (pidl1->mkid.cb < pidl2->mkid.cb)
664         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
665     else if (pidl1->mkid.cb > pidl2->mkid.cb)
666         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
667
668     firstpidl = ILCloneFirst(pidl1);
669     pidl1 = ILGetNext(pidl1);
670     pidl2 = ILGetNext(pidl2);
671     
672     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
673     if (SUCCEEDED(hr)) {
674         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
675         IShellFolder2_Release(psf);
676     }
677
678     ILFree(firstpidl);
679     return hr;
680 }
681
682 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
683     REFIID riid, void** ppv)
684 {
685     HRESULT hr = E_INVALIDARG;
686         
687     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
688     
689     if (!ppv) return E_INVALIDARG;
690     *ppv = NULL;
691     
692     if (IsEqualIID(&IID_IShellView, riid)) {
693         LPSHELLVIEW pShellView;
694         
695         pShellView = IShellView_Constructor((IShellFolder*)iface);
696         if (pShellView) {
697             hr = IShellView_QueryInterface(pShellView, riid, ppv);
698             IShellView_Release(pShellView);
699         }
700     } 
701     
702     return hr;
703 }
704
705 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
706     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
707 {
708     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
709     HRESULT hr = S_OK;
710         
711     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
712  
713     if (!rgfInOut || (cidl && !apidl)) 
714         return E_INVALIDARG;
715     
716     if (cidl == 0) {
717         *rgfInOut &= This->m_dwAttributes;
718     } else {
719         char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
720         UINT i;
721
722         *rgfInOut &= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|
723                      SFGAO_FILESYSTEM;
724         lstrcpyA(szAbsolutePath, This->m_pszPath);
725         pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
726         for (i=0; i<cidl; i++) {
727             if ((*rgfInOut & SFGAO_FILESYSTEM) && !(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
728                 struct stat fileStat;
729                 char *pszName = _ILGetTextPointer(apidl[i]);
730                 if (!pszName) return E_INVALIDARG;
731                 lstrcpyA(pszRelativePath, pszName);
732                 if (stat(szAbsolutePath, &fileStat) || !UNIXFS_is_dos_device(&fileStat))
733                     *rgfInOut &= ~SFGAO_FILESYSTEM;
734             }
735             if (!_ILIsFolder(apidl[i])) 
736                 *rgfInOut &= ~(SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR);
737         }
738     }
739     
740     return hr;
741 }
742
743 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
744     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
745 {
746     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
747     
748     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
749         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
750
751     if (IsEqualIID(&IID_IContextMenu, riid)) {
752         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
753         return S_OK;
754     } else if (IsEqualIID(&IID_IDataObject, riid)) {
755         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
756         return S_OK;
757     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
758         LPITEMIDLIST pidl;
759         if (cidl != 1) return E_FAIL;
760         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
761         *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
762         SHFree(pidl);
763         return S_OK;
764     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
765         LPITEMIDLIST pidl;
766         if (cidl != 1) return E_FAIL;
767         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
768         *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
769         SHFree(pidl);
770         return S_OK;
771     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
772         FIXME("IDropTarget\n");
773         return E_FAIL;
774     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
775         FIXME("IShellLinkW\n");
776         return E_FAIL;
777     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
778         FIXME("IShellLinkA\n");
779         return E_FAIL;
780     } else {
781         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
782         return E_NOINTERFACE;
783     }
784 }
785
786 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
787     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
788 {
789     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
790     HRESULT hr = S_OK;    
791
792     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
793     
794     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
795         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
796     {
797         if (!pidl->mkid.cb) {
798             lpName->uType = STRRET_CSTR;
799             strcpy(lpName->u.cStr, This->m_pszPath);
800             if (This->m_dwPathMode == PATHMODE_DOS) {
801                 char path[MAX_PATH];
802                 GetFullPathNameA(lpName->u.cStr, MAX_PATH, path, NULL);
803                 PathRemoveBackslashA(path);
804                 strcpy(lpName->u.cStr, path);
805             }
806         } else {
807             IShellFolder *pSubFolder;
808             USHORT emptyIDL = 0;
809
810             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
811             if (!SUCCEEDED(hr)) return hr;
812        
813             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
814             IShellFolder_Release(pSubFolder);
815         }
816     } else {
817         char *pszFileName = _ILGetTextPointer(pidl);
818         lpName->uType = STRRET_CSTR;
819         strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
820     }
821
822     /* If in dos mode, do some post-processing on the path.
823      * (e.g. remove filename extension, if uFlags & SHGDN_FOREDITING) 
824      */
825     if (SUCCEEDED(hr) && This->m_dwPathMode == PATHMODE_DOS && !_ILIsFolder(pidl))
826         SHELL_FS_ProcessDisplayFilename(lpName->u.cStr, uFlags);
827     
828     TRACE("--> %s\n", lpName->u.cStr);
829     
830     return hr;
831 }
832
833 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd, 
834     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
835 {
836     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
837
838     char szSrc[FILENAME_MAX], szDest[FILENAME_MAX], szDosDest[MAX_PATH];
839     WCHAR wszDosDest[MAX_PATH];
840     int cBasePathLen = lstrlenA(This->m_pszPath);
841     struct stat statDest;
842     LPITEMIDLIST pidlSrc, pidlDest;
843    
844     TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
845           iface, hwnd, pidl, debugstr_w(lpszName), uFlags, ppidlOut); 
846
847     /* pidl has to contain a single non-empty SHITEMID */
848     if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
849         return E_INVALIDARG;
850    
851     if (ppidlOut)
852         *ppidlOut = NULL;
853     
854     /* build source path */
855     memcpy(szSrc, This->m_pszPath, cBasePathLen);
856     lstrcpyA(szSrc+cBasePathLen, _ILGetTextPointer(pidl));
857
858     /* build destination path */
859     if (uFlags & SHGDN_FORPARSING) { /* absolute path in lpszName */
860         WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest, FILENAME_MAX, NULL, NULL);
861     } else {
862         WCHAR wszSrcRelative[MAX_PATH];
863         memcpy(szDest, This->m_pszPath, cBasePathLen);
864         WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest+cBasePathLen, 
865                             FILENAME_MAX-cBasePathLen, NULL, NULL);
866
867         /* uFlags is SHGDN_FOREDITING of SHGDN_FORADDRESSBAR. If the filename's 
868          * extension is hidden to the user, we have to append it. */
869         if (_ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) && 
870             SHELL_FS_HideExtension(wszSrcRelative))
871         {
872             char *pszExt = PathFindExtensionA(_ILGetTextPointer(pidl));
873             lstrcatA(szDest, pszExt);
874         }
875     } 
876
877     TRACE("src=%s dest=%s\n", szSrc, szDest);
878
879     /* Fail, if destination does already exist */
880     if (!stat(szDest, &statDest)) 
881         return E_FAIL;
882
883     /* Rename the file */
884     if (rename(szSrc, szDest)) 
885         return E_FAIL;
886     
887     /* Build a pidl for the path of the renamed file */
888     if (!GetFullPathNameA(szDest, MAX_PATH, szDosDest, NULL) ||
889         !MultiByteToWideChar(CP_UNIXCP, 0, szDosDest, -1, wszDosDest, MAX_PATH) ||
890         !UNIXFS_path_to_pidl(This, wszDosDest, &pidlDest))
891     {
892         rename(szDest, szSrc); /* Undo the renaming */
893         return E_FAIL;
894     }
895     
896     /* Inform the shell */
897     pidlSrc = ILCombine(This->m_pidlLocation, pidl);
898     if (_ILIsFolder(ILFindLastID(pidlDest))) 
899         SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
900     else 
901         SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
902     ILFree(pidlSrc);
903     ILFree(pidlDest);
904     
905     if (ppidlOut) 
906         _ILCreateFromPathW(wszDosDest, ppidlOut);
907     
908     return S_OK;
909 }
910
911 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
912     IEnumExtraSearch **ppEnum) 
913 {
914     TRACE("stub\n");
915     return E_NOTIMPL;
916 }
917
918 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
919     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
920 {
921     TRACE("stub\n");
922     return E_NOTIMPL;
923 }
924
925 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
926     UINT iColumn, SHCOLSTATEF *pcsFlags)
927 {
928     TRACE("stub\n");
929     return E_NOTIMPL;
930 }
931
932 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
933     GUID *pguid)
934 {
935     TRACE("stub\n");
936     return E_NOTIMPL;
937 }
938
939 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
940     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
941 {
942     TRACE("stub\n");
943     return E_NOTIMPL;
944 }
945
946 #define SHELLVIEWCOLUMNS 7 
947
948 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
949     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
950 {
951     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
952     HRESULT hr = E_FAIL;
953     struct passwd *pPasswd;
954     struct group *pGroup;
955     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
956         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
957         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
958         {IDS_SHV_COLUMN3,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
959         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
960         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
961         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
962         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
963     };
964
965     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
966     
967     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
968         return E_INVALIDARG;
969
970     if (!pidl) {
971         psd->fmt = SFHeader[iColumn].fmt;
972         psd->cxChar = SFHeader[iColumn].cxChar;
973         psd->str.uType = STRRET_CSTR;
974         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
975         return S_OK;
976     } else {
977         struct stat statItem;
978         if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
979             char szPath[FILENAME_MAX], *pszFile = _ILGetTextPointer(pidl);
980             if (!pszFile) 
981                 return E_INVALIDARG;
982             lstrcpyA(szPath, This->m_pszPath);
983             lstrcatA(szPath, pszFile);
984             if (stat(szPath, &statItem)) 
985                 return E_INVALIDARG;
986         }
987         psd->str.u.cStr[0] = '\0';
988         psd->str.uType = STRRET_CSTR;
989         switch (iColumn) {
990             case 0:
991                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
992                 break;
993             case 1:
994                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
995                 break;
996             case 2:
997                 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
998                 break;
999             case 3:
1000                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1001                 break;
1002             case 4:
1003                 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1004                 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1005                 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1006                 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1007                 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1008                 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1009                 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1010                 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1011                 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1012                 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1013                 psd->str.u.cStr[10] = '\0';
1014                 break;
1015             case 5:
1016                 pPasswd = getpwuid(statItem.st_uid);
1017                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1018                 break;
1019             case 6:
1020                 pGroup = getgrgid(statItem.st_gid);
1021                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1022                 break;
1023         }
1024     }
1025     
1026     return hr;
1027 }
1028
1029 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1030     SHCOLUMNID *pscid) 
1031 {
1032     TRACE("stub\n");
1033     return E_NOTIMPL;
1034 }
1035
1036 /* VTable for UnixFolder's IShellFolder2 interface.
1037  */
1038 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1039     UnixFolder_IShellFolder2_QueryInterface,
1040     UnixFolder_IShellFolder2_AddRef,
1041     UnixFolder_IShellFolder2_Release,
1042     UnixFolder_IShellFolder2_ParseDisplayName,
1043     UnixFolder_IShellFolder2_EnumObjects,
1044     UnixFolder_IShellFolder2_BindToObject,
1045     UnixFolder_IShellFolder2_BindToStorage,
1046     UnixFolder_IShellFolder2_CompareIDs,
1047     UnixFolder_IShellFolder2_CreateViewObject,
1048     UnixFolder_IShellFolder2_GetAttributesOf,
1049     UnixFolder_IShellFolder2_GetUIObjectOf,
1050     UnixFolder_IShellFolder2_GetDisplayNameOf,
1051     UnixFolder_IShellFolder2_SetNameOf,
1052     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1053     UnixFolder_IShellFolder2_EnumSearches,
1054     UnixFolder_IShellFolder2_GetDefaultColumn,
1055     UnixFolder_IShellFolder2_GetDefaultColumnState,
1056     UnixFolder_IShellFolder2_GetDetailsEx,
1057     UnixFolder_IShellFolder2_GetDetailsOf,
1058     UnixFolder_IShellFolder2_MapColumnToSCID
1059 };
1060
1061 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* This, REFIID riid, 
1062     void** ppvObject)
1063 {
1064     return UnixFolder_IShellFolder2_QueryInterface(
1065                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This), riid, ppvObject);
1066 }
1067
1068 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* This)
1069 {
1070     return UnixFolder_IShellFolder2_AddRef(
1071                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1072 }
1073
1074 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* This)
1075 {
1076     return UnixFolder_IShellFolder2_Release(
1077                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1078 }
1079
1080 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* This, CLSID* pClassID)
1081 {
1082     TRACE("stub\n");
1083     return E_NOTIMPL;
1084 }
1085
1086 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1087 {
1088     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1089     struct stat statPrefix;
1090     LPCITEMIDLIST current = pidl, root;
1091     DWORD dwPathLen;
1092     char *pNextDir, szBasePath[FILENAME_MAX] = "/";
1093     
1094     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1095
1096     /* Find the UnixFolderClass root */
1097     while (current->mkid.cb) {
1098         if (_ILIsSpecialFolder(current) && 
1099             (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
1100              IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current))))
1101         {
1102             break;
1103         }
1104         current = ILGetNext(current);
1105     }
1106
1107     if (current && current->mkid.cb) {
1108         root = current = ILGetNext(current);
1109         dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
1110     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1111         /* Path rooted at Desktop */
1112         WCHAR wszDesktopPath[MAX_PATH];
1113         if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
1114            return E_FAIL;
1115         PathAddBackslashW(wszDesktopPath);
1116         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1117             return E_FAIL;
1118         dwPathLen = strlen(szBasePath) + 1;
1119         root = current = pidl;
1120     } else {
1121         ERR("Unknown pidl type!\n");
1122         pdump(pidl);
1123         return E_INVALIDARG;
1124     }
1125     
1126     /* Determine the path's length bytes */
1127     while (current && current->mkid.cb) {
1128         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
1129         current = ILGetNext(current);
1130     };
1131
1132     /* Build the path */
1133     This->m_dwAttributes = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
1134     This->m_pidlLocation = ILClone(pidl);
1135     This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
1136     if (!This->m_pszPath || !This->m_pidlLocation) {
1137         WARN("SHAlloc failed!\n");
1138         return E_FAIL;
1139     }
1140     current = root;
1141     strcpy(pNextDir, szBasePath);
1142     pNextDir += strlen(szBasePath);
1143     if (This->m_dwPathMode == PATHMODE_UNIX)
1144         This->m_dwAttributes |= SFGAO_FILESYSTEM;
1145     if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1146         *pNextDir = '\0';
1147         if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1148             This->m_dwAttributes |= SFGAO_FILESYSTEM;
1149     }
1150     while (current && current->mkid.cb) {
1151         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
1152         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
1153         if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1154             *pNextDir = '\0';
1155             if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1156                 This->m_dwAttributes |= SFGAO_FILESYSTEM;
1157         }
1158         *pNextDir++ = '/';
1159         current = ILGetNext(current);
1160     }
1161     *pNextDir='\0';
1162  
1163     return S_OK;
1164 }
1165
1166 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1167 {
1168     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1169     
1170     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1171
1172     if (!ppidl)
1173         return E_POINTER;
1174     *ppidl = ILClone (This->m_pidlLocation);
1175     return S_OK;
1176 }
1177
1178 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc, 
1179     LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1180 {
1181     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1182     WCHAR wszTargetDosPath[MAX_PATH];
1183     char szTargetPath[FILENAME_MAX] = "";
1184     
1185     TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1186
1187     /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1188     if (!ppfti) 
1189         return IPersistFolder3_Initialize(iface, pidlRoot);
1190
1191     if (ppfti->csidl != -1) {
1192         if (SUCCEEDED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath))) {
1193             UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1194         }
1195     } else if (*ppfti->szTargetParsingName) {
1196         UNIXFS_get_unix_path(ppfti->szTargetParsingName, szTargetPath);
1197     } else if (ppfti->pidlTargetFolder) {
1198         if (SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath)) {
1199             UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1200         }
1201     }
1202
1203     This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1204     if (!This->m_pszPath) 
1205         return E_FAIL;
1206     lstrcpyA(This->m_pszPath, szTargetPath);
1207     This->m_pidlLocation = ILClone(pidlRoot);
1208     This->m_dwAttributes = (ppfti->dwAttributes == -1) ? ppfti->dwAttributes :
1209         SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1210
1211     return S_OK;
1212 }
1213
1214 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface, 
1215     PERSIST_FOLDER_TARGET_INFO *ppfti)
1216 {
1217     FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1218     return E_NOTIMPL;
1219 }
1220
1221 /* VTable for UnixFolder's IPersistFolder interface.
1222  */
1223 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1224     UnixFolder_IPersistFolder3_QueryInterface,
1225     UnixFolder_IPersistFolder3_AddRef,
1226     UnixFolder_IPersistFolder3_Release,
1227     UnixFolder_IPersistFolder3_GetClassID,
1228     UnixFolder_IPersistFolder3_Initialize,
1229     UnixFolder_IPersistFolder3_GetCurFolder,
1230     UnixFolder_IPersistFolder3_InitializeEx,
1231     UnixFolder_IPersistFolder3_GetFolderTargetInfo
1232 };
1233
1234 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid, 
1235     void** ppvObject)
1236 {
1237     return UnixFolder_IShellFolder2_QueryInterface(
1238                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface), riid, ppvObject);
1239 }
1240
1241 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1242 {
1243     return UnixFolder_IShellFolder2_AddRef(
1244                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1245 }
1246
1247 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1248 {
1249     return UnixFolder_IShellFolder2_Release(
1250                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1251 }
1252
1253 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1254 {
1255     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1256     IEnumIDList *pEnum;
1257     HRESULT hr;
1258     LPITEMIDLIST pidlElem;
1259     DWORD dwFetched;
1260     int i;
1261     static const char szNewFolder[] = "New Folder";
1262
1263     TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1264     
1265     if (uLen < sizeof(szNewFolder)+3)
1266         return E_INVALIDARG;
1267
1268     hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1269                                    SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1270     if (SUCCEEDED(hr)) {
1271         lstrcpyA(lpName, szNewFolder);
1272         IEnumIDList_Reset(pEnum);
1273         i = 2;
1274         while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1275             if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1276                 IEnumIDList_Reset(pEnum);
1277                 sprintf(lpName, "%s %d", szNewFolder, i++);
1278                 if (i > 99) {
1279                     hr = E_FAIL;
1280                     break;
1281                 }
1282             }
1283         }
1284         IEnumIDList_Release(pEnum);
1285     }
1286     return hr;
1287 }
1288
1289 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName, 
1290     LPITEMIDLIST* ppidlOut)
1291 {
1292     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1293     char szNewDir[FILENAME_MAX];
1294
1295     TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1296
1297     if (ppidlOut)
1298         *ppidlOut = NULL;
1299     
1300     lstrcpyA(szNewDir, This->m_pszPath);
1301     lstrcatA(szNewDir, pszName);
1302
1303     if (mkdir(szNewDir, 0755)) {
1304         char szMessage[256 + FILENAME_MAX];
1305         char szCaption[256];
1306
1307         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1308         sprintf(szMessage, szCaption, szNewDir);
1309         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1310         MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1311
1312         return E_FAIL;
1313     } else {
1314         LPITEMIDLIST pidlRelative;
1315         WCHAR wszName[MAX_PATH];
1316
1317         /* Inform the shell */
1318         MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1319         if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1320             LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1321             if (ppidlOut)
1322                 *ppidlOut = pidlRelative;
1323             else
1324                 ILFree(pidlRelative);
1325             SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1326             ILFree(pidlAbsolute);
1327         }
1328         return S_OK;
1329     }
1330 }
1331
1332 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl, 
1333     LPCITEMIDLIST* apidl)
1334 {
1335     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1336     char szAbsolute[FILENAME_MAX], *pszRelative;
1337     LPITEMIDLIST pidlAbsolute;
1338     HRESULT hr = S_OK;
1339     UINT i;
1340     
1341     TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1342
1343     lstrcpyA(szAbsolute, This->m_pszPath);
1344     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1345     
1346     for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1347         lstrcpyA(pszRelative, _ILGetTextPointer(apidl[i]));
1348         pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1349         if (_ILIsFolder(apidl[i])) {
1350             if (rmdir(szAbsolute)) {
1351                 hr = E_FAIL;
1352             } else {
1353                 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1354             }
1355         } else if (_ILIsValue(apidl[i])) {
1356             if (unlink(szAbsolute)) {
1357                 hr = E_FAIL;
1358             } else {
1359                 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1360             }
1361         }
1362         ILFree(pidlAbsolute);
1363     }
1364         
1365     return hr;
1366 }
1367
1368 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom, 
1369     UINT cidl, LPCITEMIDLIST *apidl)
1370 {
1371     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1372     DWORD dwAttributes;
1373     UINT i;
1374     HRESULT hr;
1375     char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1376     
1377     TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1378
1379     if (!psfFrom || !cidl || !apidl)
1380         return E_INVALIDARG;
1381
1382     /* All source items have to be filesystem items. */
1383     dwAttributes = SFGAO_FILESYSTEM;
1384     hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1385     if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) 
1386         return E_INVALIDARG;
1387
1388     lstrcpyA(szAbsoluteDst, This->m_pszPath);
1389     pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1390     
1391     for (i=0; i<cidl; i++) {
1392         WCHAR wszSrc[MAX_PATH];
1393         char szSrc[FILENAME_MAX];
1394         STRRET strret;
1395
1396         /* Build the unix path of the current source item. */
1397         if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1398             return E_FAIL;
1399         if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1400             return E_FAIL;
1401         if (!UNIXFS_get_unix_path(wszSrc, szSrc)) 
1402             return E_FAIL;
1403
1404         /* Build the unix path of the current destination item */
1405         lstrcpyA(pszRelativeDst, _ILGetTextPointer(apidl[i]));
1406
1407         FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1408     }
1409     return S_OK;
1410 }
1411
1412 /* VTable for UnixFolder's ISFHelper interface
1413  */
1414 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1415     UnixFolder_ISFHelper_QueryInterface,
1416     UnixFolder_ISFHelper_AddRef,
1417     UnixFolder_ISFHelper_Release,
1418     UnixFolder_ISFHelper_GetUniqueName,
1419     UnixFolder_ISFHelper_AddFolder,
1420     UnixFolder_ISFHelper_DeleteItems,
1421     UnixFolder_ISFHelper_CopyItems
1422 };
1423
1424 /******************************************************************************
1425  * Unix[Dos]Folder_Constructor [Internal]
1426  *
1427  * PARAMS
1428  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1429  *  riid      [I] Interface asked for by the client.
1430  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1431  *
1432  * NOTES
1433  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1434  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1435  *  compatible signature.
1436  *
1437  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1438  *  means that paths are converted from dos to unix and back at the interfaces.
1439  */
1440 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode) {
1441     HRESULT hr = E_FAIL;
1442     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1443     
1444     if(pUnixFolder) {
1445         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1446         pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
1447         pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
1448         pUnixFolder->m_cRef = 0;
1449         pUnixFolder->m_pszPath = NULL;
1450         pUnixFolder->m_dwPathMode = dwPathMode;
1451
1452         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1453         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1454         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1455     }
1456     return hr;
1457 }
1458
1459 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1460     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1461     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX);
1462 }
1463
1464 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1465     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1466     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS);
1467 }
1468
1469 /******************************************************************************
1470  * UnixSubFolderIterator
1471  *
1472  * Class whose heap based objects represent iterators over the sub-directories
1473  * of a given UnixFolder object. 
1474  */
1475
1476 /* UnixSubFolderIterator object layout and typedef.
1477  */
1478 typedef struct _UnixSubFolderIterator {
1479     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1480     LONG m_cRef;
1481     SHCONTF m_fFilter;
1482     DIR *m_dirFolder;
1483     char m_szFolder[FILENAME_MAX];
1484 } UnixSubFolderIterator;
1485
1486 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1487     TRACE("(iterator=%p)\n", iterator);
1488
1489     if (iterator->m_dirFolder)
1490         closedir(iterator->m_dirFolder);
1491     SHFree(iterator);
1492 }
1493
1494 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1495     REFIID riid, void** ppv)
1496 {
1497     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1498     
1499     if (!ppv) return E_INVALIDARG;
1500     
1501     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1502         *ppv = iface;
1503     } else {
1504         *ppv = NULL;
1505         return E_NOINTERFACE;
1506     }
1507
1508     IEnumIDList_AddRef(iface);
1509     return S_OK;
1510 }
1511                             
1512 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1513 {
1514     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1515
1516     TRACE("(iface=%p)\n", iface);
1517    
1518     return InterlockedIncrement(&This->m_cRef);
1519 }
1520
1521 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1522 {
1523     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1524     ULONG cRef;
1525     
1526     TRACE("(iface=%p)\n", iface);
1527
1528     cRef = InterlockedDecrement(&This->m_cRef);
1529     
1530     if (!cRef) 
1531         UnixSubFolderIterator_Destroy(This);
1532
1533     return cRef;
1534 }
1535
1536 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1537     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1538 {
1539     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1540     ULONG i = 0;
1541
1542     /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
1543     if (This->m_dirFolder) {
1544         char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
1545         struct dirent *pDirEntry;
1546
1547         while (i < celt) {
1548             pDirEntry = readdir(This->m_dirFolder);
1549             if (!pDirEntry) break; /* No more entries */
1550             if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
1551
1552             /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
1553              * and see if it passes the filter. 
1554              */
1555             lstrcpyA(pszRelativePath, pDirEntry->d_name);
1556             rgelt[i] = (LPITEMIDLIST)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(lstrlenA(pszRelativePath))+sizeof(USHORT));
1557             if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
1558                 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter)) 
1559             {
1560                 SHFree(rgelt[i]);
1561                 continue;
1562             }
1563             memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
1564             i++;
1565         }
1566         *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
1567     }
1568     
1569     if (pceltFetched)
1570         *pceltFetched = i;
1571
1572     return (i == 0) ? S_FALSE : S_OK;
1573 }
1574     
1575 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1576 {
1577     LPITEMIDLIST *apidl;
1578     ULONG cFetched;
1579     HRESULT hr;
1580     
1581     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1582
1583     /* Call IEnumIDList::Next and delete the resulting pidls. */
1584     apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
1585     hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
1586     if (SUCCEEDED(hr))
1587         while (cFetched--) 
1588             SHFree(apidl[cFetched]);
1589     SHFree(apidl);
1590
1591     return hr;
1592 }
1593
1594 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1595 {
1596     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1597         
1598     TRACE("(iface=%p)\n", iface);
1599
1600     if (This->m_dirFolder)
1601         rewinddir(This->m_dirFolder);
1602     
1603     return S_OK;
1604 }
1605
1606 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1607     IEnumIDList** ppenum)
1608 {
1609     TRACE("stub\n");
1610     return E_NOTIMPL;
1611 }
1612
1613 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1614  */
1615 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1616     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1617     UnixSubFolderIterator_IEnumIDList_AddRef,
1618     UnixSubFolderIterator_IEnumIDList_Release,
1619     UnixSubFolderIterator_IEnumIDList_Next,
1620     UnixSubFolderIterator_IEnumIDList_Skip,
1621     UnixSubFolderIterator_IEnumIDList_Reset,
1622     UnixSubFolderIterator_IEnumIDList_Clone
1623 };
1624
1625 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1626     UnixSubFolderIterator *iterator;
1627
1628     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1629     
1630     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1631     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1632     iterator->m_cRef = 0;
1633     iterator->m_fFilter = fFilter;
1634     iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
1635     lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
1636
1637     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1638     
1639     return (IUnknown*)iterator;
1640 }