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