Also scan HKEY_CURRENT_USER for shell namespace extensions in
[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 ISFHelperVtbl *lpISFHelperVtbl;
192     LONG m_cRef;
193     CHAR *m_pszPath;             /* Target path of the shell folder */
194     LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
195     DWORD m_dwPathMode;
196     DWORD m_dwAttributes;
197     const CLSID *m_pCLSID;
198 } UnixFolder;
199
200 /******************************************************************************
201  * UNIXFS_is_rooted_at_desktop [Internal]
202  *
203  * Checks if the unixfs namespace extension is rooted at desktop level.
204  *
205  * RETURNS
206  *  TRUE, if unixfs is rooted at desktop level
207  *  FALSE, if not.
208  */
209 BOOL UNIXFS_is_rooted_at_desktop(void) {
210     HKEY hKey;
211     WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = { 
212         'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
213         'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
214         'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
215         'N','a','m','e','S','p','a','c','e','\\',0 }; 
216
217     if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
218         RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS) 
219     {
220         RegCloseKey(hKey);
221         return TRUE;
222     }
223     return FALSE;
224 }
225
226 /******************************************************************************
227  * UNIXFS_is_pidl_of_type [INTERNAL]
228  *
229  * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
230  *
231  * PARAMS
232  *  pIDL    [I] The ITEMIDLIST to be checked.
233  *  fFilter [I] Shell condition flags, which specify the filter.
234  *
235  * RETURNS
236  *  TRUE, if pIDL is accepted by fFilter
237  *  FALSE, otherwise
238  */
239 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
240     LPPIDLDATA pIDLData = _ILGetDataPointer(pIDL);
241     if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData && 
242         (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN)) 
243     {
244         return FALSE;
245     }
246     if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
247     if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
248     return FALSE;
249 }
250
251 /******************************************************************************
252  * UNIXFS_is_dos_device [Internal]
253  *
254  * Determines if a unix directory corresponds to any dos device.
255  *
256  * PARAMS
257  *  statPath [I] The stat struct of the directory, as returned by stat(2).
258  *
259  * RETURNS
260  *  TRUE, if statPath corresponds to any dos drive letter
261  *  FALSE, otherwise
262  */
263 static BOOL UNIXFS_is_dos_device(const struct stat *statPath) {
264     struct stat statDrive;
265     char *pszDrivePath;
266     DWORD dwDriveMap;
267     WCHAR wszDosDevice[4] = { 'A', ':', '\\', 0 };
268
269     for (dwDriveMap = GetLogicalDrives(); dwDriveMap; dwDriveMap >>= 1, wszDosDevice[0]++) {
270         if (!(dwDriveMap & 0x1)) continue;
271         pszDrivePath = wine_get_unix_file_name(wszDosDevice);
272         if (pszDrivePath && !stat(pszDrivePath, &statDrive)) {
273             HeapFree(GetProcessHeap(), 0, pszDrivePath);
274             if ((statPath->st_dev == statDrive.st_dev) && (statPath->st_ino == statDrive.st_ino))
275                 return TRUE;
276         }
277     }
278     return FALSE;
279 }
280
281 /******************************************************************************
282  * UNIXFS_get_unix_path [Internal]
283  *
284  * Convert an absolute dos path to an absolute canonicalized unix path.
285  * Evaluate "/.", "/.." and symbolic links.
286  *
287  * PARAMS
288  *  pszDosPath       [I] An absolute dos path
289  *  pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
290  *
291  * RETURNS
292  *  Success, TRUE
293  *  Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
294  */
295 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
296 {
297     char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
298     struct stat fileStat;
299     
300     TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
301     
302     if (!pszDosPath || pszDosPath[1] != ':')
303         return FALSE;
304
305     pszUnixPath = wine_get_unix_file_name(pszDosPath);
306     if (!pszUnixPath) return FALSE;   
307     strcpy(szPath, pszUnixPath);
308     HeapFree(GetProcessHeap(), 0, pszUnixPath);
309    
310     /* pCanonicalTail always points to the end of the canonical path constructed
311      * thus far. pPathTail points to the still to be processed part of the input
312      * path. pElement points to the path element currently investigated.
313      */
314     *pszCanonicalPath = '\0';
315     pCanonicalTail = pszCanonicalPath;
316     pPathTail = szPath;
317
318     do {
319         char cTemp;
320         int cLinks = 0;
321             
322         pElement = pPathTail;
323         pPathTail = strchr(pPathTail+1, '/');
324         if (!pPathTail) /* Last path element may not be terminated by '/'. */ 
325             pPathTail = pElement + strlen(pElement);
326         /* Temporarily terminate the current path element. Will be restored later. */
327         cTemp = *pPathTail;
328         *pPathTail = '\0';
329
330         /* Skip "/." path elements */
331         if (!strcmp("/.", pElement)) {
332             *pPathTail = cTemp;
333             continue;
334         }
335
336         /* Remove last element in canonical path for "/.." elements, then skip. */
337         if (!strcmp("/..", pElement)) {
338             char *pTemp = strrchr(pszCanonicalPath, '/');
339             if (pTemp)
340                 pCanonicalTail = pTemp;
341             *pCanonicalTail = '\0';
342             *pPathTail = cTemp;
343             continue;
344         }
345        
346         /* lstat returns zero on success. */
347         if (lstat(szPath, &fileStat)) 
348             return FALSE;
349
350         if (S_ISLNK(fileStat.st_mode)) {
351             char szSymlink[FILENAME_MAX];
352             int cLinkLen, cTailLen;
353           
354             /* Avoid infinite loop for recursive links. */
355             if (++cLinks > 64) 
356                 return FALSE;
357             
358             cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
359             if (cLinkLen < 0) 
360                 return FALSE;
361
362             *pPathTail = cTemp;
363             cTailLen = strlen(pPathTail);
364            
365             if (szSymlink[0] == '/') {
366                 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
367                 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
368                     return FALSE;
369                     
370                 /* Avoid double slashes. */
371                 if (szSymlink[cLinkLen-1] == '/' && pPathTail[0] == '/') {
372                     szSymlink[cLinkLen-1] = '\0';
373                     cLinkLen--;
374                 }
375                 
376                 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
377                 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
378                 *pszCanonicalPath = '\0';
379                 pCanonicalTail = pszCanonicalPath;
380                 pPathTail = szPath;
381             } else {
382                 /* Relative link. Expand into szPath and continue. */
383                 char szTemp[FILENAME_MAX];
384                 int cTailLen = strlen(pPathTail);
385
386                 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
387                     return FALSE;
388
389                 memcpy(szTemp, pPathTail, cTailLen + 1);
390                 memcpy(pElement + 1, szSymlink, cLinkLen);
391                 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
392                 pPathTail = pElement;
393             }
394         } else {
395             /* Regular directory or file. Copy to canonical path */
396             if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
397                 return FALSE;
398                 
399             memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
400             pCanonicalTail += pPathTail - pElement;
401             *pPathTail = cTemp;
402         }
403     } while (pPathTail[0] == '/');
404    
405     TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
406     
407     return TRUE;
408 }
409
410 /******************************************************************************
411  * UNIXFS_build_shitemid [Internal]
412  *
413  * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into 
414  * buffer 'pIDL'.
415  *
416  * PARAMS
417  *  pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
418  *  pIDL        [O] SHITEMID will be constructed here.
419  *
420  * RETURNS
421  *  Success: A pointer to the terminating '\0' character of path.
422  *  Failure: NULL
423  *
424  * NOTES
425  *  Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
426  *  If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
427  *  a 0 USHORT value.
428  */
429 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
430     LARGE_INTEGER time;
431     FILETIME fileTime;
432     LPPIDLDATA pIDLData;
433     struct stat fileStat;
434     char *pszComponent;
435     int cComponentLen;
436
437     TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
438
439     /* Compute the SHITEMID's length and wipe it. */
440     pszComponent = strrchr(pszUnixPath, '/') + 1;
441     cComponentLen = strlen(pszComponent);
442     memset(pIDL, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen));
443     ((LPSHITEMID)pIDL)->cb = SHITEMID_LEN_FROM_NAME_LEN(cComponentLen) ;
444    
445     /* We are only interested in regular files and directories. */
446     if (stat(pszUnixPath, &fileStat)) return NULL;
447     if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
448     
449     /* Set shell32's standard SHITEMID data fields. */
450     pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
451     pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
452     pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
453     RtlSecondsSince1970ToTime( fileStat.st_mtime, &time );
454     fileTime.dwLowDateTime = time.u.LowPart;
455     fileTime.dwHighDateTime = time.u.HighPart;
456     FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
457     pIDLData->u.file.uFileAttribs = 0;
458     if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
459     if (pszComponent[0] == '.') pIDLData->u.file.uFileAttribs |=  FILE_ATTRIBUTE_HIDDEN;
460     memcpy(pIDLData->u.file.szNames, pszComponent, cComponentLen);
461
462     return pszComponent + cComponentLen;
463 }
464
465 /******************************************************************************
466  * UNIXFS_path_to_pidl [Internal]
467  *
468  * PARAMS
469  *  pUnixFolder [I] If path is relative, pUnixFolder represents the base path
470  *  path        [I] An absolute unix or dos path or a path relativ to pUnixFolder
471  *  ppidl       [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
472  *  
473  * RETURNS
474  *  Success: TRUE
475  *  Failure: FALSE, invalid params or out of memory
476  *
477  * NOTES
478  *  pUnixFolder also carries the information if the path is expected to be unix or dos.
479  */
480 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
481     LPITEMIDLIST pidl;
482     int cSubDirs, cPidlLen, cPathLen;
483     char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
484
485     TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
486    
487     if (!ppidl || !path)
488         return FALSE;
489
490     /* Build an absolute path and let pNextPathElement point to the interesting 
491      * relative sub-path. We need the absolute path to call 'stat', but the pidl
492      * will only contain the relative part.
493      */
494     if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':')) 
495     {
496         /* Absolute dos path. Convert to unix */
497         if (!UNIXFS_get_unix_path(path, szCompletePath))
498             return FALSE;
499         pNextPathElement = szCompletePath;
500     } 
501     else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/')) 
502     {
503         /* Absolute unix path. Just convert to ANSI. */
504         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL); 
505         pNextPathElement = szCompletePath;
506     } 
507     else 
508     {
509         /* Relative dos or unix path. Concat with this folder's path */
510         int cBasePathLen = strlen(pUnixFolder->m_pszPath);
511         memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
512         WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen, 
513                             FILENAME_MAX - cBasePathLen, NULL, NULL);
514         pNextPathElement = szCompletePath + cBasePathLen - 1;
515         
516         /* If in dos mode, replace '\' with '/' */
517         if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
518             char *pBackslash = strchr(pNextPathElement, '\\');
519             while (pBackslash) {
520                 *pBackslash = '/';
521                 pBackslash = strchr(pBackslash, '\\');
522             }
523         }
524     }
525
526     /* Special case for the root folder. */
527     if (!strcmp(szCompletePath, "/")) {
528         *ppidl = pidl = (LPITEMIDLIST)SHAlloc(sizeof(USHORT));
529         if (!pidl) return FALSE;
530         pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
531         return TRUE;
532     }
533     
534     /* Remove trailing slash, if present */
535     cPathLen = strlen(szCompletePath);
536     if (szCompletePath[cPathLen-1] == '/') 
537         szCompletePath[cPathLen-1] = '\0';
538
539     if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
540         ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
541         return FALSE;
542     }
543     
544     /* At this point, we have an absolute unix path in szCompletePath 
545      * and the relative portion of it in pNextPathElement. Both starting with '/'
546      * and _not_ terminated by a '/'. */
547     TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
548     
549     /* Count the number of sub-directories in the path */
550     cSubDirs = 0;
551     pSlash = pNextPathElement;
552     while (pSlash) {
553         cSubDirs++;
554         pSlash = strchr(pSlash+1, '/');
555     }
556   
557     /* Allocate enough memory to hold the path. The -cSubDirs is for the '/' 
558      * characters, which are not stored in the ITEMIDLIST. */
559     cPidlLen = strlen(pNextPathElement) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
560     *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
561     if (!pidl) return FALSE;
562
563     /* Concatenate the SHITEMIDs of the sub-directories. */
564     while (*pNextPathElement) {
565         pSlash = strchr(pNextPathElement+1, '/');
566         if (pSlash) *pSlash = '\0';
567         pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
568         if (pSlash) *pSlash = '/';
569             
570         if (!pNextPathElement) {
571             SHFree(*ppidl);
572             return FALSE;
573         }
574         pidl = ILGetNext(pidl);
575     }
576     pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
577
578     if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */ 
579         ERR("Computed length of pidl incorrect. Please report.\n");
580     
581     return TRUE;
582 }
583
584 /******************************************************************************
585  * UNIXFS_initialize_target_folder [Internal]
586  *
587  *  Initialize the m_pszPath member of an UnixFolder, given an absolute unix
588  *  base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
589  *  specifies the location in the shell namespace alone. 
590  * 
591  * PARAMS
592  *  This          [IO] The UnixFolder, whose target path is to be initialized
593  *  szBasePath    [I]  The absolute base path
594  *  pidlSubFolder [I]  Relative part of the path, given as an ITEMIDLIST
595  *  dwAttributes  [I]  Attributes to add to the Folders m_dwAttributes member 
596  *                     (Used to pass the SFGAO_FILESYSTEM flag down the path)
597  * RETURNS
598  *  Success: S_OK,
599  *  Failure: E_FAIL
600  */
601 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
602     LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
603 {
604     LPCITEMIDLIST current = pidlSubFolder;
605     DWORD dwPathLen = strlen(szBasePath)+1;
606     struct stat statPrefix;
607     char *pNextDir;
608         
609     /* Determine the path's length bytes */
610     while (current && current->mkid.cb) {
611         dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
612         current = ILGetNext(current);
613     };
614
615     /* Build the path and compute the attributes*/
616     This->m_dwAttributes = 
617             dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
618     This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
619     if (!This->m_pszPath) {
620         WARN("SHAlloc failed!\n");
621         return E_FAIL;
622     }
623     current = pidlSubFolder;
624     strcpy(pNextDir, szBasePath);
625     pNextDir += strlen(szBasePath);
626     if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
627         This->m_dwAttributes |= SFGAO_FILESYSTEM;
628     if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
629         *pNextDir = '\0';
630         if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
631             This->m_dwAttributes |= SFGAO_FILESYSTEM;
632     }
633     while (current && current->mkid.cb) {
634         memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
635         pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
636         if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
637             *pNextDir = '\0';
638             if (!stat(This->m_pszPath, &statPrefix) && UNIXFS_is_dos_device(&statPrefix))
639                 This->m_dwAttributes |= SFGAO_FILESYSTEM;
640         }
641         *pNextDir++ = '/';
642         current = ILGetNext(current);
643     }
644     *pNextDir='\0';
645  
646     return S_OK;
647 }
648
649 /******************************************************************************
650  * UnixFolder
651  *
652  * Class whose heap based instances represent unix filesystem directories.
653  */
654
655 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
656     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
657     
658     SHFree(pUnixFolder->m_pszPath);
659     ILFree(pUnixFolder->m_pidlLocation);
660     SHFree(pUnixFolder);
661 }
662
663 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid, 
664     void **ppv) 
665 {
666     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
667         
668     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
669     
670     if (!ppv) return E_INVALIDARG;
671     
672     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) || 
673         IsEqualIID(&IID_IShellFolder2, riid)) 
674     {
675         *ppv = &This->lpIShellFolder2Vtbl;
676     } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) || 
677                IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid)) 
678     {
679         *ppv = &This->lpIPersistFolder3Vtbl;
680     } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
681         *ppv = &This->lpIPersistPropertyBagVtbl;
682     } else if (IsEqualIID(&IID_ISFHelper, riid)) {
683         *ppv = &This->lpISFHelperVtbl;
684     } else {
685         *ppv = NULL;
686         return E_NOINTERFACE;
687     }
688
689     IUnknown_AddRef((IUnknown*)*ppv);
690     return S_OK;
691 }
692
693 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
694     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
695
696     TRACE("(iface=%p)\n", iface);
697
698     return InterlockedIncrement(&This->m_cRef);
699 }
700
701 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
702     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
703     ULONG cRef;
704     
705     TRACE("(iface=%p)\n", iface);
706
707     cRef = InterlockedDecrement(&This->m_cRef);
708     
709     if (!cRef) 
710         UnixFolder_Destroy(This);
711
712     return cRef;
713 }
714
715 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner, 
716     LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl, 
717     ULONG* pdwAttributes)
718 {
719     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
720     BOOL result;
721
722     TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
723           "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName), 
724           pchEaten, ppidl, pdwAttributes);
725
726     result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
727     if (result && pdwAttributes && *pdwAttributes)
728     {
729         IShellFolder *pParentSF;
730         LPCITEMIDLIST pidlLast;
731         HRESULT hr;
732         
733         hr = SHBindToParent(*ppidl, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
734         if (FAILED(hr)) return E_FAIL;
735         IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
736         IShellFolder_Release(pParentSF);
737     }
738
739     if (!result) TRACE("FAILED!\n");
740     return result ? S_OK : E_FAIL;
741 }
742
743 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
744
745 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner, 
746     SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
747 {
748     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
749     IUnknown *newIterator;
750     HRESULT hr;
751     
752     TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n", 
753             iface, hwndOwner, grfFlags, ppEnumIDList);
754
755     if (!This->m_pszPath) {
756         WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
757         return E_UNEXPECTED;
758     }
759
760     newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
761     hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
762     IUnknown_Release(newIterator);
763     
764     return hr;
765 }
766
767 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
768
769 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
770     LPBC pbcReserved, REFIID riid, void** ppvOut)
771 {
772     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
773     IPersistFolder3 *persistFolder;
774     HRESULT hr;
775     const CLSID *clsidChild;
776         
777     TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n", 
778             iface, pidl, pbcReserved, riid, ppvOut);
779
780     if (!pidl || !pidl->mkid.cb)
781         return E_INVALIDARG;
782    
783     if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
784         /* Children of FolderShortcuts are ShellFSFolders on Windows. 
785          * Unixfs' counterpart is UnixDosFolder. */
786         clsidChild = &CLSID_UnixDosFolder;    
787     } else {
788         clsidChild = This->m_pCLSID;
789     }
790     
791     hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
792     if (!SUCCEEDED(hr)) return hr;
793     hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
794    
795     if (SUCCEEDED(hr)) {
796         UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
797         subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
798         hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
799                                              This->m_dwAttributes & SFGAO_FILESYSTEM);
800     } 
801
802     IPersistFolder3_Release(persistFolder);
803     
804     return hr;
805 }
806
807 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl, 
808     LPBC pbcReserved, REFIID riid, void** ppvObj)
809 {
810     FIXME("stub\n");
811     return E_NOTIMPL;
812 }
813
814 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam, 
815     LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
816 {
817     BOOL isEmpty1, isEmpty2;
818     HRESULT hr = E_FAIL;
819     LPITEMIDLIST firstpidl;
820     IShellFolder2 *psf;
821     int compare;
822
823     TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
824     
825     isEmpty1 = !pidl1 || !pidl1->mkid.cb;
826     isEmpty2 = !pidl2 || !pidl2->mkid.cb;
827
828     if (isEmpty1 && isEmpty2) 
829         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
830     else if (isEmpty1)
831         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
832     else if (isEmpty2)
833         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
834
835     if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2)) 
836         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
837     if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
838         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
839
840     compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE, 
841                              _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
842                              _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
843     
844     if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN)) 
845         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
846
847     if (pidl1->mkid.cb < pidl2->mkid.cb)
848         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
849     else if (pidl1->mkid.cb > pidl2->mkid.cb)
850         return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
851
852     firstpidl = ILCloneFirst(pidl1);
853     pidl1 = ILGetNext(pidl1);
854     pidl2 = ILGetNext(pidl2);
855     
856     hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
857     if (SUCCEEDED(hr)) {
858         hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
859         IShellFolder2_Release(psf);
860     }
861
862     ILFree(firstpidl);
863     return hr;
864 }
865
866 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
867     REFIID riid, void** ppv)
868 {
869     HRESULT hr = E_INVALIDARG;
870         
871     TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
872     
873     if (!ppv) return E_INVALIDARG;
874     *ppv = NULL;
875     
876     if (IsEqualIID(&IID_IShellView, riid)) {
877         LPSHELLVIEW pShellView;
878         
879         pShellView = IShellView_Constructor((IShellFolder*)iface);
880         if (pShellView) {
881             hr = IShellView_QueryInterface(pShellView, riid, ppv);
882             IShellView_Release(pShellView);
883         }
884     } 
885     
886     return hr;
887 }
888
889 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl, 
890     LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
891 {
892     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
893     HRESULT hr = S_OK;
894         
895     TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
896  
897     if (!rgfInOut || (cidl && !apidl)) 
898         return E_INVALIDARG;
899     
900     if (cidl == 0) {
901         *rgfInOut &= This->m_dwAttributes;
902     } else {
903         char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
904         UINT i;
905
906         *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
907                     SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
908         lstrcpyA(szAbsolutePath, This->m_pszPath);
909         pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
910         for (i=0; i<cidl; i++) {
911             if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
912                 struct stat fileStat;
913                 char *pszName = _ILGetTextPointer(apidl[i]);
914                 if (!pszName) return E_INVALIDARG;
915                 lstrcpyA(pszRelativePath, pszName);
916                 if (stat(szAbsolutePath, &fileStat) || !UNIXFS_is_dos_device(&fileStat))
917                     *rgfInOut &= ~SFGAO_FILESYSTEM;
918             }
919             if (_ILIsFolder(apidl[i])) 
920                 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
921         }
922     }
923     
924     return hr;
925 }
926
927 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner, 
928     UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
929 {
930     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
931     
932     TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
933         iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
934
935     if (IsEqualIID(&IID_IContextMenu, riid)) {
936         *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
937         return S_OK;
938     } else if (IsEqualIID(&IID_IDataObject, riid)) {
939         *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
940         return S_OK;
941     } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
942         LPITEMIDLIST pidl;
943         if (cidl != 1) return E_FAIL;
944         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
945         *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
946         SHFree(pidl);
947         return S_OK;
948     } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
949         LPITEMIDLIST pidl;
950         if (cidl != 1) return E_FAIL;
951         pidl = ILCombine(This->m_pidlLocation, apidl[0]);
952         *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
953         SHFree(pidl);
954         return S_OK;
955     } else if (IsEqualIID(&IID_IDropTarget, riid)) {
956         FIXME("IDropTarget\n");
957         return E_FAIL;
958     } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
959         FIXME("IShellLinkW\n");
960         return E_FAIL;
961     } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
962         FIXME("IShellLinkA\n");
963         return E_FAIL;
964     } else {
965         FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
966         return E_NOINTERFACE;
967     }
968 }
969
970 /******************************************************************************
971  * Translate file name from unix to ANSI encoding.
972  */
973 static void strcpyn_U2A(char *win_fn, UINT win_fn_len, const char *unix_fn)
974 {
975     UINT len;
976     WCHAR *unicode_fn;
977
978     len = MultiByteToWideChar(CP_UNIXCP, 0, unix_fn, -1, NULL, 0);
979     unicode_fn = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
980     MultiByteToWideChar(CP_UNIXCP, 0, unix_fn, -1, unicode_fn, len);
981
982     WideCharToMultiByte(CP_ACP, 0, unicode_fn, len, win_fn, win_fn_len, NULL, NULL);
983     HeapFree(GetProcessHeap(), 0, unicode_fn);
984 }
985
986 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface, 
987     LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
988 {
989     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
990     HRESULT hr = S_OK;    
991
992     TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
993     
994     if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
995         (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
996     {
997         if (!pidl || !pidl->mkid.cb) {
998             lpName->uType = STRRET_CSTR;
999             if (This->m_dwPathMode == PATHMODE_UNIX) {
1000                 strcpyn_U2A(lpName->u.cStr, MAX_PATH, This->m_pszPath);
1001             } else {
1002                 WCHAR *pwszDosPath = wine_get_dos_file_name(This->m_pszPath);
1003                 if (!pwszDosPath)
1004                     return HRESULT_FROM_WIN32(GetLastError());
1005                 PathRemoveBackslashW(pwszDosPath);
1006                 WideCharToMultiByte(CP_UNIXCP, 0, pwszDosPath, -1, lpName->u.cStr, MAX_PATH, NULL, NULL);
1007                 HeapFree(GetProcessHeap(), 0, pwszDosPath);        
1008             }
1009         } else {
1010             IShellFolder *pSubFolder;
1011             SHITEMID emptyIDL = { 0, { 0 } };
1012
1013             hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1014             if (!SUCCEEDED(hr)) return hr;
1015        
1016             hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1017             IShellFolder_Release(pSubFolder);
1018         }
1019     } else {
1020         char *pszFileName = _ILGetTextPointer(pidl);
1021         lpName->uType = STRRET_CSTR;
1022         strcpyn_U2A(lpName->u.cStr, MAX_PATH, pszFileName ? pszFileName : "");
1023     }
1024
1025     /* If in dos mode, do some post-processing on the path.
1026      * (e.g. remove filename extension, if uFlags & SHGDN_FOREDITING) 
1027      */
1028     if (SUCCEEDED(hr) && This->m_dwPathMode == PATHMODE_DOS && !_ILIsFolder(pidl))
1029         SHELL_FS_ProcessDisplayFilename(lpName->u.cStr, uFlags);
1030     
1031     TRACE("--> %s\n", lpName->u.cStr);
1032     
1033     return hr;
1034 }
1035
1036 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd, 
1037     LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1038 {
1039     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1040
1041     char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1042     WCHAR *pwszDosDest;
1043     int cBasePathLen = lstrlenA(This->m_pszPath);
1044     struct stat statDest;
1045     LPITEMIDLIST pidlSrc, pidlDest;
1046    
1047     TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
1048           iface, hwnd, pidl, debugstr_w(lpszName), uFlags, ppidlOut); 
1049
1050     /* pidl has to contain a single non-empty SHITEMID */
1051     if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1052         return E_INVALIDARG;
1053    
1054     if (ppidlOut)
1055         *ppidlOut = NULL;
1056     
1057     /* build source path */
1058     memcpy(szSrc, This->m_pszPath, cBasePathLen);
1059     lstrcpyA(szSrc+cBasePathLen, _ILGetTextPointer(pidl));
1060
1061     /* build destination path */
1062     if (uFlags & SHGDN_FORPARSING) { /* absolute path in lpszName */
1063         WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest, FILENAME_MAX, NULL, NULL);
1064     } else {
1065         WCHAR wszSrcRelative[MAX_PATH];
1066         memcpy(szDest, This->m_pszPath, cBasePathLen);
1067         WideCharToMultiByte(CP_UNIXCP, 0, lpszName, -1, szDest+cBasePathLen, 
1068                             FILENAME_MAX-cBasePathLen, NULL, NULL);
1069
1070         /* uFlags is SHGDN_FOREDITING of SHGDN_FORADDRESSBAR. If the filename's 
1071          * extension is hidden to the user, we have to append it. */
1072         if (_ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) && 
1073             SHELL_FS_HideExtension(wszSrcRelative))
1074         {
1075             char *pszExt = PathFindExtensionA(_ILGetTextPointer(pidl));
1076             lstrcatA(szDest, pszExt);
1077         }
1078     } 
1079
1080     TRACE("src=%s dest=%s\n", szSrc, szDest);
1081
1082     /* Fail, if destination does already exist */
1083     if (!stat(szDest, &statDest)) 
1084         return E_FAIL;
1085
1086     /* Rename the file */
1087     if (rename(szSrc, szDest)) 
1088         return E_FAIL;
1089     
1090     /* Build a pidl for the path of the renamed file */
1091     pwszDosDest = wine_get_dos_file_name(szDest);
1092     if (!pwszDosDest || !UNIXFS_path_to_pidl(This, pwszDosDest, &pidlDest)) {
1093         HeapFree(GetProcessHeap(), 0, pwszDosDest);
1094         rename(szDest, szSrc); /* Undo the renaming */
1095         return E_FAIL;
1096     }
1097     
1098     /* Inform the shell */
1099     pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1100     if (_ILIsFolder(ILFindLastID(pidlDest))) 
1101         SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1102     else 
1103         SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1104     ILFree(pidlSrc);
1105     ILFree(pidlDest);
1106     
1107     if (ppidlOut) 
1108         _ILCreateFromPathW(pwszDosDest, ppidlOut);
1109     
1110     HeapFree(GetProcessHeap(), 0, pwszDosDest);
1111     return S_OK;
1112 }
1113
1114 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface, 
1115     IEnumExtraSearch **ppEnum) 
1116 {
1117     FIXME("stub\n");
1118     return E_NOTIMPL;
1119 }
1120
1121 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface, 
1122     DWORD dwReserved, ULONG *pSort, ULONG *pDisplay) 
1123 {
1124     FIXME("stub\n");
1125     return E_NOTIMPL;
1126 }
1127
1128 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface, 
1129     UINT iColumn, SHCOLSTATEF *pcsFlags)
1130 {
1131     FIXME("stub\n");
1132     return E_NOTIMPL;
1133 }
1134
1135 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface, 
1136     GUID *pguid)
1137 {
1138     FIXME("stub\n");
1139     return E_NOTIMPL;
1140 }
1141
1142 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface, 
1143     LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1144 {
1145     FIXME("stub\n");
1146     return E_NOTIMPL;
1147 }
1148
1149 #define SHELLVIEWCOLUMNS 7 
1150
1151 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface, 
1152     LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1153 {
1154     UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1155     HRESULT hr = E_FAIL;
1156     struct passwd *pPasswd;
1157     struct group *pGroup;
1158     static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1159         {IDS_SHV_COLUMN1,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1160         {IDS_SHV_COLUMN2,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1161         {IDS_SHV_COLUMN3,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1162         {IDS_SHV_COLUMN4,  SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1163         {IDS_SHV_COLUMN5,  SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1164         {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1165         {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR  | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1166     };
1167
1168     TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1169     
1170     if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1171         return E_INVALIDARG;
1172
1173     if (!pidl) {
1174         psd->fmt = SFHeader[iColumn].fmt;
1175         psd->cxChar = SFHeader[iColumn].cxChar;
1176         psd->str.uType = STRRET_CSTR;
1177         LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1178         return S_OK;
1179     } else {
1180         struct stat statItem;
1181         if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1182             char szPath[FILENAME_MAX], *pszFile = _ILGetTextPointer(pidl);
1183             if (!pszFile) 
1184                 return E_INVALIDARG;
1185             lstrcpyA(szPath, This->m_pszPath);
1186             lstrcatA(szPath, pszFile);
1187             if (stat(szPath, &statItem)) 
1188                 return E_INVALIDARG;
1189         }
1190         psd->str.u.cStr[0] = '\0';
1191         psd->str.uType = STRRET_CSTR;
1192         switch (iColumn) {
1193             case 0:
1194                 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1195                 break;
1196             case 1:
1197                 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1198                 break;
1199             case 2:
1200                 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1201                 break;
1202             case 3:
1203                 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1204                 break;
1205             case 4:
1206                 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1207                 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1208                 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1209                 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1210                 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1211                 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1212                 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1213                 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1214                 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1215                 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1216                 psd->str.u.cStr[10] = '\0';
1217                 break;
1218             case 5:
1219                 pPasswd = getpwuid(statItem.st_uid);
1220                 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1221                 break;
1222             case 6:
1223                 pGroup = getgrgid(statItem.st_gid);
1224                 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1225                 break;
1226         }
1227     }
1228     
1229     return hr;
1230 }
1231
1232 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1233     SHCOLUMNID *pscid) 
1234 {
1235     FIXME("stub\n");
1236     return E_NOTIMPL;
1237 }
1238
1239 /* VTable for UnixFolder's IShellFolder2 interface.
1240  */
1241 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1242     UnixFolder_IShellFolder2_QueryInterface,
1243     UnixFolder_IShellFolder2_AddRef,
1244     UnixFolder_IShellFolder2_Release,
1245     UnixFolder_IShellFolder2_ParseDisplayName,
1246     UnixFolder_IShellFolder2_EnumObjects,
1247     UnixFolder_IShellFolder2_BindToObject,
1248     UnixFolder_IShellFolder2_BindToStorage,
1249     UnixFolder_IShellFolder2_CompareIDs,
1250     UnixFolder_IShellFolder2_CreateViewObject,
1251     UnixFolder_IShellFolder2_GetAttributesOf,
1252     UnixFolder_IShellFolder2_GetUIObjectOf,
1253     UnixFolder_IShellFolder2_GetDisplayNameOf,
1254     UnixFolder_IShellFolder2_SetNameOf,
1255     UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1256     UnixFolder_IShellFolder2_EnumSearches,
1257     UnixFolder_IShellFolder2_GetDefaultColumn,
1258     UnixFolder_IShellFolder2_GetDefaultColumnState,
1259     UnixFolder_IShellFolder2_GetDetailsEx,
1260     UnixFolder_IShellFolder2_GetDetailsOf,
1261     UnixFolder_IShellFolder2_MapColumnToSCID
1262 };
1263
1264 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* This, REFIID riid, 
1265     void** ppvObject)
1266 {
1267     return UnixFolder_IShellFolder2_QueryInterface(
1268                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This), riid, ppvObject);
1269 }
1270
1271 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* This)
1272 {
1273     return UnixFolder_IShellFolder2_AddRef(
1274                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1275 }
1276
1277 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* This)
1278 {
1279     return UnixFolder_IShellFolder2_Release(
1280                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder3, This));
1281 }
1282
1283 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1284 {    
1285     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1286     
1287     TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1288     
1289     if (!pClassID)
1290         return E_INVALIDARG;
1291
1292     memcpy(pClassID, This->m_pCLSID, sizeof(CLSID));
1293     return S_OK;
1294 }
1295
1296 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1297 {
1298     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1299     LPCITEMIDLIST current = pidl;
1300     char szBasePath[FILENAME_MAX] = "/";
1301     
1302     TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1303
1304     /* Find the UnixFolderClass root */
1305     while (current->mkid.cb) {
1306         if (_ILIsSpecialFolder(current) && IsEqualIID(This->m_pCLSID, _ILGetGUIDPointer(current)))
1307             break;
1308         current = ILGetNext(current);
1309     }
1310
1311     if (current && current->mkid.cb) {
1312         if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1313             WCHAR wszMyDocumentsPath[MAX_PATH];
1314             if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1315                 return E_FAIL;
1316             PathAddBackslashW(wszMyDocumentsPath);
1317             if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1318                 return E_FAIL;
1319         } 
1320         current = ILGetNext(current);
1321     } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1322         /* Path rooted at Desktop */
1323         WCHAR wszDesktopPath[MAX_PATH];
1324         if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE)) 
1325             return E_FAIL;
1326         PathAddBackslashW(wszDesktopPath);
1327         if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1328             return E_FAIL;
1329         current = pidl;
1330     } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1331         /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1332          * specifies the location in the shell namespace, but leaves the
1333          * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1334         This->m_pidlLocation = ILClone(pidl);
1335         return S_OK;
1336     } else {
1337         ERR("Unknown pidl type!\n");
1338         pdump(pidl);
1339         return E_INVALIDARG;
1340     }
1341   
1342     This->m_pidlLocation = ILClone(pidl);
1343     return UNIXFS_initialize_target_folder(This, szBasePath, current, 0); 
1344 }
1345
1346 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1347 {
1348     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1349     
1350     TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1351
1352     if (!ppidl)
1353         return E_POINTER;
1354     *ppidl = ILClone (This->m_pidlLocation);
1355     return S_OK;
1356 }
1357
1358 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc, 
1359     LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1360 {
1361     UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1362     WCHAR wszTargetDosPath[MAX_PATH];
1363     char szTargetPath[FILENAME_MAX] = "";
1364     
1365     TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1366
1367     /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1368     if (!ppfti) 
1369         return IPersistFolder3_Initialize(iface, pidlRoot);
1370
1371     if (ppfti->csidl != -1) {
1372         if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1373             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1374         {
1375             return E_FAIL;
1376         }
1377     } else if (*ppfti->szTargetParsingName) {
1378         lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1379         PathAddBackslashW(wszTargetDosPath);
1380         if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1381             return E_FAIL;
1382         }
1383     } else if (ppfti->pidlTargetFolder) {
1384         if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1385             !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1386         {
1387             return E_FAIL;
1388         }
1389     } else {
1390         return E_FAIL;
1391     }
1392
1393     This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1394     if (!This->m_pszPath) 
1395         return E_FAIL;
1396     lstrcpyA(This->m_pszPath, szTargetPath);
1397     This->m_pidlLocation = ILClone(pidlRoot);
1398     This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1399         (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1400
1401     return S_OK;
1402 }
1403
1404 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface, 
1405     PERSIST_FOLDER_TARGET_INFO *ppfti)
1406 {
1407     FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1408     return E_NOTIMPL;
1409 }
1410
1411 /* VTable for UnixFolder's IPersistFolder interface.
1412  */
1413 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1414     UnixFolder_IPersistFolder3_QueryInterface,
1415     UnixFolder_IPersistFolder3_AddRef,
1416     UnixFolder_IPersistFolder3_Release,
1417     UnixFolder_IPersistFolder3_GetClassID,
1418     UnixFolder_IPersistFolder3_Initialize,
1419     UnixFolder_IPersistFolder3_GetCurFolder,
1420     UnixFolder_IPersistFolder3_InitializeEx,
1421     UnixFolder_IPersistFolder3_GetFolderTargetInfo
1422 };
1423
1424 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* This,
1425     REFIID riid, void** ppvObject)
1426 {
1427     return UnixFolder_IShellFolder2_QueryInterface(
1428                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This), riid, ppvObject);
1429 }
1430
1431 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* This)
1432 {
1433     return UnixFolder_IShellFolder2_AddRef(
1434                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1435 }
1436
1437 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* This)
1438 {
1439     return UnixFolder_IShellFolder2_Release(
1440                 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistPropertyBag, This));
1441 }
1442
1443 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface, 
1444     CLSID* pClassID)
1445 {
1446     return UnixFolder_IPersistFolder3_GetClassID(
1447         (IPersistFolder3*)&ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)->lpIPersistFolder3Vtbl,
1448         pClassID);
1449 }
1450
1451 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1452 {
1453     FIXME("() stub\n");
1454     return E_NOTIMPL;
1455 }
1456
1457 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface, 
1458     IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1459 {
1460      UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1461      static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1462      PERSIST_FOLDER_TARGET_INFO pftiTarget;
1463      VARIANT var;
1464      HRESULT hr;
1465      
1466      TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1467  
1468      if (!pPropertyBag)
1469          return E_POINTER;
1470  
1471      /* Get 'Target' property from the property bag. */
1472      V_VT(&var) = VT_BSTR;
1473      hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1474      if (FAILED(hr)) 
1475          return E_FAIL;
1476      lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1477      SysFreeString(V_BSTR(&var));
1478  
1479      pftiTarget.pidlTargetFolder = NULL;
1480      lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1481      pftiTarget.dwAttributes = -1;
1482      pftiTarget.csidl = -1;
1483  
1484      return UnixFolder_IPersistFolder3_InitializeEx(
1485                  STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1486 }
1487
1488 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1489     IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1490 {
1491     FIXME("() stub\n");
1492     return E_NOTIMPL;
1493 }
1494
1495 /* VTable for UnixFolder's IPersistPropertyBag interface.
1496  */
1497 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1498     UnixFolder_IPersistPropertyBag_QueryInterface,
1499     UnixFolder_IPersistPropertyBag_AddRef,
1500     UnixFolder_IPersistPropertyBag_Release,
1501     UnixFolder_IPersistPropertyBag_GetClassID,
1502     UnixFolder_IPersistPropertyBag_InitNew,
1503     UnixFolder_IPersistPropertyBag_Load,
1504     UnixFolder_IPersistPropertyBag_Save
1505 };
1506
1507 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid, 
1508     void** ppvObject)
1509 {
1510     return UnixFolder_IShellFolder2_QueryInterface(
1511                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface), riid, ppvObject);
1512 }
1513
1514 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1515 {
1516     return UnixFolder_IShellFolder2_AddRef(
1517                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1518 }
1519
1520 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1521 {
1522     return UnixFolder_IShellFolder2_Release(
1523                 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1524 }
1525
1526 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1527 {
1528     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1529     IEnumIDList *pEnum;
1530     HRESULT hr;
1531     LPITEMIDLIST pidlElem;
1532     DWORD dwFetched;
1533     int i;
1534     static const char szNewFolder[] = "New Folder";
1535
1536     TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1537     
1538     if (uLen < sizeof(szNewFolder)+3)
1539         return E_INVALIDARG;
1540
1541     hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1542                                    SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1543     if (SUCCEEDED(hr)) {
1544         lstrcpyA(lpName, szNewFolder);
1545         IEnumIDList_Reset(pEnum);
1546         i = 2;
1547         while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1548             if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1549                 IEnumIDList_Reset(pEnum);
1550                 sprintf(lpName, "%s %d", szNewFolder, i++);
1551                 if (i > 99) {
1552                     hr = E_FAIL;
1553                     break;
1554                 }
1555             }
1556         }
1557         IEnumIDList_Release(pEnum);
1558     }
1559     return hr;
1560 }
1561
1562 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName, 
1563     LPITEMIDLIST* ppidlOut)
1564 {
1565     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1566     char szNewDir[FILENAME_MAX];
1567
1568     TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1569
1570     if (ppidlOut)
1571         *ppidlOut = NULL;
1572     
1573     lstrcpyA(szNewDir, This->m_pszPath);
1574     lstrcatA(szNewDir, pszName);
1575
1576     if (mkdir(szNewDir, 0755)) {
1577         char szMessage[256 + FILENAME_MAX];
1578         char szCaption[256];
1579
1580         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1581         sprintf(szMessage, szCaption, szNewDir);
1582         LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1583         MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1584
1585         return E_FAIL;
1586     } else {
1587         LPITEMIDLIST pidlRelative;
1588         WCHAR wszName[MAX_PATH];
1589
1590         /* Inform the shell */
1591         MultiByteToWideChar(CP_UNIXCP, 0, pszName, -1, wszName, MAX_PATH);
1592         if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1593             LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1594             if (ppidlOut)
1595                 *ppidlOut = pidlRelative;
1596             else
1597                 ILFree(pidlRelative);
1598             SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1599             ILFree(pidlAbsolute);
1600         }
1601         return S_OK;
1602     }
1603 }
1604
1605 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl, 
1606     LPCITEMIDLIST* apidl)
1607 {
1608     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1609     char szAbsolute[FILENAME_MAX], *pszRelative;
1610     LPITEMIDLIST pidlAbsolute;
1611     HRESULT hr = S_OK;
1612     UINT i;
1613     
1614     TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1615
1616     lstrcpyA(szAbsolute, This->m_pszPath);
1617     pszRelative = szAbsolute + lstrlenA(szAbsolute);
1618     
1619     for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1620         lstrcpyA(pszRelative, _ILGetTextPointer(apidl[i]));
1621         pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1622         if (_ILIsFolder(apidl[i])) {
1623             if (rmdir(szAbsolute)) {
1624                 hr = E_FAIL;
1625             } else {
1626                 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1627             }
1628         } else if (_ILIsValue(apidl[i])) {
1629             if (unlink(szAbsolute)) {
1630                 hr = E_FAIL;
1631             } else {
1632                 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1633             }
1634         }
1635         ILFree(pidlAbsolute);
1636     }
1637         
1638     return hr;
1639 }
1640
1641 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom, 
1642     UINT cidl, LPCITEMIDLIST *apidl)
1643 {
1644     UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1645     DWORD dwAttributes;
1646     UINT i;
1647     HRESULT hr;
1648     char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1649     
1650     TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1651
1652     if (!psfFrom || !cidl || !apidl)
1653         return E_INVALIDARG;
1654
1655     /* All source items have to be filesystem items. */
1656     dwAttributes = SFGAO_FILESYSTEM;
1657     hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1658     if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM)) 
1659         return E_INVALIDARG;
1660
1661     lstrcpyA(szAbsoluteDst, This->m_pszPath);
1662     pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1663     
1664     for (i=0; i<cidl; i++) {
1665         WCHAR wszSrc[MAX_PATH];
1666         char szSrc[FILENAME_MAX];
1667         STRRET strret;
1668
1669         /* Build the unix path of the current source item. */
1670         if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1671             return E_FAIL;
1672         if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1673             return E_FAIL;
1674         if (!UNIXFS_get_unix_path(wszSrc, szSrc)) 
1675             return E_FAIL;
1676
1677         /* Build the unix path of the current destination item */
1678         lstrcpyA(pszRelativeDst, _ILGetTextPointer(apidl[i]));
1679
1680         FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1681     }
1682     return S_OK;
1683 }
1684
1685 /* VTable for UnixFolder's ISFHelper interface
1686  */
1687 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1688     UnixFolder_ISFHelper_QueryInterface,
1689     UnixFolder_ISFHelper_AddRef,
1690     UnixFolder_ISFHelper_Release,
1691     UnixFolder_ISFHelper_GetUniqueName,
1692     UnixFolder_ISFHelper_AddFolder,
1693     UnixFolder_ISFHelper_DeleteItems,
1694     UnixFolder_ISFHelper_CopyItems
1695 };
1696
1697 /******************************************************************************
1698  * Unix[Dos]Folder_Constructor [Internal]
1699  *
1700  * PARAMS
1701  *  pUnkOuter [I] Outer class for aggregation. Currently ignored.
1702  *  riid      [I] Interface asked for by the client.
1703  *  ppv       [O] Pointer to an riid interface to the UnixFolder object.
1704  *
1705  * NOTES
1706  *  Those are the only functions exported from shfldr_unixfs.c. They are called from
1707  *  shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1708  *  compatible signature.
1709  *
1710  *  The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1711  *  means that paths are converted from dos to unix and back at the interfaces.
1712  */
1713 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID) 
1714 {
1715     HRESULT hr = E_FAIL;
1716     UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1717    
1718     if (pUnkOuter) {
1719         FIXME("Aggregation not yet implemented!\n");
1720         return CLASS_E_NOAGGREGATION;
1721     }
1722     
1723     if(pUnixFolder) {
1724         pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1725         pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
1726         pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
1727         pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
1728         pUnixFolder->m_cRef = 0;
1729         pUnixFolder->m_pszPath = NULL;
1730         pUnixFolder->m_pidlLocation = NULL;
1731         pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
1732         pUnixFolder->m_dwAttributes = 0;
1733         pUnixFolder->m_pCLSID = pCLSID;
1734
1735         UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1736         hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1737         UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1738     }
1739     return hr;
1740 }
1741
1742 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1743     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1744     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
1745 }
1746
1747 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1748     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1749     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
1750 }
1751
1752 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1753     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1754     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
1755 }
1756
1757 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1758     TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1759     return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
1760 }
1761
1762 /******************************************************************************
1763  * UnixSubFolderIterator
1764  *
1765  * Class whose heap based objects represent iterators over the sub-directories
1766  * of a given UnixFolder object. 
1767  */
1768
1769 /* UnixSubFolderIterator object layout and typedef.
1770  */
1771 typedef struct _UnixSubFolderIterator {
1772     const IEnumIDListVtbl *lpIEnumIDListVtbl;
1773     LONG m_cRef;
1774     SHCONTF m_fFilter;
1775     DIR *m_dirFolder;
1776     char m_szFolder[FILENAME_MAX];
1777 } UnixSubFolderIterator;
1778
1779 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1780     TRACE("(iterator=%p)\n", iterator);
1781
1782     if (iterator->m_dirFolder)
1783         closedir(iterator->m_dirFolder);
1784     SHFree(iterator);
1785 }
1786
1787 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface, 
1788     REFIID riid, void** ppv)
1789 {
1790     TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1791     
1792     if (!ppv) return E_INVALIDARG;
1793     
1794     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1795         *ppv = iface;
1796     } else {
1797         *ppv = NULL;
1798         return E_NOINTERFACE;
1799     }
1800
1801     IEnumIDList_AddRef(iface);
1802     return S_OK;
1803 }
1804                             
1805 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1806 {
1807     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1808
1809     TRACE("(iface=%p)\n", iface);
1810    
1811     return InterlockedIncrement(&This->m_cRef);
1812 }
1813
1814 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1815 {
1816     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1817     ULONG cRef;
1818     
1819     TRACE("(iface=%p)\n", iface);
1820
1821     cRef = InterlockedDecrement(&This->m_cRef);
1822     
1823     if (!cRef) 
1824         UnixSubFolderIterator_Destroy(This);
1825
1826     return cRef;
1827 }
1828
1829 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt, 
1830     LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1831 {
1832     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1833     ULONG i = 0;
1834
1835     /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
1836     if (This->m_dirFolder) {
1837         char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
1838         struct dirent *pDirEntry;
1839
1840         while (i < celt) {
1841             pDirEntry = readdir(This->m_dirFolder);
1842             if (!pDirEntry) break; /* No more entries */
1843             if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
1844
1845             /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
1846              * and see if it passes the filter. 
1847              */
1848             lstrcpyA(pszRelativePath, pDirEntry->d_name);
1849             rgelt[i] = (LPITEMIDLIST)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(lstrlenA(pszRelativePath))+sizeof(USHORT));
1850             if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
1851                 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter)) 
1852             {
1853                 SHFree(rgelt[i]);
1854                 continue;
1855             }
1856             memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
1857             i++;
1858         }
1859         *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
1860     }
1861     
1862     if (pceltFetched)
1863         *pceltFetched = i;
1864
1865     return (i == 0) ? S_FALSE : S_OK;
1866 }
1867     
1868 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1869 {
1870     LPITEMIDLIST *apidl;
1871     ULONG cFetched;
1872     HRESULT hr;
1873     
1874     TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1875
1876     /* Call IEnumIDList::Next and delete the resulting pidls. */
1877     apidl = (LPITEMIDLIST*)SHAlloc(celt * sizeof(LPITEMIDLIST));
1878     hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
1879     if (SUCCEEDED(hr))
1880         while (cFetched--) 
1881             SHFree(apidl[cFetched]);
1882     SHFree(apidl);
1883
1884     return hr;
1885 }
1886
1887 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1888 {
1889     UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1890         
1891     TRACE("(iface=%p)\n", iface);
1892
1893     if (This->m_dirFolder)
1894         rewinddir(This->m_dirFolder);
1895     
1896     return S_OK;
1897 }
1898
1899 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This, 
1900     IEnumIDList** ppenum)
1901 {
1902     FIXME("stub\n");
1903     return E_NOTIMPL;
1904 }
1905
1906 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1907  */
1908 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1909     UnixSubFolderIterator_IEnumIDList_QueryInterface,
1910     UnixSubFolderIterator_IEnumIDList_AddRef,
1911     UnixSubFolderIterator_IEnumIDList_Release,
1912     UnixSubFolderIterator_IEnumIDList_Next,
1913     UnixSubFolderIterator_IEnumIDList_Skip,
1914     UnixSubFolderIterator_IEnumIDList_Reset,
1915     UnixSubFolderIterator_IEnumIDList_Clone
1916 };
1917
1918 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1919     UnixSubFolderIterator *iterator;
1920
1921     TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1922     
1923     iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1924     iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1925     iterator->m_cRef = 0;
1926     iterator->m_fFilter = fFilter;
1927     iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
1928     lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
1929
1930     UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1931     
1932     return (IUnknown*)iterator;
1933 }