Prepare for adding new tests.
[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     ULONG 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                 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
284                 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
285                 *pszCanonicalPath = '\0';
286                     pCanonicalTail = pszCanonicalPath;
287                     pPathTail = szPath;
288             } else {
289                 /* Relative link. Expand into szPath and continue. */
290                 char szTemp[FILENAME_MAX];
291                 int cTailLen = strlen(pPathTail);
292
293                 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
294                     return FALSE;
295
296                 memcpy(szTemp, pPathTail, cTailLen + 1);
297                 memcpy(pElement + 1, szSymlink, cLinkLen);
298                 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
299                 pPathTail = pElement;
300             }
301         } else {
302             /* Regular directory or file. Copy to canonical path */
303             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
304                 return FALSE;
305                 
306             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
307             pCanonicalTail += pPathTail - pElement;
308             *pPathTail = cTemp;
309         }
310     } while (pPathTail[0] == '/' && pPathTail[1]); /* Also handles paths terminated by '/' */
311    
312     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
313     
314     return TRUE;
315 }
316
317 /******************************************************************************
318  * UNIXFS_build_shitemid [Internal]
319  *
320  * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into 
321  * buffer 'pIDL'. Decorates the SHITEMID with information from a stat system call.
322  *
323  * PARAMS
324  *  pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
325  *  bParentIsFS [I] Set, if the parent of the last path component is within the wine filesystem.
326  *  pIDL        [O] SHITEMID will be constructed here.
327  *
328  * RETURNS
329  *  Success: A pointer to the terminating '\0' character of path.
330  *  Failure: NULL
331  *
332  * NOTES
333  *  Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
334  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
335  *  a 0 USHORT value.
336  */
337 static char* UNIXFS_build_shitemid(char *pszUnixPath, BOOL bParentIsFS, void *pIDL) {
338     LARGE_INTEGER time;
339     FILETIME fileTime;
340     LPPIDLDATA pIDLData;
341     struct stat fileStat;
342     StatStruct *pStatStruct;
343     char *pszComponent;
344     int cComponentLen;
345
346     TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
347
348     /* Compute the SHITEMID's length and wipe it. */
349     pszComponent = strrchr(pszUnixPath, '/') + 1;
350     cComponentLen = strlen(pszComponent);
351     memset(pIDL, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen));
352     ((LPSHITEMID)pIDL)->cb = SHITEMID_LEN_FROM_NAME_LEN(cComponentLen) ;
353    
354     /* We are only interested in regular files and directories. */
355     if (stat(pszUnixPath, &fileStat)) return NULL;
356     if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
357     
358     pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pIDL);
359     pStatStruct->st_mode = fileStat.st_mode;
360     pStatStruct->st_uid = fileStat.st_uid;
361     pStatStruct->st_gid = fileStat.st_gid;
362     pStatStruct->sfAttr = S_ISDIR(fileStat.st_mode) ? (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR) : 0;
363     if (bParentIsFS || UNIXFS_is_dos_device(&fileStat)) pStatStruct->sfAttr |= SFGAO_FILESYSTEM;
364
365     /* Set shell32's standard SHITEMID data fields. */
366     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
367     pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
368     pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
369     RtlSecondsSince1970ToTime( fileStat.st_mtime, &time );
370     fileTime.dwLowDateTime = time.u.LowPart;
371     fileTime.dwHighDateTime = time.u.HighPart;
372     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
373     pIDLData->u.file.uFileAttribs = 0;
374     if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
375     if (pszComponent[0] == '.') pIDLData->u.file.uFileAttribs |=  FILE_ATTRIBUTE_HIDDEN;
376     memcpy(pIDLData->u.file.szNames, pszComponent, cComponentLen);
377
378     return pszComponent + cComponentLen;
379 }
380
381 /******************************************************************************
382  * UNIXFS_path_to_pidl [Internal]
383  *
384  * PARAMS
385  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
386  *  path        [I] An absolute unix or dos path or a path relativ to pUnixFolder
387  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
388  *  
389  * RETURNS
390  *  Success: TRUE
391  *  Failure: FALSE, invalid params or out of memory
392  *
393  * NOTES
394  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
395  */
396 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
397     LPITEMIDLIST pidl;
398     int cSubDirs, cPidlLen, cPathLen;
399     char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
400     BOOL bParentIsFS = dwRootAttr & SFGAO_FILESYSTEM;
401
402     TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
403    
404     if (!ppidl || !path)
405         return FALSE;
406
407     /* Build an absolute path and let pNextPathElement point to the interesting 
408      * relative sub-path. We need the absolute path to call 'stat', but the pidl
409      * will only contain the relative part.
410      */
411     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
412     {
413         /* Absolute dos path. Convert to unix */
414         if (!UNIXFS_get_unix_path(path, szCompletePath))
415             return FALSE;
416         pNextPathElement = szCompletePath;
417     } 
418     else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/')) 
419     {
420         /* Absolute unix path. Just convert to ANSI. */
421         WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL); 
422         pNextPathElement = szCompletePath;
423     } 
424     else 
425     {
426         /* Relative dos or unix path. Concat with this folder's path */
427         int cBasePathLen = strlen(pUnixFolder->m_pszPath);
428         memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
429         WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath + cBasePathLen, 
430                             FILENAME_MAX - cBasePathLen, NULL, NULL);
431         pNextPathElement = szCompletePath + cBasePathLen - 1;
432         
433         /* If in dos mode, replace '\' with '/' */
434         if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
435             char *pBackslash = strchr(pNextPathElement, '\\');
436             while (pBackslash) {
437                 *pBackslash = '/';
438                 pBackslash = strchr(pBackslash, '\\');
439             }
440         }
441     }
442
443     /* Remove trailing slash, if present */
444     cPathLen = strlen(szCompletePath);
445     if (szCompletePath[cPathLen-1] == '/') 
446         szCompletePath[cPathLen-1] = '\0';
447
448     if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
449         ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
450         return FALSE;
451     }
452     
453     /* At this point, we have an absolute unix path in szCompletePath 
454      * and the relative portion of it in pNextPathElement. Both starting with '/'
455      * and _not_ terminated by a '/'. */
456     TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
457     
458     /* Count the number of sub-directories in the path */
459     cSubDirs = 0;
460     pSlash = pNextPathElement;
461     while (pSlash) {
462         cSubDirs++;
463         pSlash = strchr(pSlash+1, '/');
464     }
465   
466     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
467      * characters, which are not stored in the ITEMIDLIST. */
468     cPidlLen = strlen(pNextPathElement) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
469     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
470     if (!pidl) return FALSE;
471
472     /* Concatenate the SHITEMIDs of the sub-directories. */
473     while (*pNextPathElement) {
474         pSlash = strchr(pNextPathElement+1, '/');
475         if (pSlash) *pSlash = '\0';
476         pNextPathElement = UNIXFS_build_shitemid(szCompletePath, bParentIsFS, pidl);
477         if (pSlash) *pSlash = '/';
478             
479         if (!pNextPathElement) {
480             SHFree(pidl);
481             return FALSE;
482         }
483         if (!bParentIsFS && (LPSTATSTRUCT_FROM_LPSHITEMID(pidl)->sfAttr & SFGAO_FILESYSTEM)) 
484             bParentIsFS = TRUE;
485         pidl = ILGetNext(pidl);
486     }
487     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
488
489     if ((int)pidl-(int)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */ 
490         ERR("Computed length of pidl incorrect. Please report.\n");
491     
492     return TRUE;
493 }
494
495 /******************************************************************************
496  * UNIXFS_pidl_to_path [Internal]
497  *
498  * Construct the unix path that corresponds to a fully qualified ITEMIDLIST
499  *
500  * PARAMS
501  *  pidl [I] ITEMIDLIST that specifies the absolute location of the folder
502  *  path [O] The corresponding unix path as a zero terminated ascii string
503  *
504  * RETURNS
505  *  Success: TRUE
506  *  Failure: FALSE, pidl doesn't specify a unix path or out of memory
507  */
508 static BOOL UNIXFS_pidl_to_path(LPCITEMIDLIST pidl, UnixFolder *pUnixFolder) {
509     LPCITEMIDLIST current = pidl, root;
510     DWORD dwPathLen;
511     char *pNextDir, szBasePath[FILENAME_MAX] = "/";
512
513     TRACE("(pidl=%p, pUnixFolder=%p)\n", pidl, pUnixFolder);
514     pdump(pidl);
515     
516     pUnixFolder->m_pszPath = NULL;
517
518     /* Find the UnixFolderClass root */
519     while (current->mkid.cb) {
520         if (_ILIsDrive(current) || /* The dos drive, which maps to '/' */
521             (_ILIsSpecialFolder(current) && 
522              (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
523               IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current)))))
524         {
525             break;
526         }
527         current = ILGetNext(current);
528     }
529
530     if (current && current->mkid.cb) {
531         root = current = ILGetNext(current);
532         dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
533     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
534         /* Path rooted at Desktop */
535         WCHAR wszDesktopPath[MAX_PATH];
536         if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
537            return FALSE;
538         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
539             return FALSE;
540         dwPathLen = strlen(szBasePath) + 1;
541         root = current;
542     } else {
543         ERR("Unknown pidl type!\n");
544         pdump(pidl);
545         return FALSE;
546     }
547     
548     /* Determine the path's length bytes */
549     while (current && current->mkid.cb) {
550         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
551         current = ILGetNext(current);
552     };
553
554     /* Build the path */
555     pUnixFolder->m_pszPath = pNextDir = SHAlloc(dwPathLen);
556     if (!pUnixFolder->m_pszPath) {
557         WARN("SHAlloc failed!\n");
558         return FALSE;
559     }
560     current = root;
561     strcpy(pNextDir, szBasePath);
562     pNextDir += strlen(szBasePath);
563     while (current && current->mkid.cb) {
564         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
565         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
566         *pNextDir++ = '/';
567         current = ILGetNext(current);
568     }
569     *pNextDir='\0';
570     
571     TRACE("--> %s\n", pUnixFolder->m_pszPath);
572     return TRUE;
573 }
574
575 /******************************************************************************
576  * UNIXFS_build_subfolder_pidls [Internal]
577  *
578  * Builds an array of subfolder PIDLs relative to a unix directory
579  *
580  * PARAMS
581  *  path   [I] Name of a unix directory as a zero terminated ascii string
582  *  apidl  [O] The array of PIDLs
583  *  pCount [O] Size of apidl
584  *
585  * RETURNS
586  *  Success: TRUE
587  *  Failure: FALSE, path is not a valid unix directory or out of memory
588  *
589  * NOTES
590  *  The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
591  *  to release each PIDL as well as the array itself with SHFree.
592  */
593 static BOOL UNIXFS_build_subfolder_pidls(UnixFolder *pUnixFolder)
594 {
595     struct dirent *pDirEntry;
596     struct stat fileStat;
597     DIR *dir;
598     DWORD cDirEntries, i;
599     USHORT sLen;
600     char *pszFQPath;
601     BOOL bParentIsFS = dwRootAttr & SFGAO_FILESYSTEM;
602     
603     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
604     
605     pUnixFolder->m_apidlSubDirs = NULL;
606     pUnixFolder->m_cSubDirs = 0;
607
608     if (pUnixFolder->m_pidlLocation) {
609         StatStruct *statStruct = 
610             LPSTATSTRUCT_FROM_LPSHITEMID(ILFindLastID(pUnixFolder->m_pidlLocation));
611         if (statStruct) bParentIsFS = statStruct->sfAttr & SFGAO_FILESYSTEM;
612     }
613   
614     dir = opendir(pUnixFolder->m_pszPath);
615     if (!dir) {
616         WARN("Failed to open directory '%s'.\n", pUnixFolder->m_pszPath);
617         return FALSE;
618     }
619
620     /* Allocate space for fully qualified paths */
621     pszFQPath = SHAlloc(strlen(pUnixFolder->m_pszPath) + PATH_MAX);
622     if (!pszFQPath) {
623         WARN("SHAlloc failed!\n");
624         return FALSE;
625     }
626  
627     /* Count number of directory entries. */
628     for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) { 
629         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
630         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
631         if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
632     }
633
634     /* If there are no entries, we are done. */
635     if (cDirEntries == 0) {
636         closedir(dir);
637         SHFree(pszFQPath);
638         return TRUE;
639     }
640
641     /* Allocate the array of PIDLs */
642     pUnixFolder->m_apidlSubDirs = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
643     if (!pUnixFolder->m_apidlSubDirs) {
644         WARN("SHAlloc failed!\n");
645         return FALSE;
646     }
647   
648     /* Allocate and initialize one SHITEMID per sub-directory. */
649     for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
650         LPSHITEMID pid;
651             
652         if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
653
654         sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
655    
656         sLen = strlen(pDirEntry->d_name);
657         pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
658         if (!pid) {
659             WARN("SHAlloc failed!\n");
660             return FALSE;
661         }
662         if (!UNIXFS_build_shitemid(pszFQPath, bParentIsFS, pid)) {
663             SHFree(pid);
664             continue;
665         }
666         memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
667
668         pUnixFolder->m_apidlSubDirs[i++] = (LPITEMIDLIST)pid;
669     }
670     
671     pUnixFolder->m_cSubDirs = i;    
672     closedir(dir);
673     SHFree(pszFQPath);
674
675     return TRUE;
676 }
677
678 /******************************************************************************
679  * UnixFolder
680  *
681  * Class whose heap based instances represent unix filesystem directories.
682  */
683
684 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
685     DWORD i;
686
687     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
688     
689     if (pUnixFolder->m_apidlSubDirs) 
690         for (i=0; i < pUnixFolder->m_cSubDirs; i++) 
691             SHFree(pUnixFolder->m_apidlSubDirs[i]);
692     SHFree(pUnixFolder->m_apidlSubDirs);
693     SHFree(pUnixFolder->m_pszPath);
694     ILFree(pUnixFolder->m_pidlLocation);
695     SHFree(pUnixFolder);
696 }
697
698 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
699     void **ppv) 
700 {
701     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
702         
703     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
704     
705     if (!ppv) return E_INVALIDARG;
706     
707     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
708         IsEqualIID(&IID_IShellFolder2, riid)) 
709     {
710         *ppv = &This->lpIShellFolder2Vtbl;
711     } else if (IsEqualIID(&IID_IPersistFolder2, riid) || IsEqualIID(&IID_IPersistFolder, riid) || 
712                IsEqualIID(&IID_IPersist, riid)) 
713     {
714         *ppv = &This->lpIPersistFolder2Vtbl;
715     } else {
716         *ppv = NULL;
717         return E_NOINTERFACE;
718     }
719
720     IUnknown_AddRef((IUnknown*)*ppv);
721     return S_OK;
722 }
723
724 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
725     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
726
727     TRACE("(iface=%p)\n", iface);
728
729     return InterlockedIncrement(&This->m_cRef);
730 }
731
732 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
733     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
734     ULONG cRef;
735     
736     TRACE("(iface=%p)\n", iface);
737
738     cRef = InterlockedDecrement(&This->m_cRef);
739     
740     if (!cRef) 
741         UnixFolder_Destroy(This);
742
743     return cRef;
744 }
745
746 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
747     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
748     ULONG* pdwAttributes)
749 {
750     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
751     BOOL result;
752
753     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
754           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
755           pchEaten, ppidl, pdwAttributes);
756
757     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
758     if (result && pdwAttributes && *pdwAttributes)
759     {
760         /* need to traverse to the last element for the attribute */
761         LPCITEMIDLIST pidl, last_pidl;
762         pidl = last_pidl = *ppidl;
763         while(pidl && pidl->mkid.cb)
764         {
765             last_pidl = pidl;
766             pidl = ILGetNext(pidl);
767         }
768         SHELL32_GetItemAttributes((IShellFolder*)iface, last_pidl, pdwAttributes);
769     }
770
771     if (!result) TRACE("FAILED!\n");
772     return result ? S_OK : E_FAIL;
773 }
774
775 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
776
777 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
778     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
779 {
780     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
781     IUnknown *newIterator;
782     HRESULT hr;
783     
784     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
785             iface, hwndOwner, grfFlags, ppEnumIDList);
786
787     if (This->m_cSubDirs == -1) 
788         UNIXFS_build_subfolder_pidls(This);
789     
790     newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
791     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
792     IUnknown_Release(newIterator);
793     
794     return hr;
795 }
796
797 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
798     LPBC pbcReserved, REFIID riid, void** ppvOut)
799 {
800     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
801     IPersistFolder2 *persistFolder;
802     LPITEMIDLIST pidlSubFolder;
803     HRESULT hr;
804         
805     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
806             iface, pidl, pbcReserved, riid, ppvOut);
807
808     if (!pidl || !pidl->mkid.cb)
809         return E_INVALIDARG;
810     
811     if (This->m_dwPathMode == PATHMODE_DOS)
812         hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
813     else
814         hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
815         
816     if (!SUCCEEDED(hr)) return hr;
817     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
818     
819     pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
820     IPersistFolder2_Initialize(persistFolder, pidlSubFolder);
821     IPersistFolder2_Release(persistFolder);
822     ILFree(pidlSubFolder);
823
824     return hr;
825 }
826
827 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
828     LPBC pbcReserved, REFIID riid, void** ppvObj)
829 {
830     TRACE("stub\n");
831     return E_NOTIMPL;
832 }
833
834 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
835     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
836 {
837     BOOL isEmpty1, isEmpty2;
838     HRESULT hr = E_FAIL;
839     LPITEMIDLIST firstpidl;
840     IShellFolder2 *psf;
841     int compare;
842
843     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
844     
845     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
846     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
847
848     if (isEmpty1 && isEmpty2) 
849         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
850     else if (isEmpty1)
851         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
852     else if (isEmpty2)
853         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
854
855     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
856         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
857     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
858         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
859
860     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
861                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
862                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
863     
864     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
865         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
866
867     if (pidl1->mkid.cb < pidl2->mkid.cb)
868         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
869     else if (pidl1->mkid.cb > pidl2->mkid.cb)
870         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
871
872     firstpidl = ILCloneFirst(pidl1);
873     pidl1 = ILGetNext(pidl1);
874     pidl2 = ILGetNext(pidl2);
875     
876     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
877     if (SUCCEEDED(hr)) {
878         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
879         IShellFolder2_Release(psf);
880     }
881
882     ILFree(firstpidl);
883     return hr;
884 }
885
886 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
887     REFIID riid, void** ppv)
888 {
889     HRESULT hr = E_INVALIDARG;
890         
891     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
892     
893     if (!ppv) return E_INVALIDARG;
894     *ppv = NULL;
895     
896     if (IsEqualIID(&IID_IShellView, riid)) {
897         LPSHELLVIEW pShellView;
898         
899         pShellView = IShellView_Constructor((IShellFolder*)iface);
900         if (pShellView) {
901             hr = IShellView_QueryInterface(pShellView, riid, ppv);
902             IShellView_Release(pShellView);
903         }
904     } 
905     
906     return hr;
907 }
908
909 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
910     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
911 {
912     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
913     const StatStruct *pStatStruct;
914         
915     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
916  
917     if (!rgfInOut || (cidl && !apidl)) 
918         return E_INVALIDARG;
919     
920     if (cidl == 0) {
921         if (!strcmp(This->m_pszPath, "/")) {
922             *rgfInOut &= dwRootAttr;
923         } else {
924             pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(ILFindLastID(This->m_pidlLocation));
925             if (!pStatStruct) return E_FAIL;                
926             *rgfInOut &= pStatStruct->sfAttr;
927         }
928     } else {
929         UINT i;
930         for (i=0; i<cidl; i++) {
931             pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(apidl[i]);
932             if (!pStatStruct) return E_INVALIDARG;
933             *rgfInOut &= pStatStruct->sfAttr;
934         }
935     }
936     
937     return S_OK;
938 }
939
940 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
941     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
942 {
943     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
944     
945     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
946         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
947
948     if (IsEqualIID(&IID_IContextMenu, riid)) {
949         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
950         return S_OK;
951     } else if (IsEqualIID(&IID_IDataObject, riid)) {
952         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
953         return S_OK;
954     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
955         LPITEMIDLIST pidl;
956         if (cidl != 1) return E_FAIL;
957         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
958         *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
959         SHFree(pidl);
960         return S_OK;
961     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
962         LPITEMIDLIST pidl;
963         if (cidl != 1) return E_FAIL;
964         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
965         *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
966         SHFree(pidl);
967         return S_OK;
968     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
969         FIXME("IDropTarget\n");
970         return E_FAIL;
971     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
972         FIXME("IShellLinkW\n");
973         return E_FAIL;
974     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
975         FIXME("IShellLinkA\n");
976         return E_FAIL;
977     } else {
978         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
979         return E_NOINTERFACE;
980     }
981 }
982
983 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
984     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
985 {
986     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
987     HRESULT hr = S_OK;    
988
989     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
990     
991     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
992         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
993     {
994         if (!pidl->mkid.cb) {
995             lpName->uType = STRRET_CSTR;
996             strcpy(lpName->u.cStr, This->m_pszPath);
997             if (This->m_dwPathMode == PATHMODE_DOS) {
998                 char path[MAX_PATH];
999                 GetFullPathNameA(lpName->u.cStr, MAX_PATH, path, NULL);
1000                 PathRemoveBackslashA(path);
1001                 strcpy(lpName->u.cStr, path);
1002             }
1003         } else {
1004             IShellFolder *pSubFolder;
1005             USHORT emptyIDL = 0;
1006
1007             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1008             if (!SUCCEEDED(hr)) return hr;
1009        
1010             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1011             IShellFolder_Release(pSubFolder);
1012         }
1013     } else {
1014         char *pszFileName = _ILGetTextPointer(pidl);
1015         lpName->uType = STRRET_CSTR;
1016         strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
1017     }
1018
1019     TRACE("--> %s\n", lpName->u.cStr);
1020     
1021     return hr;
1022 }
1023
1024 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* This, HWND hwnd, 
1025     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1026 {
1027     TRACE("stub\n");
1028     return E_NOTIMPL;
1029 }
1030
1031 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
1032     IEnumExtraSearch **ppEnum) 
1033 {
1034     TRACE("stub\n");
1035     return E_NOTIMPL;
1036 }
1037
1038 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
1039     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
1040 {
1041     TRACE("stub\n");
1042     return E_NOTIMPL;
1043 }
1044
1045 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1046     UINT iColumn, SHCOLSTATEF *pcsFlags)
1047 {
1048     TRACE("stub\n");
1049     return E_NOTIMPL;
1050 }
1051
1052 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1053     GUID *pguid)
1054 {
1055     TRACE("stub\n");
1056     return E_NOTIMPL;
1057 }
1058
1059 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1060     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1061 {
1062     TRACE("stub\n");
1063     return E_NOTIMPL;
1064 }
1065
1066 #define SHELLVIEWCOLUMNS 6 
1067
1068 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1069     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1070 {
1071     HRESULT hr = E_FAIL;
1072     struct passwd *pPasswd;
1073     struct group *pGroup;
1074     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1075         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1076         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1077         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1078         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1079         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
1080         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
1081     };
1082
1083     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1084     
1085     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1086         return E_INVALIDARG;
1087
1088     if (!pidl) {
1089         psd->fmt = SFHeader[iColumn].fmt;
1090         psd->cxChar = SFHeader[iColumn].cxChar;
1091         psd->str.uType = STRRET_CSTR;
1092         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1093         return S_OK;
1094     } else {
1095         StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
1096         psd->str.u.cStr[0] = '\0';
1097         psd->str.uType = STRRET_CSTR;
1098         switch (iColumn) {
1099             case 0:
1100                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1101                 break;
1102             case 1:
1103                 psd->str.u.cStr[0] = S_ISDIR(pStatStruct->st_mode) ? 'd' : '-';
1104                 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
1105                 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
1106                 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
1107                 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
1108                 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
1109                 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
1110                 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
1111                 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
1112                 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
1113                 psd->str.u.cStr[10] = '\0';
1114                 break;
1115             case 2:
1116                 pPasswd = getpwuid(pStatStruct->st_uid);
1117                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1118                 break;
1119             case 3:
1120                 pGroup = getgrgid(pStatStruct->st_gid);
1121                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1122                 break;
1123             case 4:
1124                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1125                 break;
1126             case 5:
1127                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1128                 break;
1129         }
1130     }
1131     
1132     return hr;
1133 }
1134
1135 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1136     SHCOLUMNID *pscid) 
1137 {
1138     TRACE("stub\n");
1139     return E_NOTIMPL;
1140 }
1141
1142 /* VTable for UnixFolder's IShellFolder2 interface.
1143  */
1144 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1145     UnixFolder_IShellFolder2_QueryInterface,
1146     UnixFolder_IShellFolder2_AddRef,
1147     UnixFolder_IShellFolder2_Release,
1148     UnixFolder_IShellFolder2_ParseDisplayName,
1149     UnixFolder_IShellFolder2_EnumObjects,
1150     UnixFolder_IShellFolder2_BindToObject,
1151     UnixFolder_IShellFolder2_BindToStorage,
1152     UnixFolder_IShellFolder2_CompareIDs,
1153     UnixFolder_IShellFolder2_CreateViewObject,
1154     UnixFolder_IShellFolder2_GetAttributesOf,
1155     UnixFolder_IShellFolder2_GetUIObjectOf,
1156     UnixFolder_IShellFolder2_GetDisplayNameOf,
1157     UnixFolder_IShellFolder2_SetNameOf,
1158     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1159     UnixFolder_IShellFolder2_EnumSearches,
1160     UnixFolder_IShellFolder2_GetDefaultColumn,
1161     UnixFolder_IShellFolder2_GetDefaultColumnState,
1162     UnixFolder_IShellFolder2_GetDetailsEx,
1163     UnixFolder_IShellFolder2_GetDetailsOf,
1164     UnixFolder_IShellFolder2_MapColumnToSCID
1165 };
1166
1167 static HRESULT WINAPI UnixFolder_IPersistFolder2_QueryInterface(IPersistFolder2* This, REFIID riid, 
1168     void** ppvObject)
1169 {
1170     return UnixFolder_IShellFolder2_QueryInterface(
1171                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This), riid, ppvObject);
1172 }
1173
1174 static ULONG WINAPI UnixFolder_IPersistFolder2_AddRef(IPersistFolder2* This)
1175 {
1176     return UnixFolder_IShellFolder2_AddRef(
1177                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1178 }
1179
1180 static ULONG WINAPI UnixFolder_IPersistFolder2_Release(IPersistFolder2* This)
1181 {
1182     return UnixFolder_IShellFolder2_Release(
1183                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1184 }
1185
1186 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetClassID(IPersistFolder2* This, CLSID* pClassID)
1187 {
1188     TRACE("stub\n");
1189     return E_NOTIMPL;
1190 }
1191
1192 static HRESULT WINAPI UnixFolder_IPersistFolder2_Initialize(IPersistFolder2* iface, LPCITEMIDLIST pidl)
1193 {
1194     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1195     
1196     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1197
1198     This->m_pidlLocation = ILClone(pidl);
1199     
1200     pdump(pidl);
1201     
1202     if (!UNIXFS_pidl_to_path(pidl, This))
1203         return E_FAIL;
1204
1205     if (!strcmp(This->m_pszPath, "/")) {
1206         struct stat statRoot;
1207         if (stat(This->m_pszPath, &statRoot)) return E_FAIL;
1208         dwRootAttr = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1209         if (UNIXFS_is_dos_device(&statRoot)) dwRootAttr |= SFGAO_FILESYSTEM;
1210     }
1211     
1212     return S_OK;
1213 }
1214
1215 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetCurFolder(IPersistFolder2* iface, LPITEMIDLIST* ppidl)
1216 {
1217     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1218     
1219     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1220
1221     if (!ppidl)
1222         return E_POINTER;
1223     *ppidl = ILClone (This->m_pidlLocation);
1224     return S_OK;
1225 }
1226     
1227 /* VTable for UnixFolder's IPersistFolder interface.
1228  */
1229 static const IPersistFolder2Vtbl UnixFolder_IPersistFolder2_Vtbl = {
1230     UnixFolder_IPersistFolder2_QueryInterface,
1231     UnixFolder_IPersistFolder2_AddRef,
1232     UnixFolder_IPersistFolder2_Release,
1233     UnixFolder_IPersistFolder2_GetClassID,
1234     UnixFolder_IPersistFolder2_Initialize,
1235     UnixFolder_IPersistFolder2_GetCurFolder
1236 };
1237
1238 /******************************************************************************
1239  * Unix[Dos]Folder_Constructor [Internal]
1240  *
1241  * PARAMS
1242  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1243  *  riid      [I] Interface asked for by the client.
1244  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1245  *
1246  * NOTES
1247  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1248  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1249  *  compatible signature.
1250  *
1251  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1252  *  means that paths are converted from dos to unix and back at the interfaces.
1253  */
1254 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode) {
1255     HRESULT hr = E_FAIL;
1256     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1257     
1258     if(pUnixFolder) {
1259         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1260         pUnixFolder->lpIPersistFolder2Vtbl = &UnixFolder_IPersistFolder2_Vtbl;
1261         pUnixFolder->m_cRef = 0;
1262         pUnixFolder->m_pszPath = NULL;
1263         pUnixFolder->m_apidlSubDirs = NULL;
1264         pUnixFolder->m_cSubDirs = -1;
1265         pUnixFolder->m_dwPathMode = dwPathMode;
1266
1267         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1268         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1269         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1270     }
1271     return hr;
1272 }
1273
1274 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1275     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1276     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX);
1277 }
1278
1279 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1280     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1281     return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS);
1282 }
1283
1284 /******************************************************************************
1285  * UnixSubFolderIterator
1286  *
1287  * Class whose heap based objects represent iterators over the sub-directories
1288  * of a given UnixFolder object. 
1289  */
1290
1291 /* UnixSubFolderIterator object layout and typedef.
1292  */
1293 typedef struct _UnixSubFolderIterator {
1294     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1295     ULONG m_cRef;
1296     UnixFolder *m_pUnixFolder;
1297     ULONG m_cIdx;
1298     SHCONTF m_fFilter;
1299 } UnixSubFolderIterator;
1300
1301 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1302     TRACE("(iterator=%p)\n", iterator);
1303         
1304     UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1305     SHFree(iterator);
1306 }
1307
1308 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1309     REFIID riid, void** ppv)
1310 {
1311     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1312     
1313     if (!ppv) return E_INVALIDARG;
1314     
1315     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1316         *ppv = iface;
1317     } else {
1318         *ppv = NULL;
1319         return E_NOINTERFACE;
1320     }
1321
1322     IEnumIDList_AddRef(iface);
1323     return S_OK;
1324 }
1325                             
1326 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1327 {
1328     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1329
1330     TRACE("(iface=%p)\n", iface);
1331    
1332     return InterlockedIncrement(&This->m_cRef);
1333 }
1334
1335 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1336 {
1337     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1338     ULONG cRef;
1339     
1340     TRACE("(iface=%p)\n", iface);
1341
1342     cRef = InterlockedDecrement(&This->m_cRef);
1343     
1344     if (!cRef) 
1345         UnixSubFolderIterator_Destroy(This);
1346
1347     return cRef;
1348 }
1349
1350 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1351     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1352 {
1353     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1354     ULONG i;
1355
1356     TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1357    
1358     for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1359         LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1360         if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1361             rgelt[i] = ILClone(pCurrent);
1362             i++;
1363         }
1364     }
1365
1366     if (pceltFetched)
1367         *pceltFetched = i;
1368
1369     return i == celt ? S_OK : S_FALSE;
1370 }
1371
1372 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1373 {
1374     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1375     ULONG i;
1376     
1377     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1378
1379     for (i=0; i < celt; i++) {
1380         while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1381                !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter)) 
1382         {
1383             This->m_cIdx++;
1384         }
1385         This->m_cIdx++;
1386     }
1387     
1388     if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1389         This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1390         return S_FALSE;
1391     } else {
1392         return S_OK;
1393     }
1394 }
1395
1396 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1397 {
1398     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1399         
1400     TRACE("(iface=%p)\n", iface);
1401
1402     This->m_cIdx = 0;
1403     
1404     return S_OK;
1405 }
1406
1407 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1408     IEnumIDList** ppenum)
1409 {
1410     TRACE("stub\n");
1411     return E_NOTIMPL;
1412 }
1413
1414 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1415  */
1416 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1417     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1418     UnixSubFolderIterator_IEnumIDList_AddRef,
1419     UnixSubFolderIterator_IEnumIDList_Release,
1420     UnixSubFolderIterator_IEnumIDList_Next,
1421     UnixSubFolderIterator_IEnumIDList_Skip,
1422     UnixSubFolderIterator_IEnumIDList_Reset,
1423     UnixSubFolderIterator_IEnumIDList_Clone
1424 };
1425
1426 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1427     UnixSubFolderIterator *iterator;
1428
1429     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1430     
1431     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1432     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1433     iterator->m_cRef = 0;
1434     iterator->m_cIdx = 0;
1435     iterator->m_pUnixFolder = pUnixFolder;
1436     iterator->m_fFilter = fFilter;
1437
1438     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1439     UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1440     
1441     return (IUnknown*)iterator;
1442 }