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