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