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