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