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