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