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