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