Moved mciavi32 to the top-level dlls directory.
[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 WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
598     LPBC pbcReserved, REFIID riid, void** ppvOut)
599 {
600     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
601     IPersistFolder3 *persistFolder;
602     LPITEMIDLIST pidlSubFolder;
603     HRESULT hr;
604         
605     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
606             iface, pidl, pbcReserved, riid, ppvOut);
607
608     if (!pidl || !pidl->mkid.cb)
609         return E_INVALIDARG;
610     
611     if (This->m_dwPathMode == PATHMODE_DOS)
612         hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
613     else
614         hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder3, (void**)&persistFolder);
615         
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) && 
1113             (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
1114              IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current))))
1115         {
1116             break;
1117         }
1118         current = ILGetNext(current);
1119     }
1120
1121     if (current && current->mkid.cb) {
1122         root = current = ILGetNext(current);
1123         dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
1124     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1125         /* Path rooted at Desktop */
1126         WCHAR wszDesktopPath[MAX_PATH];
1127         if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
1128            return E_FAIL;
1129         PathAddBackslashW(wszDesktopPath);
1130         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1131             return E_FAIL;
1132         dwPathLen = strlen(szBasePath) + 1;
1133         root = current = pidl;
1134     } else {
1135         ERR("Unknown pidl type!\n");
1136         pdump(pidl);
1137         return E_INVALIDARG;
1138     }
1139     
1140     /* Determine the path's length bytes */
1141     while (current && current->mkid.cb) {
1142         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
1143         current = ILGetNext(current);
1144     };
1145
1146     /* Build the path */
1147     This->m_dwAttributes = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
1148     This->m_pidlLocation = ILClone(pidl);
1149     This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
1150     if (!This->m_pszPath || !This->m_pidlLocation) {
1151         WARN("SHAlloc failed!\n");
1152         return E_FAIL;
1153     }
1154     current = root;
1155     strcpy(pNextDir, szBasePath);
1156     pNextDir += strlen(szBasePath);
1157     if (This->m_dwPathMode == PATHMODE_UNIX)
1158         This->m_dwAttributes |= SFGAO_FILESYSTEM;
1159     if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1160         *pNextDir = '\0';
1161         if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1162             This->m_dwAttributes |= SFGAO_FILESYSTEM;
1163     }
1164     while (current && current->mkid.cb) {
1165         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
1166         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
1167         if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1168             *pNextDir = '\0';
1169             if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
1170                 This->m_dwAttributes |= SFGAO_FILESYSTEM;
1171         }
1172         *pNextDir++ = '/';
1173         current = ILGetNext(current);
1174     }
1175     *pNextDir='\0';
1176  
1177     return S_OK;
1178 }
1179
1180 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1181 {
1182     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1183     
1184     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1185
1186     if (!ppidl)
1187         return E_POINTER;
1188     *ppidl = ILClone (This->m_pidlLocation);
1189     return S_OK;
1190 }
1191
1192 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc, 
1193     LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1194 {
1195     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1196     WCHAR wszTargetDosPath[MAX_PATH];
1197     char szTargetPath[FILENAME_MAX] = "";
1198     
1199     TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1200
1201     /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1202     if (!ppfti) 
1203         return IPersistFolder3_Initialize(iface, pidlRoot);
1204
1205     if (ppfti->csidl != -1) {
1206         if (SUCCEEDED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath))) {
1207             UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1208         }
1209     } else if (*ppfti->szTargetParsingName) {
1210         UNIXFS_get_unix_path(ppfti->szTargetParsingName, szTargetPath);
1211     } else if (ppfti->pidlTargetFolder) {
1212         if (SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath)) {
1213             UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath);
1214         }
1215     }
1216
1217     This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1218     if (!This->m_pszPath) 
1219         return E_FAIL;
1220     lstrcpyA(This->m_pszPath, szTargetPath);
1221     This->m_pidlLocation = ILClone(pidlRoot);
1222     This->m_dwAttributes = (ppfti->dwAttributes == -1) ? ppfti->dwAttributes :
1223         SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM;
1224
1225     return S_OK;
1226 }
1227
1228 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface, 
1229     PERSIST_FOLDER_TARGET_INFO *ppfti)
1230 {
1231     FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1232     return E_NOTIMPL;
1233 }
1234
1235 /* VTable for UnixFolder's IPersistFolder interface.
1236  */
1237 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1238     UnixFolder_IPersistFolder3_QueryInterface,
1239     UnixFolder_IPersistFolder3_AddRef,
1240     UnixFolder_IPersistFolder3_Release,
1241     UnixFolder_IPersistFolder3_GetClassID,
1242     UnixFolder_IPersistFolder3_Initialize,
1243     UnixFolder_IPersistFolder3_GetCurFolder,
1244     UnixFolder_IPersistFolder3_InitializeEx,
1245     UnixFolder_IPersistFolder3_GetFolderTargetInfo
1246 };
1247
1248 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* This,
1249     REFIID riid, void** ppvObject)
1250 {
1251     return UnixFolder_IShellFolder2_QueryInterface(
1252                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This), riid, ppvObject);
1253 }
1254
1255 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* This)
1256 {
1257     return UnixFolder_IShellFolder2_AddRef(
1258                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1259 }
1260
1261 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* This)
1262 {
1263     return UnixFolder_IShellFolder2_Release(
1264                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1265 }
1266
1267 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface, 
1268     CLSID* pClassID)
1269 {
1270     return UnixFolder_IPersistFolder3_GetClassID(
1271         (IPersistFolder3*)&ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)->lpIPersistFolder3Vtbl,
1272         pClassID);
1273 }
1274
1275 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1276 {
1277     TRACE("() stub\n");
1278     return E_NOTIMPL;
1279 }
1280
1281 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface, 
1282     IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1283 {
1284     TRACE("() stub\n");
1285     return E_NOTIMPL;
1286 }
1287
1288 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1289     IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1290 {
1291     TRACE("() stub\n");
1292     return E_NOTIMPL;
1293 }
1294
1295 /* VTable for UnixFolder's IPersistPropertyBag interface.
1296  */
1297 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1298     UnixFolder_IPersistPropertyBag_QueryInterface,
1299     UnixFolder_IPersistPropertyBag_AddRef,
1300     UnixFolder_IPersistPropertyBag_Release,
1301     UnixFolder_IPersistPropertyBag_GetClassID,
1302     UnixFolder_IPersistPropertyBag_InitNew,
1303     UnixFolder_IPersistPropertyBag_Load,
1304     UnixFolder_IPersistPropertyBag_Save
1305 };
1306
1307 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid, 
1308     void** ppvObject)
1309 {
1310     return UnixFolder_IShellFolder2_QueryInterface(
1311                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface), riid, ppvObject);
1312 }
1313
1314 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1315 {
1316     return UnixFolder_IShellFolder2_AddRef(
1317                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1318 }
1319
1320 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1321 {
1322     return UnixFolder_IShellFolder2_Release(
1323                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1324 }
1325
1326 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1327 {
1328     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1329     IEnumIDList *pEnum;
1330     HRESULT hr;
1331     LPITEMIDLIST pidlElem;
1332     DWORD dwFetched;
1333     int i;
1334     static const char szNewFolder[] = "New Folder";
1335
1336     TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1337     
1338     if (uLen < sizeof(szNewFolder)+3)
1339         return E_INVALIDARG;
1340
1341     hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1342                                    SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1343     if (SUCCEEDED(hr)) {
1344         lstrcpyA(lpName, szNewFolder);
1345         IEnumIDList_Reset(pEnum);
1346         i = 2;
1347         while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1348             if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1349                 IEnumIDList_Reset(pEnum);
1350                 sprintf(lpName, "%s %d", szNewFolder, i++);
1351                 if (i > 99) {
1352                     hr = E_FAIL;
1353                     break;
1354                 }
1355             }
1356         }
1357         IEnumIDList_Release(pEnum);
1358     }
1359     return hr;
1360 }
1361
1362 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName, 
1363     LPITEMIDLIST* ppidlOut)
1364 {
1365     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1366     char szNewDir[FILENAME_MAX];
1367
1368     TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1369
1370     if (ppidlOut)
1371         *ppidlOut = NULL;
1372     
1373     lstrcpyA(szNewDir, This->m_pszPath);
1374     lstrcatA(szNewDir, pszName);
1375
1376     if (mkdir(szNewDir, 0755)) {
1377         char szMessage[256 + FILENAME_MAX];
1378         char szCaption[256];
1379
1380         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1381         sprintf(szMessage, szCaption, szNewDir);
1382         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1383         MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1384
1385         return E_FAIL;
1386     } else {
1387         LPITEMIDLIST pidlRelative;
1388         WCHAR wszName[MAX_PATH];
1389
1390         /* Inform the shell */
1391         MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1392         if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1393             LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1394             if (ppidlOut)
1395                 *ppidlOut = pidlRelative;
1396             else
1397                 ILFree(pidlRelative);
1398             SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1399             ILFree(pidlAbsolute);
1400         }
1401         return S_OK;
1402     }
1403 }
1404
1405 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl, 
1406     LPCITEMIDLIST* apidl)
1407 {
1408     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1409     char szAbsolute[FILENAME_MAX], *pszRelative;
1410     LPITEMIDLIST pidlAbsolute;
1411     HRESULT hr = S_OK;
1412     UINT i;
1413     
1414     TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1415
1416     lstrcpyA(szAbsolute, This->m_pszPath);
1417     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1418     
1419     for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1420         lstrcpyA(pszRelative, _ILGetTextPointer(apidl[i]));
1421         pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1422         if (_ILIsFolder(apidl[i])) {
1423             if (rmdir(szAbsolute)) {
1424                 hr = E_FAIL;
1425             } else {
1426                 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1427             }
1428         } else if (_ILIsValue(apidl[i])) {
1429             if (unlink(szAbsolute)) {
1430                 hr = E_FAIL;
1431             } else {
1432                 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1433             }
1434         }
1435         ILFree(pidlAbsolute);
1436     }
1437         
1438     return hr;
1439 }
1440
1441 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom, 
1442     UINT cidl, LPCITEMIDLIST *apidl)
1443 {
1444     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1445     DWORD dwAttributes;
1446     UINT i;
1447     HRESULT hr;
1448     char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1449     
1450     TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1451
1452     if (!psfFrom || !cidl || !apidl)
1453         return E_INVALIDARG;
1454
1455     /* All source items have to be filesystem items. */
1456     dwAttributes = SFGAO_FILESYSTEM;
1457     hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1458     if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) 
1459         return E_INVALIDARG;
1460
1461     lstrcpyA(szAbsoluteDst, This->m_pszPath);
1462     pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1463     
1464     for (i=0; i<cidl; i++) {
1465         WCHAR wszSrc[MAX_PATH];
1466         char szSrc[FILENAME_MAX];
1467         STRRET strret;
1468
1469         /* Build the unix path of the current source item. */
1470         if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1471             return E_FAIL;
1472         if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1473             return E_FAIL;
1474         if (!UNIXFS_get_unix_path(wszSrc, szSrc)) 
1475             return E_FAIL;
1476
1477         /* Build the unix path of the current destination item */
1478         lstrcpyA(pszRelativeDst, _ILGetTextPointer(apidl[i]));
1479
1480         FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1481     }
1482     return S_OK;
1483 }
1484
1485 /* VTable for UnixFolder's ISFHelper interface
1486  */
1487 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1488     UnixFolder_ISFHelper_QueryInterface,
1489     UnixFolder_ISFHelper_AddRef,
1490     UnixFolder_ISFHelper_Release,
1491     UnixFolder_ISFHelper_GetUniqueName,
1492     UnixFolder_ISFHelper_AddFolder,
1493     UnixFolder_ISFHelper_DeleteItems,
1494     UnixFolder_ISFHelper_CopyItems
1495 };
1496
1497 /******************************************************************************
1498  * Unix[Dos]Folder_Constructor [Internal]
1499  *
1500  * PARAMS
1501  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1502  *  riid      [I] Interface asked for by the client.
1503  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1504  *
1505  * NOTES
1506  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1507  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1508  *  compatible signature.
1509  *
1510  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1511  *  means that paths are converted from dos to unix and back at the interfaces.
1512  */
1513 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode, 
1514     const CLSID *pCLSID) 
1515 {
1516     HRESULT hr = E_FAIL;
1517     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1518     
1519     if(pUnixFolder) {
1520         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1521         pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
1522         pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
1523         pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
1524         pUnixFolder->m_cRef = 0;
1525         pUnixFolder->m_pszPath = NULL;
1526         pUnixFolder->m_pidlLocation = NULL;
1527         pUnixFolder->m_dwPathMode = dwPathMode;
1528         pUnixFolder->m_dwAttributes = 0;
1529         pUnixFolder->m_pCLSID = pCLSID;
1530
1531         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1532         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1533         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1534     }
1535     return hr;
1536 }
1537
1538 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1539     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1540     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX, &CLSID_UnixFolder);
1541 }
1542
1543 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1544     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1545     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS, &CLSID_UnixDosFolder);
1546 }
1547
1548 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1549     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1550     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS, &CLSID_FolderShortcut);
1551 }
1552
1553 /******************************************************************************
1554  * UnixSubFolderIterator
1555  *
1556  * Class whose heap based objects represent iterators over the sub-directories
1557  * of a given UnixFolder object. 
1558  */
1559
1560 /* UnixSubFolderIterator object layout and typedef.
1561  */
1562 typedef struct _UnixSubFolderIterator {
1563     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1564     LONG m_cRef;
1565     SHCONTF m_fFilter;
1566     DIR *m_dirFolder;
1567     char m_szFolder[FILENAME_MAX];
1568 } UnixSubFolderIterator;
1569
1570 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1571     TRACE("(iterator=%p)\n", iterator);
1572
1573     if (iterator->m_dirFolder)
1574         closedir(iterator->m_dirFolder);
1575     SHFree(iterator);
1576 }
1577
1578 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1579     REFIID riid, void** ppv)
1580 {
1581     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1582     
1583     if (!ppv) return E_INVALIDARG;
1584     
1585     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1586         *ppv = iface;
1587     } else {
1588         *ppv = NULL;
1589         return E_NOINTERFACE;
1590     }
1591
1592     IEnumIDList_AddRef(iface);
1593     return S_OK;
1594 }
1595                             
1596 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1597 {
1598     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1599
1600     TRACE("(iface=%p)\n", iface);
1601    
1602     return InterlockedIncrement(&This->m_cRef);
1603 }
1604
1605 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1606 {
1607     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1608     ULONG cRef;
1609     
1610     TRACE("(iface=%p)\n", iface);
1611
1612     cRef = InterlockedDecrement(&This->m_cRef);
1613     
1614     if (!cRef) 
1615         UnixSubFolderIterator_Destroy(This);
1616
1617     return cRef;
1618 }
1619
1620 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1621     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1622 {
1623     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1624     ULONG i = 0;
1625
1626     /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
1627     if (This->m_dirFolder) {
1628         char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
1629         struct dirent *pDirEntry;
1630
1631         while (i < celt) {
1632             pDirEntry = readdir(This->m_dirFolder);
1633             if (!pDirEntry) break; /* No more entries */
1634             if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
1635
1636             /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
1637              * and see if it passes the filter. 
1638              */
1639             lstrcpyA(pszRelativePath, pDirEntry->d_name);
1640             rgelt[i] = (LPITEMIDLIST)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(lstrlenA(pszRelativePath))+sizeof(USHORT));
1641             if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
1642                 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter)) 
1643             {
1644                 SHFree(rgelt[i]);
1645                 continue;
1646             }
1647             memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
1648             i++;
1649         }
1650         *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
1651     }
1652     
1653     if (pceltFetched)
1654         *pceltFetched = i;
1655
1656     return (i == 0) ? S_FALSE : S_OK;
1657 }
1658     
1659 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1660 {
1661     LPITEMIDLIST *apidl;
1662     ULONG cFetched;
1663     HRESULT hr;
1664     
1665     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1666
1667     /* Call IEnumIDList::Next and delete the resulting pidls. */
1668     apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
1669     hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
1670     if (SUCCEEDED(hr))
1671         while (cFetched--) 
1672             SHFree(apidl[cFetched]);
1673     SHFree(apidl);
1674
1675     return hr;
1676 }
1677
1678 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1679 {
1680     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1681         
1682     TRACE("(iface=%p)\n", iface);
1683
1684     if (This->m_dirFolder)
1685         rewinddir(This->m_dirFolder);
1686     
1687     return S_OK;
1688 }
1689
1690 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1691     IEnumIDList** ppenum)
1692 {
1693     TRACE("stub\n");
1694     return E_NOTIMPL;
1695 }
1696
1697 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1698  */
1699 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1700     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1701     UnixSubFolderIterator_IEnumIDList_AddRef,
1702     UnixSubFolderIterator_IEnumIDList_Release,
1703     UnixSubFolderIterator_IEnumIDList_Next,
1704     UnixSubFolderIterator_IEnumIDList_Skip,
1705     UnixSubFolderIterator_IEnumIDList_Reset,
1706     UnixSubFolderIterator_IEnumIDList_Clone
1707 };
1708
1709 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1710     UnixSubFolderIterator *iterator;
1711
1712     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1713     
1714     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1715     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1716     iterator->m_cRef = 0;
1717     iterator->m_fFilter = fFilter;
1718     iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
1719     lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
1720
1721     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1722     
1723     return (IUnknown*)iterator;
1724 }