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