Allow for unixfs folders to be rooted at desktop level.
[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 #include <unistd.h>
27 #ifdef HAVE_SYS_STAT_H
28 # include <sys/stat.h>
29 #endif
30 #ifdef HAVE_PWD_H
31 # include <pwd.h>
32 #endif
33 #include <grp.h>
34 #include <limits.h>
35
36 #define COBJMACROS
37 #define NONAMELESSUNION
38 #define NONAMELESSSTRUCT
39
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winuser.h"
43 #include "objbase.h"
44 #include "winreg.h"
45 #include "winternl.h"
46 #include "wine/debug.h"
47
48 #include "shell32_main.h"
49 #include "shfldr.h"
50 #include "shresdef.h"
51 #include "pidl.h"
52
53 WINE_DEFAULT_DEBUG_CHANNEL(shell);
54
55 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
56 const GUID CLSID_UnixDosFolder = {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
57
58 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
59 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
60
61 /* FileStruct reserves one byte for szNames, thus we don't have to 
62  * alloc a byte for the terminating '\0' of 'name'. Two of the
63  * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
64  * type field. One is for FileStruct's szNames field, to terminate
65  * the alternate DOS name, which we don't use here. And then there's
66  * the additional StatStruct.
67  */
68 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
69     (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char)+sizeof(StatStruct))
70 #define NAME_LEN_FROM_LPSHITEMID(s) \
71     (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char)-sizeof(StatStruct))
72 #define LPSTATSTRUCT_FROM_LPSHITEMID(s) ((StatStruct*)(((LPBYTE)s)+((LPSHITEMID)s)->cb-sizeof(StatStruct)))
73
74 #define PATHMODE_UNIX 0
75 #define PATHMODE_DOS  1
76
77 /* This structure is appended to shell32's FileStruct type in IDLs to store unix
78  * filesystem specific informationen extracted with the stat system call.
79  */
80 typedef struct tagStatStruct {
81     mode_t st_mode;
82     uid_t  st_uid;
83     gid_t  st_gid;
84 } StatStruct;
85
86 /* UnixFolder object layout and typedef.
87  */
88 typedef struct _UnixFolder {
89     const IShellFolder2Vtbl  *lpIShellFolder2Vtbl;
90     const IPersistFolder2Vtbl *lpIPersistFolder2Vtbl;
91     ULONG m_cRef;
92     CHAR *m_pszPath;
93     LPITEMIDLIST m_pidlLocation;
94     LPITEMIDLIST *m_apidlSubDirs;
95     DWORD m_cSubDirs;
96     DWORD m_dwPathMode;
97 } UnixFolder;
98
99 /******************************************************************************
100  * UNIXFS_is_pidl_of_type [INTERNAL]
101  *
102  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
103  *
104  * PARAMS
105  *  pIDL    [I] The ITEMIDLIST to be checked.
106  *  fFilter [I] Shell condition flags, which specify the filter.
107  *
108  * RETURNS
109  *  TRUE, if pIDL is accepted by fFilter
110  *  FALSE, otherwise
111  */
112 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
113     LPSTR pszText = _ILGetTextPointer(pIDL);
114     if (!pszText) return FALSE;
115     if (pszText[0] == '.' && !(fFilter & SHCONTF_INCLUDEHIDDEN)) return FALSE;
116     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
117     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
118     return FALSE;
119 }
120
121 /******************************************************************************
122  * UNIXFS_build_shitemid [Internal]
123  *
124  * Constructs a new SHITEMID for a single path item (up to the next '/' or 
125  * '\0') into a buffer. Decorates the SHITEMID with information from a stat 
126  * system call.
127  *
128  * PARAMS
129  *  name   [I] The name of the next path item. Terminated by either '\0' or '/'.
130  *  pStat  [I] A stat struct variable obtained by a stat system call on the file.
131  *  buffer [O] SHITEMID will be constructed here.
132  *
133  * RETURNS
134  *  A pointer to the next '/' or '\0' character in name.
135  *  
136  * NOTES
137  *  Minimum size of buffer is SHITEMID_LEN_FROM_NAME_LEN(strlen(name)).
138  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
139  *  a 0 USHORT value.
140  */
141 static char* UNIXFS_build_shitemid(char *name, struct stat *pStat, void *buffer) {
142     LARGE_INTEGER time;
143     FILETIME fileTime;
144     LPPIDLDATA pIDLData;
145     StatStruct *pStatStruct;
146     int cNameLen;
147     char *pSlash;
148
149     TRACE("(name=%s, pStat=%p, buffer=%p)\n", debugstr_a(name), pStat, buffer);
150
151     pSlash = strchr(name, '/');
152     cNameLen = pSlash ? pSlash - name : strlen(name); 
153     
154     memset(buffer, 0, SHITEMID_LEN_FROM_NAME_LEN(cNameLen));
155     ((LPSHITEMID)buffer)->cb = SHITEMID_LEN_FROM_NAME_LEN(cNameLen) ;
156     
157     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)buffer);
158     pIDLData->type = S_ISDIR(pStat->st_mode) ? PT_FOLDER : PT_VALUE;
159     pIDLData->u.file.dwFileSize = (DWORD)pStat->st_size;
160     RtlSecondsSince1970ToTime( pStat->st_mtime, &time );
161     fileTime.dwLowDateTime = time.u.LowPart;
162     fileTime.dwHighDateTime = time.u.HighPart;
163     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
164     pIDLData->u.file.uFileAttribs = 
165         (S_ISDIR(pStat->st_mode) ? FILE_ATTRIBUTE_DIRECTORY : 0) |
166         (name[0] == '.' ? FILE_ATTRIBUTE_HIDDEN : 0);
167     memcpy(pIDLData->u.file.szNames, name, cNameLen);
168
169     pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(buffer);
170     pStatStruct->st_mode = pStat->st_mode;
171     pStatStruct->st_uid = pStat->st_uid;
172     pStatStruct->st_gid = pStat->st_gid;
173
174     return pSlash ? pSlash+1 : (name + cNameLen);
175 }
176
177 /******************************************************************************
178  * UNIXFS_get_unix_path [Internal]
179  *
180  * Convert an absolute dos path to an absolute canonicalized unix path.
181  * Evaluate "/.", "/.." and symbolic links.
182  *
183  * PARAMS
184  *  pszDosPath       [I] An absolute dos path
185  *  pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
186  *
187  * RETURNS
188  *  Success, TRUE
189  *  Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
190  */
191 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
192 {
193     char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
194     struct stat fileStat;
195     
196     TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
197     
198     if (!pszDosPath || pszDosPath[1] != ':')
199         return FALSE;
200
201     pszUnixPath = wine_get_unix_file_name(pszDosPath);
202     if (!pszUnixPath) return FALSE;   
203     strcpy(szPath, pszUnixPath);
204     HeapFree(GetProcessHeap(), 0, pszUnixPath);
205    
206     /* pCanonicalTail always points to the end of the canonical path constructed
207      * thus far. pPathTail points to the still to be processed part of the input
208      * path. pElement points to the path element currently investigated.
209      */
210     *pszCanonicalPath = '\0';
211     pCanonicalTail = pszCanonicalPath;
212     pPathTail = szPath;
213
214     do {
215         char cTemp;
216         int cLinks = 0;
217             
218         pElement = pPathTail;
219         pPathTail = strchr(pPathTail+1, '/');
220         if (!pPathTail) /* Last path element may not be terminated by '/'. */ 
221             pPathTail = pElement + strlen(pElement);
222         /* Temporarily terminate the current path element. Will be restored later. */
223         cTemp = *pPathTail;
224         *pPathTail = '\0';
225
226         /* Skip "/." path elements */
227         if (!strcmp("/.", pElement)) {
228             *pPathTail = cTemp;
229             continue;
230         }
231
232         /* Remove last element in canonical path for "/.." elements, then skip. */
233         if (!strcmp("/..", pElement)) {
234             char *pTemp = strrchr(pszCanonicalPath, '/');
235             if (pTemp)
236                 pCanonicalTail = pTemp;
237             *pCanonicalTail = '\0';
238             *pPathTail = cTemp;
239             continue;
240         }
241        
242         /* lstat returns zero on success. */
243         if (lstat(szPath, &fileStat)) 
244             return FALSE;
245
246         if (S_ISLNK(fileStat.st_mode)) {
247             char szSymlink[FILENAME_MAX];
248             int cLinkLen, cTailLen;
249           
250             /* Avoid infinite loop for recursive links. */
251             if (++cLinks > 64) 
252                 return FALSE;
253             
254             cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
255             if (cLinkLen < 0) 
256                 return FALSE;
257
258             *pPathTail = cTemp;
259             cTailLen = strlen(pPathTail);
260            
261             if (szSymlink[0] == '/') {
262                 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
263                 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
264                     return FALSE;
265                     
266                 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
267                 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
268                 *pszCanonicalPath = '\0';
269                     pCanonicalTail = pszCanonicalPath;
270                     pPathTail = szPath;
271             } else {
272                 /* Relative link. Expand into szPath and continue. */
273                 char szTemp[FILENAME_MAX];
274                 int cTailLen = strlen(pPathTail);
275
276                 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
277                     return FALSE;
278
279                 memcpy(szTemp, pPathTail, cTailLen + 1);
280                 memcpy(pElement + 1, szSymlink, cLinkLen);
281                 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
282                 pPathTail = pElement;
283             }
284         } else {
285             /* Regular directory or file. Copy to canonical path */
286             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
287                 return FALSE;
288                 
289             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
290             pCanonicalTail += pPathTail - pElement;
291             *pPathTail = cTemp;
292         }
293     } while (pPathTail[0] == '/' && pPathTail[1]); /* Also handles paths terminated by '/' */
294    
295     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
296     
297     return TRUE;
298 }
299
300 /******************************************************************************
301  * UNIXFS_path_to_pidl [Internal]
302  *
303  * PARAMS
304  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
305  *  path        [I] An absolute unix or dos path or a path relativ to pUnixFolder
306  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
307  *  
308  * RETURNS
309  *  Success: TRUE
310  *  Failure: FALSE, invalid params or out of memory
311  *
312  * NOTES
313  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
314  */
315 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
316     LPITEMIDLIST pidl;
317     struct stat fileStat;
318     int cSubDirs, cPidlLen, res, cPathLen;
319     char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
320
321     TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
322    
323     if (!ppidl || !path)
324         return FALSE;
325
326     cPathLen = lstrlenW(path);
327     
328     /* Build an absolute path and let pNextPathElement point to the interesting 
329      * relative sub-path. We need the absolute path to call 'stat', but the pidl
330      * will only contain the relative part.
331      */
332     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
333     {
334         /* Absolute dos path. Convert to unix */
335         if (!UNIXFS_get_unix_path(path, szCompletePath))
336             return FALSE;
337         pNextPathElement = szCompletePath + 1;
338     } 
339     else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/')) 
340     {
341         /* Absolute unix path. Just convert to ANSI. */
342         WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL); 
343         pNextPathElement = szCompletePath + 1;
344     } 
345     else 
346     {
347         /* Relative dos or unix path. Concat with this folder's path */
348         int cBasePathLen = strlen(pUnixFolder->m_pszPath);
349         memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
350         WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath + cBasePathLen, 
351                             FILENAME_MAX - cBasePathLen, NULL, NULL);
352         pNextPathElement = szCompletePath + cBasePathLen;
353         
354         /* If in dos mode, replace '\' with '/' */
355         if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
356             char *pBackslash = strchr(pNextPathElement, '\\');
357             while (pBackslash) {
358                 *pBackslash = '/';
359                 pBackslash = strchr(pBackslash, '\\');
360             }
361         }
362     }
363
364     /* At this point, we have an absolute unix path in szCompletePath. */
365     TRACE("complete path: %s\n", szCompletePath);
366     
367     /* Count the number of sub-directories in the path */
368     cSubDirs = 1; /* Path may not be terminated with '/', thus start with 1 */
369     pSlash = strchr(pNextPathElement, '/');
370     while (pSlash && pSlash[1]) {
371         cSubDirs++;
372         pSlash = strchr(pSlash+1, '/');
373     }
374   
375     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
376      * characters, which are not stored in the ITEMIDLIST. */
377     cPidlLen = strlen(pNextPathElement) - cSubDirs + 1 + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
378     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
379     if (!pidl) return FALSE;
380
381     /* Concatenate the SHITEMIDs of the sub-directories. */
382     while (*pNextPathElement) {
383         pSlash = strchr(pNextPathElement, '/');
384         if (pSlash) {
385             *pSlash = '\0';
386             res = stat(szCompletePath, &fileStat);
387             *pSlash = '/';
388             if (res) {
389                 SHFree(pidl);
390                 return FALSE;
391             }
392         } else {
393             if (stat(szCompletePath, &fileStat)) {
394                 SHFree(pidl);
395                 return FALSE;
396             }
397         }
398                 
399         pNextPathElement = UNIXFS_build_shitemid(pNextPathElement, &fileStat, pidl);
400         pidl = ILGetNext(pidl);
401     }
402     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
403
404     if ((int)pidl-(int)*ppidl+sizeof(USHORT) > cPidlLen) /* We've corrupted the heap :( */ 
405         ERR("Computed length of pidl to small. Please report.\n");
406     
407     return TRUE;
408 }
409
410 /******************************************************************************
411  * UNIXFS_pidl_to_path [Internal]
412  *
413  * Construct the unix path that corresponds to a fully qualified ITEMIDLIST
414  *
415  * PARAMS
416  *  pidl [I] ITEMIDLIST that specifies the absolute location of the folder
417  *  path [O] The corresponding unix path as a zero terminated ascii string
418  *
419  * RETURNS
420  *  Success: TRUE
421  *  Failure: FALSE, pidl doesn't specify a unix path or out of memory
422  */
423 static BOOL UNIXFS_pidl_to_path(LPCITEMIDLIST pidl, UnixFolder *pUnixFolder) {
424     LPCITEMIDLIST current = pidl, root;
425     DWORD dwPathLen;
426     char *pNextDir, szBasePath[FILENAME_MAX] = "/";
427
428     TRACE("(pidl=%p, pUnixFolder=%p)\n", pidl, pUnixFolder);
429     pdump(pidl);
430     
431     pUnixFolder->m_pszPath = NULL;
432
433     /* Find the UnixFolderClass root */
434     while (current->mkid.cb) {
435         if (_ILIsDrive(current) || /* The dos drive, which maps to '/' */
436             (_ILIsSpecialFolder(current) && 
437              (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
438               IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current)))))
439         {
440             break;
441         }
442         current = ILGetNext(current);
443     }
444
445     if (current && current->mkid.cb) {
446         root = current = ILGetNext(current);
447         dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
448     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
449         /* Path rooted at Desktop */
450         WCHAR wszDesktopPath[MAX_PATH];
451         if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
452            return FALSE;
453         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
454             return FALSE;
455         dwPathLen = strlen(szBasePath) + 1;
456         root = current;
457     } else {
458         ERR("Unknown pidl type!\n");
459         pdump(pidl);
460         return FALSE;
461     }
462     
463     /* Determine the path's length bytes */
464     while (current && current->mkid.cb) {
465         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
466         current = ILGetNext(current);
467     };
468
469     /* Build the path */
470     pUnixFolder->m_pszPath = pNextDir = SHAlloc(dwPathLen);
471     if (!pUnixFolder->m_pszPath) {
472         WARN("SHAlloc failed!\n");
473         return FALSE;
474     }
475     current = root;
476     strcpy(pNextDir, szBasePath);
477     pNextDir += strlen(szBasePath);
478     while (current && current->mkid.cb) {
479         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
480         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
481         *pNextDir++ = '/';
482         current = ILGetNext(current);
483     }
484     *pNextDir='\0';
485     
486     TRACE("--> %s\n", pUnixFolder->m_pszPath);
487     return TRUE;
488 }
489
490 /******************************************************************************
491  * UNIXFS_build_subfolder_pidls [Internal]
492  *
493  * Builds an array of subfolder PIDLs relative to a unix directory
494  *
495  * PARAMS
496  *  path   [I] Name of a unix directory as a zero terminated ascii string
497  *  apidl  [O] The array of PIDLs
498  *  pCount [O] Size of apidl
499  *
500  * RETURNS
501  *  Success: TRUE
502  *  Failure: FALSE, path is not a valid unix directory or out of memory
503  *
504  * NOTES
505  *  The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
506  *  to release each PIDL as well as the array itself with SHFree.
507  */
508 static BOOL UNIXFS_build_subfolder_pidls(UnixFolder *pUnixFolder)
509 {
510     struct dirent *pDirEntry;
511     struct stat fileStat;
512     DIR *dir;
513     DWORD cDirEntries, i;
514     USHORT sLen;
515     char *pszFQPath;
516
517     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
518     
519     pUnixFolder->m_apidlSubDirs = NULL;
520     pUnixFolder->m_cSubDirs = 0;
521   
522     dir = opendir(pUnixFolder->m_pszPath);
523     if (!dir) {
524         WARN("Failed to open directory '%s'.\n", pUnixFolder->m_pszPath);
525         return FALSE;
526     }
527
528     /* Allocate space for fully qualified paths */
529     pszFQPath = SHAlloc(strlen(pUnixFolder->m_pszPath) + PATH_MAX);
530     if (!pszFQPath) {
531         WARN("SHAlloc failed!\n");
532         return FALSE;
533     }
534  
535     /* Count number of directory entries. */
536     for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) { 
537         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
538         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
539         if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
540     }
541
542     /* If there are no entries, we are done. */
543     if (cDirEntries == 0) {
544         closedir(dir);
545         SHFree(pszFQPath);
546         return TRUE;
547     }
548
549     /* Allocate the array of PIDLs */
550     pUnixFolder->m_apidlSubDirs = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
551     if (!pUnixFolder->m_apidlSubDirs) {
552         WARN("SHAlloc failed!\n");
553         return FALSE;
554     }
555   
556     /* Allocate and initialize one SHITEMID per sub-directory. */
557     for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
558         LPSHITEMID pid;
559             
560         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
561
562         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
563         if (stat(pszFQPath, &fileStat)) continue;
564         if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) continue;
565    
566         sLen = strlen(pDirEntry->d_name);
567         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
568         if (!pid) {
569             WARN("SHAlloc failed!\n");
570             return FALSE;
571         }
572         UNIXFS_build_shitemid(pDirEntry->d_name, &fileStat, pid);
573         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
574
575         pUnixFolder->m_apidlSubDirs[i++] = (LPITEMIDLIST)pid;
576     }
577     
578     pUnixFolder->m_cSubDirs = i;    
579     closedir(dir);
580     SHFree(pszFQPath);
581
582     return TRUE;
583 }
584
585 /******************************************************************************
586  * UnixFolderIcon 
587  *
588  * Singleton class, which is used by the shell to extract icons to represent
589  * folders in tree- and listviews. Currently, all this singleton does is to
590  * provide the shell with the absolute path to "shell32.dll" and with the 
591  * indices of the closed and opened folder icons in the resources of this dll.
592  */
593
594 /* UnixFolderIcon object layout and typedef.
595  */
596 typedef struct _UnixFolderIcon {
597     const IExtractIconWVtbl *lpIExtractIconWVtbl;
598     BOOL bFolder;
599 } UnixFolderIcon;
600
601 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_QueryInterface(IExtractIconW *iface, REFIID riid, 
602     void **ppv) 
603 {
604     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
605     
606     if (!ppv) return E_INVALIDARG;
607     
608     if (IsEqualIID(&IID_IUnknown, riid) ||
609         IsEqualIID(&IID_IExtractIconW, riid))
610     {
611         *ppv = iface;
612     } else {
613         *ppv = NULL;
614         return E_NOINTERFACE;
615     }
616
617     IExtractIconW_AddRef(iface);
618     return S_OK;
619 }
620
621 static ULONG WINAPI UnixFolderIcon_IExtractIconW_AddRef(IExtractIconW *iface) {
622     TRACE("(iface=%p)\n", iface);
623     return 2;
624 }
625
626 static ULONG WINAPI UnixFolderIcon_IExtractIconW_Release(IExtractIconW *iface) {
627     TRACE("(iface=%p)\n", iface);
628     return 1;
629 }
630
631 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_GetIconLocation(IExtractIconW *iface, 
632     UINT uFlags, LPWSTR szIconFile, UINT cchMax, INT* piIndex, UINT* pwFlags)
633 {
634     UnixFolderIcon *This = ADJUST_THIS(UnixFolderIcon, IExtractIconW, iface);
635         
636     TRACE("(iface=%p, uFlags=%u, szIconFile=%s, cchMax=%u, piIndex=%p, pwFlags=%p)\n",
637             iface, uFlags, debugstr_w(szIconFile), cchMax, piIndex, pwFlags);
638     
639     lstrcpynW(szIconFile, swShell32Name, cchMax);
640     if (This->bFolder) {
641         *piIndex = (uFlags & GIL_OPENICON) ? -IDI_SHELL_FOLDER_OPEN : -IDI_SHELL_FOLDER;
642     } else {
643         *piIndex = -IDI_SHELL_DOCUMENT;
644     }
645     *pwFlags = 0;
646
647     return S_OK;
648 }
649
650 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_Extract(
651     IExtractIconW *iface, LPCWSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, 
652     UINT nIconSize)
653 {
654     TRACE("(iface=%p, pszFile=%s, nIconIndex=%u, phiconLarge=%p, phiconSmall=%p, nIconSize=%u)"
655           "stub\n", iface, debugstr_w(pszFile), nIconIndex, phiconLarge, phiconSmall, nIconSize);
656
657     return E_NOTIMPL;
658 }
659
660 /* VTable for the IExtractIconW interface of the UnixFolderIcon class. 
661  */
662 static const IExtractIconWVtbl UnixFolderIcon_IExtractIconW_Vtbl = {
663     UnixFolderIcon_IExtractIconW_QueryInterface,
664     UnixFolderIcon_IExtractIconW_AddRef,
665     UnixFolderIcon_IExtractIconW_Release,
666     UnixFolderIcon_IExtractIconW_GetIconLocation,
667     UnixFolderIcon_IExtractIconW_Extract
668 };
669
670 /* The singleton instance
671  */
672 UnixFolderIcon UnixFolderIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, TRUE };
673 UnixFolderIcon UnixDocumentIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, FALSE };
674
675 /******************************************************************************
676  * UnixFolder
677  *
678  * Class whose heap based instances represent unix filesystem directories.
679  */
680
681 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
682     DWORD i;
683
684     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
685     
686     if (pUnixFolder->m_apidlSubDirs) 
687         for (i=0; i < pUnixFolder->m_cSubDirs; i++) 
688             SHFree(pUnixFolder->m_apidlSubDirs[i]);
689     SHFree(pUnixFolder->m_apidlSubDirs);
690     SHFree(pUnixFolder->m_pszPath);
691     ILFree(pUnixFolder->m_pidlLocation);
692     SHFree(pUnixFolder);
693 }
694
695 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
696     void **ppv) 
697 {
698     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
699         
700     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
701     
702     if (!ppv) return E_INVALIDARG;
703     
704     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
705         IsEqualIID(&IID_IShellFolder2, riid)) 
706     {
707         *ppv = &This->lpIShellFolder2Vtbl;
708     } else if (IsEqualIID(&IID_IPersistFolder2, riid) || IsEqualIID(&IID_IPersistFolder, riid) || 
709                IsEqualIID(&IID_IPersist, riid)) 
710     {
711         *ppv = &This->lpIPersistFolder2Vtbl;
712     } else {
713         *ppv = NULL;
714         return E_NOINTERFACE;
715     }
716
717     IUnknown_AddRef((IUnknown*)*ppv);
718     return S_OK;
719 }
720
721 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
722     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
723
724     TRACE("(iface=%p)\n", iface);
725
726     return InterlockedIncrement(&This->m_cRef);
727 }
728
729 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
730     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
731     ULONG cRef;
732     
733     TRACE("(iface=%p)\n", iface);
734
735     cRef = InterlockedDecrement(&This->m_cRef);
736     
737     if (!cRef) 
738         UnixFolder_Destroy(This);
739
740     return cRef;
741 }
742
743 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
744     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
745     ULONG* pdwAttributes)
746 {
747     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
748     BOOL result;
749
750     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
751           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
752           pchEaten, ppidl, pdwAttributes);
753
754     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
755     if (result && pdwAttributes && *pdwAttributes)
756     {
757         /* need to traverse to the last element for the attribute */
758         LPCITEMIDLIST pidl, last_pidl;
759         pidl = last_pidl = *ppidl;
760         while(pidl && pidl->mkid.cb)
761         {
762             last_pidl = pidl;
763             pidl = ILGetNext(pidl);
764         }
765         SHELL32_GetItemAttributes((IShellFolder*)iface, last_pidl, pdwAttributes);
766     }
767
768     if (!result) TRACE("FAILED!\n");
769     return result ? S_OK : E_FAIL;
770 }
771
772 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
773
774 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
775     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
776 {
777     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
778     IUnknown *newIterator;
779     HRESULT hr;
780     
781     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
782             iface, hwndOwner, grfFlags, ppEnumIDList);
783
784     newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
785     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
786     IUnknown_Release(newIterator);
787     
788     return hr;
789 }
790
791 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
792     LPBC pbcReserved, REFIID riid, void** ppvOut)
793 {
794     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
795     IPersistFolder2 *persistFolder;
796     LPITEMIDLIST pidlSubFolder;
797     HRESULT hr;
798         
799     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
800             iface, pidl, pbcReserved, riid, ppvOut);
801
802     if (!pidl || !pidl->mkid.cb)
803         return E_INVALIDARG;
804     
805     if (This->m_dwPathMode == PATHMODE_DOS)
806         hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
807     else
808         hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
809         
810     if (!SUCCEEDED(hr)) return hr;
811     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
812     
813     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
814     IPersistFolder2_Initialize(persistFolder, pidlSubFolder);
815     IPersistFolder2_Release(persistFolder);
816     ILFree(pidlSubFolder);
817
818     return hr;
819 }
820
821 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
822     LPBC pbcReserved, REFIID riid, void** ppvObj)
823 {
824     TRACE("stub\n");
825     return E_NOTIMPL;
826 }
827
828 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
829     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
830 {
831     BOOL isEmpty1, isEmpty2;
832     HRESULT hr = E_FAIL;
833     LPITEMIDLIST firstpidl;
834     IShellFolder2 *psf;
835     int compare;
836
837     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
838     
839     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
840     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
841
842     if (isEmpty1 && isEmpty2) 
843         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
844     else if (isEmpty1)
845         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
846     else if (isEmpty2)
847         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
848
849     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
850         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
851     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
852         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
853
854     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
855                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
856                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
857     
858     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
859         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
860
861     if (pidl1->mkid.cb < pidl2->mkid.cb)
862         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
863     else if (pidl1->mkid.cb > pidl2->mkid.cb)
864         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
865
866     firstpidl = ILCloneFirst(pidl1);
867     pidl1 = ILGetNext(pidl1);
868     pidl2 = ILGetNext(pidl2);
869     
870     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
871     if (SUCCEEDED(hr)) {
872         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
873         IShellFolder2_Release(psf);
874     }
875
876     ILFree(firstpidl);
877     return hr;
878 }
879
880 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
881     REFIID riid, void** ppv)
882 {
883     HRESULT hr = E_INVALIDARG;
884         
885     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
886     
887     if (!ppv) return E_INVALIDARG;
888     *ppv = NULL;
889     
890     if (IsEqualIID(&IID_IShellView, riid)) {
891         LPSHELLVIEW pShellView;
892         
893         pShellView = IShellView_Constructor((IShellFolder*)iface);
894         if (pShellView) {
895             hr = IShellView_QueryInterface(pShellView, riid, ppv);
896             IShellView_Release(pShellView);
897         }
898     } 
899     
900     return hr;
901 }
902
903 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
904     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
905 {
906     UINT i;
907     SFGAOF flags= ~(SFGAOF)0;
908         
909     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p) semi-stub\n", iface, cidl, apidl, rgfInOut);
910    
911     for (i=0; i<cidl; i++) {
912         LPPIDLDATA pData = _ILGetDataPointer(apidl[i]);
913         if (!pData) continue;
914         if (pData->type == PT_FOLDER) flags &= (SFGAO_FILESYSTEM|SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER);
915         if (pData->type == PT_VALUE) flags &= SFGAO_FILESYSTEM;
916     }
917     
918     *rgfInOut = *rgfInOut & flags;
919             
920     return S_OK;
921 }
922
923 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
924     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
925 {
926     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
927     
928     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
929         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
930
931     if (IsEqualIID(&IID_IContextMenu, riid)) {
932         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
933         return S_OK;
934     } else if (IsEqualIID(&IID_IDataObject, riid)) {
935         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
936         return S_OK;
937     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
938         FIXME("IExtractIconA\n");
939         return E_FAIL;
940     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
941         if (cidl != 1) return E_FAIL;
942         if (_ILIsFolder(apidl[0])) 
943             *ppvOut = &UnixFolderIconSingleton;
944         else
945             *ppvOut = &UnixDocumentIconSingleton;
946         return S_OK;
947     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
948         FIXME("IDropTarget\n");
949         return E_FAIL;
950     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
951         FIXME("IShellLinkW\n");
952         return E_FAIL;
953     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
954         FIXME("IShellLinkA\n");
955         return E_FAIL;
956     } else {
957         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
958         return E_NOINTERFACE;
959     }
960 }
961
962 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
963     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
964 {
965     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
966     HRESULT hr = S_OK;    
967
968     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
969     
970     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
971         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
972     {
973         if (!pidl->mkid.cb) {
974             lpName->uType = STRRET_CSTR;
975             strcpy(lpName->u.cStr, This->m_pszPath);
976             if (This->m_dwPathMode == PATHMODE_DOS) {
977                 char path[MAX_PATH];
978                 GetFullPathNameA(lpName->u.cStr, MAX_PATH, path, NULL);
979                 strcpy(lpName->u.cStr, path);
980             }
981         } else {
982             IShellFolder *pSubFolder;
983             USHORT emptyIDL = 0;
984
985             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
986             if (!SUCCEEDED(hr)) return hr;
987        
988             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
989             IShellFolder_Release(pSubFolder);
990         }
991     } else {
992         char *pszFileName = _ILGetTextPointer(pidl);
993         lpName->uType = STRRET_CSTR;
994         strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
995     }
996
997     TRACE("--> %s\n", lpName->u.cStr);
998     
999     return hr;
1000 }
1001
1002 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* This, HWND hwnd, 
1003     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1004 {
1005     TRACE("stub\n");
1006     return E_NOTIMPL;
1007 }
1008
1009 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
1010     IEnumExtraSearch **ppEnum) 
1011 {
1012     TRACE("stub\n");
1013     return E_NOTIMPL;
1014 }
1015
1016 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
1017     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
1018 {
1019     TRACE("stub\n");
1020     return E_NOTIMPL;
1021 }
1022
1023 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1024     UINT iColumn, SHCOLSTATEF *pcsFlags)
1025 {
1026     TRACE("stub\n");
1027     return E_NOTIMPL;
1028 }
1029
1030 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1031     GUID *pguid)
1032 {
1033     TRACE("stub\n");
1034     return E_NOTIMPL;
1035 }
1036
1037 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1038     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1039 {
1040     TRACE("stub\n");
1041     return E_NOTIMPL;
1042 }
1043
1044 #define SHELLVIEWCOLUMNS 6 
1045
1046 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1047     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1048 {
1049     HRESULT hr = E_FAIL;
1050     struct passwd *pPasswd;
1051     struct group *pGroup;
1052     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1053         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1054         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1055         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1056         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1057         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
1058         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
1059     };
1060
1061     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1062     
1063     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1064         return E_INVALIDARG;
1065
1066     if (!pidl) {
1067         psd->fmt = SFHeader[iColumn].fmt;
1068         psd->cxChar = SFHeader[iColumn].cxChar;
1069         psd->str.uType = STRRET_CSTR;
1070         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1071         return S_OK;
1072     } else {
1073         StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
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(pStatStruct->st_mode) ? 'd' : '-';
1082                 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
1083                 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
1084                 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
1085                 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
1086                 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
1087                 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
1088                 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
1089                 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
1090                 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
1091                 psd->str.u.cStr[10] = '\0';
1092                 break;
1093             case 2:
1094                 pPasswd = getpwuid(pStatStruct->st_uid);
1095                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1096                 break;
1097             case 3:
1098                 pGroup = getgrgid(pStatStruct->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     
1174     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1175
1176     This->m_pidlLocation = ILClone(pidl);
1177     
1178     pdump(pidl);
1179     
1180     if (!UNIXFS_pidl_to_path(pidl, This))
1181         return E_FAIL;
1182     UNIXFS_build_subfolder_pidls(This);
1183    
1184     return S_OK;
1185 }
1186
1187 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetCurFolder(IPersistFolder2* iface, LPITEMIDLIST* ppidl)
1188 {
1189     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1190     
1191     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1192
1193     if (!ppidl)
1194         return E_POINTER;
1195     *ppidl = ILClone (This->m_pidlLocation);
1196     return S_OK;
1197 }
1198     
1199 /* VTable for UnixFolder's IPersistFolder interface.
1200  */
1201 static const IPersistFolder2Vtbl UnixFolder_IPersistFolder2_Vtbl = {
1202     UnixFolder_IPersistFolder2_QueryInterface,
1203     UnixFolder_IPersistFolder2_AddRef,
1204     UnixFolder_IPersistFolder2_Release,
1205     UnixFolder_IPersistFolder2_GetClassID,
1206     UnixFolder_IPersistFolder2_Initialize,
1207     UnixFolder_IPersistFolder2_GetCurFolder
1208 };
1209
1210 /******************************************************************************
1211  * Unix[Dos]Folder_Constructor [Internal]
1212  *
1213  * PARAMS
1214  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1215  *  riid      [I] Interface asked for by the client.
1216  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1217  *
1218  * NOTES
1219  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1220  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1221  *  compatible signature.
1222  *
1223  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1224  *  means that paths are converted from dos to unix and back at the interfaces.
1225  */
1226 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode) {
1227     HRESULT hr = E_FAIL;
1228     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1229     
1230     if(pUnixFolder) {
1231         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1232         pUnixFolder->lpIPersistFolder2Vtbl = &UnixFolder_IPersistFolder2_Vtbl;
1233         pUnixFolder->m_cRef = 0;
1234         pUnixFolder->m_pszPath = NULL;
1235         pUnixFolder->m_apidlSubDirs = NULL;
1236         pUnixFolder->m_cSubDirs = 0;
1237         pUnixFolder->m_dwPathMode = dwPathMode;
1238
1239         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1240         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1241         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1242     }
1243     return hr;
1244 }
1245
1246 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1247     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1248     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX);
1249 }
1250
1251 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1252     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1253     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS);
1254 }
1255
1256 /******************************************************************************
1257  * UnixSubFolderIterator
1258  *
1259  * Class whose heap based objects represent iterators over the sub-directories
1260  * of a given UnixFolder object. 
1261  */
1262
1263 /* UnixSubFolderIterator object layout and typedef.
1264  */
1265 typedef struct _UnixSubFolderIterator {
1266     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1267     ULONG m_cRef;
1268     UnixFolder *m_pUnixFolder;
1269     ULONG m_cIdx;
1270     SHCONTF m_fFilter;
1271 } UnixSubFolderIterator;
1272
1273 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1274     TRACE("(iterator=%p)\n", iterator);
1275         
1276     UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1277     SHFree(iterator);
1278 }
1279
1280 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1281     REFIID riid, void** ppv)
1282 {
1283     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1284     
1285     if (!ppv) return E_INVALIDARG;
1286     
1287     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1288         *ppv = iface;
1289     } else {
1290         *ppv = NULL;
1291         return E_NOINTERFACE;
1292     }
1293
1294     IEnumIDList_AddRef(iface);
1295     return S_OK;
1296 }
1297                             
1298 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1299 {
1300     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1301
1302     TRACE("(iface=%p)\n", iface);
1303    
1304     return InterlockedIncrement(&This->m_cRef);
1305 }
1306
1307 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1308 {
1309     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1310     ULONG cRef;
1311     
1312     TRACE("(iface=%p)\n", iface);
1313
1314     cRef = InterlockedDecrement(&This->m_cRef);
1315     
1316     if (!cRef) 
1317         UnixSubFolderIterator_Destroy(This);
1318
1319     return cRef;
1320 }
1321
1322 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1323     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1324 {
1325     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1326     ULONG i;
1327
1328     TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1329    
1330     for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1331         LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1332         if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1333             rgelt[i] = ILClone(pCurrent);
1334             i++;
1335         }
1336     }
1337
1338     if (pceltFetched)
1339         *pceltFetched = i;
1340
1341     return i == celt ? S_OK : S_FALSE;
1342 }
1343
1344 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1345 {
1346     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1347     ULONG i;
1348     
1349     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1350
1351     for (i=0; i < celt; i++) {
1352         while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1353                !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter)) 
1354         {
1355             This->m_cIdx++;
1356         }
1357         This->m_cIdx++;
1358     }
1359     
1360     if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1361         This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1362         return S_FALSE;
1363     } else {
1364         return S_OK;
1365     }
1366 }
1367
1368 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1369 {
1370     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1371         
1372     TRACE("(iface=%p)\n", iface);
1373
1374     This->m_cIdx = 0;
1375     
1376     return S_OK;
1377 }
1378
1379 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1380     IEnumIDList** ppenum)
1381 {
1382     TRACE("stub\n");
1383     return E_NOTIMPL;
1384 }
1385
1386 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1387  */
1388 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1389     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1390     UnixSubFolderIterator_IEnumIDList_AddRef,
1391     UnixSubFolderIterator_IEnumIDList_Release,
1392     UnixSubFolderIterator_IEnumIDList_Next,
1393     UnixSubFolderIterator_IEnumIDList_Skip,
1394     UnixSubFolderIterator_IEnumIDList_Reset,
1395     UnixSubFolderIterator_IEnumIDList_Clone
1396 };
1397
1398 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1399     UnixSubFolderIterator *iterator;
1400
1401     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1402     
1403     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1404     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1405     iterator->m_cRef = 0;
1406     iterator->m_cIdx = 0;
1407     iterator->m_pUnixFolder = pUnixFolder;
1408     iterator->m_fFilter = fFilter;
1409
1410     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1411     UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1412     
1413     return (IUnknown*)iterator;
1414 }