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