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