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