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