msi: Don't flag an error in the InstallServices action if the component does not...
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 /*
22  * As you know, windows and unix do have a different philosophy with regard to
23  * the question of how a filesystem should be laid out. While we unix geeks
24  * learned to love the 'one-tree-rooted-at-/' approach, windows has in fact
25  * a whole forest of filesystem trees, each of which is typically identified by
26  * a drive letter.
27  *
28  * We would like wine to integrate as smoothly as possible (that is without
29  * sacrificing win32 compatibility) into the unix environment. For the
30  * filesystem question, this means we really would like those windows
31  * applications to work with unix path- and file-names. Unfortunately, this
32  * seems to be impossible in general. Therefore we have those symbolic links
33  * in wine's 'dosdevices' directory, which are used to simulate drives
34  * to keep windows applications happy. And as a consequence, we have those
35  * drive letters show up now and then in GUI applications running under wine,
36  * which gets the unix hardcore fans all angry, shouting at us @#!&$%* wine
37  * hackers that we are seducing the big companies not to port their applications
38  * to unix.
39  *
40  * DOS paths do appear at various places in GUI applications. Sometimes, they
41  * show up in the title bar of an application's window. They tend to accumulate
42  * in the most-recently-used section of the file-menu. And I've even seen some
43  * in a configuration dialog's edit control. In those examples, wine can't do a
44  * lot about this, since path-names can't be told apart from ordinary strings
45  * here. That's different in the file dialogs, though.
46  *
47  * With the introduction of the 'shell' in win32, Microsoft established an 
48  * abstraction layer on top of the filesystem, called the shell namespace (I was
49  * told that Gnome's virtual filesystem is conceptually similar). In the shell
50  * namespace, one doesn't use ascii- or unicode-strings to uniquely identify
51  * objects. Instead Microsoft introduced item-identifier-lists (The c type is 
52  * called ITEMIDLIST) as an abstraction of path-names. As you probably would
53  * have guessed, an item-identifier-list is a list of item-identifiers (whose
54  * c type's funny name is SHITEMID), which are opaque binary objects. This means
55  * that no application (apart from Microsoft Office) should make any assumptions
56  * on the internal structure of these SHITEMIDs. 
57  *
58  * Since the user prefers to be presented the good-old DOS file-names instead of 
59  * binary ITEMIDLISTs, a translation method between string-based file-names and
60  * ITEMIDLISTs was established. At the core of this are the COM-Interface
61  * IShellFolder and especially it's methods ParseDisplayName and 
62  * GetDisplayNameOf. Basically, you give a DOS-path (let's say C:\windows) to
63  * ParseDisplayName and get a SHITEMID similar to <Desktop|My Computer|C:|windows|>.
64  * Since it's opaque, you can't see the 'C', the 'windows' and the other stuff.
65  * You can only figure out that the ITEMIDLIST is composed of four SHITEMIDS.
66  * The file dialog applies IShellFolder's BindToObject method to bind to each of
67  * those four objects (Desktop, My Computer, C: and windows. All of them have to 
68  * implement the IShellFolder interface.) and asks them how they would like to be 
69  * displayed (basically their icon and the string displayed). If the file dialog 
70  * asks <Desktop|My Computer|C:|windows> which sub-objects it contains (via 
71  * EnumObjects) it gets a list of opaque SHITEMIDs, which can be concatenated to 
72  * <Desktop|...|windows> to build a new ITEMIDLIST and browse, for instance, 
73  * into <system32>. This means the file dialog browses the shell namespace by 
74  * identifying objects via ITEMIDLISTs. Once the user has selected a location to 
75  * save his valuable file, the file dialog calls IShellFolder's GetDisplayNameOf
76  * method to translate the ITEMIDLIST back to a DOS filename.
77  * 
78  * It seems that one intention of the shell namespace concept was to make it 
79  * possible to have objects in the namespace, which don't have any counterpart 
80  * in the filesystem. The 'My Computer' shell folder object is one instance
81  * which comes to mind (Go try to save a file into 'My Computer' on windows.)
82  * So, to make matters a little more complex, before the file dialog asks a
83  * shell namespace object for it's DOS path, it asks if it actually has one.
84  * This is done via the IShellFolder::GetAttributesOf method, which sets the
85  * SFGAO_FILESYSTEM if - and only if - it has.
86  *
87  * The two things, described in the previous two paragraphs, are what unixfs is
88  * based on. So basically, if UnixDosFolder's ParseDisplayName method is called 
89  * with a 'c:\windows' path-name, it doesn't return an 
90  * <Desktop|My Computer|C:|windows|> ITEMIDLIST. Instead, it uses 
91  * shell32's wine_get_unix_path_name and the _posix_ (which means not the win32) 
92  * fileio api's to figure out that c: is mapped to - let's say - 
93  * /home/mjung/.wine/drive_c and then constructs a 
94  * <Desktop|/|home|mjung|.wine|drive_c> ITEMIDLIST. Which is what the file 
95  * dialog uses to display the folder and file objects, which is why you see a 
96  * unix path. When the user has found a nice place for his file and hits the
97  * save button, the ITEMIDLIST of the selected folder object is passed to 
98  * GetDisplayNameOf, which returns a _DOS_ path name 
99  * (like H:\home_of_my_new_file out of <|Desktop|/|home|mjung|home_of_my_new_file|>).
100  * Unixfs basically mounts your dos devices together in order to construct
101  * a copy of your unix filesystem structure.
102  *
103  * But what if none of the symbolic links in 'dosdevices' points to '/', you 
104  * might ask ("And I don't want wine have access to my complete hard drive, you 
105  * *%&1#!"). No problem, as I stated above, unixfs uses the _posix_ apis to 
106  * construct the ITEMIDLISTs. Folders, which aren't accessible via a drive letter,
107  * don't have the SFGAO_FILESYSTEM flag set. So the file dialogs shouldn't allow
108  * the user to select such a folder for file storage (And if it does anyhow, it 
109  * will not be able to return a valid path, since there is none). Think of those 
110  * folders as a hierarchy of 'My Computer'-like folders, which happen to be a 
111  * shadow of your unix filesystem tree. And since all of this stuff doesn't 
112  * change anything at all in wine's fileio api's, windows applications will have 
113  * no more access rights as they had before. 
114  *
115  * To sum it all up, you can still safely run wine with you root account (Just
116  * kidding, don't do it.)
117  *
118  * If you are now standing in front of your computer, shouting hotly 
119  * "I am not convinced, Mr. Rumsfeld^H^H^H^H^H^H^H^H^H^H^H^H", fire up regedit
120  * and delete HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\
121  * Explorer\Desktop\Namespace\{9D20AAE8-0625-44B0-9CA7-71889C2254D9} and you 
122  * will be back in the pre-unixfs days.
123  */
124
125 #include "config.h"
126 #include "wine/port.h"
127
128 #include <stdio.h>
129 #include <stdarg.h>
130 #include <limits.h>
131 #include <errno.h>
132 #ifdef HAVE_DIRENT_H
133 # include <dirent.h>
134 #endif
135 #include <stdlib.h>
136 #ifdef HAVE_UNISTD_H
137 # include <unistd.h>
138 #endif
139 #ifdef HAVE_SYS_STAT_H
140 # include <sys/stat.h>
141 #endif
142 #ifdef HAVE_PWD_H
143 # include <pwd.h>
144 #endif
145 #ifdef HAVE_GRP_H
146 # include <grp.h>
147 #endif
148
149 #define COBJMACROS
150 #define NONAMELESSUNION
151 #define NONAMELESSSTRUCT
152
153 #include "windef.h"
154 #include "winbase.h"
155 #include "winuser.h"
156 #include "objbase.h"
157 #include "winreg.h"
158 #include "shlwapi.h"
159 #include "winternl.h"
160 #include "wine/debug.h"
161
162 #include "shell32_main.h"
163 #include "shellfolder.h"
164 #include "shfldr.h"
165 #include "shresdef.h"
166 #include "pidl.h"
167 #include "debughlp.h"
168
169 WINE_DEFAULT_DEBUG_CHANNEL(shell);
170
171 #if !defined(__MINGW32__) && !defined(_MSC_VER)
172
173 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
174 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
175
176 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
177     ( sizeof(USHORT)      /* SHITEMID's cb field. */ \
178     + sizeof(PIDLTYPE)    /* PIDLDATA's type field. */ \
179     + sizeof(FileStruct)  /* Well, the FileStruct. */ \
180     - sizeof(char)        /* One char too much in FileStruct. */ \
181     + sizeof(FileStructW) /* You name it. */ \
182     - sizeof(WCHAR)       /* One WCHAR too much in FileStructW. */ \
183     + sizeof(WORD) ))     /* Offset of FileStructW field in PIDL. */
184
185 #define PATHMODE_UNIX 0
186 #define PATHMODE_DOS  1
187
188 static const WCHAR wFileSystemBindData[] = {
189     'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d',' ','D','a','t','a',0};
190
191 /* UnixFolder object layout and typedef.
192  */
193 typedef struct _UnixFolder {
194     const IShellFolder2Vtbl       *lpIShellFolder2Vtbl;
195     const IPersistFolder3Vtbl     *lpIPersistFolder3Vtbl;
196     const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
197     const IDropTargetVtbl         *lpIDropTargetVtbl;
198     const ISFHelperVtbl           *lpISFHelperVtbl;
199     LONG         m_cRef;
200     CHAR         *m_pszPath;     /* Target path of the shell folder (CP_UNIXCP) */
201     LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
202     DWORD        m_dwPathMode;
203     DWORD        m_dwAttributes;
204     const CLSID  *m_pCLSID;
205     DWORD        m_dwDropEffectsMask;
206 } UnixFolder;
207
208 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
209 static UINT cfShellIDList = 0;
210
211 /******************************************************************************
212  * UNIXFS_filename_from_shitemid [Internal]
213  *
214  *  Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
215  * 
216  * PARAMS
217  *  pidl           [I] A simple SHITEMID
218  *  pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
219  *
220  * RETURNS
221  *  Success: Number of bytes necessary to store the CP_UNIXCP encoded filename 
222  *   _without_ the terminating NUL.
223  *  Failure: 0
224  *  
225  * NOTES
226  *  Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
227  *  may be NULL, if you are only interested in the return value. 
228  */
229 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
230     FileStructW *pFileStructW = _ILGetFileStructW(pidl);
231     int cLen = 0;
232
233     if (pFileStructW) {
234         cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
235             pszPathElement ? FILENAME_MAX : 0, 0, 0);
236     } else {
237         /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
238          * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
239         char *pszText = _ILGetTextPointer(pidl);
240         WCHAR *pwszPathElement = NULL;
241         int cWideChars;
242     
243         cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
244         if (!cWideChars) goto cleanup;
245
246         pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
247         if (!pwszPathElement) goto cleanup;
248     
249         cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
250         if (!cWideChars) goto cleanup; 
251
252         cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement, 
253             pszPathElement ? FILENAME_MAX : 0, 0, 0);
254
255     cleanup:
256         SHFree(pwszPathElement);
257     }
258         
259     if (cLen) cLen--; /* Don't count terminating NUL! */
260     return cLen;
261 }
262
263 /******************************************************************************
264  * UNIXFS_shitemid_len_from_filename [Internal]
265  *
266  * Computes the necessary length of a pidl to hold a path element
267  *
268  * PARAMS
269  *  szPathElement    [I] The path element string in CP_UNIXCP encoding.
270  *  ppszPathElement  [O] Path element string in CP_ACP encoding.
271  *  ppwszPathElement [O] Path element string as WCHAR string.
272  *
273  * RETURNS
274  *  Success: Length in bytes of a SHITEMID representing szPathElement
275  *  Failure: 0
276  * 
277  * NOTES
278  *  Provide NULL values if not interested in pp(w)szPathElement. Otherwise
279  *  caller is responsible to free ppszPathElement and ppwszPathElement with
280  *  SHFree.
281  */
282 static USHORT UNIXFS_shitemid_len_from_filename(
283     const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement) 
284 {
285     USHORT cbPidlLen = 0;
286     WCHAR *pwszPathElement = NULL;
287     char *pszPathElement = NULL;
288     int cWideChars, cChars;
289
290     /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
291      * codepage or it might be a real multi-byte encoding like utf-8. There is no
292      * other way to figure out the length of the corresponding WCHAR and CP_ACP 
293      * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
294     
295     cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
296     if (!cWideChars) goto cleanup;
297
298     pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
299     if (!pwszPathElement) goto cleanup;
300
301     cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
302     if (!cWideChars) goto cleanup; 
303
304     cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
305     if (!cChars) goto cleanup;
306
307     pszPathElement = SHAlloc(cChars);
308     if (!pszPathElement) goto cleanup;
309
310     cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
311     if (!cChars) goto cleanup;
312
313     /* (cChars & 0x1) is for the potential alignment byte */
314     cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
315     
316 cleanup:
317     if (cbPidlLen && ppszPathElement) 
318         *ppszPathElement = pszPathElement;
319     else 
320         SHFree(pszPathElement);
321
322     if (cbPidlLen && ppwszPathElement)
323         *ppwszPathElement = pwszPathElement;
324     else
325         SHFree(pwszPathElement);
326
327     return cbPidlLen;
328 }
329
330 /******************************************************************************
331  * UNIXFS_is_pidl_of_type [Internal]
332  *
333  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
334  *
335  * PARAMS
336  *  pIDL    [I] The ITEMIDLIST to be checked.
337  *  fFilter [I] Shell condition flags, which specify the filter.
338  *
339  * RETURNS
340  *  TRUE, if pIDL is accepted by fFilter
341  *  FALSE, otherwise
342  */
343 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
344     const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
345     if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData && 
346         (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN)) 
347     {
348         return FALSE;
349     }
350     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
351     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
352     return FALSE;
353 }
354
355 /******************************************************************************
356  * UNIXFS_get_unix_path [Internal]
357  *
358  * Convert an absolute dos path to an absolute unix path.
359  * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
360  *
361  * PARAMS
362  *  pszDosPath       [I] An absolute dos path
363  *  pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
364  *
365  * RETURNS
366  *  Success, TRUE
367  *  Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
368  */
369 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
370 {
371     char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath, has_failed = 0, mb_path[FILENAME_MAX];
372     WCHAR wszDrive[] = { '?', ':', '\\', 0 }, dospath[PATH_MAX], *dospath_end;
373     int cDriveSymlinkLen;
374     
375     TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
376
377     if (!pszDosPath || pszDosPath[1] != ':')
378         return FALSE;
379
380     /* Get the canonicalized unix path corresponding to the drive letter. */
381     wszDrive[0] = pszDosPath[0];
382     pszUnixPath = wine_get_unix_file_name(wszDrive);
383     if (!pszUnixPath) return FALSE;
384     cDriveSymlinkLen = strlen(pszUnixPath);
385     pElement = realpath(pszUnixPath, szPath);
386     HeapFree(GetProcessHeap(), 0, pszUnixPath);
387     if (!pElement) return FALSE;
388     if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
389
390     /* Append the part relative to the drive symbolic link target. */
391     lstrcpyW(dospath, pszDosPath);
392     dospath_end = dospath + lstrlenW(dospath);
393     /* search for the most valid UNIX path possible, then append missing
394      * path parts */
395     while(!(pszUnixPath = wine_get_unix_file_name(dospath))){
396         if(has_failed){
397             *dospath_end = '/';
398             --dospath_end;
399         }else
400             has_failed = 1;
401         while(*dospath_end != '\\' && *dospath_end != '/'){
402             --dospath_end;
403             if(dospath_end < dospath)
404                 break;
405         }
406         *dospath_end = '\0';
407     }
408     if(dospath_end < dospath)
409         return FALSE;
410     strcat(szPath, pszUnixPath + cDriveSymlinkLen);
411     HeapFree(GetProcessHeap(), 0, pszUnixPath);
412
413     if(has_failed && WideCharToMultiByte(CP_UNIXCP, 0, dospath_end + 1, -1,
414                 mb_path, FILENAME_MAX, NULL, NULL) > 0){
415         strcat(szPath, "/");
416         strcat(szPath, mb_path);
417     }
418
419     /* pCanonicalTail always points to the end of the canonical path constructed
420      * thus far. pPathTail points to the still to be processed part of the input
421      * path. pElement points to the path element currently investigated.
422      */
423     *pszCanonicalPath = '\0';
424     pCanonicalTail = pszCanonicalPath;
425     pPathTail = szPath;
426
427     do {
428         char cTemp;
429             
430         pElement = pPathTail;
431         pPathTail = strchr(pPathTail+1, '/');
432         if (!pPathTail) /* Last path element may not be terminated by '/'. */ 
433             pPathTail = pElement + strlen(pElement);
434         /* Temporarily terminate the current path element. Will be restored later. */
435         cTemp = *pPathTail;
436         *pPathTail = '\0';
437
438         /* Skip "/." path elements */
439         if (!strcmp("/.", pElement)) {
440             *pPathTail = cTemp;
441         } else if (!strcmp("/..", pElement)) {
442             /* Remove last element in canonical path for "/.." elements, then skip. */
443             char *pTemp = strrchr(pszCanonicalPath, '/');
444             if (pTemp)
445                 pCanonicalTail = pTemp;
446             *pCanonicalTail = '\0';
447             *pPathTail = cTemp;
448         } else {
449             /* Directory or file. Copy to canonical path */
450             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
451                 return FALSE;
452                 
453             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
454             pCanonicalTail += pPathTail - pElement;
455             *pPathTail = cTemp;
456         }
457     } while (pPathTail[0] == '/');
458    
459     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
460     
461     return TRUE;
462 }
463
464 /******************************************************************************
465  * UNIXFS_seconds_since_1970_to_dos_date_time [Internal]
466  *
467  * Convert unix time to FAT time
468  *
469  * PARAMS 
470  *  ss1970 [I] Unix time (seconds since 1970)
471  *  pDate  [O] Corresponding FAT date
472  *  pTime  [O] Corresponding FAT time
473  */
474 static inline void UNIXFS_seconds_since_1970_to_dos_date_time(
475     time_t ss1970, LPWORD pDate, LPWORD pTime)
476 {
477     LARGE_INTEGER time;
478     FILETIME fileTime;
479
480     RtlSecondsSince1970ToTime( ss1970, &time );
481     fileTime.dwLowDateTime = time.u.LowPart;
482     fileTime.dwHighDateTime = time.u.HighPart;
483     FileTimeToDosDateTime(&fileTime, pDate, pTime);
484 }
485
486 /******************************************************************************
487  * UNIXFS_build_shitemid [Internal]
488  *
489  * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into 
490  * buffer 'pIDL'.
491  *
492  * PARAMS
493  *  pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
494  *  pbc         [I] Bind context for this action, used to determine if the file must exist
495  *  pIDL        [O] SHITEMID will be constructed here.
496  *
497  * RETURNS
498  *  Success: A pointer to the terminating '\0' character of path.
499  *  Failure: NULL
500  *
501  * NOTES
502  *  Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
503  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
504  *  a 0 USHORT value.
505  */
506 static char* UNIXFS_build_shitemid(char *pszUnixPath, LPBC pbc, void *pIDL) {
507     LPPIDLDATA pIDLData;
508     struct stat fileStat;
509     char *pszComponentU, *pszComponentA;
510     WCHAR *pwszComponentW;
511     int cComponentULen, cComponentALen;
512     USHORT cbLen;
513     FileStructW *pFileStructW;
514     WORD uOffsetW, *pOffsetW;
515     BOOL must_exist = TRUE;
516
517     TRACE("(pszUnixPath=%s, pbc=%p, pIDL=%p)\n", debugstr_a(pszUnixPath), pbc, pIDL);
518
519     if (pbc){
520         IUnknown *unk;
521         IFileSystemBindData *fsb;
522         HRESULT hr;
523
524         hr = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &unk);
525         if (SUCCEEDED(hr)) {
526             hr = IUnknown_QueryInterface(unk, &IID_IFileSystemBindData, (LPVOID*)&fsb);
527             if (SUCCEEDED(hr)) {
528                 /* Windows tries to get WIN32_FIND_DATAW structure from
529                  * fsb here for no known reason */
530                 must_exist = FALSE;
531                 IFileSystemBindData_Release(fsb);
532             }
533             IUnknown_Release(unk);
534         }
535     }
536
537     /* We are only interested in regular files and directories. */
538     if (stat(pszUnixPath, &fileStat)){
539         if (must_exist || errno != ENOENT)
540             return NULL;
541     }else
542         if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode))
543             return NULL;
544     
545     /* Compute the SHITEMID's length and wipe it. */
546     pszComponentU = strrchr(pszUnixPath, '/') + 1;
547     cComponentULen = strlen(pszComponentU);
548     cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
549     if (!cbLen) return NULL;
550     memset(pIDL, 0, cbLen);
551     ((LPSHITEMID)pIDL)->cb = cbLen;
552     
553     /* Set shell32's standard SHITEMID data fields. */
554     pIDLData = _ILGetDataPointer(pIDL);
555     pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
556     pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
557     UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pIDLData->u.file.uFileDate, 
558         &pIDLData->u.file.uFileTime);
559     pIDLData->u.file.uFileAttribs = 0;
560     if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
561     if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |=  FILE_ATTRIBUTE_HIDDEN;
562     cComponentALen = lstrlenA(pszComponentA) + 1;
563     memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
564     
565     pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
566     uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
567     pFileStructW->cbLen = cbLen - uOffsetW;
568     UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pFileStructW->uCreationDate, 
569         &pFileStructW->uCreationTime);
570     UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_atime, &pFileStructW->uLastAccessDate,
571         &pFileStructW->uLastAccessTime);
572     lstrcpyW(pFileStructW->wszName, pwszComponentW);
573
574     pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
575     *pOffsetW = uOffsetW;
576     
577     SHFree(pszComponentA);
578     SHFree(pwszComponentW);
579     
580     return pszComponentU + cComponentULen;
581 }
582
583 /******************************************************************************
584  * UNIXFS_path_to_pidl [Internal]
585  *
586  * PARAMS
587  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
588  *  path        [I] An absolute unix or dos path or a path relative to pUnixFolder
589  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
590  *  
591  * RETURNS
592  *  Success: S_OK
593  *  Failure: Error code, invalid params or out of memory
594  *
595  * NOTES
596  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
597  */
598 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, LPBC pbc, const WCHAR *path,
599         LPITEMIDLIST *ppidl) {
600     LPITEMIDLIST pidl;
601     int cPidlLen, cPathLen;
602     char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
603     WCHAR *pwszPath;
604
605     TRACE("pUnixFolder=%p, pbc=%p, path=%s, ppidl=%p\n", pUnixFolder, pbc, debugstr_w(path), ppidl);
606    
607     if (!ppidl || !path)
608         return E_INVALIDARG;
609
610     /* Build an absolute path and let pNextPathElement point to the interesting 
611      * relative sub-path. We need the absolute path to call 'stat', but the pidl
612      * will only contain the relative part.
613      */
614     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
615     {
616         /* Absolute dos path. Convert to unix */
617         if (!UNIXFS_get_unix_path(path, szCompletePath))
618             return E_FAIL;
619         pNextPathElement = szCompletePath;
620     } 
621     else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/')) 
622     {
623         /* Absolute unix path. Just convert to ANSI. */
624         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL); 
625         pNextPathElement = szCompletePath;
626     } 
627     else 
628     {
629         /* Relative dos or unix path. Concat with this folder's path */
630         int cBasePathLen = strlen(pUnixFolder->m_pszPath);
631         memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
632         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen, 
633                             FILENAME_MAX - cBasePathLen, NULL, NULL);
634         pNextPathElement = szCompletePath + cBasePathLen - 1;
635         
636         /* If in dos mode, replace '\' with '/' */
637         if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
638             char *pBackslash = strchr(pNextPathElement, '\\');
639             while (pBackslash) {
640                 *pBackslash = '/';
641                 pBackslash = strchr(pBackslash, '\\');
642             }
643         }
644     }
645
646     /* Special case for the root folder. */
647     if (!strcmp(szCompletePath, "/")) {
648         *ppidl = pidl = SHAlloc(sizeof(USHORT));
649         if (!pidl) return E_FAIL;
650         pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
651         return S_OK;
652     }
653     
654     /* Remove trailing slash, if present */
655     cPathLen = strlen(szCompletePath);
656     if (szCompletePath[cPathLen-1] == '/') 
657         szCompletePath[cPathLen-1] = '\0';
658
659     if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
660         ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
661         return E_FAIL;
662     }
663     
664     /* At this point, we have an absolute unix path in szCompletePath 
665      * and the relative portion of it in pNextPathElement. Both starting with '/'
666      * and _not_ terminated by a '/'. */
667     TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
668     
669     /* Convert to CP_ACP and WCHAR */
670     if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
671         return E_FAIL;
672
673     /* Compute the length of the complete ITEMIDLIST */
674     cPidlLen = 0;
675     pSlash = pszAPath;
676     while (pSlash) {
677         pNextSlash = strchr(pSlash+1, '/');
678         cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
679             (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1); 
680         pSlash = pNextSlash;
681     }
682
683     /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
684      * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
685      * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
686     cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
687
688     SHFree(pszAPath);
689     SHFree(pwszPath);
690
691     *ppidl = pidl = SHAlloc(cPidlLen);
692     if (!pidl) return E_FAIL;
693
694     /* Concatenate the SHITEMIDs of the sub-directories. */
695     while (*pNextPathElement) {
696         pSlash = strchr(pNextPathElement+1, '/');
697         if (pSlash) *pSlash = '\0';
698         pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pbc, pidl);
699         if (pSlash) *pSlash = '/';
700             
701         if (!pNextPathElement) {
702             SHFree(*ppidl);
703             *ppidl = NULL;
704             return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
705         }
706         pidl = ILGetNext(pidl);
707     }
708     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
709
710     if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */ 
711         ERR("Computed length of pidl incorrect. Please report.\n");
712     
713     return S_OK;
714 }
715
716 /******************************************************************************
717  * UNIXFS_initialize_target_folder [Internal]
718  *
719  *  Initialize the m_pszPath member of an UnixFolder, given an absolute unix
720  *  base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
721  *  specifies the location in the shell namespace alone. 
722  * 
723  * PARAMS
724  *  This          [IO] The UnixFolder, whose target path is to be initialized
725  *  szBasePath    [I]  The absolute base path
726  *  pidlSubFolder [I]  Relative part of the path, given as an ITEMIDLIST
727  *  dwAttributes  [I]  Attributes to add to the Folders m_dwAttributes member 
728  *                     (Used to pass the SFGAO_FILESYSTEM flag down the path)
729  * RETURNS
730  *  Success: S_OK,
731  *  Failure: E_FAIL
732  */
733 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
734     LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
735 {
736     LPCITEMIDLIST current = pidlSubFolder;
737     DWORD dwPathLen = strlen(szBasePath)+1;
738     char *pNextDir;
739     WCHAR *dos_name;
740
741     /* Determine the path's length bytes */
742     while (!_ILIsEmpty(current)) {
743         dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
744         current = ILGetNext(current);
745     };
746
747     /* Build the path and compute the attributes*/
748     This->m_dwAttributes = 
749             dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
750     This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
751     if (!This->m_pszPath) {
752         WARN("SHAlloc failed!\n");
753         return E_FAIL;
754     }
755     current = pidlSubFolder;
756     strcpy(pNextDir, szBasePath);
757     pNextDir += strlen(szBasePath);
758     if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
759         This->m_dwAttributes |= SFGAO_FILESYSTEM;
760     while (!_ILIsEmpty(current)) {
761         pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
762         *pNextDir++ = '/';
763         current = ILGetNext(current);
764     }
765     *pNextDir='\0';
766
767     if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
768         ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
769     {
770         This->m_dwAttributes |= SFGAO_FILESYSTEM;
771         HeapFree( GetProcessHeap(), 0, dos_name );
772     }
773
774     return S_OK;
775 }
776
777 /******************************************************************************
778  * UNIXFS_copy [Internal]
779  *
780  *  Copy pwszDosSrc to pwszDosDst.
781  *
782  * PARAMS
783  *  pwszDosSrc [I]  absolute path of the source
784  *  pwszDosDst [I]  absolute path of the destination
785  *
786  * RETURNS
787  *  Success: S_OK,
788  *  Failure: E_FAIL
789  */
790 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
791 {
792     SHFILEOPSTRUCTW op;
793     LPWSTR pwszSrc, pwszDst;
794     HRESULT res = E_OUTOFMEMORY;
795     UINT iSrcLen, iDstLen;
796
797     if (!pwszDosSrc || !pwszDosDst)
798         return E_FAIL;
799
800     iSrcLen = lstrlenW(pwszDosSrc);
801     iDstLen = lstrlenW(pwszDosDst);
802     pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
803     pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
804
805     if (pwszSrc && pwszDst) {
806         lstrcpyW(pwszSrc, pwszDosSrc);
807         lstrcpyW(pwszDst, pwszDosDst);
808         /* double null termination */
809         pwszSrc[iSrcLen + 1] = 0;
810         pwszDst[iDstLen + 1] = 0;
811
812         ZeroMemory(&op, sizeof(op));
813         op.hwnd = GetActiveWindow();
814         op.wFunc = FO_COPY;
815         op.pFrom = pwszSrc;
816         op.pTo = pwszDst;
817         op.fFlags = FOF_ALLOWUNDO;
818         if (!SHFileOperationW(&op))
819         {
820             WARN("SHFileOperationW failed\n");
821             res = E_FAIL;
822         }
823         else
824             res = S_OK;
825     }
826
827     HeapFree(GetProcessHeap(), 0, pwszSrc);
828     HeapFree(GetProcessHeap(), 0, pwszDst);
829     return res;
830 }
831
832 /******************************************************************************
833  * UnixFolder
834  *
835  * Class whose heap based instances represent unix filesystem directories.
836  */
837
838 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
839     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
840     
841     SHFree(pUnixFolder->m_pszPath);
842     ILFree(pUnixFolder->m_pidlLocation);
843     SHFree(pUnixFolder);
844 }
845
846 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
847     void **ppv) 
848 {
849     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
850         
851     TRACE("(iface=%p, riid=%s, ppv=%p)\n", iface, shdebugstr_guid(riid), ppv);
852     
853     if (!ppv) return E_INVALIDARG;
854     
855     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
856         IsEqualIID(&IID_IShellFolder2, riid)) 
857     {
858         *ppv = STATIC_CAST(IShellFolder2, This);
859     } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) || 
860                IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) 
861     {
862         *ppv = STATIC_CAST(IPersistFolder3, This);
863     } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
864         *ppv = STATIC_CAST(IPersistPropertyBag, This);
865     } else if (IsEqualIID(&IID_ISFHelper, riid)) {
866         *ppv = STATIC_CAST(ISFHelper, This);
867     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
868         *ppv = STATIC_CAST(IDropTarget, This);
869         if (!cfShellIDList) 
870             cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
871     } else {
872         *ppv = NULL;
873         TRACE("Unimplemented interface %s\n", shdebugstr_guid(riid));
874         return E_NOINTERFACE;
875     }
876
877     IUnknown_AddRef((IUnknown*)*ppv);
878     return S_OK;
879 }
880
881 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
882     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
883
884     TRACE("(iface=%p)\n", iface);
885
886     return InterlockedIncrement(&This->m_cRef);
887 }
888
889 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
890     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
891     ULONG cRef;
892     
893     TRACE("(iface=%p)\n", iface);
894
895     cRef = InterlockedDecrement(&This->m_cRef);
896     
897     if (!cRef) 
898         UnixFolder_Destroy(This);
899
900     return cRef;
901 }
902
903 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
904     LPBC pbc, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
905     ULONG* pdwAttributes)
906 {
907     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
908     HRESULT result;
909
910     TRACE("(iface=%p, hwndOwner=%p, pbc=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
911           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbc, debugstr_w(lpszDisplayName),
912           pchEaten, ppidl, pdwAttributes);
913
914     result = UNIXFS_path_to_pidl(This, pbc, lpszDisplayName, ppidl);
915     if (SUCCEEDED(result) && pdwAttributes && *pdwAttributes)
916     {
917         IShellFolder *pParentSF;
918         LPCITEMIDLIST pidlLast;
919         LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
920         HRESULT hr;
921         
922         hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
923         if (FAILED(hr)) {
924             FIXME("SHBindToParent failed! hr = %08x\n", hr);
925             ILFree(pidlComplete);
926             return E_FAIL;
927         }
928         IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
929         IShellFolder_Release(pParentSF);
930         ILFree(pidlComplete);
931     }
932
933     if (FAILED(result)) TRACE("FAILED!\n");
934     return result;
935 }
936
937 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
938
939 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
940     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
941 {
942     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
943     IUnknown *newIterator;
944     HRESULT hr;
945     
946     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08x, ppEnumIDList=%p)\n", 
947             iface, hwndOwner, grfFlags, ppEnumIDList);
948
949     if (!This->m_pszPath) {
950         WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
951         return E_UNEXPECTED;
952     }
953
954     newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
955     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
956     IUnknown_Release(newIterator);
957     
958     return hr;
959 }
960
961 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
962
963 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
964     LPBC pbcReserved, REFIID riid, void** ppvOut)
965 {
966     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
967     IPersistFolder3 *persistFolder;
968     HRESULT hr;
969     const CLSID *clsidChild;
970         
971     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
972             iface, pidl, pbcReserved, riid, ppvOut);
973
974     if (_ILIsEmpty(pidl))
975         return E_INVALIDARG;
976    
977     /* Don't bind to files */
978     if (_ILIsValue(ILFindLastID(pidl)))
979         return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
980
981     if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
982         /* Children of FolderShortcuts are ShellFSFolders on Windows. 
983          * Unixfs' counterpart is UnixDosFolder. */
984         clsidChild = &CLSID_UnixDosFolder;    
985     } else {
986         clsidChild = This->m_pCLSID;
987     }
988
989     hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
990     if (FAILED(hr)) return hr;
991     hr = IPersistFolder_QueryInterface(persistFolder, riid, ppvOut);
992
993     if (SUCCEEDED(hr)) {
994         UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
995         subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
996         hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
997                                              This->m_dwAttributes & SFGAO_FILESYSTEM);
998     } 
999
1000     IPersistFolder3_Release(persistFolder);
1001     
1002     return hr;
1003 }
1004
1005 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
1006     LPBC pbcReserved, REFIID riid, void** ppvObj)
1007 {
1008     FIXME("stub\n");
1009     return E_NOTIMPL;
1010 }
1011
1012 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
1013     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
1014 {
1015     BOOL isEmpty1, isEmpty2;
1016     HRESULT hr = E_FAIL;
1017     LPITEMIDLIST firstpidl;
1018     IShellFolder2 *psf;
1019     int compare;
1020
1021     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
1022     
1023     isEmpty1 = _ILIsEmpty(pidl1);
1024     isEmpty2 = _ILIsEmpty(pidl2);
1025
1026     if (isEmpty1 && isEmpty2) 
1027         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1028     else if (isEmpty1)
1029         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1030     else if (isEmpty2)
1031         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1032
1033     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
1034         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1035     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1036         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1037
1038     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
1039                              _ILGetTextPointer(pidl1), -1,
1040                              _ILGetTextPointer(pidl2), -1);
1041     
1042     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
1043         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1044
1045     if (pidl1->mkid.cb < pidl2->mkid.cb)
1046         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1047     else if (pidl1->mkid.cb > pidl2->mkid.cb)
1048         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1049
1050     firstpidl = ILCloneFirst(pidl1);
1051     pidl1 = ILGetNext(pidl1);
1052     pidl2 = ILGetNext(pidl2);
1053
1054     isEmpty1 = _ILIsEmpty(pidl1);
1055     isEmpty2 = _ILIsEmpty(pidl2);
1056
1057     if (isEmpty1 && isEmpty2)
1058         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
1059     else if (isEmpty1)
1060         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1061     else if (isEmpty2)
1062         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1063     else if (SUCCEEDED(IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (void**)&psf))) {
1064         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1065         IShellFolder2_Release(psf);
1066     }
1067
1068     ILFree(firstpidl);
1069     return hr;
1070 }
1071
1072 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1073     REFIID riid, void** ppv)
1074 {
1075     HRESULT hr = E_INVALIDARG;
1076         
1077     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1078     
1079     if (!ppv) return E_INVALIDARG;
1080     *ppv = NULL;
1081     
1082     if (IsEqualIID(&IID_IShellView, riid)) {
1083         LPSHELLVIEW pShellView;
1084         
1085         pShellView = IShellView_Constructor((IShellFolder*)iface);
1086         if (pShellView) {
1087             hr = IShellView_QueryInterface(pShellView, riid, ppv);
1088             IShellView_Release(pShellView);
1089         }
1090     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1091         hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1092     }
1093     
1094     return hr;
1095 }
1096
1097 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
1098     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1099 {
1100     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1101     HRESULT hr = S_OK;
1102         
1103     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1104  
1105     if (!rgfInOut || (cidl && !apidl)) 
1106         return E_INVALIDARG;
1107     
1108     if (cidl == 0) {
1109         *rgfInOut &= This->m_dwAttributes;
1110     } else {
1111         char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1112         UINT i;
1113
1114         *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1115                     SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1116         lstrcpyA(szAbsolutePath, This->m_pszPath);
1117         pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1118         for (i=0; i<cidl; i++) {
1119             if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1120                 WCHAR *dos_name;
1121                 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath)) 
1122                     return E_INVALIDARG;
1123                 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1124                     *rgfInOut &= ~SFGAO_FILESYSTEM;
1125                 else
1126                     HeapFree( GetProcessHeap(), 0, dos_name );
1127             }
1128             if (_ILIsFolder(apidl[i])) 
1129                 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1130         }
1131     }
1132     
1133     return hr;
1134 }
1135
1136 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
1137     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1138 {
1139     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1140     UINT i;
1141     HRESULT hr;
1142     
1143     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1144         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1145
1146     if (!cidl || !apidl || !riid || !ppvOut) 
1147         return E_INVALIDARG;
1148
1149     for (i=0; i<cidl; i++) 
1150         if (!apidl[i]) 
1151             return E_INVALIDARG;
1152
1153     if(cidl == 1) {
1154         hr = SHELL32_CreateExtensionUIObject(iface, *apidl, riid, ppvOut);
1155         if(hr != S_FALSE)
1156             return hr;
1157     }
1158     
1159     if (IsEqualIID(&IID_IContextMenu, riid)) {
1160         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1161         return S_OK;
1162     } else if (IsEqualIID(&IID_IDataObject, riid)) {
1163         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1164         return S_OK;
1165     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1166         LPITEMIDLIST pidl;
1167         if (cidl != 1) return E_INVALIDARG;
1168         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1169         *ppvOut = IExtractIconA_Constructor(pidl);
1170         SHFree(pidl);
1171         return S_OK;
1172     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1173         LPITEMIDLIST pidl;
1174         if (cidl != 1) return E_INVALIDARG;
1175         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1176         *ppvOut = IExtractIconW_Constructor(pidl);
1177         SHFree(pidl);
1178         return S_OK;
1179     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1180         if (cidl != 1) return E_INVALIDARG;
1181         return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1182     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1183         FIXME("IShellLinkW\n");
1184         return E_FAIL;
1185     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1186         FIXME("IShellLinkA\n");
1187         return E_FAIL;
1188     } else {
1189         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1190         return E_NOINTERFACE;
1191     }
1192 }
1193
1194 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
1195     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1196 {
1197     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1198     SHITEMID emptyIDL = { 0, { 0 } };
1199     HRESULT hr = S_OK;    
1200
1201     TRACE("(iface=%p, pidl=%p, uFlags=%x, lpName=%p)\n", iface, pidl, uFlags, lpName);
1202     
1203     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1204         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1205     {
1206         if (_ILIsEmpty(pidl)) {
1207             lpName->uType = STRRET_WSTR;
1208             if (This->m_dwPathMode == PATHMODE_UNIX) {
1209                 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1210                 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1211                 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1212                 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1213             } else {
1214                 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1215                 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1216                 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1217                 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1218                 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1219                 PathRemoveBackslashW(lpName->u.pOleStr);
1220                 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1221             }
1222         } else if (_ILIsValue(pidl)) {
1223             STRRET str;
1224             PWSTR path, file;
1225
1226             /* We are looking for the complete path to a file */
1227
1228             /* Get the complete path for the current folder object */
1229             hr = IShellFolder_GetDisplayNameOf(iface, (LPITEMIDLIST)&emptyIDL, uFlags, &str);
1230             if (SUCCEEDED(hr)) {
1231                 hr = StrRetToStrW(&str, NULL, &path);
1232                 if (SUCCEEDED(hr)) {
1233
1234                     /* Get the child filename */
1235                     hr = IShellFolder_GetDisplayNameOf(iface, pidl, SHGDN_FORPARSING | SHGDN_INFOLDER, &str);
1236                     if (SUCCEEDED(hr)) {
1237                         hr = StrRetToStrW(&str, NULL, &file);
1238                         if (SUCCEEDED(hr)) {
1239                             static const WCHAR slashW = '/';
1240                             UINT len_path = strlenW(path), len_file = strlenW(file);
1241
1242                             /* Now, combine them */
1243                             lpName->uType = STRRET_WSTR;
1244                             lpName->u.pOleStr = SHAlloc( (len_path + len_file + 2)*sizeof(WCHAR) );
1245                             lstrcpyW(lpName->u.pOleStr, path);
1246                             if (This->m_dwPathMode == PATHMODE_UNIX &&
1247                                lpName->u.pOleStr[len_path-1] != slashW) {
1248                                 lpName->u.pOleStr[len_path] = slashW;
1249                                 lpName->u.pOleStr[len_path+1] = '\0';
1250                             } else
1251                                 PathAddBackslashW(lpName->u.pOleStr);
1252                             lstrcatW(lpName->u.pOleStr, file);
1253
1254                             CoTaskMemFree(file);
1255                         } else
1256                             WARN("Failed to convert strret (file)\n");
1257                     }
1258                     CoTaskMemFree(path);
1259                 } else
1260                     WARN("Failed to convert strret (path)\n");
1261             }
1262         } else {
1263             IShellFolder *pSubFolder;
1264
1265             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1266             if (SUCCEEDED(hr)) {
1267                 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1268                 IShellFolder_Release(pSubFolder);
1269             } else if (FAILED(hr) && !_ILIsPidlSimple(pidl)) {
1270                 LPITEMIDLIST pidl_parent = ILClone(pidl);
1271                 LPITEMIDLIST pidl_child = ILFindLastID(pidl);
1272
1273                 /* Might be a file, try binding to its parent */
1274                 ILRemoveLastID(pidl_parent);
1275                 hr = IShellFolder_BindToObject(iface, pidl_parent, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1276                 if (SUCCEEDED(hr)) {
1277                     hr = IShellFolder_GetDisplayNameOf(pSubFolder, pidl_child, uFlags, lpName);
1278                     IShellFolder_Release(pSubFolder);
1279                 }
1280                 ILFree(pidl_parent);
1281             }
1282         }
1283     } else {
1284         WCHAR wszFileName[MAX_PATH];
1285         if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1286         lpName->uType = STRRET_WSTR;
1287         lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1288         if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1289         lstrcpyW(lpName->u.pOleStr, wszFileName);
1290         if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS && 
1291             !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1292         {
1293             PathRemoveExtensionW(lpName->u.pOleStr);
1294         }
1295     }
1296
1297     TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1298     
1299     return hr;
1300 }
1301
1302 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd, 
1303     LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1304 {
1305     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1306
1307     static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1308     char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1309     WCHAR wszSrcRelative[MAX_PATH], *pwszExt = NULL;
1310     unsigned int i;
1311     int cBasePathLen = lstrlenA(This->m_pszPath), cNameLen;
1312     struct stat statDest;
1313     LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1314     LPOLESTR lpwszName;
1315     HRESULT hr;
1316    
1317     TRACE("(iface=%p, hwnd=%p, pidl=%p, lpcwszName=%s, uFlags=0x%08x, ppidlOut=%p)\n",
1318           iface, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut); 
1319
1320     /* prepare to fail */
1321     if (ppidlOut)
1322         *ppidlOut = NULL;
1323     
1324     /* pidl has to contain a single non-empty SHITEMID */
1325     if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1326         return E_INVALIDARG;
1327  
1328     /* check for invalid characters in lpcwszName. */
1329     for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1330         if (StrChrW(lpcwszName, awcInvalidChars[i]))
1331             return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1332
1333     /* build source path */
1334     memcpy(szSrc, This->m_pszPath, cBasePathLen);
1335     UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1336
1337     /* build destination path */
1338     memcpy(szDest, This->m_pszPath, cBasePathLen);
1339     WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen, 
1340                         FILENAME_MAX-cBasePathLen, NULL, NULL);
1341
1342     /* If the filename's extension is hidden to the user, we have to append it. */
1343     if (!(uFlags & SHGDN_FORPARSING) && 
1344         _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) && 
1345         SHELL_FS_HideExtension(wszSrcRelative))
1346     {
1347         int cLenDest = strlen(szDest);
1348         pwszExt = PathFindExtensionW(wszSrcRelative);
1349         WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest, 
1350             FILENAME_MAX - cLenDest, NULL, NULL);
1351     }
1352
1353     TRACE("src=%s dest=%s\n", szSrc, szDest);
1354
1355     /* Fail, if destination does already exist */
1356     if (!stat(szDest, &statDest)) 
1357         return E_FAIL;
1358
1359     /* Rename the file */
1360     if (rename(szSrc, szDest)) 
1361         return E_FAIL;
1362     
1363     /* Build a pidl for the path of the renamed file */
1364     cNameLen = lstrlenW(lpcwszName) + 1;
1365     if(pwszExt)
1366         cNameLen += lstrlenW(pwszExt);
1367     lpwszName = SHAlloc(cNameLen*sizeof(WCHAR)); /* due to const correctness. */
1368     lstrcpyW(lpwszName, lpcwszName);
1369     if(pwszExt)
1370         lstrcatW(lpwszName, pwszExt);
1371
1372     hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1373     SHFree(lpwszName);
1374     if (FAILED(hr)) {
1375         rename(szDest, szSrc); /* Undo the renaming */
1376         return E_FAIL;
1377     }
1378     pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1379     ILFree(pidlRelativeDest);
1380     pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1381     
1382     /* Inform the shell */
1383     if (_ILIsFolder(ILFindLastID(pidlDest))) 
1384         SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1385     else 
1386         SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1387     
1388     if (ppidlOut) 
1389         *ppidlOut = ILClone(ILFindLastID(pidlDest));
1390         
1391     ILFree(pidlSrc);
1392     ILFree(pidlDest);
1393     
1394     return S_OK;
1395 }
1396
1397 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
1398     IEnumExtraSearch **ppEnum) 
1399 {
1400     FIXME("stub\n");
1401     return E_NOTIMPL;
1402 }
1403
1404 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
1405     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
1406 {
1407     TRACE("(iface=%p,dwReserved=%x,pSort=%p,pDisplay=%p)\n", iface, dwReserved, pSort, pDisplay);
1408
1409     if (pSort)
1410         *pSort = 0;
1411     if (pDisplay)
1412         *pDisplay = 0;
1413
1414     return S_OK;
1415 }
1416
1417 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1418     UINT iColumn, SHCOLSTATEF *pcsFlags)
1419 {
1420     FIXME("stub\n");
1421     return E_NOTIMPL;
1422 }
1423
1424 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1425     GUID *pguid)
1426 {
1427     FIXME("stub\n");
1428     return E_NOTIMPL;
1429 }
1430
1431 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1432     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1433 {
1434     FIXME("stub\n");
1435     return E_NOTIMPL;
1436 }
1437
1438 #define SHELLVIEWCOLUMNS 7 
1439
1440 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1441     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1442 {
1443     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1444     HRESULT hr = E_FAIL;
1445     struct passwd *pPasswd;
1446     struct group *pGroup;
1447     struct stat statItem;
1448
1449     static const shvheader unixfs_header[SHELLVIEWCOLUMNS] = {
1450         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1451         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1452         {IDS_SHV_COLUMN3,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1453         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1454         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1455         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1456         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1457     };
1458
1459     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1460     
1461     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1462         return E_INVALIDARG;
1463
1464     if (!pidl)
1465         return SHELL32_GetColumnDetails(unixfs_header, iColumn, psd);
1466
1467     if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1468         char szPath[FILENAME_MAX];
1469         strcpy(szPath, This->m_pszPath);
1470         if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1471             return E_INVALIDARG;
1472         if (stat(szPath, &statItem))
1473             return E_INVALIDARG;
1474     }
1475
1476     psd->str.u.cStr[0] = '\0';
1477     psd->str.uType = STRRET_CSTR;
1478
1479     switch (iColumn) {
1480         case 0:
1481             hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1482             break;
1483         case 1:
1484             _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1485             break;
1486         case 2:
1487             _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1488             break;
1489         case 3:
1490             _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1491             break;
1492         case 4:
1493             psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1494             psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1495             psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1496             psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1497             psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1498             psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1499             psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1500             psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1501             psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1502             psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1503             psd->str.u.cStr[10] = '\0';
1504             break;
1505         case 5:
1506             pPasswd = getpwuid(statItem.st_uid);
1507             if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1508             break;
1509         case 6:
1510             pGroup = getgrgid(statItem.st_gid);
1511             if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1512             break;
1513     }
1514     
1515     return hr;
1516 }
1517
1518 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1519     SHCOLUMNID *pscid) 
1520 {
1521     FIXME("stub\n");
1522     return E_NOTIMPL;
1523 }
1524
1525 /* VTable for UnixFolder's IShellFolder2 interface.
1526  */
1527 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1528     UnixFolder_IShellFolder2_QueryInterface,
1529     UnixFolder_IShellFolder2_AddRef,
1530     UnixFolder_IShellFolder2_Release,
1531     UnixFolder_IShellFolder2_ParseDisplayName,
1532     UnixFolder_IShellFolder2_EnumObjects,
1533     UnixFolder_IShellFolder2_BindToObject,
1534     UnixFolder_IShellFolder2_BindToStorage,
1535     UnixFolder_IShellFolder2_CompareIDs,
1536     UnixFolder_IShellFolder2_CreateViewObject,
1537     UnixFolder_IShellFolder2_GetAttributesOf,
1538     UnixFolder_IShellFolder2_GetUIObjectOf,
1539     UnixFolder_IShellFolder2_GetDisplayNameOf,
1540     UnixFolder_IShellFolder2_SetNameOf,
1541     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1542     UnixFolder_IShellFolder2_EnumSearches,
1543     UnixFolder_IShellFolder2_GetDefaultColumn,
1544     UnixFolder_IShellFolder2_GetDefaultColumnState,
1545     UnixFolder_IShellFolder2_GetDetailsEx,
1546     UnixFolder_IShellFolder2_GetDetailsOf,
1547     UnixFolder_IShellFolder2_MapColumnToSCID
1548 };
1549
1550 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid, 
1551     void** ppvObject)
1552 {
1553     return UnixFolder_IShellFolder2_QueryInterface(
1554         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1555 }
1556
1557 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1558 {
1559     return UnixFolder_IShellFolder2_AddRef(
1560         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1561 }
1562
1563 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1564 {
1565     return UnixFolder_IShellFolder2_Release(
1566         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1567 }
1568
1569 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1570 {    
1571     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1572     
1573     TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1574     
1575     if (!pClassID)
1576         return E_INVALIDARG;
1577
1578     *pClassID = *This->m_pCLSID;
1579     return S_OK;
1580 }
1581
1582 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1583 {
1584     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1585     LPCITEMIDLIST current = pidl;
1586     char szBasePath[FILENAME_MAX] = "/";
1587     
1588     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1589
1590     /* Find the UnixFolderClass root */
1591     while (current->mkid.cb) {
1592         if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1593             (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1594         {
1595             break;
1596         }
1597         current = ILGetNext(current);
1598     }
1599
1600     if (current->mkid.cb) {
1601         if (_ILIsDrive(current)) {
1602             WCHAR wszDrive[4] = { '?', ':', '\\', 0 };
1603             wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1604             if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1605                 return E_FAIL;
1606         } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1607             WCHAR wszMyDocumentsPath[MAX_PATH];
1608             if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1609                 return E_FAIL;
1610             PathAddBackslashW(wszMyDocumentsPath);
1611             if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1612                 return E_FAIL;
1613         } 
1614         current = ILGetNext(current);
1615     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1616         /* Path rooted at Desktop */
1617         WCHAR wszDesktopPath[MAX_PATH];
1618         if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE)) 
1619             return E_FAIL;
1620         PathAddBackslashW(wszDesktopPath);
1621         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1622             return E_FAIL;
1623         current = pidl;
1624     } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1625         /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1626          * specifies the location in the shell namespace, but leaves the
1627          * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1628         This->m_pidlLocation = ILClone(pidl);
1629         return S_OK;
1630     } else {
1631         ERR("Unknown pidl type!\n");
1632         pdump(pidl);
1633         return E_INVALIDARG;
1634     }
1635   
1636     This->m_pidlLocation = ILClone(pidl);
1637     return UNIXFS_initialize_target_folder(This, szBasePath, current, 0); 
1638 }
1639
1640 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1641 {
1642     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1643     
1644     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1645
1646     if (!ppidl)
1647         return E_POINTER;
1648     *ppidl = ILClone (This->m_pidlLocation);
1649     return S_OK;
1650 }
1651
1652 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc, 
1653     LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1654 {
1655     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1656     WCHAR wszTargetDosPath[MAX_PATH];
1657     char szTargetPath[FILENAME_MAX] = "";
1658     
1659     TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1660
1661     /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1662     if (!ppfti) 
1663         return IPersistFolder3_Initialize(iface, pidlRoot);
1664
1665     if (ppfti->csidl != -1) {
1666         if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1667             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1668         {
1669             return E_FAIL;
1670         }
1671     } else if (*ppfti->szTargetParsingName) {
1672         lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1673         PathAddBackslashW(wszTargetDosPath);
1674         if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1675             return E_FAIL;
1676         }
1677     } else if (ppfti->pidlTargetFolder) {
1678         if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1679             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1680         {
1681             return E_FAIL;
1682         }
1683     } else {
1684         return E_FAIL;
1685     }
1686
1687     This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1688     if (!This->m_pszPath) 
1689         return E_FAIL;
1690     lstrcpyA(This->m_pszPath, szTargetPath);
1691     This->m_pidlLocation = ILClone(pidlRoot);
1692     This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1693         (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1694
1695     return S_OK;
1696 }
1697
1698 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface, 
1699     PERSIST_FOLDER_TARGET_INFO *ppfti)
1700 {
1701     FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1702     return E_NOTIMPL;
1703 }
1704
1705 /* VTable for UnixFolder's IPersistFolder interface.
1706  */
1707 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1708     UnixFolder_IPersistFolder3_QueryInterface,
1709     UnixFolder_IPersistFolder3_AddRef,
1710     UnixFolder_IPersistFolder3_Release,
1711     UnixFolder_IPersistFolder3_GetClassID,
1712     UnixFolder_IPersistFolder3_Initialize,
1713     UnixFolder_IPersistFolder3_GetCurFolder,
1714     UnixFolder_IPersistFolder3_InitializeEx,
1715     UnixFolder_IPersistFolder3_GetFolderTargetInfo
1716 };
1717
1718 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1719     REFIID riid, void** ppv)
1720 {
1721     return UnixFolder_IShellFolder2_QueryInterface(
1722         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1723 }
1724
1725 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1726 {
1727     return UnixFolder_IShellFolder2_AddRef(
1728         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1729 }
1730
1731 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1732 {
1733     return UnixFolder_IShellFolder2_Release(
1734         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1735 }
1736
1737 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface, 
1738     CLSID* pClassID)
1739 {
1740     return UnixFolder_IPersistFolder3_GetClassID(
1741         STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1742 }
1743
1744 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1745 {
1746     FIXME("() stub\n");
1747     return E_NOTIMPL;
1748 }
1749
1750 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface, 
1751     IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1752 {
1753      UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1754      static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1755      PERSIST_FOLDER_TARGET_INFO pftiTarget;
1756      VARIANT var;
1757      HRESULT hr;
1758      
1759      TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1760  
1761      if (!pPropertyBag)
1762          return E_POINTER;
1763  
1764      /* Get 'Target' property from the property bag. */
1765      V_VT(&var) = VT_BSTR;
1766      hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1767      if (FAILED(hr)) 
1768          return E_FAIL;
1769      lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1770      SysFreeString(V_BSTR(&var));
1771  
1772      pftiTarget.pidlTargetFolder = NULL;
1773      lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1774      pftiTarget.dwAttributes = -1;
1775      pftiTarget.csidl = -1;
1776  
1777      return UnixFolder_IPersistFolder3_InitializeEx(
1778                  STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1779 }
1780
1781 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1782     IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1783 {
1784     FIXME("() stub\n");
1785     return E_NOTIMPL;
1786 }
1787
1788 /* VTable for UnixFolder's IPersistPropertyBag interface.
1789  */
1790 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1791     UnixFolder_IPersistPropertyBag_QueryInterface,
1792     UnixFolder_IPersistPropertyBag_AddRef,
1793     UnixFolder_IPersistPropertyBag_Release,
1794     UnixFolder_IPersistPropertyBag_GetClassID,
1795     UnixFolder_IPersistPropertyBag_InitNew,
1796     UnixFolder_IPersistPropertyBag_Load,
1797     UnixFolder_IPersistPropertyBag_Save
1798 };
1799
1800 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid, 
1801     void** ppvObject)
1802 {
1803     return UnixFolder_IShellFolder2_QueryInterface(
1804         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1805 }
1806
1807 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1808 {
1809     return UnixFolder_IShellFolder2_AddRef(
1810         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1811 }
1812
1813 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1814 {
1815     return UnixFolder_IShellFolder2_Release(
1816         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1817 }
1818
1819 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1820 {
1821     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1822     IEnumIDList *pEnum;
1823     HRESULT hr;
1824     LPITEMIDLIST pidlElem;
1825     DWORD dwFetched;
1826     int i;
1827     WCHAR wszNewFolder[25];
1828     static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1829
1830     TRACE("(iface=%p, pwszName=%p, uLen=%u)\n", iface, pwszName, uLen);
1831
1832     LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1833
1834     if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1835         return E_INVALIDARG;
1836
1837     hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1838                                    SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1839     if (SUCCEEDED(hr)) {
1840         lstrcpynW(pwszName, wszNewFolder, uLen);
1841         IEnumIDList_Reset(pEnum);
1842         i = 2;
1843         while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1844             WCHAR wszTemp[MAX_PATH];
1845             _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1846             if (!lstrcmpiW(wszTemp, pwszName)) {
1847                 IEnumIDList_Reset(pEnum);
1848                 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1849                 if (i > 99) {
1850                     hr = E_FAIL;
1851                     break;
1852                 }
1853             }
1854         }
1855         IEnumIDList_Release(pEnum);
1856     }
1857     return hr;
1858 }
1859
1860 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName, 
1861     LPITEMIDLIST* ppidlOut)
1862 {
1863     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1864     char szNewDir[FILENAME_MAX];
1865     int cBaseLen;
1866
1867     TRACE("(iface=%p, hwnd=%p, pwszName=%s, ppidlOut=%p)\n", 
1868             iface, hwnd, debugstr_w(pwszName), ppidlOut);
1869
1870     if (ppidlOut)
1871         *ppidlOut = NULL;
1872
1873     if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1874         return E_FAIL;
1875     
1876     lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1877     cBaseLen = lstrlenA(szNewDir);
1878     WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1879    
1880     if (mkdir(szNewDir, 0777)) {
1881         char szMessage[256 + FILENAME_MAX];
1882         char szCaption[256];
1883
1884         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1885         sprintf(szMessage, szCaption, szNewDir);
1886         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1887         MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1888
1889         return E_FAIL;
1890     } else {
1891         LPITEMIDLIST pidlRelative;
1892
1893         /* Inform the shell */
1894         if (SUCCEEDED(UNIXFS_path_to_pidl(This, NULL, pwszName, &pidlRelative))) {
1895             LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1896             if (ppidlOut)
1897                 *ppidlOut = pidlRelative;
1898             else
1899                 ILFree(pidlRelative);
1900             SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1901             ILFree(pidlAbsolute);
1902         } else return E_FAIL;
1903         return S_OK;
1904     }
1905 }
1906
1907 /*
1908  * Delete specified files by converting the path to DOS paths and calling
1909  * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1910  * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1911  * the files using syscalls
1912  */
1913 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1914 {
1915     char szAbsolute[FILENAME_MAX], *pszRelative;
1916     LPWSTR wszPathsList, wszListPos;
1917     SHFILEOPSTRUCTW op;
1918     HRESULT ret;
1919     UINT i;
1920     
1921     lstrcpyA(szAbsolute, This->m_pszPath);
1922     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1923     
1924     wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1925     if (wszPathsList == NULL)
1926         return E_OUTOFMEMORY;
1927     for (i=0; i<cidl; i++) {
1928         LPWSTR wszDosPath;
1929         
1930         if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1931             continue;
1932         if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1933         {
1934             HeapFree(GetProcessHeap(), 0, wszPathsList);
1935             return E_INVALIDARG;
1936         }
1937         wszDosPath = wine_get_dos_file_name(szAbsolute);
1938         if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1939         {
1940             HeapFree(GetProcessHeap(), 0, wszPathsList);
1941             HeapFree(GetProcessHeap(), 0, wszDosPath);
1942             return S_FALSE;
1943         }
1944         lstrcpyW(wszListPos, wszDosPath);
1945         wszListPos += lstrlenW(wszListPos)+1;
1946         HeapFree(GetProcessHeap(), 0, wszDosPath);
1947     }
1948     *wszListPos = 0;
1949     
1950     ZeroMemory(&op, sizeof(op));
1951     op.hwnd = GetActiveWindow();
1952     op.wFunc = FO_DELETE;
1953     op.pFrom = wszPathsList;
1954     op.fFlags = FOF_ALLOWUNDO;
1955     if (!SHFileOperationW(&op))
1956     {
1957         WARN("SHFileOperationW failed\n");
1958         ret = E_FAIL;
1959     }
1960     else
1961         ret = S_OK;
1962
1963     HeapFree(GetProcessHeap(), 0, wszPathsList);
1964     return ret;
1965 }
1966
1967 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1968 {
1969     char szAbsolute[FILENAME_MAX], *pszRelative;
1970     static const WCHAR empty[] = {0};
1971     UINT i;
1972     
1973     if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
1974         return S_OK;
1975     
1976     lstrcpyA(szAbsolute, This->m_pszPath);
1977     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1978     
1979     for (i=0; i<cidl; i++) {
1980         if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1981             return E_INVALIDARG;
1982         if (_ILIsFolder(apidl[i])) {
1983             if (rmdir(szAbsolute))
1984                 return E_FAIL;
1985         } else if (_ILIsValue(apidl[i])) {
1986             if (unlink(szAbsolute))
1987                 return E_FAIL;
1988         }
1989     }
1990     return S_OK;
1991 }
1992
1993 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl, 
1994     LPCITEMIDLIST* apidl)
1995 {
1996     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1997     char szAbsolute[FILENAME_MAX], *pszRelative;
1998     LPITEMIDLIST pidlAbsolute;
1999     HRESULT hr = S_OK;
2000     UINT i;
2001     struct stat st;
2002     
2003     TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
2004
2005     hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
2006     if (hr == S_FALSE)
2007         hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
2008
2009     lstrcpyA(szAbsolute, This->m_pszPath);
2010     pszRelative = szAbsolute + lstrlenA(szAbsolute);
2011     
2012     /* we need to manually send the notifies if the files doesn't exist */
2013     for (i=0; i<cidl; i++) {
2014         if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
2015             continue;
2016         pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
2017         if (stat(szAbsolute, &st))
2018         {
2019             if (_ILIsFolder(apidl[i])) {
2020                 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
2021             } else if (_ILIsValue(apidl[i])) {
2022                 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
2023             }
2024         }
2025         ILFree(pidlAbsolute);
2026     }
2027         
2028     return hr;
2029 }
2030
2031 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom, 
2032     UINT cidl, LPCITEMIDLIST *apidl)
2033 {
2034     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
2035     DWORD dwAttributes;
2036     UINT i;
2037     HRESULT hr;
2038     char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
2039     
2040     TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p)\n", iface, psfFrom, cidl, apidl);
2041
2042     if (!psfFrom || !cidl || !apidl)
2043         return E_INVALIDARG;
2044
2045     /* All source items have to be filesystem items. */
2046     dwAttributes = SFGAO_FILESYSTEM;
2047     hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
2048     if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) 
2049         return E_INVALIDARG;
2050
2051     lstrcpyA(szAbsoluteDst, This->m_pszPath);
2052     pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
2053     
2054     for (i=0; i<cidl; i++) {
2055         WCHAR wszSrc[MAX_PATH];
2056         char szSrc[FILENAME_MAX];
2057         STRRET strret;
2058         HRESULT res;
2059         WCHAR *pwszDosSrc, *pwszDosDst;
2060
2061         /* Build the unix path of the current source item. */
2062         if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
2063             return E_FAIL;
2064         if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
2065             return E_FAIL;
2066         if (!UNIXFS_get_unix_path(wszSrc, szSrc)) 
2067             return E_FAIL;
2068
2069         /* Build the unix path of the current destination item */
2070         UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
2071
2072         pwszDosSrc = wine_get_dos_file_name(szSrc);
2073         pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
2074
2075         if (pwszDosSrc && pwszDosDst)
2076             res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
2077         else
2078             res = E_OUTOFMEMORY;
2079
2080         HeapFree(GetProcessHeap(), 0, pwszDosSrc);
2081         HeapFree(GetProcessHeap(), 0, pwszDosDst);
2082
2083         if (res != S_OK)
2084             return res;
2085     }
2086     return S_OK;
2087 }
2088
2089 /* VTable for UnixFolder's ISFHelper interface
2090  */
2091 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
2092     UnixFolder_ISFHelper_QueryInterface,
2093     UnixFolder_ISFHelper_AddRef,
2094     UnixFolder_ISFHelper_Release,
2095     UnixFolder_ISFHelper_GetUniqueName,
2096     UnixFolder_ISFHelper_AddFolder,
2097     UnixFolder_ISFHelper_DeleteItems,
2098     UnixFolder_ISFHelper_CopyItems
2099 };
2100
2101 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid, 
2102     void** ppvObject)
2103 {
2104     return UnixFolder_IShellFolder2_QueryInterface(
2105         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
2106 }
2107
2108 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
2109 {
2110     return UnixFolder_IShellFolder2_AddRef(
2111         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2112 }
2113
2114 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
2115 {
2116     return UnixFolder_IShellFolder2_Release(
2117         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2118 }
2119
2120 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2121 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2122
2123 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2124     DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2125 {
2126     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2127     FORMATETC format;
2128     STGMEDIUM medium; 
2129         
2130     TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n",
2131         iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2132
2133     if (!pdwEffect || !pDataObject)
2134         return E_INVALIDARG;
2135   
2136     /* Compute a mask of supported drop-effects for this shellfolder object and the given data 
2137      * object. Dropping is only supported on folders, which represent filesystem locations. One
2138      * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2139      * folder is not identical to the target folder. */
2140     This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2141     InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2142     if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2143         _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2144         SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2145     {
2146         LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2147         This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2148
2149         if (pidaShellIDList) { /* Files can only be moved between two different folders */
2150             if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2151                 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2152             GlobalUnlock(medium.u.hGlobal);
2153         }
2154     }
2155
2156     *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2157     
2158     return S_OK;
2159 }
2160
2161 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState, 
2162     POINTL pt, DWORD *pdwEffect)
2163 {
2164     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2165     
2166     TRACE("(iface=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n", iface, dwKeyState, 
2167         pt.x, pt.y, pdwEffect);
2168
2169     if (!pdwEffect)
2170         return E_INVALIDARG;
2171
2172     *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2173     
2174     return S_OK;
2175 }
2176
2177 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
2178     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2179     
2180     TRACE("(iface=%p)\n", iface);
2181  
2182     This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2183     
2184     return S_OK;
2185 }
2186
2187 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2188     DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2189 {
2190     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2191     FORMATETC format;
2192     STGMEDIUM medium; 
2193     HRESULT hr;
2194
2195     TRACE("(iface=%p, pDataObject=%p, dwKeyState=%d, pt={.x=%d, .y=%d}, pdwEffect=%p) semi-stub\n",
2196         iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2197
2198     InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2199     hr = IDataObject_GetData(pDataObject, &format, &medium);
2200     if (FAILED(hr))
2201         return hr;
2202
2203     if (medium.tymed == TYMED_HGLOBAL) {
2204         IShellFolder *psfSourceFolder, *psfDesktopFolder;
2205         LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2206         STRRET strret;
2207         UINT i;
2208     
2209         if (!pidaShellIDList) 
2210             return HRESULT_FROM_WIN32(GetLastError());
2211         
2212         hr = SHGetDesktopFolder(&psfDesktopFolder);
2213         if (FAILED(hr)) {
2214             GlobalUnlock(medium.u.hGlobal);
2215             return hr;
2216         }
2217
2218         hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL, 
2219                                        &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2220         IShellFolder_Release(psfDesktopFolder);
2221         if (FAILED(hr)) {
2222             GlobalUnlock(medium.u.hGlobal);
2223             return hr;
2224         }
2225
2226         for (i = 0; i < pidaShellIDList->cidl; i++) {
2227             WCHAR wszSourcePath[MAX_PATH];
2228
2229             hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2230                                                SHGDN_FORPARSING, &strret);
2231             if (FAILED(hr)) 
2232                 break;
2233
2234             hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2235             if (FAILED(hr)) 
2236                 break;
2237
2238             switch (*pdwEffect) {
2239                 case DROPEFFECT_MOVE:
2240                     FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2241                     break;
2242                 case DROPEFFECT_COPY:
2243                     FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2244                     break;
2245                 case DROPEFFECT_LINK:
2246                     FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2247                     break;
2248             }
2249         }
2250     
2251         IShellFolder_Release(psfSourceFolder);
2252         GlobalUnlock(medium.u.hGlobal);
2253         return hr;
2254     }
2255  
2256     return E_NOTIMPL;
2257 }
2258
2259 /* VTable for UnixFolder's IDropTarget interface
2260  */
2261 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2262     UnixFolder_IDropTarget_QueryInterface,
2263     UnixFolder_IDropTarget_AddRef,
2264     UnixFolder_IDropTarget_Release,
2265     UnixFolder_IDropTarget_DragEnter,
2266     UnixFolder_IDropTarget_DragOver,
2267     UnixFolder_IDropTarget_DragLeave,
2268     UnixFolder_IDropTarget_Drop
2269 };
2270
2271 /******************************************************************************
2272  * Unix[Dos]Folder_Constructor [Internal]
2273  *
2274  * PARAMS
2275  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
2276  *  riid      [I] Interface asked for by the client.
2277  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
2278  *
2279  * NOTES
2280  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
2281  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2282  *  compatible signature.
2283  *
2284  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2285  *  means that paths are converted from dos to unix and back at the interfaces.
2286  */
2287 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID) 
2288 {
2289     HRESULT hr = E_FAIL;
2290     UnixFolder *pUnixFolder;
2291    
2292     if (pUnkOuter) {
2293         FIXME("Aggregation not yet implemented!\n");
2294         return CLASS_E_NOAGGREGATION;
2295     }
2296     
2297     pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2298
2299     if(pUnixFolder) {
2300         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2301         pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2302         pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2303         pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2304         pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2305         pUnixFolder->m_cRef = 0;
2306         pUnixFolder->m_pszPath = NULL;
2307         pUnixFolder->m_pidlLocation = NULL;
2308         pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2309         pUnixFolder->m_dwAttributes = 0;
2310         pUnixFolder->m_pCLSID = pCLSID;
2311         pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2312
2313         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2314         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2315         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2316     }
2317     return hr;
2318 }
2319
2320 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2321     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2322     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2323 }
2324
2325 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2326     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2327     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2328 }
2329
2330 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2331     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2332     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2333 }
2334
2335 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2336     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2337     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2338 }
2339
2340 /******************************************************************************
2341  * UnixSubFolderIterator
2342  *
2343  * Class whose heap based objects represent iterators over the sub-directories
2344  * of a given UnixFolder object. 
2345  */
2346
2347 /* UnixSubFolderIterator object layout and typedef.
2348  */
2349 typedef struct _UnixSubFolderIterator {
2350     const IEnumIDListVtbl *lpIEnumIDListVtbl;
2351     LONG m_cRef;
2352     SHCONTF m_fFilter;
2353     DIR *m_dirFolder;
2354     char m_szFolder[FILENAME_MAX];
2355 } UnixSubFolderIterator;
2356
2357 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2358     TRACE("(iterator=%p)\n", iterator);
2359
2360     if (iterator->m_dirFolder)
2361         closedir(iterator->m_dirFolder);
2362     SHFree(iterator);
2363 }
2364
2365 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
2366     REFIID riid, void** ppv)
2367 {
2368     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2369     
2370     if (!ppv) return E_INVALIDARG;
2371     
2372     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2373         *ppv = iface;
2374     } else {
2375         *ppv = NULL;
2376         return E_NOINTERFACE;
2377     }
2378
2379     IEnumIDList_AddRef(iface);
2380     return S_OK;
2381 }
2382                             
2383 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2384 {
2385     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2386
2387     TRACE("(iface=%p)\n", iface);
2388    
2389     return InterlockedIncrement(&This->m_cRef);
2390 }
2391
2392 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2393 {
2394     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2395     ULONG cRef;
2396     
2397     TRACE("(iface=%p)\n", iface);
2398
2399     cRef = InterlockedDecrement(&This->m_cRef);
2400     
2401     if (!cRef) 
2402         UnixSubFolderIterator_Destroy(This);
2403
2404     return cRef;
2405 }
2406
2407 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
2408     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2409 {
2410     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2411     ULONG i = 0;
2412
2413     /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2414     if (This->m_dirFolder) {
2415         char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2416         struct dirent *pDirEntry;
2417
2418         while (i < celt) {
2419             pDirEntry = readdir(This->m_dirFolder);
2420             if (!pDirEntry) break; /* No more entries */
2421             if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2422
2423             /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2424              * and see if it passes the filter. 
2425              */
2426             lstrcpyA(pszRelativePath, pDirEntry->d_name);
2427             rgelt[i] = SHAlloc(
2428                 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2429             if (!UNIXFS_build_shitemid(This->m_szFolder, NULL, rgelt[i]) ||
2430                 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter)) 
2431             {
2432                 SHFree(rgelt[i]);
2433                 continue;
2434             }
2435             memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2436             i++;
2437         }
2438         *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2439     }
2440     
2441     if (pceltFetched)
2442         *pceltFetched = i;
2443
2444     return (i == 0) ? S_FALSE : S_OK;
2445 }
2446     
2447 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2448 {
2449     LPITEMIDLIST *apidl;
2450     ULONG cFetched;
2451     HRESULT hr;
2452     
2453     TRACE("(iface=%p, celt=%d)\n", iface, celt);
2454
2455     /* Call IEnumIDList::Next and delete the resulting pidls. */
2456     apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2457     hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2458     if (SUCCEEDED(hr))
2459         while (cFetched--) 
2460             SHFree(apidl[cFetched]);
2461     SHFree(apidl);
2462
2463     return hr;
2464 }
2465
2466 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2467 {
2468     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2469         
2470     TRACE("(iface=%p)\n", iface);
2471
2472     if (This->m_dirFolder)
2473         rewinddir(This->m_dirFolder);
2474     
2475     return S_OK;
2476 }
2477
2478 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
2479     IEnumIDList** ppenum)
2480 {
2481     FIXME("stub\n");
2482     return E_NOTIMPL;
2483 }
2484
2485 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2486  */
2487 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2488     UnixSubFolderIterator_IEnumIDList_QueryInterface,
2489     UnixSubFolderIterator_IEnumIDList_AddRef,
2490     UnixSubFolderIterator_IEnumIDList_Release,
2491     UnixSubFolderIterator_IEnumIDList_Next,
2492     UnixSubFolderIterator_IEnumIDList_Skip,
2493     UnixSubFolderIterator_IEnumIDList_Reset,
2494     UnixSubFolderIterator_IEnumIDList_Clone
2495 };
2496
2497 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2498     UnixSubFolderIterator *iterator;
2499
2500     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2501     
2502     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2503     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2504     iterator->m_cRef = 0;
2505     iterator->m_fFilter = fFilter;
2506     iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2507     lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2508
2509     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2510     
2511     return (IUnknown*)iterator;
2512 }
2513
2514 #else /* __MINGW32__ || _MSC_VER */
2515
2516 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2517 {
2518     return E_NOTIMPL;
2519 }
2520
2521 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2522 {
2523     return E_NOTIMPL;
2524 }
2525
2526 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2527 {
2528     return E_NOTIMPL;
2529 }
2530
2531 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv)
2532 {
2533     return E_NOTIMPL;
2534 }
2535
2536 #endif /* __MINGW32__ || _MSC_VER */
2537
2538 /******************************************************************************
2539  * UNIXFS_is_rooted_at_desktop [Internal]
2540  *
2541  * Checks if the unixfs namespace extension is rooted at desktop level.
2542  *
2543  * RETURNS
2544  *  TRUE, if unixfs is rooted at desktop level
2545  *  FALSE, if not.
2546  */
2547 BOOL UNIXFS_is_rooted_at_desktop(void) {
2548     HKEY hKey;
2549     WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
2550         'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
2551         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
2552         'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
2553         'N','a','m','e','S','p','a','c','e','\\',0 };
2554
2555     if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
2556         RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
2557     {
2558         RegCloseKey(hKey);
2559         return TRUE;
2560     }
2561     return FALSE;
2562 }