2 * UNIXFS - Shell namespace extension for the unix filesystem
4 * Copyright (C) 2005 Michael Jung
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.
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.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
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
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
40 * DOS paths do appear at various places in GUI applications. Sometimes, they
41 * show up in the title bar of an application's window. They tend to accumulate
42 * in the most-recently-used section of the file-menu. And I've even seen some
43 * in a configuration dialog's edit control. In those examples, wine can't do a
44 * lot about this, since path-names can't be told apart from ordinary strings
45 * here. That's different in the file dialogs, though.
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.
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.
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.
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.
103 * But what if none of the symbolic links in 'dosdevices' points to '/', you
104 * might ask ("And I don't want wine have access to my complete hard drive, you
105 * *%&1#!"). No problem, as I stated above, unixfs uses the _posix_ apis to
106 * construct the ITEMIDLISTs. Folders, which aren't accessible via a drive letter,
107 * don't have the SFGAO_FILESYSTEM flag set. So the file dialogs shouldn't allow
108 * the user to select such a folder for file storage (And if it does anyhow, it
109 * will not be able to return a valid path, since there is none). Think of those
110 * folders as a hierarchy of 'My Computer'-like folders, which happen to be a
111 * shadow of your unix filesystem tree. And since all of this stuff doesn't
112 * change anything at all in wine's fileio api's, windows applications will have
113 * no more access rights as they had before.
115 * To sum it all up, you can still safely run wine with you root account (Just
116 * kidding, don't do it.)
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.
126 #include "wine/port.h"
138 #ifdef HAVE_SYS_STAT_H
139 # include <sys/stat.h>
147 #define NONAMELESSUNION
148 #define NONAMELESSSTRUCT
156 #include "winternl.h"
157 #include "wine/debug.h"
159 #include "shell32_main.h"
160 #include "shellfolder.h"
162 #include "shresdef.h"
165 WINE_DEFAULT_DEBUG_CHANNEL(shell);
167 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
168 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
170 #define LEN_SHITEMID_FIXED_PART ((USHORT) \
171 ( sizeof(USHORT) /* SHITEMID's cb field. */ \
172 + sizeof(PIDLTYPE) /* PIDLDATA's type field. */ \
173 + sizeof(FileStruct) /* Well, the FileStruct. */ \
174 - sizeof(char) /* One char too much in FileStruct. */ \
175 + sizeof(FileStructW) /* You name it. */ \
176 - sizeof(WCHAR) /* One WCHAR too much in FileStructW. */ \
177 + sizeof(WORD) )) /* Offset of FileStructW field in PIDL. */
179 #define PATHMODE_UNIX 0
180 #define PATHMODE_DOS 1
182 /* UnixFolder object layout and typedef.
184 typedef struct _UnixFolder {
185 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
186 const IPersistFolder3Vtbl *lpIPersistFolder3Vtbl;
187 const IPersistPropertyBagVtbl *lpIPersistPropertyBagVtbl;
188 const IDropTargetVtbl *lpIDropTargetVtbl;
189 const ISFHelperVtbl *lpISFHelperVtbl;
191 CHAR *m_pszPath; /* Target path of the shell folder (CP_UNIXCP) */
192 LPITEMIDLIST m_pidlLocation; /* Location in the shell namespace */
194 DWORD m_dwAttributes;
195 const CLSID *m_pCLSID;
196 DWORD m_dwDropEffectsMask;
199 /* Will hold the registered clipboard format identifier for ITEMIDLISTS. */
200 static UINT cfShellIDList = 0;
202 /******************************************************************************
203 * UNIXFS_is_rooted_at_desktop [Internal]
205 * Checks if the unixfs namespace extension is rooted at desktop level.
208 * TRUE, if unixfs is rooted at desktop level
211 BOOL UNIXFS_is_rooted_at_desktop(void) {
213 WCHAR wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
214 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
215 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
216 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
217 'N','a','m','e','S','p','a','c','e','\\',0 };
219 if (StringFromGUID2(&CLSID_UnixDosFolder, wszRootedAtDesktop + 69, CHARS_IN_GUID) &&
220 RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
228 /******************************************************************************
229 * UNIXFS_filename_from_shitemid [Internal]
231 * Get CP_UNIXCP encoded filename corresponding to the first item of a pidl
234 * pidl [I] A simple SHITEMID
235 * pszPathElement [O] Filename in CP_UNIXCP encoding will be stored here
238 * Success: Number of bytes necessary to store the CP_UNIXCP encoded filename
239 * _without_ the terminating NUL.
243 * Size of the buffer at pszPathElement has to be FILENAME_MAX. pszPathElement
244 * may be NULL, if you are only interested in the return value.
246 static int UNIXFS_filename_from_shitemid(LPCITEMIDLIST pidl, char* pszPathElement) {
247 FileStructW *pFileStructW = _ILGetFileStructW(pidl);
251 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pFileStructW->wszName, -1, pszPathElement,
252 pszPathElement ? FILENAME_MAX : 0, 0, 0);
254 /* There might be pidls slipping in from shfldr_fs.c, which don't contain the
255 * FileStructW field. In this case, we have to convert from CP_ACP to CP_UNIXCP. */
256 char *pszText = _ILGetTextPointer(pidl);
257 WCHAR *pwszPathElement = NULL;
260 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, NULL, 0);
261 if (!cWideChars) goto cleanup;
263 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
264 if (!pwszPathElement) goto cleanup;
266 cWideChars = MultiByteToWideChar(CP_ACP, 0, pszText, -1, pwszPathElement, cWideChars);
267 if (!cWideChars) goto cleanup;
269 cLen = WideCharToMultiByte(CP_UNIXCP, 0, pwszPathElement, -1, pszPathElement,
270 pszPathElement ? FILENAME_MAX : 0, 0, 0);
273 SHFree(pwszPathElement);
276 if (cLen) cLen--; /* Don't count terminating NUL! */
280 /******************************************************************************
281 * UNIXFS_shitemid_len_from_filename [Internal]
283 * Computes the necessary length of a pidl to hold a path element
286 * szPathElement [I] The path element string in CP_UNIXCP encoding.
287 * ppszPathElement [O] Path element string in CP_ACP encoding.
288 * ppwszPathElement [O] Path element string as WCHAR string.
291 * Success: Length in bytes of a SHITEMID representing szPathElement
295 * Provide NULL values if not interested in pp(w)szPathElement. Otherwise
296 * caller is responsible to free ppszPathElement and ppwszPathElement with
299 static USHORT UNIXFS_shitemid_len_from_filename(
300 const char *szPathElement, char **ppszPathElement, WCHAR **ppwszPathElement)
302 USHORT cbPidlLen = 0;
303 WCHAR *pwszPathElement = NULL;
304 char *pszPathElement = NULL;
305 int cWideChars, cChars;
307 /* There and Back Again: A Hobbit's Holiday. CP_UNIXCP might be some ANSI
308 * codepage or it might be a real multi-byte encoding like utf-8. There is no
309 * other way to figure out the length of the corresponding WCHAR and CP_ACP
310 * strings without actually doing the full CP_UNIXCP -> WCHAR -> CP_ACP cycle. */
312 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, NULL, 0);
313 if (!cWideChars) goto cleanup;
315 pwszPathElement = SHAlloc(cWideChars * sizeof(WCHAR));
316 if (!pwszPathElement) goto cleanup;
318 cWideChars = MultiByteToWideChar(CP_UNIXCP, 0, szPathElement, -1, pwszPathElement, cWideChars);
319 if (!cWideChars) goto cleanup;
321 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, NULL, 0, 0, 0);
322 if (!cChars) goto cleanup;
324 pszPathElement = SHAlloc(cChars);
325 if (!pszPathElement) goto cleanup;
327 cChars = WideCharToMultiByte(CP_ACP, 0, pwszPathElement, -1, pszPathElement, cChars, 0, 0);
328 if (!cChars) goto cleanup;
330 /* (cChars & 0x1) is for the potential alignment byte */
331 cbPidlLen = LEN_SHITEMID_FIXED_PART + cChars + (cChars & 0x1) + cWideChars * sizeof(WCHAR);
334 if (cbPidlLen && ppszPathElement)
335 *ppszPathElement = pszPathElement;
337 SHFree(pszPathElement);
339 if (cbPidlLen && ppwszPathElement)
340 *ppwszPathElement = pwszPathElement;
342 SHFree(pwszPathElement);
347 /******************************************************************************
348 * UNIXFS_is_pidl_of_type [Internal]
350 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
353 * pIDL [I] The ITEMIDLIST to be checked.
354 * fFilter [I] Shell condition flags, which specify the filter.
357 * TRUE, if pIDL is accepted by fFilter
360 static inline BOOL UNIXFS_is_pidl_of_type(LPCITEMIDLIST pIDL, SHCONTF fFilter) {
361 const PIDLDATA *pIDLData = _ILGetDataPointer(pIDL);
362 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
363 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
367 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
368 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
372 /******************************************************************************
373 * UNIXFS_get_unix_path [Internal]
375 * Convert an absolute dos path to an absolute unix path.
376 * Evaluate "/.", "/.." and the symbolic links in $WINEPREFIX/dosdevices.
379 * pszDosPath [I] An absolute dos path
380 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
384 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
386 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
388 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
389 WCHAR wszDrive[] = { '?', ':', '\\', 0 };
390 int cDriveSymlinkLen;
392 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
394 if (!pszDosPath || pszDosPath[1] != ':')
397 /* Get the canonicalized unix path corresponding to the drive letter. */
398 wszDrive[0] = pszDosPath[0];
399 pszUnixPath = wine_get_unix_file_name(wszDrive);
400 if (!pszUnixPath) return FALSE;
401 cDriveSymlinkLen = strlen(pszUnixPath);
402 pElement = realpath(pszUnixPath, szPath);
403 HeapFree(GetProcessHeap(), 0, pszUnixPath);
404 if (!pElement) return FALSE;
405 if (szPath[strlen(szPath)-1] != '/') strcat(szPath, "/");
407 /* Append the part relative to the drive symbolic link target. */
408 pszUnixPath = wine_get_unix_file_name(pszDosPath);
409 if (!pszUnixPath) return FALSE;
410 strcat(szPath, pszUnixPath + cDriveSymlinkLen);
411 HeapFree(GetProcessHeap(), 0, pszUnixPath);
413 /* pCanonicalTail always points to the end of the canonical path constructed
414 * thus far. pPathTail points to the still to be processed part of the input
415 * path. pElement points to the path element currently investigated.
417 *pszCanonicalPath = '\0';
418 pCanonicalTail = pszCanonicalPath;
424 pElement = pPathTail;
425 pPathTail = strchr(pPathTail+1, '/');
426 if (!pPathTail) /* Last path element may not be terminated by '/'. */
427 pPathTail = pElement + strlen(pElement);
428 /* Temporarily terminate the current path element. Will be restored later. */
432 /* Skip "/." path elements */
433 if (!strcmp("/.", pElement)) {
435 } else if (!strcmp("/..", pElement)) {
436 /* Remove last element in canonical path for "/.." elements, then skip. */
437 char *pTemp = strrchr(pszCanonicalPath, '/');
439 pCanonicalTail = pTemp;
440 *pCanonicalTail = '\0';
443 /* Directory or file. Copy to canonical path */
444 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
447 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
448 pCanonicalTail += pPathTail - pElement;
451 } while (pPathTail[0] == '/');
453 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
458 /******************************************************************************
459 * UNIXFS_seconds_since_1970_to_dos_date_time [Internal]
461 * Convert unix time to FAT time
464 * ss1970 [I] Unix time (seconds since 1970)
465 * pDate [O] Corresponding FAT date
466 * pTime [O] Corresponding FAT time
468 static inline void UNIXFS_seconds_since_1970_to_dos_date_time(
469 time_t ss1970, LPWORD pDate, LPWORD pTime)
474 RtlSecondsSince1970ToTime( ss1970, &time );
475 fileTime.dwLowDateTime = time.u.LowPart;
476 fileTime.dwHighDateTime = time.u.HighPart;
477 FileTimeToDosDateTime(&fileTime, pDate, pTime);
480 /******************************************************************************
481 * UNIXFS_build_shitemid [Internal]
483 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
487 * pszUnixPath [I] An absolute path. The SHITEMID will be built for the last component.
488 * pIDL [O] SHITEMID will be constructed here.
491 * Success: A pointer to the terminating '\0' character of path.
495 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
496 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
499 static char* UNIXFS_build_shitemid(char *pszUnixPath, void *pIDL) {
501 struct stat fileStat;
502 char *pszComponentU, *pszComponentA;
503 WCHAR *pwszComponentW;
504 int cComponentULen, cComponentALen;
506 FileStructW *pFileStructW;
507 WORD uOffsetW, *pOffsetW;
509 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
511 /* We are only interested in regular files and directories. */
512 if (stat(pszUnixPath, &fileStat)) return NULL;
513 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
515 /* Compute the SHITEMID's length and wipe it. */
516 pszComponentU = strrchr(pszUnixPath, '/') + 1;
517 cComponentULen = strlen(pszComponentU);
518 cbLen = UNIXFS_shitemid_len_from_filename(pszComponentU, &pszComponentA, &pwszComponentW);
519 if (!cbLen) return NULL;
520 memset(pIDL, 0, cbLen);
521 ((LPSHITEMID)pIDL)->cb = cbLen;
523 /* Set shell32's standard SHITEMID data fields. */
524 pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
525 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
526 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
527 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pIDLData->u.file.uFileDate,
528 &pIDLData->u.file.uFileTime);
529 pIDLData->u.file.uFileAttribs = 0;
530 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
531 if (pszComponentU[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
532 cComponentALen = lstrlenA(pszComponentA) + 1;
533 memcpy(pIDLData->u.file.szNames, pszComponentA, cComponentALen);
535 pFileStructW = (FileStructW*)(pIDLData->u.file.szNames + cComponentALen + (cComponentALen & 0x1));
536 uOffsetW = (WORD)(((LPBYTE)pFileStructW) - ((LPBYTE)pIDL));
537 pFileStructW->cbLen = cbLen - uOffsetW;
538 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_mtime, &pFileStructW->uCreationDate,
539 &pFileStructW->uCreationTime);
540 UNIXFS_seconds_since_1970_to_dos_date_time(fileStat.st_atime, &pFileStructW->uLastAccessDate,
541 &pFileStructW->uLastAccessTime);
542 lstrcpyW(pFileStructW->wszName, pwszComponentW);
544 pOffsetW = (WORD*)(((LPBYTE)pIDL) + cbLen - sizeof(WORD));
545 *pOffsetW = uOffsetW;
547 SHFree(pszComponentA);
548 SHFree(pwszComponentW);
550 return pszComponentU + cComponentULen;
553 /******************************************************************************
554 * UNIXFS_path_to_pidl [Internal]
557 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
558 * path [I] An absolute unix or dos path or a path relative to pUnixFolder
559 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
563 * Failure: Error code, invalid params or out of memory
566 * pUnixFolder also carries the information if the path is expected to be unix or dos.
568 static HRESULT UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
570 int cPidlLen, cPathLen;
571 char *pSlash, *pNextSlash, szCompletePath[FILENAME_MAX], *pNextPathElement, *pszAPath;
574 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
579 /* Build an absolute path and let pNextPathElement point to the interesting
580 * relative sub-path. We need the absolute path to call 'stat', but the pidl
581 * will only contain the relative part.
583 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
585 /* Absolute dos path. Convert to unix */
586 if (!UNIXFS_get_unix_path(path, szCompletePath))
588 pNextPathElement = szCompletePath;
590 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
592 /* Absolute unix path. Just convert to ANSI. */
593 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
594 pNextPathElement = szCompletePath;
598 /* Relative dos or unix path. Concat with this folder's path */
599 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
600 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
601 WideCharToMultiByte(CP_UNIXCP, 0, path, -1, szCompletePath + cBasePathLen,
602 FILENAME_MAX - cBasePathLen, NULL, NULL);
603 pNextPathElement = szCompletePath + cBasePathLen - 1;
605 /* If in dos mode, replace '\' with '/' */
606 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
607 char *pBackslash = strchr(pNextPathElement, '\\');
610 pBackslash = strchr(pBackslash, '\\');
615 /* Special case for the root folder. */
616 if (!strcmp(szCompletePath, "/")) {
617 *ppidl = pidl = SHAlloc(sizeof(USHORT));
618 if (!pidl) return E_FAIL;
619 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
623 /* Remove trailing slash, if present */
624 cPathLen = strlen(szCompletePath);
625 if (szCompletePath[cPathLen-1] == '/')
626 szCompletePath[cPathLen-1] = '\0';
628 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
629 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
633 /* At this point, we have an absolute unix path in szCompletePath
634 * and the relative portion of it in pNextPathElement. Both starting with '/'
635 * and _not_ terminated by a '/'. */
636 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
638 /* Convert to CP_ACP and WCHAR */
639 if (!UNIXFS_shitemid_len_from_filename(pNextPathElement, &pszAPath, &pwszPath))
642 /* Compute the length of the complete ITEMIDLIST */
646 pNextSlash = strchr(pSlash+1, '/');
647 cPidlLen += LEN_SHITEMID_FIXED_PART + /* Fixed part length plus potential alignment byte. */
648 (pNextSlash ? (pNextSlash - pSlash) & 0x1 : lstrlenA(pSlash) & 0x1);
652 /* The USHORT is for the ITEMIDLIST terminator. The NUL terminators for the sub-path-strings
653 * are accounted for by the '/' separators, which are not stored in the SHITEMIDs. Above we
654 * have ensured that the number of '/'s exactly matches the number of sub-path-strings. */
655 cPidlLen += lstrlenA(pszAPath) + lstrlenW(pwszPath) * sizeof(WCHAR) + sizeof(USHORT);
660 *ppidl = pidl = SHAlloc(cPidlLen);
661 if (!pidl) return E_FAIL;
663 /* Concatenate the SHITEMIDs of the sub-directories. */
664 while (*pNextPathElement) {
665 pSlash = strchr(pNextPathElement+1, '/');
666 if (pSlash) *pSlash = '\0';
667 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, pidl);
668 if (pSlash) *pSlash = '/';
670 if (!pNextPathElement) {
673 return HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
675 pidl = ILGetNext(pidl);
677 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
679 if ((char *)pidl-(char *)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
680 ERR("Computed length of pidl incorrect. Please report.\n");
685 /******************************************************************************
686 * UNIXFS_initialize_target_folder [Internal]
688 * Initialize the m_pszPath member of an UnixFolder, given an absolute unix
689 * base path and a relative ITEMIDLIST. Leave the m_pidlLocation member, which
690 * specifies the location in the shell namespace alone.
693 * This [IO] The UnixFolder, whose target path is to be initialized
694 * szBasePath [I] The absolute base path
695 * pidlSubFolder [I] Relative part of the path, given as an ITEMIDLIST
696 * dwAttributes [I] Attributes to add to the Folders m_dwAttributes member
697 * (Used to pass the SFGAO_FILESYSTEM flag down the path)
702 static HRESULT UNIXFS_initialize_target_folder(UnixFolder *This, const char *szBasePath,
703 LPCITEMIDLIST pidlSubFolder, DWORD dwAttributes)
705 LPCITEMIDLIST current = pidlSubFolder;
706 DWORD dwPathLen = strlen(szBasePath)+1;
710 /* Determine the path's length bytes */
711 while (current && current->mkid.cb) {
712 dwPathLen += UNIXFS_filename_from_shitemid(current, NULL) + 1; /* For the '/' */
713 current = ILGetNext(current);
716 /* Build the path and compute the attributes*/
717 This->m_dwAttributes =
718 dwAttributes|SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
719 This->m_pszPath = pNextDir = SHAlloc(dwPathLen);
720 if (!This->m_pszPath) {
721 WARN("SHAlloc failed!\n");
724 current = pidlSubFolder;
725 strcpy(pNextDir, szBasePath);
726 pNextDir += strlen(szBasePath);
727 if (This->m_dwPathMode == PATHMODE_UNIX || IsEqualCLSID(&CLSID_MyDocuments, This->m_pCLSID))
728 This->m_dwAttributes |= SFGAO_FILESYSTEM;
729 while (current && current->mkid.cb) {
730 pNextDir += UNIXFS_filename_from_shitemid(current, pNextDir);
732 current = ILGetNext(current);
736 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM) &&
737 ((dos_name = wine_get_dos_file_name(This->m_pszPath))))
739 This->m_dwAttributes |= SFGAO_FILESYSTEM;
740 HeapFree( GetProcessHeap(), 0, dos_name );
746 /******************************************************************************
747 * UNIXFS_copy [Internal]
749 * Copy pwszDosSrc to pwszDosDst.
752 * pwszDosSrc [I] absolute path of the source
753 * pwszDosDst [I] absolute path of the destination
759 static HRESULT UNIXFS_copy(LPCWSTR pwszDosSrc, LPCWSTR pwszDosDst)
762 LPWSTR pwszSrc, pwszDst;
763 HRESULT res = E_OUTOFMEMORY;
764 UINT iSrcLen, iDstLen;
766 if (!pwszDosSrc || !pwszDosDst)
769 iSrcLen = lstrlenW(pwszDosSrc);
770 iDstLen = lstrlenW(pwszDosDst);
771 pwszSrc = HeapAlloc(GetProcessHeap(), 0, (iSrcLen + 2) * sizeof(WCHAR));
772 pwszDst = HeapAlloc(GetProcessHeap(), 0, (iDstLen + 2) * sizeof(WCHAR));
774 if (pwszSrc && pwszDst) {
775 lstrcpyW(pwszSrc, pwszDosSrc);
776 lstrcpyW(pwszDst, pwszDosDst);
777 /* double null termination */
778 pwszSrc[iSrcLen + 1] = 0;
779 pwszDst[iDstLen + 1] = 0;
781 ZeroMemory(&op, sizeof(op));
782 op.hwnd = GetActiveWindow();
786 op.fFlags = FOF_ALLOWUNDO;
787 if (!SHFileOperationW(&op))
789 WARN("SHFileOperationW failed\n");
796 HeapFree(GetProcessHeap(), 0, pwszSrc);
797 HeapFree(GetProcessHeap(), 0, pwszDst);
801 /******************************************************************************
804 * Class whose heap based instances represent unix filesystem directories.
807 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
808 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
810 SHFree(pUnixFolder->m_pszPath);
811 ILFree(pUnixFolder->m_pidlLocation);
815 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
818 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
820 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
822 if (!ppv) return E_INVALIDARG;
824 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
825 IsEqualIID(&IID_IShellFolder2, riid))
827 *ppv = STATIC_CAST(IShellFolder2, This);
828 } else if (IsEqualIID(&IID_IPersistFolder3, riid) || IsEqualIID(&IID_IPersistFolder2, riid) ||
829 IsEqualIID(&IID_IPersistFolder, riid) || IsEqualIID(&IID_IPersist, riid))
831 *ppv = STATIC_CAST(IPersistFolder3, This);
832 } else if (IsEqualIID(&IID_IPersistPropertyBag, riid)) {
833 *ppv = STATIC_CAST(IPersistPropertyBag, This);
834 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
835 *ppv = STATIC_CAST(ISFHelper, This);
836 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
837 *ppv = STATIC_CAST(IDropTarget, This);
839 cfShellIDList = RegisterClipboardFormatW(CFSTR_SHELLIDLISTW);
842 return E_NOINTERFACE;
845 IUnknown_AddRef((IUnknown*)*ppv);
849 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
850 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
852 TRACE("(iface=%p)\n", iface);
854 return InterlockedIncrement(&This->m_cRef);
857 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
858 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
861 TRACE("(iface=%p)\n", iface);
863 cRef = InterlockedDecrement(&This->m_cRef);
866 UnixFolder_Destroy(This);
871 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
872 LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
873 ULONG* pdwAttributes)
875 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
878 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
879 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName),
880 pchEaten, ppidl, pdwAttributes);
882 result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
883 if (SUCCEEDED(result) && pdwAttributes && *pdwAttributes)
885 IShellFolder *pParentSF;
886 LPCITEMIDLIST pidlLast;
887 LPITEMIDLIST pidlComplete = ILCombine(This->m_pidlLocation, *ppidl);
890 hr = SHBindToParent(pidlComplete, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
892 FIXME("SHBindToParent failed! hr = %08x\n", hr);
893 ILFree(pidlComplete);
896 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
897 IShellFolder_Release(pParentSF);
898 ILFree(pidlComplete);
901 if (FAILED(result)) TRACE("FAILED!\n");
905 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter);
907 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
908 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
910 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
911 IUnknown *newIterator;
914 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08x, ppEnumIDList=%p)\n",
915 iface, hwndOwner, grfFlags, ppEnumIDList);
917 if (!This->m_pszPath) {
918 WARN("EnumObjects called on uninitialized UnixFolder-object!\n");
922 newIterator = UnixSubFolderIterator_Constructor(This, grfFlags);
923 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
924 IUnknown_Release(newIterator);
929 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID);
931 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
932 LPBC pbcReserved, REFIID riid, void** ppvOut)
934 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
935 IPersistFolder3 *persistFolder;
937 const CLSID *clsidChild;
939 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
940 iface, pidl, pbcReserved, riid, ppvOut);
942 if (!pidl || !pidl->mkid.cb)
945 if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
946 /* Children of FolderShortcuts are ShellFSFolders on Windows.
947 * Unixfs' counterpart is UnixDosFolder. */
948 clsidChild = &CLSID_UnixDosFolder;
950 clsidChild = This->m_pCLSID;
953 hr = CreateUnixFolder(NULL, &IID_IPersistFolder3, (void**)&persistFolder, clsidChild);
954 if (FAILED(hr)) return hr;
955 hr = IPersistFolder_QueryInterface(persistFolder, riid, ppvOut);
958 UnixFolder *subfolder = ADJUST_THIS(UnixFolder, IPersistFolder3, persistFolder);
959 subfolder->m_pidlLocation = ILCombine(This->m_pidlLocation, pidl);
960 hr = UNIXFS_initialize_target_folder(subfolder, This->m_pszPath, pidl,
961 This->m_dwAttributes & SFGAO_FILESYSTEM);
964 IPersistFolder3_Release(persistFolder);
969 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
970 LPBC pbcReserved, REFIID riid, void** ppvObj)
976 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
977 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
979 BOOL isEmpty1, isEmpty2;
981 LPITEMIDLIST firstpidl;
985 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
987 isEmpty1 = !pidl1 || !pidl1->mkid.cb;
988 isEmpty2 = !pidl2 || !pidl2->mkid.cb;
990 if (isEmpty1 && isEmpty2)
991 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
993 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
995 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
997 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
998 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
999 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
1000 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1002 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
1003 _ILGetTextPointer(pidl1), -1,
1004 _ILGetTextPointer(pidl2), -1);
1006 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
1007 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
1009 if (pidl1->mkid.cb < pidl2->mkid.cb)
1010 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
1011 else if (pidl1->mkid.cb > pidl2->mkid.cb)
1012 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
1014 firstpidl = ILCloneFirst(pidl1);
1015 pidl1 = ILGetNext(pidl1);
1016 pidl2 = ILGetNext(pidl2);
1018 hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
1019 if (SUCCEEDED(hr)) {
1020 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
1021 IShellFolder2_Release(psf);
1028 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
1029 REFIID riid, void** ppv)
1031 HRESULT hr = E_INVALIDARG;
1033 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
1035 if (!ppv) return E_INVALIDARG;
1038 if (IsEqualIID(&IID_IShellView, riid)) {
1039 LPSHELLVIEW pShellView;
1041 pShellView = IShellView_Constructor((IShellFolder*)iface);
1043 hr = IShellView_QueryInterface(pShellView, riid, ppv);
1044 IShellView_Release(pShellView);
1046 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1047 hr = IShellFolder2_QueryInterface(iface, &IID_IDropTarget, ppv);
1053 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
1054 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
1056 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1059 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
1061 if (!rgfInOut || (cidl && !apidl))
1062 return E_INVALIDARG;
1065 *rgfInOut &= This->m_dwAttributes;
1067 char szAbsolutePath[FILENAME_MAX], *pszRelativePath;
1070 *rgfInOut = SFGAO_CANCOPY|SFGAO_CANMOVE|SFGAO_CANLINK|SFGAO_CANRENAME|SFGAO_CANDELETE|
1071 SFGAO_HASPROPSHEET|SFGAO_DROPTARGET|SFGAO_FILESYSTEM;
1072 lstrcpyA(szAbsolutePath, This->m_pszPath);
1073 pszRelativePath = szAbsolutePath + lstrlenA(szAbsolutePath);
1074 for (i=0; i<cidl; i++) {
1075 if (!(This->m_dwAttributes & SFGAO_FILESYSTEM)) {
1077 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelativePath))
1078 return E_INVALIDARG;
1079 if (!(dos_name = wine_get_dos_file_name( szAbsolutePath )))
1080 *rgfInOut &= ~SFGAO_FILESYSTEM;
1082 HeapFree( GetProcessHeap(), 0, dos_name );
1084 if (_ILIsFolder(apidl[i]))
1085 *rgfInOut |= SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1092 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
1093 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
1095 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1098 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
1099 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
1101 if (!cidl || !apidl || !riid || !ppvOut)
1102 return E_INVALIDARG;
1104 for (i=0; i<cidl; i++)
1106 return E_INVALIDARG;
1108 if (IsEqualIID(&IID_IContextMenu, riid)) {
1109 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
1111 } else if (IsEqualIID(&IID_IDataObject, riid)) {
1112 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
1114 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
1116 if (cidl != 1) return E_INVALIDARG;
1117 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1118 *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
1121 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
1123 if (cidl != 1) return E_INVALIDARG;
1124 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
1125 *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
1128 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
1129 if (cidl != 1) return E_INVALIDARG;
1130 return IShellFolder2_BindToObject(iface, apidl[0], NULL, &IID_IDropTarget, ppvOut);
1131 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
1132 FIXME("IShellLinkW\n");
1134 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1135 FIXME("IShellLinkA\n");
1138 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1139 return E_NOINTERFACE;
1143 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1144 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1146 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1149 TRACE("(iface=%p, pidl=%p, uFlags=%x, lpName=%p)\n", iface, pidl, uFlags, lpName);
1151 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1152 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1154 if (!pidl || !pidl->mkid.cb) {
1155 lpName->uType = STRRET_WSTR;
1156 if (This->m_dwPathMode == PATHMODE_UNIX) {
1157 UINT len = MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, NULL, 0);
1158 lpName->u.pOleStr = SHAlloc(len * sizeof(WCHAR));
1159 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1160 MultiByteToWideChar(CP_UNIXCP, 0, This->m_pszPath, -1, lpName->u.pOleStr, len);
1162 LPWSTR pwszDosFileName = wine_get_dos_file_name(This->m_pszPath);
1163 if (!pwszDosFileName) return HRESULT_FROM_WIN32(GetLastError());
1164 lpName->u.pOleStr = SHAlloc((lstrlenW(pwszDosFileName) + 1) * sizeof(WCHAR));
1165 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1166 lstrcpyW(lpName->u.pOleStr, pwszDosFileName);
1167 PathRemoveBackslashW(lpName->u.pOleStr);
1168 HeapFree(GetProcessHeap(), 0, pwszDosFileName);
1171 IShellFolder *pSubFolder;
1172 SHITEMID emptyIDL = { 0, { 0 } };
1174 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1175 if (FAILED(hr)) return hr;
1177 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1178 IShellFolder_Release(pSubFolder);
1181 WCHAR wszFileName[MAX_PATH];
1182 if (!_ILSimpleGetTextW(pidl, wszFileName, MAX_PATH)) return E_INVALIDARG;
1183 lpName->uType = STRRET_WSTR;
1184 lpName->u.pOleStr = SHAlloc((lstrlenW(wszFileName)+1)*sizeof(WCHAR));
1185 if (!lpName->u.pOleStr) return HRESULT_FROM_WIN32(GetLastError());
1186 lstrcpyW(lpName->u.pOleStr, wszFileName);
1187 if (!(GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) && This->m_dwPathMode == PATHMODE_DOS &&
1188 !_ILIsFolder(pidl) && wszFileName[0] != '.' && SHELL_FS_HideExtension(wszFileName))
1190 PathRemoveExtensionW(lpName->u.pOleStr);
1194 TRACE("--> %s\n", debugstr_w(lpName->u.pOleStr));
1199 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1200 LPCITEMIDLIST pidl, LPCOLESTR lpcwszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1202 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1204 static const WCHAR awcInvalidChars[] = { '\\', '/', ':', '*', '?', '"', '<', '>', '|' };
1205 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1206 WCHAR wszSrcRelative[MAX_PATH];
1208 int cBasePathLen = lstrlenA(This->m_pszPath);
1209 struct stat statDest;
1210 LPITEMIDLIST pidlSrc, pidlDest, pidlRelativeDest;
1214 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpcwszName=%s, uFlags=0x%08x, ppidlOut=%p)\n",
1215 iface, hwnd, pidl, debugstr_w(lpcwszName), uFlags, ppidlOut);
1217 /* prepare to fail */
1221 /* pidl has to contain a single non-empty SHITEMID */
1222 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1223 return E_INVALIDARG;
1225 /* check for invalid characters in lpcwszName. */
1226 for (i=0; i < sizeof(awcInvalidChars)/sizeof(*awcInvalidChars); i++)
1227 if (StrChrW(lpcwszName, awcInvalidChars[i]))
1228 return HRESULT_FROM_WIN32(ERROR_CANCELLED);
1230 /* build source path */
1231 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1232 UNIXFS_filename_from_shitemid(pidl, szSrc + cBasePathLen);
1234 /* build destination path */
1235 memcpy(szDest, This->m_pszPath, cBasePathLen);
1236 WideCharToMultiByte(CP_UNIXCP, 0, lpcwszName, -1, szDest+cBasePathLen,
1237 FILENAME_MAX-cBasePathLen, NULL, NULL);
1239 /* If the filename's extension is hidden to the user, we have to append it. */
1240 if (!(uFlags & SHGDN_FORPARSING) &&
1241 _ILSimpleGetTextW(pidl, wszSrcRelative, MAX_PATH) &&
1242 SHELL_FS_HideExtension(wszSrcRelative))
1244 WCHAR *pwszExt = PathFindExtensionW(wszSrcRelative);
1245 int cLenDest = strlen(szDest);
1246 WideCharToMultiByte(CP_UNIXCP, 0, pwszExt, -1, szDest + cLenDest,
1247 FILENAME_MAX - cLenDest, NULL, NULL);
1250 TRACE("src=%s dest=%s\n", szSrc, szDest);
1252 /* Fail, if destination does already exist */
1253 if (!stat(szDest, &statDest))
1256 /* Rename the file */
1257 if (rename(szSrc, szDest))
1260 /* Build a pidl for the path of the renamed file */
1261 lpwszName = SHAlloc((lstrlenW(lpcwszName)+1)*sizeof(WCHAR)); /* due to const correctness. */
1262 lstrcpyW(lpwszName, lpcwszName);
1263 hr = IShellFolder2_ParseDisplayName(iface, NULL, NULL, lpwszName, NULL, &pidlRelativeDest, NULL);
1266 rename(szDest, szSrc); /* Undo the renaming */
1269 pidlDest = ILCombine(This->m_pidlLocation, pidlRelativeDest);
1270 ILFree(pidlRelativeDest);
1271 pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1273 /* Inform the shell */
1274 if (_ILIsFolder(ILFindLastID(pidlDest)))
1275 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1277 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1280 *ppidlOut = ILClone(ILFindLastID(pidlDest));
1288 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
1289 IEnumExtraSearch **ppEnum)
1295 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1296 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1302 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1303 UINT iColumn, SHCOLSTATEF *pcsFlags)
1309 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1316 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
1317 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1323 #define SHELLVIEWCOLUMNS 7
1325 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
1326 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1328 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1329 HRESULT hr = E_FAIL;
1330 struct passwd *pPasswd;
1331 struct group *pGroup;
1332 static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1333 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1334 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1335 {IDS_SHV_COLUMN3, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1336 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 12},
1337 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 9},
1338 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1339 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7}
1342 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1344 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1345 return E_INVALIDARG;
1348 psd->fmt = SFHeader[iColumn].fmt;
1349 psd->cxChar = SFHeader[iColumn].cxChar;
1350 psd->str.uType = STRRET_CSTR;
1351 LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1354 struct stat statItem;
1355 if (iColumn == 4 || iColumn == 5 || iColumn == 6) {
1356 char szPath[FILENAME_MAX];
1357 strcpy(szPath, This->m_pszPath);
1358 if (!UNIXFS_filename_from_shitemid(pidl, szPath + strlen(szPath)))
1359 return E_INVALIDARG;
1360 if (stat(szPath, &statItem))
1361 return E_INVALIDARG;
1363 psd->str.u.cStr[0] = '\0';
1364 psd->str.uType = STRRET_CSTR;
1367 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1370 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1373 _ILGetFileType (pidl, psd->str.u.cStr, MAX_PATH);
1376 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1379 psd->str.u.cStr[0] = S_ISDIR(statItem.st_mode) ? 'd' : '-';
1380 psd->str.u.cStr[1] = (statItem.st_mode & S_IRUSR) ? 'r' : '-';
1381 psd->str.u.cStr[2] = (statItem.st_mode & S_IWUSR) ? 'w' : '-';
1382 psd->str.u.cStr[3] = (statItem.st_mode & S_IXUSR) ? 'x' : '-';
1383 psd->str.u.cStr[4] = (statItem.st_mode & S_IRGRP) ? 'r' : '-';
1384 psd->str.u.cStr[5] = (statItem.st_mode & S_IWGRP) ? 'w' : '-';
1385 psd->str.u.cStr[6] = (statItem.st_mode & S_IXGRP) ? 'x' : '-';
1386 psd->str.u.cStr[7] = (statItem.st_mode & S_IROTH) ? 'r' : '-';
1387 psd->str.u.cStr[8] = (statItem.st_mode & S_IWOTH) ? 'w' : '-';
1388 psd->str.u.cStr[9] = (statItem.st_mode & S_IXOTH) ? 'x' : '-';
1389 psd->str.u.cStr[10] = '\0';
1392 pPasswd = getpwuid(statItem.st_uid);
1393 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1396 pGroup = getgrgid(statItem.st_gid);
1397 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1405 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1412 /* VTable for UnixFolder's IShellFolder2 interface.
1414 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1415 UnixFolder_IShellFolder2_QueryInterface,
1416 UnixFolder_IShellFolder2_AddRef,
1417 UnixFolder_IShellFolder2_Release,
1418 UnixFolder_IShellFolder2_ParseDisplayName,
1419 UnixFolder_IShellFolder2_EnumObjects,
1420 UnixFolder_IShellFolder2_BindToObject,
1421 UnixFolder_IShellFolder2_BindToStorage,
1422 UnixFolder_IShellFolder2_CompareIDs,
1423 UnixFolder_IShellFolder2_CreateViewObject,
1424 UnixFolder_IShellFolder2_GetAttributesOf,
1425 UnixFolder_IShellFolder2_GetUIObjectOf,
1426 UnixFolder_IShellFolder2_GetDisplayNameOf,
1427 UnixFolder_IShellFolder2_SetNameOf,
1428 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1429 UnixFolder_IShellFolder2_EnumSearches,
1430 UnixFolder_IShellFolder2_GetDefaultColumn,
1431 UnixFolder_IShellFolder2_GetDefaultColumnState,
1432 UnixFolder_IShellFolder2_GetDetailsEx,
1433 UnixFolder_IShellFolder2_GetDetailsOf,
1434 UnixFolder_IShellFolder2_MapColumnToSCID
1437 static HRESULT WINAPI UnixFolder_IPersistFolder3_QueryInterface(IPersistFolder3* iface, REFIID riid,
1440 return UnixFolder_IShellFolder2_QueryInterface(
1441 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)), riid, ppvObject);
1444 static ULONG WINAPI UnixFolder_IPersistFolder3_AddRef(IPersistFolder3* iface)
1446 return UnixFolder_IShellFolder2_AddRef(
1447 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1450 static ULONG WINAPI UnixFolder_IPersistFolder3_Release(IPersistFolder3* iface)
1452 return UnixFolder_IShellFolder2_Release(
1453 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistFolder3, iface)));
1456 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetClassID(IPersistFolder3* iface, CLSID* pClassID)
1458 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1460 TRACE("(iface=%p, pClassId=%p)\n", iface, pClassID);
1463 return E_INVALIDARG;
1465 *pClassID = *This->m_pCLSID;
1469 static HRESULT WINAPI UnixFolder_IPersistFolder3_Initialize(IPersistFolder3* iface, LPCITEMIDLIST pidl)
1471 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1472 LPCITEMIDLIST current = pidl;
1473 char szBasePath[FILENAME_MAX] = "/";
1475 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1477 /* Find the UnixFolderClass root */
1478 while (current->mkid.cb) {
1479 if ((_ILIsDrive(current) && IsEqualCLSID(This->m_pCLSID, &CLSID_ShellFSFolder)) ||
1480 (_ILIsSpecialFolder(current) && IsEqualCLSID(This->m_pCLSID, _ILGetGUIDPointer(current))))
1484 current = ILGetNext(current);
1487 if (current && current->mkid.cb) {
1488 if (_ILIsDrive(current)) {
1489 WCHAR wszDrive[4] = { '?', ':', '\\', 0 };
1490 wszDrive[0] = (WCHAR)*_ILGetTextPointer(current);
1491 if (!UNIXFS_get_unix_path(wszDrive, szBasePath))
1493 } else if (IsEqualIID(&CLSID_MyDocuments, _ILGetGUIDPointer(current))) {
1494 WCHAR wszMyDocumentsPath[MAX_PATH];
1495 if (!SHGetSpecialFolderPathW(0, wszMyDocumentsPath, CSIDL_PERSONAL, FALSE))
1497 PathAddBackslashW(wszMyDocumentsPath);
1498 if (!UNIXFS_get_unix_path(wszMyDocumentsPath, szBasePath))
1501 current = ILGetNext(current);
1502 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
1503 /* Path rooted at Desktop */
1504 WCHAR wszDesktopPath[MAX_PATH];
1505 if (!SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOPDIRECTORY, FALSE))
1507 PathAddBackslashW(wszDesktopPath);
1508 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
1511 } else if (IsEqualCLSID(This->m_pCLSID, &CLSID_FolderShortcut)) {
1512 /* FolderShortcuts' Initialize method only sets the ITEMIDLIST, which
1513 * specifies the location in the shell namespace, but leaves the
1514 * target folder (m_pszPath) alone. See unit tests in tests/shlfolder.c */
1515 This->m_pidlLocation = ILClone(pidl);
1518 ERR("Unknown pidl type!\n");
1520 return E_INVALIDARG;
1523 This->m_pidlLocation = ILClone(pidl);
1524 return UNIXFS_initialize_target_folder(This, szBasePath, current, 0);
1527 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetCurFolder(IPersistFolder3* iface, LPITEMIDLIST* ppidl)
1529 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1531 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1535 *ppidl = ILClone (This->m_pidlLocation);
1539 static HRESULT WINAPI UnixFolder_IPersistFolder3_InitializeEx(IPersistFolder3 *iface, IBindCtx *pbc,
1540 LPCITEMIDLIST pidlRoot, const PERSIST_FOLDER_TARGET_INFO *ppfti)
1542 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder3, iface);
1543 WCHAR wszTargetDosPath[MAX_PATH];
1544 char szTargetPath[FILENAME_MAX] = "";
1546 TRACE("(iface=%p, pbc=%p, pidlRoot=%p, ppfti=%p)\n", iface, pbc, pidlRoot, ppfti);
1548 /* If no PERSIST_FOLDER_TARGET_INFO is given InitializeEx is equivalent to Initialize. */
1550 return IPersistFolder3_Initialize(iface, pidlRoot);
1552 if (ppfti->csidl != -1) {
1553 if (FAILED(SHGetFolderPathW(0, ppfti->csidl, NULL, 0, wszTargetDosPath)) ||
1554 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1558 } else if (*ppfti->szTargetParsingName) {
1559 lstrcpyW(wszTargetDosPath, ppfti->szTargetParsingName);
1560 PathAddBackslashW(wszTargetDosPath);
1561 if (!UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath)) {
1564 } else if (ppfti->pidlTargetFolder) {
1565 if (!SHGetPathFromIDListW(ppfti->pidlTargetFolder, wszTargetDosPath) ||
1566 !UNIXFS_get_unix_path(wszTargetDosPath, szTargetPath))
1574 This->m_pszPath = SHAlloc(lstrlenA(szTargetPath)+1);
1575 if (!This->m_pszPath)
1577 lstrcpyA(This->m_pszPath, szTargetPath);
1578 This->m_pidlLocation = ILClone(pidlRoot);
1579 This->m_dwAttributes = (ppfti->dwAttributes != -1) ? ppfti->dwAttributes :
1580 (SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME|SFGAO_FILESYSTEM);
1585 static HRESULT WINAPI UnixFolder_IPersistFolder3_GetFolderTargetInfo(IPersistFolder3 *iface,
1586 PERSIST_FOLDER_TARGET_INFO *ppfti)
1588 FIXME("(iface=%p, ppfti=%p) stub\n", iface, ppfti);
1592 /* VTable for UnixFolder's IPersistFolder interface.
1594 static const IPersistFolder3Vtbl UnixFolder_IPersistFolder3_Vtbl = {
1595 UnixFolder_IPersistFolder3_QueryInterface,
1596 UnixFolder_IPersistFolder3_AddRef,
1597 UnixFolder_IPersistFolder3_Release,
1598 UnixFolder_IPersistFolder3_GetClassID,
1599 UnixFolder_IPersistFolder3_Initialize,
1600 UnixFolder_IPersistFolder3_GetCurFolder,
1601 UnixFolder_IPersistFolder3_InitializeEx,
1602 UnixFolder_IPersistFolder3_GetFolderTargetInfo
1605 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_QueryInterface(IPersistPropertyBag* iface,
1606 REFIID riid, void** ppv)
1608 return UnixFolder_IShellFolder2_QueryInterface(
1609 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), riid, ppv);
1612 static ULONG WINAPI UnixFolder_IPersistPropertyBag_AddRef(IPersistPropertyBag* iface)
1614 return UnixFolder_IShellFolder2_AddRef(
1615 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1618 static ULONG WINAPI UnixFolder_IPersistPropertyBag_Release(IPersistPropertyBag* iface)
1620 return UnixFolder_IShellFolder2_Release(
1621 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)));
1624 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_GetClassID(IPersistPropertyBag* iface,
1627 return UnixFolder_IPersistFolder3_GetClassID(
1628 STATIC_CAST(IPersistFolder3, ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface)), pClassID);
1631 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_InitNew(IPersistPropertyBag* iface)
1637 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Load(IPersistPropertyBag *iface,
1638 IPropertyBag *pPropertyBag, IErrorLog *pErrorLog)
1640 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistPropertyBag, iface);
1641 static const WCHAR wszTarget[] = { 'T','a','r','g','e','t', 0 }, wszNull[] = { 0 };
1642 PERSIST_FOLDER_TARGET_INFO pftiTarget;
1646 TRACE("(iface=%p, pPropertyBag=%p, pErrorLog=%p)\n", iface, pPropertyBag, pErrorLog);
1651 /* Get 'Target' property from the property bag. */
1652 V_VT(&var) = VT_BSTR;
1653 hr = IPropertyBag_Read(pPropertyBag, wszTarget, &var, NULL);
1656 lstrcpyW(pftiTarget.szTargetParsingName, V_BSTR(&var));
1657 SysFreeString(V_BSTR(&var));
1659 pftiTarget.pidlTargetFolder = NULL;
1660 lstrcpyW(pftiTarget.szNetworkProvider, wszNull);
1661 pftiTarget.dwAttributes = -1;
1662 pftiTarget.csidl = -1;
1664 return UnixFolder_IPersistFolder3_InitializeEx(
1665 STATIC_CAST(IPersistFolder3, This), NULL, NULL, &pftiTarget);
1668 static HRESULT WINAPI UnixFolder_IPersistPropertyBag_Save(IPersistPropertyBag *iface,
1669 IPropertyBag *pPropertyBag, BOOL fClearDirty, BOOL fSaveAllProperties)
1675 /* VTable for UnixFolder's IPersistPropertyBag interface.
1677 static const IPersistPropertyBagVtbl UnixFolder_IPersistPropertyBag_Vtbl = {
1678 UnixFolder_IPersistPropertyBag_QueryInterface,
1679 UnixFolder_IPersistPropertyBag_AddRef,
1680 UnixFolder_IPersistPropertyBag_Release,
1681 UnixFolder_IPersistPropertyBag_GetClassID,
1682 UnixFolder_IPersistPropertyBag_InitNew,
1683 UnixFolder_IPersistPropertyBag_Load,
1684 UnixFolder_IPersistPropertyBag_Save
1687 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1690 return UnixFolder_IShellFolder2_QueryInterface(
1691 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)), riid, ppvObject);
1694 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1696 return UnixFolder_IShellFolder2_AddRef(
1697 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1700 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1702 return UnixFolder_IShellFolder2_Release(
1703 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, ISFHelper, iface)));
1706 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPWSTR pwszName, UINT uLen)
1708 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1711 LPITEMIDLIST pidlElem;
1714 WCHAR wszNewFolder[25];
1715 static const WCHAR wszFormat[] = { '%','s',' ','%','d',0 };
1717 TRACE("(iface=%p, pwszName=%p, uLen=%u)\n", iface, pwszName, uLen);
1719 LoadStringW(shell32_hInstance, IDS_NEWFOLDER, wszNewFolder, sizeof(wszNewFolder)/sizeof(WCHAR));
1721 if (uLen < sizeof(wszNewFolder)/sizeof(WCHAR)+3)
1722 return E_INVALIDARG;
1724 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1725 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1726 if (SUCCEEDED(hr)) {
1727 lstrcpynW(pwszName, wszNewFolder, uLen);
1728 IEnumIDList_Reset(pEnum);
1730 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1731 WCHAR wszTemp[MAX_PATH];
1732 _ILSimpleGetTextW(pidlElem, wszTemp, MAX_PATH);
1733 if (!lstrcmpiW(wszTemp, pwszName)) {
1734 IEnumIDList_Reset(pEnum);
1735 snprintfW(pwszName, uLen, wszFormat, wszNewFolder, i++);
1742 IEnumIDList_Release(pEnum);
1747 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCWSTR pwszName,
1748 LPITEMIDLIST* ppidlOut)
1750 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1751 char szNewDir[FILENAME_MAX];
1754 TRACE("(iface=%p, hwnd=%p, pwszName=%s, ppidlOut=%p)\n",
1755 iface, hwnd, debugstr_w(pwszName), ppidlOut);
1760 if (!This->m_pszPath || !(This->m_dwAttributes & SFGAO_FILESYSTEM))
1763 lstrcpynA(szNewDir, This->m_pszPath, FILENAME_MAX);
1764 cBaseLen = lstrlenA(szNewDir);
1765 WideCharToMultiByte(CP_UNIXCP, 0, pwszName, -1, szNewDir+cBaseLen, FILENAME_MAX-cBaseLen, 0, 0);
1767 if (mkdir(szNewDir, 0777)) {
1768 char szMessage[256 + FILENAME_MAX];
1769 char szCaption[256];
1771 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1772 sprintf(szMessage, szCaption, szNewDir);
1773 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1774 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1778 LPITEMIDLIST pidlRelative;
1780 /* Inform the shell */
1781 if (SUCCEEDED(UNIXFS_path_to_pidl(This, pwszName, &pidlRelative))) {
1782 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1784 *ppidlOut = pidlRelative;
1786 ILFree(pidlRelative);
1787 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1788 ILFree(pidlAbsolute);
1789 } else return E_FAIL;
1795 * Delete specified files by converting the path to DOS paths and calling
1796 * SHFileOperationW. If an error occurs it returns an error code. If the paths can't
1797 * be converted, S_FALSE is returned. In such situation DeleteItems will try to delete
1798 * the files using syscalls
1800 static HRESULT UNIXFS_delete_with_shfileop(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1802 char szAbsolute[FILENAME_MAX], *pszRelative;
1803 LPWSTR wszPathsList, wszListPos;
1808 lstrcpyA(szAbsolute, This->m_pszPath);
1809 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1811 wszListPos = wszPathsList = HeapAlloc(GetProcessHeap(), 0, cidl*MAX_PATH*sizeof(WCHAR)+1);
1812 if (wszPathsList == NULL)
1813 return E_OUTOFMEMORY;
1814 for (i=0; i<cidl; i++) {
1817 if (!_ILIsFolder(apidl[i]) && !_ILIsValue(apidl[i]))
1819 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1821 HeapFree(GetProcessHeap(), 0, wszPathsList);
1822 return E_INVALIDARG;
1824 wszDosPath = wine_get_dos_file_name(szAbsolute);
1825 if (wszDosPath == NULL || lstrlenW(wszDosPath) >= MAX_PATH)
1827 HeapFree(GetProcessHeap(), 0, wszPathsList);
1828 HeapFree(GetProcessHeap(), 0, wszDosPath);
1831 lstrcpyW(wszListPos, wszDosPath);
1832 wszListPos += lstrlenW(wszListPos)+1;
1833 HeapFree(GetProcessHeap(), 0, wszDosPath);
1837 ZeroMemory(&op, sizeof(op));
1838 op.hwnd = GetActiveWindow();
1839 op.wFunc = FO_DELETE;
1840 op.pFrom = wszPathsList;
1841 op.fFlags = FOF_ALLOWUNDO;
1842 if (!SHFileOperationW(&op))
1844 WARN("SHFileOperationW failed\n");
1850 HeapFree(GetProcessHeap(), 0, wszPathsList);
1854 static HRESULT UNIXFS_delete_with_syscalls(UnixFolder *This, UINT cidl, const LPCITEMIDLIST *apidl)
1856 char szAbsolute[FILENAME_MAX], *pszRelative;
1857 static const WCHAR empty[] = {0};
1860 if (!SHELL_ConfirmYesNoW(GetActiveWindow(), ASK_DELETE_SELECTED, empty))
1863 lstrcpyA(szAbsolute, This->m_pszPath);
1864 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1866 for (i=0; i<cidl; i++) {
1867 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1868 return E_INVALIDARG;
1869 if (_ILIsFolder(apidl[i])) {
1870 if (rmdir(szAbsolute))
1872 } else if (_ILIsValue(apidl[i])) {
1873 if (unlink(szAbsolute))
1880 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1881 LPCITEMIDLIST* apidl)
1883 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1884 char szAbsolute[FILENAME_MAX], *pszRelative;
1885 LPITEMIDLIST pidlAbsolute;
1890 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1892 hr = UNIXFS_delete_with_shfileop(This, cidl, apidl);
1894 hr = UNIXFS_delete_with_syscalls(This, cidl, apidl);
1896 lstrcpyA(szAbsolute, This->m_pszPath);
1897 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1899 /* we need to manually send the notifies if the files doesn't exist */
1900 for (i=0; i<cidl; i++) {
1901 if (!UNIXFS_filename_from_shitemid(apidl[i], pszRelative))
1903 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1904 if (stat(szAbsolute, &st))
1906 if (_ILIsFolder(apidl[i])) {
1907 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1908 } else if (_ILIsValue(apidl[i])) {
1909 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1912 ILFree(pidlAbsolute);
1918 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
1919 UINT cidl, LPCITEMIDLIST *apidl)
1921 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1925 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1927 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p)\n", iface, psfFrom, cidl, apidl);
1929 if (!psfFrom || !cidl || !apidl)
1930 return E_INVALIDARG;
1932 /* All source items have to be filesystem items. */
1933 dwAttributes = SFGAO_FILESYSTEM;
1934 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1935 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1936 return E_INVALIDARG;
1938 lstrcpyA(szAbsoluteDst, This->m_pszPath);
1939 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1941 for (i=0; i<cidl; i++) {
1942 WCHAR wszSrc[MAX_PATH];
1943 char szSrc[FILENAME_MAX];
1946 WCHAR *pwszDosSrc, *pwszDosDst;
1948 /* Build the unix path of the current source item. */
1949 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1951 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1953 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
1956 /* Build the unix path of the current destination item */
1957 UNIXFS_filename_from_shitemid(apidl[i], pszRelativeDst);
1959 pwszDosSrc = wine_get_dos_file_name(szSrc);
1960 pwszDosDst = wine_get_dos_file_name(szAbsoluteDst);
1962 if (pwszDosSrc && pwszDosDst)
1963 res = UNIXFS_copy(pwszDosSrc, pwszDosDst);
1965 res = E_OUTOFMEMORY;
1967 HeapFree(GetProcessHeap(), 0, pwszDosSrc);
1968 HeapFree(GetProcessHeap(), 0, pwszDosDst);
1976 /* VTable for UnixFolder's ISFHelper interface
1978 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1979 UnixFolder_ISFHelper_QueryInterface,
1980 UnixFolder_ISFHelper_AddRef,
1981 UnixFolder_ISFHelper_Release,
1982 UnixFolder_ISFHelper_GetUniqueName,
1983 UnixFolder_ISFHelper_AddFolder,
1984 UnixFolder_ISFHelper_DeleteItems,
1985 UnixFolder_ISFHelper_CopyItems
1988 static HRESULT WINAPI UnixFolder_IDropTarget_QueryInterface(IDropTarget* iface, REFIID riid,
1991 return UnixFolder_IShellFolder2_QueryInterface(
1992 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)), riid, ppvObject);
1995 static ULONG WINAPI UnixFolder_IDropTarget_AddRef(IDropTarget* iface)
1997 return UnixFolder_IShellFolder2_AddRef(
1998 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2001 static ULONG WINAPI UnixFolder_IDropTarget_Release(IDropTarget* iface)
2003 return UnixFolder_IShellFolder2_Release(
2004 STATIC_CAST(IShellFolder2, ADJUST_THIS(UnixFolder, IDropTarget, iface)));
2007 #define HIDA_GetPIDLFolder(pida) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[0])
2008 #define HIDA_GetPIDLItem(pida, i) (LPCITEMIDLIST)(((LPBYTE)pida)+(pida)->aoffset[i+1])
2010 static HRESULT WINAPI UnixFolder_IDropTarget_DragEnter(IDropTarget *iface, IDataObject *pDataObject,
2011 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2013 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2017 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n",
2018 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2020 if (!pdwEffect || !pDataObject)
2021 return E_INVALIDARG;
2023 /* Compute a mask of supported drop-effects for this shellfolder object and the given data
2024 * object. Dropping is only supported on folders, which represent filesystem locations. One
2025 * can't drop on file objects. And the 'move' drop effect is only supported, if the source
2026 * folder is not identical to the target folder. */
2027 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2028 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2029 if ((This->m_dwAttributes & SFGAO_FILESYSTEM) && /* Only drop to filesystem folders */
2030 _ILIsFolder(ILFindLastID(This->m_pidlLocation)) && /* Only drop to folders, not to files */
2031 SUCCEEDED(IDataObject_GetData(pDataObject, &format, &medium))) /* Only ShellIDList format */
2033 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2034 This->m_dwDropEffectsMask |= DROPEFFECT_COPY|DROPEFFECT_LINK;
2036 if (pidaShellIDList) { /* Files can only be moved between two different folders */
2037 if (!ILIsEqual(HIDA_GetPIDLFolder(pidaShellIDList), This->m_pidlLocation))
2038 This->m_dwDropEffectsMask |= DROPEFFECT_MOVE;
2039 GlobalUnlock(medium.u.hGlobal);
2043 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2048 static HRESULT WINAPI UnixFolder_IDropTarget_DragOver(IDropTarget *iface, DWORD dwKeyState,
2049 POINTL pt, DWORD *pdwEffect)
2051 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2053 TRACE("(iface=%p, dwKeyState=%08x, pt={.x=%d, .y=%d}, pdwEffect=%p)\n", iface, dwKeyState,
2054 pt.x, pt.y, pdwEffect);
2057 return E_INVALIDARG;
2059 *pdwEffect = KeyStateToDropEffect(dwKeyState) & This->m_dwDropEffectsMask;
2064 static HRESULT WINAPI UnixFolder_IDropTarget_DragLeave(IDropTarget *iface) {
2065 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2067 TRACE("(iface=%p)\n", iface);
2069 This->m_dwDropEffectsMask = DROPEFFECT_NONE;
2074 static HRESULT WINAPI UnixFolder_IDropTarget_Drop(IDropTarget *iface, IDataObject *pDataObject,
2075 DWORD dwKeyState, POINTL pt, DWORD *pdwEffect)
2077 UnixFolder *This = ADJUST_THIS(UnixFolder, IDropTarget, iface);
2082 TRACE("(iface=%p, pDataObject=%p, dwKeyState=%d, pt={.x=%d, .y=%d}, pdwEffect=%p) semi-stub\n",
2083 iface, pDataObject, dwKeyState, pt.x, pt.y, pdwEffect);
2085 InitFormatEtc(format, cfShellIDList, TYMED_HGLOBAL);
2086 hr = IDataObject_GetData(pDataObject, &format, &medium);
2090 if (medium.tymed == TYMED_HGLOBAL) {
2091 IShellFolder *psfSourceFolder, *psfDesktopFolder;
2092 LPIDA pidaShellIDList = GlobalLock(medium.u.hGlobal);
2096 if (!pidaShellIDList)
2097 return HRESULT_FROM_WIN32(GetLastError());
2099 hr = SHGetDesktopFolder(&psfDesktopFolder);
2101 GlobalUnlock(medium.u.hGlobal);
2105 hr = IShellFolder_BindToObject(psfDesktopFolder, HIDA_GetPIDLFolder(pidaShellIDList), NULL,
2106 &IID_IShellFolder, (LPVOID*)&psfSourceFolder);
2107 IShellFolder_Release(psfDesktopFolder);
2109 GlobalUnlock(medium.u.hGlobal);
2113 for (i = 0; i < pidaShellIDList->cidl; i++) {
2114 WCHAR wszSourcePath[MAX_PATH];
2116 hr = IShellFolder_GetDisplayNameOf(psfSourceFolder, HIDA_GetPIDLItem(pidaShellIDList, i),
2117 SHGDN_FORPARSING, &strret);
2121 hr = StrRetToBufW(&strret, NULL, wszSourcePath, MAX_PATH);
2125 switch (*pdwEffect) {
2126 case DROPEFFECT_MOVE:
2127 FIXME("Move %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2129 case DROPEFFECT_COPY:
2130 FIXME("Copy %s to %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2132 case DROPEFFECT_LINK:
2133 FIXME("Link %s from %s!\n", debugstr_w(wszSourcePath), This->m_pszPath);
2138 IShellFolder_Release(psfSourceFolder);
2139 GlobalUnlock(medium.u.hGlobal);
2146 /* VTable for UnixFolder's IDropTarget interface
2148 static const IDropTargetVtbl UnixFolder_IDropTarget_Vtbl = {
2149 UnixFolder_IDropTarget_QueryInterface,
2150 UnixFolder_IDropTarget_AddRef,
2151 UnixFolder_IDropTarget_Release,
2152 UnixFolder_IDropTarget_DragEnter,
2153 UnixFolder_IDropTarget_DragOver,
2154 UnixFolder_IDropTarget_DragLeave,
2155 UnixFolder_IDropTarget_Drop
2158 /******************************************************************************
2159 * Unix[Dos]Folder_Constructor [Internal]
2162 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
2163 * riid [I] Interface asked for by the client.
2164 * ppv [O] Pointer to an riid interface to the UnixFolder object.
2167 * Those are the only functions exported from shfldr_unixfs.c. They are called from
2168 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
2169 * compatible signature.
2171 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
2172 * means that paths are converted from dos to unix and back at the interfaces.
2174 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, const CLSID *pCLSID)
2176 HRESULT hr = E_FAIL;
2177 UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
2180 FIXME("Aggregation not yet implemented!\n");
2181 return CLASS_E_NOAGGREGATION;
2185 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
2186 pUnixFolder->lpIPersistFolder3Vtbl = &UnixFolder_IPersistFolder3_Vtbl;
2187 pUnixFolder->lpIPersistPropertyBagVtbl = &UnixFolder_IPersistPropertyBag_Vtbl;
2188 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
2189 pUnixFolder->lpIDropTargetVtbl = &UnixFolder_IDropTarget_Vtbl;
2190 pUnixFolder->m_cRef = 0;
2191 pUnixFolder->m_pszPath = NULL;
2192 pUnixFolder->m_pidlLocation = NULL;
2193 pUnixFolder->m_dwPathMode = IsEqualCLSID(&CLSID_UnixFolder, pCLSID) ? PATHMODE_UNIX : PATHMODE_DOS;
2194 pUnixFolder->m_dwAttributes = 0;
2195 pUnixFolder->m_pCLSID = pCLSID;
2196 pUnixFolder->m_dwDropEffectsMask = DROPEFFECT_NONE;
2198 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
2199 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
2200 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
2205 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2206 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2207 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixFolder);
2210 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2211 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2212 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_UnixDosFolder);
2215 HRESULT WINAPI FolderShortcut_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2216 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2217 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_FolderShortcut);
2220 HRESULT WINAPI MyDocuments_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2221 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2222 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_MyDocuments);
2225 HRESULT WINAPI ShellFSFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
2226 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
2227 return CreateUnixFolder(pUnkOuter, riid, ppv, &CLSID_ShellFSFolder);
2230 /******************************************************************************
2231 * UnixSubFolderIterator
2233 * Class whose heap based objects represent iterators over the sub-directories
2234 * of a given UnixFolder object.
2237 /* UnixSubFolderIterator object layout and typedef.
2239 typedef struct _UnixSubFolderIterator {
2240 const IEnumIDListVtbl *lpIEnumIDListVtbl;
2244 char m_szFolder[FILENAME_MAX];
2245 } UnixSubFolderIterator;
2247 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
2248 TRACE("(iterator=%p)\n", iterator);
2250 if (iterator->m_dirFolder)
2251 closedir(iterator->m_dirFolder);
2255 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
2256 REFIID riid, void** ppv)
2258 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
2260 if (!ppv) return E_INVALIDARG;
2262 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
2266 return E_NOINTERFACE;
2269 IEnumIDList_AddRef(iface);
2273 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
2275 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2277 TRACE("(iface=%p)\n", iface);
2279 return InterlockedIncrement(&This->m_cRef);
2282 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
2284 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2287 TRACE("(iface=%p)\n", iface);
2289 cRef = InterlockedDecrement(&This->m_cRef);
2292 UnixSubFolderIterator_Destroy(This);
2297 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
2298 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
2300 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2303 /* This->m_dirFolder will be NULL if the user doesn't have access rights for the dir. */
2304 if (This->m_dirFolder) {
2305 char *pszRelativePath = This->m_szFolder + lstrlenA(This->m_szFolder);
2306 struct dirent *pDirEntry;
2309 pDirEntry = readdir(This->m_dirFolder);
2310 if (!pDirEntry) break; /* No more entries */
2311 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
2313 /* Temporarily build absolute path in This->m_szFolder. Then construct a pidl
2314 * and see if it passes the filter.
2316 lstrcpyA(pszRelativePath, pDirEntry->d_name);
2317 rgelt[i] = (LPITEMIDLIST)SHAlloc(
2318 UNIXFS_shitemid_len_from_filename(pszRelativePath, NULL, NULL)+sizeof(USHORT));
2319 if (!UNIXFS_build_shitemid(This->m_szFolder, rgelt[i]) ||
2320 !UNIXFS_is_pidl_of_type(rgelt[i], This->m_fFilter))
2325 memset(((PBYTE)rgelt[i])+rgelt[i]->mkid.cb, 0, sizeof(USHORT));
2328 *pszRelativePath = '\0'; /* Restore the original path in This->m_szFolder. */
2334 return (i == 0) ? S_FALSE : S_OK;
2337 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
2339 LPITEMIDLIST *apidl;
2343 TRACE("(iface=%p, celt=%d)\n", iface, celt);
2345 /* Call IEnumIDList::Next and delete the resulting pidls. */
2346 apidl = SHAlloc(celt * sizeof(LPITEMIDLIST));
2347 hr = IEnumIDList_Next(iface, celt, apidl, &cFetched);
2350 SHFree(apidl[cFetched]);
2356 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
2358 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
2360 TRACE("(iface=%p)\n", iface);
2362 if (This->m_dirFolder)
2363 rewinddir(This->m_dirFolder);
2368 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
2369 IEnumIDList** ppenum)
2375 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
2377 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
2378 UnixSubFolderIterator_IEnumIDList_QueryInterface,
2379 UnixSubFolderIterator_IEnumIDList_AddRef,
2380 UnixSubFolderIterator_IEnumIDList_Release,
2381 UnixSubFolderIterator_IEnumIDList_Next,
2382 UnixSubFolderIterator_IEnumIDList_Skip,
2383 UnixSubFolderIterator_IEnumIDList_Reset,
2384 UnixSubFolderIterator_IEnumIDList_Clone
2387 static IUnknown *UnixSubFolderIterator_Constructor(UnixFolder *pUnixFolder, SHCONTF fFilter) {
2388 UnixSubFolderIterator *iterator;
2390 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
2392 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
2393 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
2394 iterator->m_cRef = 0;
2395 iterator->m_fFilter = fFilter;
2396 iterator->m_dirFolder = opendir(pUnixFolder->m_pszPath);
2397 lstrcpyA(iterator->m_szFolder, pUnixFolder->m_pszPath);
2399 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
2401 return (IUnknown*)iterator;