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