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