shell32: Register 'LocalizedString' values for shellfolder classes.
[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             return FALSE;
705         }
706         pidl = ILGetNext(pidl);
707     }
708     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
709
710     if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */ 
711         ERR("Computed length of pidl incorrect. Please report.\n");
712     
713     return TRUE;
714 }
715
716 /******************************************************************************
717  * UNIXFS_initialize_target_folder [Internal]
718  *
719  *  Initialize the m_pszPath member of an UnixFolder, given an absolute unix
720  *  base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
721  *  specifies the location in the shell namespace alone. 
722  * 
723  * PARAMS
724  *  This          [IO] The UnixFolder, whose target path is to be initialized
725  *  szBasePath    [I]  The absolute base path
726  *  pidlSubFolder [I]  Relative part of the path, given as an ITEMIDLIST
727  *  dwAttributes  [I]  Attributes to add to the Folders m_dwAttributes member 
728  *                     (Used to pass the SFGAO_FILESYSTEM flag down the path)
729  * RETURNS
730  *  Success: S_OK,
731  *  Failure: E_FAIL
732  */
733 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
734     LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
735 {
736     LPCITEMIDLIST current = pidlSubFolder;
737     DWORD dwPathLen = strlen(szBasePath)+1;
738     struct stat statPrefix;
739     char *pNextDir;
740         
741     /* Determine the path's length bytes */
742     while (current && current->mkid.cb) {
743         dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
744         current = ILGetNext(current);
745     };
746
747     /* Build the path and compute the attributes*/
748     This->m_dwAttributes = 
749             dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
750     This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
751     if (!This->m_pszPath) {
752         WARN("SHAlloc failed!\n");
753         return E_FAIL;
754     }
755     current = pidlSubFolder;
756     strcpy(pNextDir, szBasePath);
757     pNextDir += strlen(szBasePath);
758     if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
759         This->m_dwAttributes |= SFGAO_FILESYSTEM;
760     if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
761         *pNextDir = '\0';
762         if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
763             This->m_dwAttributes |= SFGAO_FILESYSTEM;
764     }
765     while (current && current->mkid.cb) {
766         pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
767         if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
768             *pNextDir = '\0';
769             if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
770                 This->m_dwAttributes |= SFGAO_FILESYSTEM;
771         }
772         *pNextDir++ = '/';
773         current = ILGetNext(current);
774     }
775     *pNextDir='\0';
776  
777     return S_OK;
778 }
779
780 /******************************************************************************
781  * UnixFolder
782  *
783  * Class whose heap based instances represent unix filesystem directories.
784  */
785
786 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
787     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
788     
789     SHFree(pUnixFolder->m_pszPath);
790     ILFree(pUnixFolder->m_pidlLocation);
791     SHFree(pUnixFolder);
792 }
793
794 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
795     void **ppv) 
796 {
797     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
798         
799     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
800     
801     if (!ppv) return E_INVALIDARG;
802     
803     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
804         IsEqualIID(&IID_IShellFolder2, riid)) 
805     {
806         *ppv = STATIC_CAST(IShellFolder2, This);
807     } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) || 
808                IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) 
809     {
810         *ppv = STATIC_CAST(IPersistFolder3, This);
811     } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
812         *ppv = STATIC_CAST(IPersistPropertyBag, This);
813     } else if (IsEqualIID(&IID_ISFHelper, riid)) {
814         *ppv = STATIC_CAST(ISFHelper, This);
815     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
816         *ppv = STATIC_CAST(IDropTarget, This);
817         if (!cfShellIDList) 
818             cfShellIDList = RegisterClipboardFormatA(CFSTR_SHELLIDLIST);
819     } else {
820         *ppv = NULL;
821         return E_NOINTERFACE;
822     }
823
824     IUnknown_AddRef((IUnknown*)*ppv);
825     return S_OK;
826 }
827
828 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
829     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
830
831     TRACE("(iface=%p)\n", iface);
832
833     return InterlockedIncrement(&This->m_cRef);
834 }
835
836 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
837     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
838     ULONG cRef;
839     
840     TRACE("(iface=%p)\n", iface);
841
842     cRef = InterlockedDecrement(&This->m_cRef);
843     
844     if (!cRef) 
845         UnixFolder_Destroy(This);
846
847     return cRef;
848 }
849
850 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
851     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
852     ULONG* pdwAttributes)
853 {
854     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
855     BOOL result;
856
857     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
858           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
859           pchEaten, ppidl, pdwAttributes);
860
861     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
862     if (result && pdwAttributes && *pdwAttributes)
863     {
864         IShellFolder *pParentSF;
865         LPCITEMIDLIST pidlLast;
866         LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
867         HRESULT hr;
868         
869         hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
870         if (FAILED(hr)) {
871             FIXME("SHBindToParent failed! hr = %08lx\n", hr);
872             ILFree(pidlComplete);
873             return E_FAIL;
874         }
875         IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
876         IShellFolder_Release(pParentSF);
877         ILFree(pidlComplete);
878     }
879
880     if (!result) TRACE("FAILED!\n");
881     return result ? S_OK : E_FAIL;
882 }
883
884 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
885
886 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
887     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
888 {
889     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
890     IUnknown *newIterator;
891     HRESULT hr;
892     
893     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
894             iface, hwndOwner, grfFlags, ppEnumIDList);
895
896     if (!This->m_pszPath) {
897         WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
898         return E_UNEXPECTED;
899     }
900
901     newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
902     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
903     IUnknown_Release(newIterator);
904     
905     return hr;
906 }
907
908 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
909
910 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
911     LPBC pbcReserved, REFIID riid, void** ppvOut)
912 {
913     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
914     IPersistFolder3 *persistFolder;
915     HRESULT hr;
916     const CLSID *clsidChild;
917         
918     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
919             iface, pidl, pbcReserved, riid, ppvOut);
920
921     if (!pidl || !pidl->mkid.cb)
922         return E_INVALIDARG;
923    
924     if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
925         /* Children of FolderShortcuts are ShellFSFolders on Windows. 
926          * Unixfs' counterpart is UnixDosFolder. */
927         clsidChild = &CLSID_UnixDosFolder;    
928     } else {
929         clsidChild = This->m_pCLSID;
930     }
931     
932     hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
933     if (!SUCCEEDED(hr)) return hr;
934     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
935    
936     if (SUCCEEDED(hr)) {
937         UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
938         subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
939         hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
940                                              This->m_dwAttributes & SFGAO_FILESYSTEM);
941     } 
942
943     IPersistFolder3_Release(persistFolder);
944     
945     return hr;
946 }
947
948 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
949     LPBC pbcReserved, REFIID riid, void** ppvObj)
950 {
951     FIXME("stub\n");
952     return E_NOTIMPL;
953 }
954
955 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
956     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
957 {
958     BOOL isEmpty1, isEmpty2;
959     HRESULT hr = E_FAIL;
960     LPITEMIDLIST firstpidl;
961     IShellFolder2 *psf;
962     int compare;
963
964     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
965     
966     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
967     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
968
969     if (isEmpty1 && isEmpty2) 
970         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
971     else if (isEmpty1)
972         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
973     else if (isEmpty2)
974         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
975
976     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
977         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
978     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
979         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
980
981     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
982                              _ILGetTextPointer(pidl1), -1,
983                              _ILGetTextPointer(pidl2), -1);
984     
985     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
986         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
987
988     if (pidl1->mkid.cb < pidl2->mkid.cb)
989         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
990     else if (pidl1->mkid.cb > pidl2->mkid.cb)
991         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
992
993     firstpidl = ILCloneFirst(pidl1);
994     pidl1 = ILGetNext(pidl1);
995     pidl2 = ILGetNext(pidl2);
996     
997     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
998     if (SUCCEEDED(hr)) {
999         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1000         IShellFolder2_Release(psf);
1001     }
1002
1003     ILFree(firstpidl);
1004     return hr;
1005 }
1006
1007 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1008     REFIID riid, void** ppv)
1009 {
1010     HRESULT hr = E_INVALIDARG;
1011         
1012     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1013     
1014     if (!ppv) return E_INVALIDARG;
1015     *ppv = NULL;
1016     
1017     if (IsEqualIID(&IID_IShellView, riid)) {
1018         LPSHELLVIEW pShellView;
1019         
1020         pShellView = IShellView_Constructor((IShellFolder*)iface);
1021         if (pShellView) {
1022             hr = IShellView_QueryInterface(pShellView, riid, ppv);
1023             IShellView_Release(pShellView);
1024         }
1025     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1026         hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1027     }
1028     
1029     return hr;
1030 }
1031
1032 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
1033     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1034 {
1035     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1036     HRESULT hr = S_OK;
1037         
1038     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1039  
1040     if (!rgfInOut || (cidl && !apidl)) 
1041         return E_INVALIDARG;
1042     
1043     if (cidl == 0) {
1044         *rgfInOut &= This->m_dwAttributes;
1045     } else {
1046         char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1047         UINT i;
1048
1049         *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1050                     SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1051         lstrcpyA(szAbsolutePath, This->m_pszPath);
1052         pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1053         for (i=0; i<cidl; i++) {
1054             if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1055                 struct stat fileStat;
1056                 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath)) 
1057                     return E_INVALIDARG;
1058                 if (stat(szAbsolutePath, &fileStat) || !UNIXFS_is_dos_device(&fileStat))
1059                     *rgfInOut &= ~SFGAO_FILESYSTEM;
1060             }
1061             if (_ILIsFolder(apidl[i])) 
1062                 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1063         }
1064     }
1065     
1066     return hr;
1067 }
1068
1069 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
1070     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1071 {
1072     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1073     UINT i;
1074     
1075     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1076         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1077
1078     if (!cidl || !apidl || !riid || !ppvOut) 
1079         return E_INVALIDARG;
1080
1081     for (i=0; i<cidl; i++) 
1082         if (!apidl[i]) 
1083             return E_INVALIDARG;
1084     
1085     if (IsEqualIID(&IID_IContextMenu, riid)) {
1086         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1087         return S_OK;
1088     } else if (IsEqualIID(&IID_IDataObject, riid)) {
1089         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1090         return S_OK;
1091     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1092         LPITEMIDLIST pidl;
1093         if (cidl != 1) return E_INVALIDARG;
1094         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1095         *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
1096         SHFree(pidl);
1097         return S_OK;
1098     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1099         LPITEMIDLIST pidl;
1100         if (cidl != 1) return E_INVALIDARG;
1101         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1102         *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
1103         SHFree(pidl);
1104         return S_OK;
1105     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1106         if (cidl != 1) return E_INVALIDARG;
1107         return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1108     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1109         FIXME("IShellLinkW\n");
1110         return E_FAIL;
1111     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1112         FIXME("IShellLinkA\n");
1113         return E_FAIL;
1114     } else {
1115         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1116         return E_NOINTERFACE;
1117     }
1118 }
1119
1120 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
1121     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1122 {
1123     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1124     HRESULT hr = S_OK;    
1125
1126     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
1127     
1128     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1129         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1130     {
1131         if (!pidl || !pidl->mkid.cb) {
1132             lpName->uType = STRRET_WSTR;
1133             if (This->m_dwPathMode == PATHMODE_UNIX) {
1134                 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1135                 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1136                 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1137                 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1138             } else {
1139                 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1140                 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1141                 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1142                 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1143                 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1144                 PathRemoveBackslashW(lpName->u.pOleStr);
1145                 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1146             }
1147         } else {
1148             IShellFolder *pSubFolder;
1149             SHITEMID emptyIDL = { 0, { 0 } };
1150
1151             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1152             if (!SUCCEEDED(hr)) return hr;
1153        
1154             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1155             IShellFolder_Release(pSubFolder);
1156         }
1157     } else {
1158         WCHAR wszFileName[MAX_PATH];
1159         if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1160         lpName->uType = STRRET_WSTR;
1161         lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1162         if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1163         lstrcpyW(lpName->u.pOleStr, wszFileName);
1164         if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS && 
1165             !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1166         {
1167             PathRemoveExtensionW(lpName->u.pOleStr);
1168         }
1169     }
1170
1171     TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1172     
1173     return hr;
1174 }
1175
1176 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd, 
1177     LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1178 {
1179     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1180
1181     static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1182     char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1183     WCHAR wszSrcRelative[MAX_PATH];
1184     int cBasePathLen = lstrlenA(This->m_pszPath), i;
1185     struct stat statDest;
1186     LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1187     LPOLESTR lpwszName;
1188     HRESULT hr;
1189    
1190     TRACE("(iface=%p, hwnd=%p, pidl=%p, lpcwszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
1191           iface, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut); 
1192
1193     /* prepare to fail */
1194     if (ppidlOut)
1195         *ppidlOut = NULL;
1196     
1197     /* pidl has to contain a single non-empty SHITEMID */
1198     if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1199         return E_INVALIDARG;
1200  
1201     /* check for invalid characters in lpcwszName. */
1202     for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1203         if (StrChrW(lpcwszName, awcInvalidChars[i]))
1204             return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1205
1206     /* build source path */
1207     memcpy(szSrc, This->m_pszPath, cBasePathLen);
1208     UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1209
1210     /* build destination path */
1211     memcpy(szDest, This->m_pszPath, cBasePathLen);
1212     WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen, 
1213                         FILENAME_MAX-cBasePathLen, NULL, NULL);
1214
1215     /* If the filename's extension is hidden to the user, we have to append it. */
1216     if (!(uFlags & SHGDN_FORPARSING) && 
1217         _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) && 
1218         SHELL_FS_HideExtension(wszSrcRelative))
1219     {
1220         WCHAR *pwszExt = PathFindExtensionW(wszSrcRelative);
1221         int cLenDest = strlen(szDest);
1222         WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest, 
1223             FILENAME_MAX - cLenDest, NULL, NULL);
1224     }
1225
1226     TRACE("src=%s dest=%s\n", szSrc, szDest);
1227
1228     /* Fail, if destination does already exist */
1229     if (!stat(szDest, &statDest)) 
1230         return E_FAIL;
1231
1232     /* Rename the file */
1233     if (rename(szSrc, szDest)) 
1234         return E_FAIL;
1235     
1236     /* Build a pidl for the path of the renamed file */
1237     lpwszName = SHAlloc((lstrlenW(lpcwszName)+1)*sizeof(WCHAR)); /* due to const correctness. */
1238     lstrcpyW(lpwszName, lpcwszName);
1239     hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1240     SHFree(lpwszName);
1241     if (FAILED(hr)) {
1242         rename(szDest, szSrc); /* Undo the renaming */
1243         return E_FAIL;
1244     }
1245     pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1246     ILFree(pidlRelativeDest);
1247     pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1248     
1249     /* Inform the shell */
1250     if (_ILIsFolder(ILFindLastID(pidlDest))) 
1251         SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1252     else 
1253         SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1254     
1255     if (ppidlOut) 
1256         *ppidlOut = ILClone(ILFindLastID(pidlDest));
1257         
1258     ILFree(pidlSrc);
1259     ILFree(pidlDest);
1260     
1261     return S_OK;
1262 }
1263
1264 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
1265     IEnumExtraSearch **ppEnum) 
1266 {
1267     FIXME("stub\n");
1268     return E_NOTIMPL;
1269 }
1270
1271 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
1272     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
1273 {
1274     FIXME("stub\n");
1275     return E_NOTIMPL;
1276 }
1277
1278 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1279     UINT iColumn, SHCOLSTATEF *pcsFlags)
1280 {
1281     FIXME("stub\n");
1282     return E_NOTIMPL;
1283 }
1284
1285 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1286     GUID *pguid)
1287 {
1288     FIXME("stub\n");
1289     return E_NOTIMPL;
1290 }
1291
1292 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1293     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1294 {
1295     FIXME("stub\n");
1296     return E_NOTIMPL;
1297 }
1298
1299 #define SHELLVIEWCOLUMNS 7 
1300
1301 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1302     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1303 {
1304     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1305     HRESULT hr = E_FAIL;
1306     struct passwd *pPasswd;
1307     struct group *pGroup;
1308     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1309         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1310         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1311         {IDS_SHV_COLUMN3,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1312         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1313         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1314         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1315         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1316     };
1317
1318     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1319     
1320     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1321         return E_INVALIDARG;
1322
1323     if (!pidl) {
1324         psd->fmt = SFHeader[iColumn].fmt;
1325         psd->cxChar = SFHeader[iColumn].cxChar;
1326         psd->str.uType = STRRET_CSTR;
1327         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1328         return S_OK;
1329     } else {
1330         struct stat statItem;
1331         if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1332             char szPath[FILENAME_MAX];
1333             strcpy(szPath, This->m_pszPath);
1334             if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1335                 return E_INVALIDARG;
1336             if (stat(szPath, &statItem)) 
1337                 return E_INVALIDARG;
1338         }
1339         psd->str.u.cStr[0] = '\0';
1340         psd->str.uType = STRRET_CSTR;
1341         switch (iColumn) {
1342             case 0:
1343                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1344                 break;
1345             case 1:
1346                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1347                 break;
1348             case 2:
1349                 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1350                 break;
1351             case 3:
1352                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1353                 break;
1354             case 4:
1355                 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1356                 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1357                 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1358                 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1359                 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1360                 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1361                 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1362                 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1363                 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1364                 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1365                 psd->str.u.cStr[10] = '\0';
1366                 break;
1367             case 5:
1368                 pPasswd = getpwuid(statItem.st_uid);
1369                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1370                 break;
1371             case 6:
1372                 pGroup = getgrgid(statItem.st_gid);
1373                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1374                 break;
1375         }
1376     }
1377     
1378     return hr;
1379 }
1380
1381 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1382     SHCOLUMNID *pscid) 
1383 {
1384     FIXME("stub\n");
1385     return E_NOTIMPL;
1386 }
1387
1388 /* VTable for UnixFolder's IShellFolder2 interface.
1389  */
1390 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1391     UnixFolder_IShellFolder2_QueryInterface,
1392     UnixFolder_IShellFolder2_AddRef,
1393     UnixFolder_IShellFolder2_Release,
1394     UnixFolder_IShellFolder2_ParseDisplayName,
1395     UnixFolder_IShellFolder2_EnumObjects,
1396     UnixFolder_IShellFolder2_BindToObject,
1397     UnixFolder_IShellFolder2_BindToStorage,
1398     UnixFolder_IShellFolder2_CompareIDs,
1399     UnixFolder_IShellFolder2_CreateViewObject,
1400     UnixFolder_IShellFolder2_GetAttributesOf,
1401     UnixFolder_IShellFolder2_GetUIObjectOf,
1402     UnixFolder_IShellFolder2_GetDisplayNameOf,
1403     UnixFolder_IShellFolder2_SetNameOf,
1404     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1405     UnixFolder_IShellFolder2_EnumSearches,
1406     UnixFolder_IShellFolder2_GetDefaultColumn,
1407     UnixFolder_IShellFolder2_GetDefaultColumnState,
1408     UnixFolder_IShellFolder2_GetDetailsEx,
1409     UnixFolder_IShellFolder2_GetDetailsOf,
1410     UnixFolder_IShellFolder2_MapColumnToSCID
1411 };
1412
1413 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid, 
1414     void** ppvObject)
1415 {
1416     return UnixFolder_IShellFolder2_QueryInterface(
1417         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1418 }
1419
1420 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1421 {
1422     return UnixFolder_IShellFolder2_AddRef(
1423         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1424 }
1425
1426 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1427 {
1428     return UnixFolder_IShellFolder2_Release(
1429         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1430 }
1431
1432 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1433 {    
1434     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1435     
1436     TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1437     
1438     if (!pClassID)
1439         return E_INVALIDARG;
1440
1441     memcpy(pClassID, This->m_pCLSID, sizeof(CLSID));
1442     return S_OK;
1443 }
1444
1445 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1446 {
1447     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1448     LPCITEMIDLIST current = pidl;
1449     char szBasePath[FILENAME_MAX] = "/";
1450     
1451     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1452
1453     /* Find the UnixFolderClass root */
1454     while (current->mkid.cb) {
1455         if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1456             (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1457         {
1458             break;
1459         }
1460         current = ILGetNext(current);
1461     }
1462
1463     if (current && current->mkid.cb) {
1464         if (_ILIsDrive(current)) {
1465             WCHAR wszDrive[4] = { '?', ':', '\\', 0 };
1466             wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1467             if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1468                 return E_FAIL;
1469         } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1470             WCHAR wszMyDocumentsPath[MAX_PATH];
1471             if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1472                 return E_FAIL;
1473             PathAddBackslashW(wszMyDocumentsPath);
1474             if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1475                 return E_FAIL;
1476         } 
1477         current = ILGetNext(current);
1478     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1479         /* Path rooted at Desktop */
1480         WCHAR wszDesktopPath[MAX_PATH];
1481         if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE)) 
1482             return E_FAIL;
1483         PathAddBackslashW(wszDesktopPath);
1484         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1485             return E_FAIL;
1486         current = pidl;
1487     } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1488         /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1489          * specifies the location in the shell namespace, but leaves the
1490          * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1491         This->m_pidlLocation = ILClone(pidl);
1492         return S_OK;
1493     } else {
1494         ERR("Unknown pidl type!\n");
1495         pdump(pidl);
1496         return E_INVALIDARG;
1497     }
1498   
1499     This->m_pidlLocation = ILClone(pidl);
1500     return UNIXFS_initialize_target_folder(This, szBasePath, current, 0); 
1501 }
1502
1503 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1504 {
1505     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1506     
1507     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1508
1509     if (!ppidl)
1510         return E_POINTER;
1511     *ppidl = ILClone (This->m_pidlLocation);
1512     return S_OK;
1513 }
1514
1515 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc, 
1516     LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1517 {
1518     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1519     WCHAR wszTargetDosPath[MAX_PATH];
1520     char szTargetPath[FILENAME_MAX] = "";
1521     
1522     TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1523
1524     /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1525     if (!ppfti) 
1526         return IPersistFolder3_Initialize(iface, pidlRoot);
1527
1528     if (ppfti->csidl != -1) {
1529         if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1530             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1531         {
1532             return E_FAIL;
1533         }
1534     } else if (*ppfti->szTargetParsingName) {
1535         lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1536         PathAddBackslashW(wszTargetDosPath);
1537         if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1538             return E_FAIL;
1539         }
1540     } else if (ppfti->pidlTargetFolder) {
1541         if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1542             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1543         {
1544             return E_FAIL;
1545         }
1546     } else {
1547         return E_FAIL;
1548     }
1549
1550     This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1551     if (!This->m_pszPath) 
1552         return E_FAIL;
1553     lstrcpyA(This->m_pszPath, szTargetPath);
1554     This->m_pidlLocation = ILClone(pidlRoot);
1555     This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1556         (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1557
1558     return S_OK;
1559 }
1560
1561 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface, 
1562     PERSIST_FOLDER_TARGET_INFO *ppfti)
1563 {
1564     FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1565     return E_NOTIMPL;
1566 }
1567
1568 /* VTable for UnixFolder's IPersistFolder interface.
1569  */
1570 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1571     UnixFolder_IPersistFolder3_QueryInterface,
1572     UnixFolder_IPersistFolder3_AddRef,
1573     UnixFolder_IPersistFolder3_Release,
1574     UnixFolder_IPersistFolder3_GetClassID,
1575     UnixFolder_IPersistFolder3_Initialize,
1576     UnixFolder_IPersistFolder3_GetCurFolder,
1577     UnixFolder_IPersistFolder3_InitializeEx,
1578     UnixFolder_IPersistFolder3_GetFolderTargetInfo
1579 };
1580
1581 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1582     REFIID riid, void** ppv)
1583 {
1584     return UnixFolder_IShellFolder2_QueryInterface(
1585         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1586 }
1587
1588 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1589 {
1590     return UnixFolder_IShellFolder2_AddRef(
1591         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1592 }
1593
1594 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1595 {
1596     return UnixFolder_IShellFolder2_Release(
1597         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1598 }
1599
1600 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface, 
1601     CLSID* pClassID)
1602 {
1603     return UnixFolder_IPersistFolder3_GetClassID(
1604         STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1605 }
1606
1607 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1608 {
1609     FIXME("() stub\n");
1610     return E_NOTIMPL;
1611 }
1612
1613 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface, 
1614     IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1615 {
1616      UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1617      static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1618      PERSIST_FOLDER_TARGET_INFO pftiTarget;
1619      VARIANT var;
1620      HRESULT hr;
1621      
1622      TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1623  
1624      if (!pPropertyBag)
1625          return E_POINTER;
1626  
1627      /* Get 'Target' property from the property bag. */
1628      V_VT(&var) = VT_BSTR;
1629      hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1630      if (FAILED(hr)) 
1631          return E_FAIL;
1632      lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1633      SysFreeString(V_BSTR(&var));
1634  
1635      pftiTarget.pidlTargetFolder = NULL;
1636      lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1637      pftiTarget.dwAttributes = -1;
1638      pftiTarget.csidl = -1;
1639  
1640      return UnixFolder_IPersistFolder3_InitializeEx(
1641                  STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1642 }
1643
1644 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1645     IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1646 {
1647     FIXME("() stub\n");
1648     return E_NOTIMPL;
1649 }
1650
1651 /* VTable for UnixFolder's IPersistPropertyBag interface.
1652  */
1653 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1654     UnixFolder_IPersistPropertyBag_QueryInterface,
1655     UnixFolder_IPersistPropertyBag_AddRef,
1656     UnixFolder_IPersistPropertyBag_Release,
1657     UnixFolder_IPersistPropertyBag_GetClassID,
1658     UnixFolder_IPersistPropertyBag_InitNew,
1659     UnixFolder_IPersistPropertyBag_Load,
1660     UnixFolder_IPersistPropertyBag_Save
1661 };
1662
1663 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid, 
1664     void** ppvObject)
1665 {
1666     return UnixFolder_IShellFolder2_QueryInterface(
1667         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1668 }
1669
1670 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1671 {
1672     return UnixFolder_IShellFolder2_AddRef(
1673         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1674 }
1675
1676 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1677 {
1678     return UnixFolder_IShellFolder2_Release(
1679         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1680 }
1681
1682 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1683 {
1684     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1685     IEnumIDList *pEnum;
1686     HRESULT hr;
1687     LPITEMIDLIST pidlElem;
1688     DWORD dwFetched;
1689     int i;
1690     static const char szNewFolder[] = "New Folder";
1691
1692     TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1693     
1694     if (uLen < sizeof(szNewFolder)+3)
1695         return E_INVALIDARG;
1696
1697     hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1698                                    SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1699     if (SUCCEEDED(hr)) {
1700         lstrcpyA(lpName, szNewFolder);
1701         IEnumIDList_Reset(pEnum);
1702         i = 2;
1703         while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1704             if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1705                 IEnumIDList_Reset(pEnum);
1706                 sprintf(lpName, "%s %d", szNewFolder, i++);
1707                 if (i > 99) {
1708                     hr = E_FAIL;
1709                     break;
1710                 }
1711             }
1712         }
1713         IEnumIDList_Release(pEnum);
1714     }
1715     return hr;
1716 }
1717
1718 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName, 
1719     LPITEMIDLIST* ppidlOut)
1720 {
1721     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1722     char szNewDir[FILENAME_MAX];
1723
1724     TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1725
1726     if (ppidlOut)
1727         *ppidlOut = NULL;
1728     
1729     lstrcpyA(szNewDir, This->m_pszPath);
1730     lstrcatA(szNewDir, pszName);
1731
1732     if (mkdir(szNewDir, 0755)) {
1733         char szMessage[256 + FILENAME_MAX];
1734         char szCaption[256];
1735
1736         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1737         sprintf(szMessage, szCaption, szNewDir);
1738         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1739         MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1740
1741         return E_FAIL;
1742     } else {
1743         LPITEMIDLIST pidlRelative;
1744         WCHAR wszName[MAX_PATH];
1745
1746         /* Inform the shell */
1747         MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1748         if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1749             LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1750             if (ppidlOut)
1751                 *ppidlOut = pidlRelative;
1752             else
1753                 ILFree(pidlRelative);
1754             SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1755             ILFree(pidlAbsolute);
1756         }
1757         return S_OK;
1758     }
1759 }
1760
1761 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl, 
1762     LPCITEMIDLIST* apidl)
1763 {
1764     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1765     char szAbsolute[FILENAME_MAX], *pszRelative;
1766     LPITEMIDLIST pidlAbsolute;
1767     HRESULT hr = S_OK;
1768     UINT i;
1769     
1770     TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1771
1772     lstrcpyA(szAbsolute, This->m_pszPath);
1773     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1774     
1775     for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1776         if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1777             return E_INVALIDARG;
1778         pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1779         if (_ILIsFolder(apidl[i])) {
1780             if (rmdir(szAbsolute)) {
1781                 hr = E_FAIL;
1782             } else {
1783                 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1784             }
1785         } else if (_ILIsValue(apidl[i])) {
1786             if (unlink(szAbsolute)) {
1787                 hr = E_FAIL;
1788             } else {
1789                 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1790             }
1791         }
1792         ILFree(pidlAbsolute);
1793     }
1794         
1795     return hr;
1796 }
1797
1798 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom, 
1799     UINT cidl, LPCITEMIDLIST *apidl)
1800 {
1801     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1802     DWORD dwAttributes;
1803     UINT i;
1804     HRESULT hr;
1805     char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1806     
1807     TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1808
1809     if (!psfFrom || !cidl || !apidl)
1810         return E_INVALIDARG;
1811
1812     /* All source items have to be filesystem items. */
1813     dwAttributes = SFGAO_FILESYSTEM;
1814     hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1815     if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) 
1816         return E_INVALIDARG;
1817
1818     lstrcpyA(szAbsoluteDst, This->m_pszPath);
1819     pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1820     
1821     for (i=0; i<cidl; i++) {
1822         WCHAR wszSrc[MAX_PATH];
1823         char szSrc[FILENAME_MAX];
1824         STRRET strret;
1825
1826         /* Build the unix path of the current source item. */
1827         if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1828             return E_FAIL;
1829         if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1830             return E_FAIL;
1831         if (!UNIXFS_get_unix_path(wszSrc, szSrc)) 
1832             return E_FAIL;
1833
1834         /* Build the unix path of the current destination item */
1835         UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
1836
1837         FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1838     }
1839     return S_OK;
1840 }
1841
1842 /* VTable for UnixFolder's ISFHelper interface
1843  */
1844 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1845     UnixFolder_ISFHelper_QueryInterface,
1846     UnixFolder_ISFHelper_AddRef,
1847     UnixFolder_ISFHelper_Release,
1848     UnixFolder_ISFHelper_GetUniqueName,
1849     UnixFolder_ISFHelper_AddFolder,
1850     UnixFolder_ISFHelper_DeleteItems,
1851     UnixFolder_ISFHelper_CopyItems
1852 };
1853
1854 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid, 
1855     void** ppvObject)
1856 {
1857     return UnixFolder_IShellFolder2_QueryInterface(
1858         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
1859 }
1860
1861 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
1862 {
1863     return UnixFolder_IShellFolder2_AddRef(
1864         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1865 }
1866
1867 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
1868 {
1869     return UnixFolder_IShellFolder2_Release(
1870         STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
1871 }
1872
1873 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
1874 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
1875
1876 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
1877     DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1878 {
1879     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1880     FORMATETC format;
1881     STGMEDIUM medium; 
1882         
1883     TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08lx, pt={.x=%ld, .y=%ld}, pdwEffect=%p)\n",
1884         iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
1885
1886     if (!pdwEffect || !pDataObject)
1887         return E_INVALIDARG;
1888   
1889     /* Compute a mask of supported drop-effects for this shellfolder object and the given data 
1890      * object. Dropping is only supported on folders, which represent filesystem locations. One
1891      * can't drop on file objects. And the 'move' drop effect is only supported, if the source
1892      * folder is not identical to the target folder. */
1893     This->m_dwDropEffectsMask = DROPEFFECT_NONE;
1894     InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
1895     if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
1896         _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
1897         SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
1898     {
1899         LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
1900         This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
1901
1902         if (pidaShellIDList) { /* Files can only be moved between two different folders */
1903             if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
1904                 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
1905             GlobalUnlock(medium.u.hGlobal);
1906         }
1907     }
1908
1909     *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
1910     
1911     return S_OK;
1912 }
1913
1914 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState, 
1915     POINTL pt, DWORD *pdwEffect)
1916 {
1917     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1918     
1919     TRACE("(iface=%p, dwKeyState=%08lx, pt={.x=%ld, .y=%ld}, pdwEffect=%p)\n", iface, dwKeyState, 
1920         pt.x, pt.y, pdwEffect);
1921
1922     if (!pdwEffect)
1923         return E_INVALIDARG;
1924
1925     *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
1926     
1927     return S_OK;
1928 }
1929
1930 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
1931     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1932     
1933     TRACE("(iface=%p)\n", iface);
1934  
1935     This->m_dwDropEffectsMask = DROPEFFECT_NONE;
1936     
1937     return S_OK;
1938 }
1939
1940 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
1941     DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
1942 {
1943     UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
1944     FORMATETC format;
1945     STGMEDIUM medium; 
1946     HRESULT hr;
1947
1948     TRACE("(iface=%p, pDataObject=%p, dwKeyState=%ld, pt={.x=%ld, .y=%ld}, pdwEffect=%p) semi-stub\n",
1949         iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
1950
1951     InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
1952     hr = IDataObject_GetData(pDataObject, &format, &medium);
1953     if (!SUCCEEDED(hr)) 
1954         return hr;
1955
1956     if (medium.tymed == TYMED_HGLOBAL) {
1957         IShellFolder *psfSourceFolder, *psfDesktopFolder;
1958         LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
1959         STRRET strret;
1960         UINT i;
1961     
1962         if (!pidaShellIDList) 
1963             return HRESULT_FROM_WIN32(GetLastError());
1964         
1965         hr = SHGetDesktopFolder(&psfDesktopFolder);
1966         if (FAILED(hr)) {
1967             GlobalUnlock(medium.u.hGlobal);
1968             return hr;
1969         }
1970
1971         hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL, 
1972                                        &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
1973         IShellFolder_Release(psfDesktopFolder);
1974         if (FAILED(hr)) {
1975             GlobalUnlock(medium.u.hGlobal);
1976             return hr;
1977         }
1978
1979         for (i = 0; i < pidaShellIDList->cidl; i++) {
1980             WCHAR wszSourcePath[MAX_PATH];
1981
1982             hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
1983                                                SHGDN_FORPARSING, &strret);
1984             if (FAILED(hr)) 
1985                 break;
1986
1987             hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
1988             if (FAILED(hr)) 
1989                 break;
1990
1991             switch (*pdwEffect) {
1992                 case DROPEFFECT_MOVE:
1993                     FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
1994                     break;
1995                 case DROPEFFECT_COPY:
1996                     FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
1997                     break;
1998                 case DROPEFFECT_LINK:
1999                     FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2000                     break;
2001             }
2002         }
2003     
2004         IShellFolder_Release(psfSourceFolder);
2005         GlobalUnlock(medium.u.hGlobal);
2006         return hr;
2007     }
2008  
2009     return E_NOTIMPL;
2010 }
2011
2012 /* VTable for UnixFolder's IDropTarget interface
2013  */
2014 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2015     UnixFolder_IDropTarget_QueryInterface,
2016     UnixFolder_IDropTarget_AddRef,
2017     UnixFolder_IDropTarget_Release,
2018     UnixFolder_IDropTarget_DragEnter,
2019     UnixFolder_IDropTarget_DragOver,
2020     UnixFolder_IDropTarget_DragLeave,
2021     UnixFolder_IDropTarget_Drop
2022 };
2023
2024 /******************************************************************************
2025  * Unix[Dos]Folder_Constructor [Internal]
2026  *
2027  * PARAMS
2028  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
2029  *  riid      [I] Interface asked for by the client.
2030  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
2031  *
2032  * NOTES
2033  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
2034  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2035  *  compatible signature.
2036  *
2037  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2038  *  means that paths are converted from dos to unix and back at the interfaces.
2039  */
2040 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID) 
2041 {
2042     HRESULT hr = E_FAIL;
2043     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2044    
2045     if (pUnkOuter) {
2046         FIXME("Aggregation not yet implemented!\n");
2047         return CLASS_E_NOAGGREGATION;
2048     }
2049     
2050     if(pUnixFolder) {
2051         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2052         pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2053         pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2054         pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2055         pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2056         pUnixFolder->m_cRef = 0;
2057         pUnixFolder->m_pszPath = NULL;
2058         pUnixFolder->m_pidlLocation = NULL;
2059         pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2060         pUnixFolder->m_dwAttributes = 0;
2061         pUnixFolder->m_pCLSID = pCLSID;
2062         pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2063
2064         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2065         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2066         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2067     }
2068     return hr;
2069 }
2070
2071 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2072     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2073     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2074 }
2075
2076 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2077     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2078     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2079 }
2080
2081 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2082     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2083     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2084 }
2085
2086 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2087     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2088     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2089 }
2090
2091 HRESULT WINAPI ShellFSFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2092     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2093     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_ShellFSFolder);
2094 }
2095
2096 /******************************************************************************
2097  * UnixSubFolderIterator
2098  *
2099  * Class whose heap based objects represent iterators over the sub-directories
2100  * of a given UnixFolder object. 
2101  */
2102
2103 /* UnixSubFolderIterator object layout and typedef.
2104  */
2105 typedef struct _UnixSubFolderIterator {
2106     const IEnumIDListVtbl *lpIEnumIDListVtbl;
2107     LONG m_cRef;
2108     SHCONTF m_fFilter;
2109     DIR *m_dirFolder;
2110     char m_szFolder[FILENAME_MAX];
2111 } UnixSubFolderIterator;
2112
2113 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2114     TRACE("(iterator=%p)\n", iterator);
2115
2116     if (iterator->m_dirFolder)
2117         closedir(iterator->m_dirFolder);
2118     SHFree(iterator);
2119 }
2120
2121 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
2122     REFIID riid, void** ppv)
2123 {
2124     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2125     
2126     if (!ppv) return E_INVALIDARG;
2127     
2128     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2129         *ppv = iface;
2130     } else {
2131         *ppv = NULL;
2132         return E_NOINTERFACE;
2133     }
2134
2135     IEnumIDList_AddRef(iface);
2136     return S_OK;
2137 }
2138                             
2139 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2140 {
2141     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2142
2143     TRACE("(iface=%p)\n", iface);
2144    
2145     return InterlockedIncrement(&This->m_cRef);
2146 }
2147
2148 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2149 {
2150     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2151     ULONG cRef;
2152     
2153     TRACE("(iface=%p)\n", iface);
2154
2155     cRef = InterlockedDecrement(&This->m_cRef);
2156     
2157     if (!cRef) 
2158         UnixSubFolderIterator_Destroy(This);
2159
2160     return cRef;
2161 }
2162
2163 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
2164     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2165 {
2166     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2167     ULONG i = 0;
2168
2169     /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2170     if (This->m_dirFolder) {
2171         char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2172         struct dirent *pDirEntry;
2173
2174         while (i < celt) {
2175             pDirEntry = readdir(This->m_dirFolder);
2176             if (!pDirEntry) break; /* No more entries */
2177             if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2178
2179             /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2180              * and see if it passes the filter. 
2181              */
2182             lstrcpyA(pszRelativePath, pDirEntry->d_name);
2183             rgelt[i] = (LPITEMIDLIST)SHAlloc(
2184                 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2185             if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
2186                 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter)) 
2187             {
2188                 SHFree(rgelt[i]);
2189                 continue;
2190             }
2191             memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2192             i++;
2193         }
2194         *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2195     }
2196     
2197     if (pceltFetched)
2198         *pceltFetched = i;
2199
2200     return (i == 0) ? S_FALSE : S_OK;
2201 }
2202     
2203 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2204 {
2205     LPITEMIDLIST *apidl;
2206     ULONG cFetched;
2207     HRESULT hr;
2208     
2209     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
2210
2211     /* Call IEnumIDList::Next and delete the resulting pidls. */
2212     apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
2213     hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2214     if (SUCCEEDED(hr))
2215         while (cFetched--) 
2216             SHFree(apidl[cFetched]);
2217     SHFree(apidl);
2218
2219     return hr;
2220 }
2221
2222 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2223 {
2224     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2225         
2226     TRACE("(iface=%p)\n", iface);
2227
2228     if (This->m_dirFolder)
2229         rewinddir(This->m_dirFolder);
2230     
2231     return S_OK;
2232 }
2233
2234 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
2235     IEnumIDList** ppenum)
2236 {
2237     FIXME("stub\n");
2238     return E_NOTIMPL;
2239 }
2240
2241 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2242  */
2243 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2244     UnixSubFolderIterator_IEnumIDList_QueryInterface,
2245     UnixSubFolderIterator_IEnumIDList_AddRef,
2246     UnixSubFolderIterator_IEnumIDList_Release,
2247     UnixSubFolderIterator_IEnumIDList_Next,
2248     UnixSubFolderIterator_IEnumIDList_Skip,
2249     UnixSubFolderIterator_IEnumIDList_Reset,
2250     UnixSubFolderIterator_IEnumIDList_Clone
2251 };
2252
2253 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2254     UnixSubFolderIterator *iterator;
2255
2256     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2257     
2258     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2259     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2260     iterator->m_cRef = 0;
2261     iterator->m_fFilter = fFilter;
2262     iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2263     lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2264
2265     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2266     
2267     return (IUnknown*)iterator;
2268 }