Don't open device if already open.
[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_canonicalize_path [Internal]
179  *
180  * Evaluate "/.", "/.." and symbolic links for an absolute unix path.
181  *
182  * PARAMS
183  *  pszUnixPath      [I] An absolute unix path
184  *  pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
185  *
186  * RETURNS
187  *  Success, TRUE
188  *  Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
189  */
190 static BOOL UNIXFS_canonicalize_path(const char *pszUnixPath, char *pszCanonicalPath)
191 {
192     char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX];
193     struct stat fileStat;
194     
195     TRACE("(pszUnixPath=%s, pszCanonicalPath=%p)\n", debugstr_a(pszUnixPath), pszCanonicalPath);
196     
197     if (!pszUnixPath || *pszUnixPath != '/')
198         return FALSE;
199
200     strcpy(szPath, pszUnixPath);
201    
202     /* pCanonicalTail always points to the end of the canonical path constructed
203      * thus far. pPathTail points to the still to be processed part of the input
204      * path. pElement points to the path element currently investigated.
205      */
206     *pszCanonicalPath = '\0';
207     pCanonicalTail = pszCanonicalPath;
208     pPathTail = szPath;
209
210     do {
211         char cTemp;
212         int cLinks = 0;
213             
214         pElement = pPathTail;
215         pPathTail = strchr(pPathTail+1, '/');
216         if (!pPathTail) /* Last path element may not be terminated by '/'. */ 
217             pPathTail = pElement + strlen(pElement);
218         /* Temporarily terminate the current path element. Will be restored later. */
219         cTemp = *pPathTail;
220         *pPathTail = '\0';
221
222         /* Skip "/." path elements */
223         if (!strcmp("/.", pElement)) {
224             *pPathTail = cTemp;
225             continue;
226         }
227
228         /* Remove last element in canonical path for "/.." elements, then skip. */
229         if (!strcmp("/..", pElement)) {
230             char *pTemp = strrchr(pszCanonicalPath, '/');
231             if (pTemp)
232                 pCanonicalTail = pTemp;
233             *pCanonicalTail = '\0';
234             *pPathTail = cTemp;
235             continue;
236         }
237        
238         /* lstat returns zero on success. */
239         if (lstat(szPath, &fileStat)) 
240             return FALSE;
241
242         if (S_ISLNK(fileStat.st_mode)) {
243             char szSymlink[FILENAME_MAX];
244             int cLinkLen, cTailLen;
245           
246             /* Avoid infinite loop for recursive links. */
247             if (++cLinks > 64) 
248                 return FALSE;
249             
250             cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
251             if (cLinkLen < 0) 
252                 return FALSE;
253
254             *pPathTail = cTemp;
255             cTailLen = strlen(pPathTail);
256            
257             if (szSymlink[0] == '/') {
258                 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
259                 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
260                     return FALSE;
261                     
262                 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
263                 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
264                 *pszCanonicalPath = '\0';
265                     pCanonicalTail = pszCanonicalPath;
266                     pPathTail = szPath;
267             } else {
268                 /* Relative link. Expand into szPath and continue. */
269                 char szTemp[FILENAME_MAX];
270                 int cTailLen = strlen(pPathTail);
271
272                 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
273                     return FALSE;
274
275                 memcpy(szTemp, pPathTail, cTailLen + 1);
276                 memcpy(pElement + 1, szSymlink, cLinkLen);
277                 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
278                 pPathTail = pElement;
279             }
280         } else {
281             /* Regular directory or file. Copy to canonical path */
282             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
283                 return FALSE;
284                 
285             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
286             pCanonicalTail += pPathTail - pElement;
287             *pPathTail = cTemp;
288         }
289     } while (pPathTail[0] == '/' && pPathTail[1]); /* Also handles paths terminated by '/' */
290    
291     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
292     
293     return TRUE;
294 }
295
296 /******************************************************************************
297  * UNIXFS_path_to_pidl [Internal]
298  *
299  * PARAMS
300  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
301  *  path        [I] An absolute unix or dos path or a path relativ to pUnixFolder
302  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
303  *  
304  * RETURNS
305  *  Success: TRUE
306  *  Failure: FALSE, invalid params or out of memory
307  *
308  * NOTES
309  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
310  */
311 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
312     LPITEMIDLIST pidl;
313     struct stat fileStat;
314     int cSubDirs, cPidlLen, res, cPathLen;
315     char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
316
317     TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
318    
319     if (!ppidl || !path)
320         return FALSE;
321
322     cPathLen = lstrlenW(path);
323     
324     /* Build an absolute path and let pNextPathElement point to the interesting 
325      * relative sub-path. We need the absolute path to call 'stat', but the pidl
326      * will only contain the relative part.
327      */
328     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
329     {
330         /* Absolute dos path. Convert to unix */
331         char *pszUnixPath = wine_get_unix_file_name(path);
332         if (!UNIXFS_canonicalize_path(pszUnixPath, szCompletePath)) {
333             HeapFree(GetProcessHeap(), 0, pszUnixPath);
334             return FALSE;
335         }
336         HeapFree(GetProcessHeap(), 0, pszUnixPath);
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;
427
428     TRACE("(pidl=%p, pUnixFolder=%p)\n", pidl, pUnixFolder);
429     
430     pUnixFolder->m_pszPath = NULL;
431
432     /* Find the UnixFolderClass root */
433     while (current->mkid.cb) {
434         LPPIDLDATA pData = _ILGetDataPointer(current);
435         if (!pData) return FALSE;
436         if (pData->type == PT_GUID && 
437             (IsEqualIID(&CLSID_UnixFolder, &pData->u.guid.guid) ||
438              IsEqualIID(&CLSID_UnixDosFolder, &pData->u.guid.guid)))
439         {
440             break;
441         }
442         current = ILGetNext(current);
443     }
444     root = current = ILGetNext(current);
445     
446     /* Determine the path's length bytes */
447     dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
448     while (current->mkid.cb) {
449         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
450         current = ILGetNext(current);
451     };
452
453     /* Build the path */
454     pUnixFolder->m_pszPath = pNextDir = SHAlloc(dwPathLen);
455     if (!pUnixFolder->m_pszPath) {
456         WARN("SHAlloc failed!\n");
457         return FALSE;
458     }
459     current = root;
460     *pNextDir++ = '/';
461     while (current->mkid.cb) {
462         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
463         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
464         *pNextDir++ = '/';
465         current = ILGetNext(current);
466     }
467     *pNextDir='\0';
468     
469     TRACE("--> %s\n", pUnixFolder->m_pszPath);
470     return TRUE;
471 }
472
473 /******************************************************************************
474  * UNIXFS_build_subfolder_pidls [Internal]
475  *
476  * Builds an array of subfolder PIDLs relative to a unix directory
477  *
478  * PARAMS
479  *  path   [I] Name of a unix directory as a zero terminated ascii string
480  *  apidl  [O] The array of PIDLs
481  *  pCount [O] Size of apidl
482  *
483  * RETURNS
484  *  Success: TRUE
485  *  Failure: FALSE, path is not a valid unix directory or out of memory
486  *
487  * NOTES
488  *  The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
489  *  to release each PIDL as well as the array itself with SHFree.
490  */
491 static BOOL UNIXFS_build_subfolder_pidls(UnixFolder *pUnixFolder)
492 {
493     struct dirent *pDirEntry;
494     struct stat fileStat;
495     DIR *dir;
496     DWORD cDirEntries, i;
497     USHORT sLen;
498     char *pszFQPath;
499
500     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
501     
502     pUnixFolder->m_apidlSubDirs = NULL;
503     pUnixFolder->m_cSubDirs = 0;
504   
505     dir = opendir(pUnixFolder->m_pszPath);
506     if (!dir) {
507         WARN("Failed to open directory '%s'.\n", pUnixFolder->m_pszPath);
508         return FALSE;
509     }
510
511     /* Allocate space for fully qualified paths */
512     pszFQPath = SHAlloc(strlen(pUnixFolder->m_pszPath) + PATH_MAX);
513     if (!pszFQPath) {
514         WARN("SHAlloc failed!\n");
515         return FALSE;
516     }
517  
518     /* Count number of directory entries. */
519     for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) { 
520         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
521         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
522         if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
523     }
524
525     /* If there are no entries, we are done. */
526     if (cDirEntries == 0) {
527         closedir(dir);
528         SHFree(pszFQPath);
529         return TRUE;
530     }
531
532     /* Allocate the array of PIDLs */
533     pUnixFolder->m_apidlSubDirs = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
534     if (!pUnixFolder->m_apidlSubDirs) {
535         WARN("SHAlloc failed!\n");
536         return FALSE;
537     }
538   
539     /* Allocate and initialize one SHITEMID per sub-directory. */
540     for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
541         LPSHITEMID pid;
542             
543         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
544
545         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
546         if (stat(pszFQPath, &fileStat)) continue;
547         if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) continue;
548    
549         sLen = strlen(pDirEntry->d_name);
550         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
551         if (!pid) {
552             WARN("SHAlloc failed!\n");
553             return FALSE;
554         }
555         UNIXFS_build_shitemid(pDirEntry->d_name, &fileStat, pid);
556         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
557
558         pUnixFolder->m_apidlSubDirs[i++] = (LPITEMIDLIST)pid;
559     }
560     
561     pUnixFolder->m_cSubDirs = i;    
562     closedir(dir);
563     SHFree(pszFQPath);
564
565     return TRUE;
566 }
567
568 /******************************************************************************
569  * UnixFolderIcon 
570  *
571  * Singleton class, which is used by the shell to extract icons to represent
572  * folders in tree- and listviews. Currently, all this singleton does is to
573  * provide the shell with the absolute path to "shell32.dll" and with the 
574  * indices of the closed and opened folder icons in the resources of this dll.
575  */
576
577 /* UnixFolderIcon object layout and typedef.
578  */
579 typedef struct _UnixFolderIcon {
580     const IExtractIconWVtbl *lpIExtractIconWVtbl;
581     BOOL bFolder;
582 } UnixFolderIcon;
583
584 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_QueryInterface(IExtractIconW *iface, REFIID riid, 
585     void **ppv) 
586 {
587     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
588     
589     if (!ppv) return E_INVALIDARG;
590     
591     if (IsEqualIID(&IID_IUnknown, riid) ||
592         IsEqualIID(&IID_IExtractIconW, riid))
593     {
594         *ppv = iface;
595     } else {
596         *ppv = NULL;
597         return E_NOINTERFACE;
598     }
599
600     IExtractIconW_AddRef(iface);
601     return S_OK;
602 }
603
604 static ULONG WINAPI UnixFolderIcon_IExtractIconW_AddRef(IExtractIconW *iface) {
605     TRACE("(iface=%p)\n", iface);
606     return 2;
607 }
608
609 static ULONG WINAPI UnixFolderIcon_IExtractIconW_Release(IExtractIconW *iface) {
610     TRACE("(iface=%p)\n", iface);
611     return 1;
612 }
613
614 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_GetIconLocation(IExtractIconW *iface, 
615     UINT uFlags, LPWSTR szIconFile, UINT cchMax, INT* piIndex, UINT* pwFlags)
616 {
617     UnixFolderIcon *This = ADJUST_THIS(UnixFolderIcon, IExtractIconW, iface);
618         
619     TRACE("(iface=%p, uFlags=%u, szIconFile=%s, cchMax=%u, piIndex=%p, pwFlags=%p)\n",
620             iface, uFlags, debugstr_w(szIconFile), cchMax, piIndex, pwFlags);
621     
622     lstrcpynW(szIconFile, swShell32Name, cchMax);
623     if (This->bFolder) {
624         *piIndex = (uFlags & GIL_OPENICON) ? -IDI_SHELL_FOLDER_OPEN : -IDI_SHELL_FOLDER;
625     } else {
626         *piIndex = -IDI_SHELL_DOCUMENT;
627     }
628     *pwFlags = 0;
629
630     return S_OK;
631 }
632
633 static HRESULT WINAPI UnixFolderIcon_IExtractIconW_Extract(
634     IExtractIconW *iface, LPCWSTR pszFile, UINT nIconIndex, HICON* phiconLarge, HICON* phiconSmall, 
635     UINT nIconSize)
636 {
637     TRACE("(iface=%p, pszFile=%s, nIconIndex=%u, phiconLarge=%p, phiconSmall=%p, nIconSize=%u)"
638           "stub\n", iface, debugstr_w(pszFile), nIconIndex, phiconLarge, phiconSmall, nIconSize);
639
640     return E_NOTIMPL;
641 }
642
643 /* VTable for the IExtractIconW interface of the UnixFolderIcon class. 
644  */
645 static const IExtractIconWVtbl UnixFolderIcon_IExtractIconW_Vtbl = {
646     UnixFolderIcon_IExtractIconW_QueryInterface,
647     UnixFolderIcon_IExtractIconW_AddRef,
648     UnixFolderIcon_IExtractIconW_Release,
649     UnixFolderIcon_IExtractIconW_GetIconLocation,
650     UnixFolderIcon_IExtractIconW_Extract
651 };
652
653 /* The singleton instance
654  */
655 UnixFolderIcon UnixFolderIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, TRUE };
656 UnixFolderIcon UnixDocumentIconSingleton = { &UnixFolderIcon_IExtractIconW_Vtbl, FALSE };
657
658 /******************************************************************************
659  * UnixFolder
660  *
661  * Class whose heap based instances represent unix filesystem directories.
662  */
663
664 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
665     DWORD i;
666
667     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
668     
669     if (pUnixFolder->m_apidlSubDirs) 
670         for (i=0; i < pUnixFolder->m_cSubDirs; i++) 
671             SHFree(pUnixFolder->m_apidlSubDirs[i]);
672     SHFree(pUnixFolder->m_apidlSubDirs);
673     SHFree(pUnixFolder->m_pszPath);
674     ILFree(pUnixFolder->m_pidlLocation);
675     SHFree(pUnixFolder);
676 }
677
678 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
679     void **ppv) 
680 {
681     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
682         
683     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
684     
685     if (!ppv) return E_INVALIDARG;
686     
687     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
688         IsEqualIID(&IID_IShellFolder2, riid)) 
689     {
690         *ppv = &This->lpIShellFolder2Vtbl;
691     } else if (IsEqualIID(&IID_IPersistFolder2, riid) || IsEqualIID(&IID_IPersistFolder, riid) || 
692                IsEqualIID(&IID_IPersist, riid)) 
693     {
694         *ppv = &This->lpIPersistFolder2Vtbl;
695     } else {
696         *ppv = NULL;
697         return E_NOINTERFACE;
698     }
699
700     IUnknown_AddRef((IUnknown*)*ppv);
701     return S_OK;
702 }
703
704 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
705     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
706
707     TRACE("(iface=%p)\n", iface);
708
709     return InterlockedIncrement(&This->m_cRef);
710 }
711
712 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
713     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
714     ULONG cRef;
715     
716     TRACE("(iface=%p)\n", iface);
717
718     cRef = InterlockedDecrement(&This->m_cRef);
719     
720     if (!cRef) 
721         UnixFolder_Destroy(This);
722
723     return cRef;
724 }
725
726 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
727     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
728     ULONG* pdwAttributes)
729 {
730     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
731     BOOL result;
732
733     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
734           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
735           pchEaten, ppidl, pdwAttributes);
736
737     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
738     if (result && pdwAttributes && *pdwAttributes)
739     {
740         /* need to traverse to the last element for the attribute */
741         LPCITEMIDLIST pidl, last_pidl;
742         pidl = last_pidl = *ppidl;
743         while(pidl && pidl->mkid.cb)
744         {
745             last_pidl = pidl;
746             pidl = ILGetNext(pidl);
747         }
748         SHELL32_GetItemAttributes((IShellFolder*)iface, last_pidl, pdwAttributes);
749     }
750
751     if (!result) TRACE("FAILED!\n");
752     return result ? S_OK : E_FAIL;
753 }
754
755 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
756
757 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
758     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
759 {
760     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
761     IUnknown *newIterator;
762     HRESULT hr;
763     
764     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
765             iface, hwndOwner, grfFlags, ppEnumIDList);
766
767     newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
768     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
769     IUnknown_Release(newIterator);
770     
771     return hr;
772 }
773
774 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
775     LPBC pbcReserved, REFIID riid, void** ppvOut)
776 {
777     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
778     IPersistFolder2 *persistFolder;
779     LPITEMIDLIST pidlSubFolder;
780     HRESULT hr;
781         
782     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
783             iface, pidl, pbcReserved, riid, ppvOut);
784
785     if (This->m_dwPathMode == PATHMODE_DOS)
786         hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
787     else
788         hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
789         
790     if (!SUCCEEDED(hr)) return hr;
791     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
792     
793     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
794     IPersistFolder2_Initialize(persistFolder, pidlSubFolder);
795     IPersistFolder2_Release(persistFolder);
796     ILFree(pidlSubFolder);
797
798     return hr;
799 }
800
801 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
802     LPBC pbcReserved, REFIID riid, void** ppvObj)
803 {
804     TRACE("stub\n");
805     return E_NOTIMPL;
806 }
807
808 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
809     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
810 {
811     BOOL isEmpty1, isEmpty2;
812     HRESULT hr = E_FAIL;
813     LPITEMIDLIST firstpidl;
814     IShellFolder2 *psf;
815     int compare;
816
817     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
818     
819     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
820     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
821
822     if (isEmpty1 && isEmpty2) 
823         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
824     else if (isEmpty1)
825         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
826     else if (isEmpty2)
827         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
828
829     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
830         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
831     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
832         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
833
834     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
835                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
836                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
837     
838     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
839         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
840
841     if (pidl1->mkid.cb < pidl2->mkid.cb)
842         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
843     else if (pidl1->mkid.cb > pidl2->mkid.cb)
844         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
845
846     firstpidl = ILCloneFirst(pidl1);
847     pidl1 = ILGetNext(pidl1);
848     pidl2 = ILGetNext(pidl2);
849     
850     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
851     if (SUCCEEDED(hr)) {
852         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
853         IShellFolder2_Release(psf);
854     }
855
856     ILFree(firstpidl);
857     return hr;
858 }
859
860 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
861     REFIID riid, void** ppv)
862 {
863     HRESULT hr = E_INVALIDARG;
864         
865     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
866     
867     if (!ppv) return E_INVALIDARG;
868     *ppv = NULL;
869     
870     if (IsEqualIID(&IID_IShellView, riid)) {
871         LPSHELLVIEW pShellView;
872         
873         pShellView = IShellView_Constructor((IShellFolder*)iface);
874         if (pShellView) {
875             hr = IShellView_QueryInterface(pShellView, riid, ppv);
876             IShellView_Release(pShellView);
877         }
878     } 
879     
880     return hr;
881 }
882
883 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
884     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
885 {
886     UINT i;
887     SFGAOF flags= ~(SFGAOF)0;
888         
889     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p) semi-stub\n", iface, cidl, apidl, rgfInOut);
890    
891     for (i=0; i<cidl; i++) {
892         LPPIDLDATA pData = _ILGetDataPointer(apidl[i]);
893         if (!pData) continue;
894         if (pData->type == PT_FOLDER) flags &= (SFGAO_FILESYSTEM|SFGAO_FILESYSANCESTOR|SFGAO_FOLDER|SFGAO_HASSUBFOLDER);
895         if (pData->type == PT_VALUE) flags &= SFGAO_FILESYSTEM;
896     }
897     
898     *rgfInOut = *rgfInOut & flags;
899             
900     return S_OK;
901 }
902
903 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
904     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
905 {
906     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
907     
908     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
909         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
910
911     if (IsEqualIID(&IID_IContextMenu, riid)) {
912         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
913         return S_OK;
914     } else if (IsEqualIID(&IID_IDataObject, riid)) {
915         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
916         return S_OK;
917     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
918         FIXME("IExtractIconA\n");
919         return E_FAIL;
920     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
921         if (cidl != 1) return E_FAIL;
922         if (_ILIsFolder(apidl[0])) 
923             *ppvOut = &UnixFolderIconSingleton;
924         else
925             *ppvOut = &UnixDocumentIconSingleton;
926         return S_OK;
927     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
928         FIXME("IDropTarget\n");
929         return E_FAIL;
930     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
931         FIXME("IShellLinkW\n");
932         return E_FAIL;
933     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
934         FIXME("IShellLinkA\n");
935         return E_FAIL;
936     } else {
937         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
938         return E_NOINTERFACE;
939     }
940 }
941
942 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
943     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
944 {
945     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
946     HRESULT hr = S_OK;    
947
948     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
949     
950     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
951         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
952     {
953         if (!pidl->mkid.cb) {
954             lpName->uType = STRRET_CSTR;
955             strcpy(lpName->u.cStr, This->m_pszPath);
956             if (This->m_dwPathMode == PATHMODE_DOS) {
957                 char path[MAX_PATH];
958                 GetFullPathNameA(lpName->u.cStr, MAX_PATH, path, NULL);
959                 strcpy(lpName->u.cStr, path);
960             }
961         } else {
962             IShellFolder *pSubFolder;
963             USHORT emptyIDL = 0;
964
965             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
966             if (!SUCCEEDED(hr)) return hr;
967        
968             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
969             IShellFolder_Release(pSubFolder);
970         }
971     } else {
972         char *pszFileName = _ILGetTextPointer(pidl);
973         lpName->uType = STRRET_CSTR;
974         strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
975     }
976
977     TRACE("--> %s\n", lpName->u.cStr);
978     
979     return hr;
980 }
981
982 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* This, HWND hwnd, 
983     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
984 {
985     TRACE("stub\n");
986     return E_NOTIMPL;
987 }
988
989 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
990     IEnumExtraSearch **ppEnum) 
991 {
992     TRACE("stub\n");
993     return E_NOTIMPL;
994 }
995
996 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
997     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
998 {
999     TRACE("stub\n");
1000     return E_NOTIMPL;
1001 }
1002
1003 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1004     UINT iColumn, SHCOLSTATEF *pcsFlags)
1005 {
1006     TRACE("stub\n");
1007     return E_NOTIMPL;
1008 }
1009
1010 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1011     GUID *pguid)
1012 {
1013     TRACE("stub\n");
1014     return E_NOTIMPL;
1015 }
1016
1017 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1018     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1019 {
1020     TRACE("stub\n");
1021     return E_NOTIMPL;
1022 }
1023
1024 #define SHELLVIEWCOLUMNS 6 
1025
1026 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1027     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1028 {
1029     HRESULT hr = E_FAIL;
1030     struct passwd *pPasswd;
1031     struct group *pGroup;
1032     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1033         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1034         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1035         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1036         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1037         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
1038         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
1039     };
1040
1041     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1042     
1043     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1044         return E_INVALIDARG;
1045
1046     if (!pidl) {
1047         psd->fmt = SFHeader[iColumn].fmt;
1048         psd->cxChar = SFHeader[iColumn].cxChar;
1049         psd->str.uType = STRRET_CSTR;
1050         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1051         return S_OK;
1052     } else {
1053         StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
1054         psd->str.u.cStr[0] = '\0';
1055         psd->str.uType = STRRET_CSTR;
1056         switch (iColumn) {
1057             case 0:
1058                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1059                 break;
1060             case 1:
1061                 psd->str.u.cStr[0] = S_ISDIR(pStatStruct->st_mode) ? 'd' : '-';
1062                 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
1063                 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
1064                 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
1065                 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
1066                 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
1067                 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
1068                 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
1069                 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
1070                 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
1071                 psd->str.u.cStr[10] = '\0';
1072                 break;
1073             case 2:
1074                 pPasswd = getpwuid(pStatStruct->st_uid);
1075                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1076                 break;
1077             case 3:
1078                 pGroup = getgrgid(pStatStruct->st_gid);
1079                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1080                 break;
1081             case 4:
1082                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1083                 break;
1084             case 5:
1085                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1086                 break;
1087         }
1088     }
1089     
1090     return hr;
1091 }
1092
1093 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1094     SHCOLUMNID *pscid) 
1095 {
1096     TRACE("stub\n");
1097     return E_NOTIMPL;
1098 }
1099
1100 /* VTable for UnixFolder's IShellFolder2 interface.
1101  */
1102 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1103     UnixFolder_IShellFolder2_QueryInterface,
1104     UnixFolder_IShellFolder2_AddRef,
1105     UnixFolder_IShellFolder2_Release,
1106     UnixFolder_IShellFolder2_ParseDisplayName,
1107     UnixFolder_IShellFolder2_EnumObjects,
1108     UnixFolder_IShellFolder2_BindToObject,
1109     UnixFolder_IShellFolder2_BindToStorage,
1110     UnixFolder_IShellFolder2_CompareIDs,
1111     UnixFolder_IShellFolder2_CreateViewObject,
1112     UnixFolder_IShellFolder2_GetAttributesOf,
1113     UnixFolder_IShellFolder2_GetUIObjectOf,
1114     UnixFolder_IShellFolder2_GetDisplayNameOf,
1115     UnixFolder_IShellFolder2_SetNameOf,
1116     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1117     UnixFolder_IShellFolder2_EnumSearches,
1118     UnixFolder_IShellFolder2_GetDefaultColumn,
1119     UnixFolder_IShellFolder2_GetDefaultColumnState,
1120     UnixFolder_IShellFolder2_GetDetailsEx,
1121     UnixFolder_IShellFolder2_GetDetailsOf,
1122     UnixFolder_IShellFolder2_MapColumnToSCID
1123 };
1124
1125 static HRESULT WINAPI UnixFolder_IPersistFolder2_QueryInterface(IPersistFolder2* This, REFIID riid, 
1126     void** ppvObject)
1127 {
1128     return UnixFolder_IShellFolder2_QueryInterface(
1129                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This), riid, ppvObject);
1130 }
1131
1132 static ULONG WINAPI UnixFolder_IPersistFolder2_AddRef(IPersistFolder2* This)
1133 {
1134     return UnixFolder_IShellFolder2_AddRef(
1135                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1136 }
1137
1138 static ULONG WINAPI UnixFolder_IPersistFolder2_Release(IPersistFolder2* This)
1139 {
1140     return UnixFolder_IShellFolder2_Release(
1141                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1142 }
1143
1144 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetClassID(IPersistFolder2* This, CLSID* pClassID)
1145 {
1146     TRACE("stub\n");
1147     return E_NOTIMPL;
1148 }
1149
1150 static HRESULT WINAPI UnixFolder_IPersistFolder2_Initialize(IPersistFolder2* iface, LPCITEMIDLIST pidl)
1151 {
1152     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1153     
1154     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1155
1156     This->m_pidlLocation = ILClone(pidl);
1157     
1158     pdump(pidl);
1159     
1160     UNIXFS_pidl_to_path(pidl, This);
1161     UNIXFS_build_subfolder_pidls(This);
1162    
1163     return S_OK;
1164 }
1165
1166 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetCurFolder(IPersistFolder2* iface, LPITEMIDLIST* ppidl)
1167 {
1168     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1169     
1170     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1171
1172     if (!ppidl)
1173         return E_POINTER;
1174     *ppidl = ILClone (This->m_pidlLocation);
1175     return S_OK;
1176 }
1177     
1178 /* VTable for UnixFolder's IPersistFolder interface.
1179  */
1180 static const IPersistFolder2Vtbl UnixFolder_IPersistFolder2_Vtbl = {
1181     UnixFolder_IPersistFolder2_QueryInterface,
1182     UnixFolder_IPersistFolder2_AddRef,
1183     UnixFolder_IPersistFolder2_Release,
1184     UnixFolder_IPersistFolder2_GetClassID,
1185     UnixFolder_IPersistFolder2_Initialize,
1186     UnixFolder_IPersistFolder2_GetCurFolder
1187 };
1188
1189 /******************************************************************************
1190  * Unix[Dos]Folder_Constructor [Internal]
1191  *
1192  * PARAMS
1193  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1194  *  riid      [I] Interface asked for by the client.
1195  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1196  *
1197  * NOTES
1198  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1199  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1200  *  compatible signature.
1201  *
1202  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1203  *  means that paths are converted from dos to unix and back at the interfaces.
1204  */
1205 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode) {
1206     HRESULT hr = E_FAIL;
1207     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1208     
1209     if(pUnixFolder) {
1210         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1211         pUnixFolder->lpIPersistFolder2Vtbl = &UnixFolder_IPersistFolder2_Vtbl;
1212         pUnixFolder->m_cRef = 0;
1213         pUnixFolder->m_pszPath = NULL;
1214         pUnixFolder->m_apidlSubDirs = NULL;
1215         pUnixFolder->m_cSubDirs = 0;
1216         pUnixFolder->m_dwPathMode = dwPathMode;
1217
1218         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1219         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1220         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1221     }
1222     return hr;
1223 }
1224
1225 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1226     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1227     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX);
1228 }
1229
1230 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1231     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1232     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS);
1233 }
1234
1235 /******************************************************************************
1236  * UnixSubFolderIterator
1237  *
1238  * Class whose heap based objects represent iterators over the sub-directories
1239  * of a given UnixFolder object. 
1240  */
1241
1242 /* UnixSubFolderIterator object layout and typedef.
1243  */
1244 typedef struct _UnixSubFolderIterator {
1245     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1246     ULONG m_cRef;
1247     UnixFolder *m_pUnixFolder;
1248     ULONG m_cIdx;
1249     SHCONTF m_fFilter;
1250 } UnixSubFolderIterator;
1251
1252 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1253     TRACE("(iterator=%p)\n", iterator);
1254         
1255     UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1256     SHFree(iterator);
1257 }
1258
1259 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1260     REFIID riid, void** ppv)
1261 {
1262     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1263     
1264     if (!ppv) return E_INVALIDARG;
1265     
1266     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1267         *ppv = iface;
1268     } else {
1269         *ppv = NULL;
1270         return E_NOINTERFACE;
1271     }
1272
1273     IEnumIDList_AddRef(iface);
1274     return S_OK;
1275 }
1276                             
1277 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1278 {
1279     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1280
1281     TRACE("(iface=%p)\n", iface);
1282    
1283     return InterlockedIncrement(&This->m_cRef);
1284 }
1285
1286 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1287 {
1288     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1289     ULONG cRef;
1290     
1291     TRACE("(iface=%p)\n", iface);
1292
1293     cRef = InterlockedDecrement(&This->m_cRef);
1294     
1295     if (!cRef) 
1296         UnixSubFolderIterator_Destroy(This);
1297
1298     return cRef;
1299 }
1300
1301 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1302     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1303 {
1304     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1305     ULONG i;
1306
1307     TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1308    
1309     for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1310         LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1311         if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1312             rgelt[i] = ILClone(pCurrent);
1313             i++;
1314         }
1315     }
1316
1317     if (pceltFetched)
1318         *pceltFetched = i;
1319
1320     return i == celt ? S_OK : S_FALSE;
1321 }
1322
1323 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1324 {
1325     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1326     ULONG i;
1327     
1328     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1329
1330     for (i=0; i < celt; i++) {
1331         while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1332                !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter)) 
1333         {
1334             This->m_cIdx++;
1335         }
1336         This->m_cIdx++;
1337     }
1338     
1339     if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1340         This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1341         return S_FALSE;
1342     } else {
1343         return S_OK;
1344     }
1345 }
1346
1347 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1348 {
1349     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1350         
1351     TRACE("(iface=%p)\n", iface);
1352
1353     This->m_cIdx = 0;
1354     
1355     return S_OK;
1356 }
1357
1358 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1359     IEnumIDList** ppenum)
1360 {
1361     TRACE("stub\n");
1362     return E_NOTIMPL;
1363 }
1364
1365 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1366  */
1367 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1368     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1369     UnixSubFolderIterator_IEnumIDList_AddRef,
1370     UnixSubFolderIterator_IEnumIDList_Release,
1371     UnixSubFolderIterator_IEnumIDList_Next,
1372     UnixSubFolderIterator_IEnumIDList_Skip,
1373     UnixSubFolderIterator_IEnumIDList_Reset,
1374     UnixSubFolderIterator_IEnumIDList_Clone
1375 };
1376
1377 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1378     UnixSubFolderIterator *iterator;
1379
1380     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1381     
1382     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1383     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1384     iterator->m_cRef = 0;
1385     iterator->m_cIdx = 0;
1386     iterator->m_pUnixFolder = pUnixFolder;
1387     iterator->m_fFilter = fFilter;
1388
1389     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1390     UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1391     
1392     return (IUnknown*)iterator;
1393 }