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