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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
49 #include "wine/debug.h"
51 #include "shell32_main.h"
52 #include "shellfolder.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(shell);
59 const GUID CLSID_UnixFolder = {0xcc702eb2, 0x7dc5, 0x11d9, {0xc6, 0x87, 0x00, 0x04, 0x23, 0x8a, 0x01, 0xcd}};
60 const GUID CLSID_UnixDosFolder = {0x9d20aae8, 0x0625, 0x44b0, {0x9c, 0xa7, 0x71, 0x88, 0x9c, 0x22, 0x54, 0xd9}};
62 #define ADJUST_THIS(c,m,p) ((c*)(((long)p)-(long)&(((c*)0)->lp##m##Vtbl)))
63 #define STATIC_CAST(i,p) ((i*)&p->lp##i##Vtbl)
65 /* FileStruct reserves one byte for szNames, thus we don't have to
66 * alloc a byte for the terminating '\0' of 'name'. Two of the
67 * additional bytes are for SHITEMID's cb field. One is for IDLDATA's
68 * type field. One is for FileStruct's szNames field, to terminate
69 * the alternate DOS name, which we don't use here. And then there's
70 * the additional StatStruct.
72 #define SHITEMID_LEN_FROM_NAME_LEN(n) \
73 (sizeof(USHORT)+sizeof(PIDLTYPE)+sizeof(FileStruct)+(n)+sizeof(char)+sizeof(StatStruct))
74 #define NAME_LEN_FROM_LPSHITEMID(s) \
75 (((LPSHITEMID)s)->cb-sizeof(USHORT)-sizeof(PIDLTYPE)-sizeof(FileStruct)-sizeof(char)-sizeof(StatStruct))
76 #define LPSTATSTRUCT_FROM_LPSHITEMID(s) \
77 (((s) && (((LPSHITEMID)s)->cb > SHITEMID_LEN_FROM_NAME_LEN(0))) ? \
78 ((StatStruct*)(((LPBYTE)s)+((LPSHITEMID)s)->cb-sizeof(StatStruct))) : \
81 #define PATHMODE_UNIX 0
82 #define PATHMODE_DOS 1
84 /* ShellFolder attributes of the root folder ('/') */
85 static DWORD dwRootAttr = 0;
87 /* This structure is appended to shell32's FileStruct type in IDLs to store unix
88 * filesystem specific informationen extracted with the stat system call.
90 typedef struct tagStatStruct {
97 /* UnixFolder object layout and typedef.
99 typedef struct _UnixFolder {
100 const IShellFolder2Vtbl *lpIShellFolder2Vtbl;
101 const IPersistFolder2Vtbl *lpIPersistFolder2Vtbl;
102 const ISFHelperVtbl *lpISFHelperVtbl;
105 LPITEMIDLIST m_pidlLocation;
106 LPITEMIDLIST *m_apidlSubDirs;
111 /******************************************************************************
112 * UNIXFS_is_rooted_at_desktop [Internal]
114 * Checks if the unixfs namespace extension is rooted at desktop level.
117 * TRUE, if unixfs is rooted at desktop level
120 BOOL UNIXFS_is_rooted_at_desktop(void) {
122 WCHAR *pwszCLSID_UnixDosFolder, wszRootedAtDesktop[69 + CHARS_IN_GUID] = {
123 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
124 'W','i','n','d','o','w','s','\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n','\\',
125 'E','x','p','l','o','r','e','r','\\','D','e','s','k','t','o','p','\\',
126 'N','a','m','e','S','p','a','c','e','\\',0 };
128 if (FAILED(StringFromCLSID(&CLSID_UnixDosFolder, &pwszCLSID_UnixDosFolder)))
131 lstrcatW(wszRootedAtDesktop, pwszCLSID_UnixDosFolder);
132 CoTaskMemFree(pwszCLSID_UnixDosFolder);
134 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, wszRootedAtDesktop, 0, KEY_READ, &hKey) != ERROR_SUCCESS)
141 /******************************************************************************
142 * UNIXFS_is_pidl_of_type [INTERNAL]
144 * Checks for the first SHITEMID of an ITEMIDLIST if it passes a filter.
147 * pIDL [I] The ITEMIDLIST to be checked.
148 * fFilter [I] Shell condition flags, which specify the filter.
151 * TRUE, if pIDL is accepted by fFilter
154 static inline BOOL UNIXFS_is_pidl_of_type(LPITEMIDLIST pIDL, SHCONTF fFilter) {
155 LPPIDLDATA pIDLData = _ILGetDataPointer(pIDL);
156 if (!(fFilter & SHCONTF_INCLUDEHIDDEN) && pIDLData &&
157 (pIDLData->u.file.uFileAttribs & FILE_ATTRIBUTE_HIDDEN))
161 if (_ILIsFolder(pIDL) && (fFilter & SHCONTF_FOLDERS)) return TRUE;
162 if (_ILIsValue(pIDL) && (fFilter & SHCONTF_NONFOLDERS)) return TRUE;
166 /******************************************************************************
167 * UNIXFS_is_dos_device [Internal]
169 * Determines if a unix directory corresponds to any dos device.
172 * statPath [I] The stat struct of the directory, as returned by stat(2).
175 * TRUE, if statPath corresponds to any dos drive letter
178 static BOOL UNIXFS_is_dos_device(const struct stat *statPath) {
179 struct stat statDrive;
182 WCHAR wszDosDevice[4] = { 'A', ':', '\\', 0 };
184 for (dwDriveMap = GetLogicalDrives(); dwDriveMap; dwDriveMap >>= 1, wszDosDevice[0]++) {
185 if (!(dwDriveMap & 0x1)) continue;
186 pszDrivePath = wine_get_unix_file_name(wszDosDevice);
187 if (pszDrivePath && !stat(pszDrivePath, &statDrive)) {
188 HeapFree(GetProcessHeap(), 0, pszDrivePath);
189 if ((statPath->st_dev == statDrive.st_dev) && (statPath->st_ino == statDrive.st_ino))
196 /******************************************************************************
197 * UNIXFS_get_unix_path [Internal]
199 * Convert an absolute dos path to an absolute canonicalized unix path.
200 * Evaluate "/.", "/.." and symbolic links.
203 * pszDosPath [I] An absolute dos path
204 * pszCanonicalPath [O] Buffer of length FILENAME_MAX. Will receive the canonical path.
208 * Failure, FALSE - Path not existent, too long, insufficient rights, to many symlinks
210 static BOOL UNIXFS_get_unix_path(LPCWSTR pszDosPath, char *pszCanonicalPath)
212 char *pPathTail, *pElement, *pCanonicalTail, szPath[FILENAME_MAX], *pszUnixPath;
213 struct stat fileStat;
215 TRACE("(pszDosPath=%s, pszCanonicalPath=%p)\n", debugstr_w(pszDosPath), pszCanonicalPath);
217 if (!pszDosPath || pszDosPath[1] != ':')
220 pszUnixPath = wine_get_unix_file_name(pszDosPath);
221 if (!pszUnixPath) return FALSE;
222 strcpy(szPath, pszUnixPath);
223 HeapFree(GetProcessHeap(), 0, pszUnixPath);
225 /* pCanonicalTail always points to the end of the canonical path constructed
226 * thus far. pPathTail points to the still to be processed part of the input
227 * path. pElement points to the path element currently investigated.
229 *pszCanonicalPath = '\0';
230 pCanonicalTail = pszCanonicalPath;
237 pElement = pPathTail;
238 pPathTail = strchr(pPathTail+1, '/');
239 if (!pPathTail) /* Last path element may not be terminated by '/'. */
240 pPathTail = pElement + strlen(pElement);
241 /* Temporarily terminate the current path element. Will be restored later. */
245 /* Skip "/." path elements */
246 if (!strcmp("/.", pElement)) {
251 /* Remove last element in canonical path for "/.." elements, then skip. */
252 if (!strcmp("/..", pElement)) {
253 char *pTemp = strrchr(pszCanonicalPath, '/');
255 pCanonicalTail = pTemp;
256 *pCanonicalTail = '\0';
261 /* lstat returns zero on success. */
262 if (lstat(szPath, &fileStat))
265 if (S_ISLNK(fileStat.st_mode)) {
266 char szSymlink[FILENAME_MAX];
267 int cLinkLen, cTailLen;
269 /* Avoid infinite loop for recursive links. */
273 cLinkLen = readlink(szPath, szSymlink, FILENAME_MAX);
278 cTailLen = strlen(pPathTail);
280 if (szSymlink[0] == '/') {
281 /* Absolute link. Copy to szPath, concat remaining path and start all over. */
282 if (cLinkLen + cTailLen + 1 > FILENAME_MAX)
285 /* Avoid double slashes. */
286 if (szSymlink[cLinkLen-1] == '/' && pPathTail[0] == '/') {
287 szSymlink[cLinkLen-1] = '\0';
291 memcpy(szSymlink + cLinkLen, pPathTail, cTailLen + 1);
292 memcpy(szPath, szSymlink, cLinkLen + cTailLen + 1);
293 *pszCanonicalPath = '\0';
294 pCanonicalTail = pszCanonicalPath;
297 /* Relative link. Expand into szPath and continue. */
298 char szTemp[FILENAME_MAX];
299 int cTailLen = strlen(pPathTail);
301 if (pElement - szPath + 1 + cLinkLen + cTailLen + 1 > FILENAME_MAX)
304 memcpy(szTemp, pPathTail, cTailLen + 1);
305 memcpy(pElement + 1, szSymlink, cLinkLen);
306 memcpy(pElement + 1 + cLinkLen, szTemp, cTailLen + 1);
307 pPathTail = pElement;
310 /* Regular directory or file. Copy to canonical path */
311 if (pCanonicalTail - pszCanonicalPath + pPathTail - pElement + 1 > FILENAME_MAX)
314 memcpy(pCanonicalTail, pElement, pPathTail - pElement + 1);
315 pCanonicalTail += pPathTail - pElement;
318 } while (pPathTail[0] == '/');
320 TRACE("--> %s\n", debugstr_a(pszCanonicalPath));
325 /******************************************************************************
326 * UNIXFS_build_shitemid [Internal]
328 * Constructs a new SHITEMID for the last component of path 'pszUnixPath' into
329 * buffer 'pIDL'. Decorates the SHITEMID with information from a stat system call.
332 * pszUnixPath [I] An absolute path. The SHITEMID will be build for the last component.
333 * bParentIsFS [I] Set, if the parent of the last path component is within the wine filesystem.
334 * pIDL [O] SHITEMID will be constructed here.
337 * Success: A pointer to the terminating '\0' character of path.
341 * Minimum size of pIDL is SHITEMID_LEN_FROM_NAME_LEN(strlen(last_component_of_path)).
342 * If what you need is a PIDLLIST with a single SHITEMID, don't forget to append
345 static char* UNIXFS_build_shitemid(char *pszUnixPath, BOOL bParentIsFS, void *pIDL) {
349 struct stat fileStat;
350 StatStruct *pStatStruct;
354 TRACE("(pszUnixPath=%s, pIDL=%p)\n", debugstr_a(pszUnixPath), pIDL);
356 /* Compute the SHITEMID's length and wipe it. */
357 pszComponent = strrchr(pszUnixPath, '/') + 1;
358 cComponentLen = strlen(pszComponent);
359 memset(pIDL, 0, SHITEMID_LEN_FROM_NAME_LEN(cComponentLen));
360 ((LPSHITEMID)pIDL)->cb = SHITEMID_LEN_FROM_NAME_LEN(cComponentLen) ;
362 /* We are only interested in regular files and directories. */
363 if (stat(pszUnixPath, &fileStat)) return NULL;
364 if (!S_ISDIR(fileStat.st_mode) && !S_ISREG(fileStat.st_mode)) return NULL;
366 pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pIDL);
367 pStatStruct->st_mode = fileStat.st_mode;
368 pStatStruct->st_uid = fileStat.st_uid;
369 pStatStruct->st_gid = fileStat.st_gid;
370 if (S_ISDIR(fileStat.st_mode))
371 pStatStruct->sfAttr = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR|SFGAO_CANRENAME;
373 pStatStruct->sfAttr = SFGAO_CANRENAME;
374 if (bParentIsFS || UNIXFS_is_dos_device(&fileStat)) pStatStruct->sfAttr |= SFGAO_FILESYSTEM;
376 /* Set shell32's standard SHITEMID data fields. */
377 pIDLData = _ILGetDataPointer((LPCITEMIDLIST)pIDL);
378 pIDLData->type = S_ISDIR(fileStat.st_mode) ? PT_FOLDER : PT_VALUE;
379 pIDLData->u.file.dwFileSize = (DWORD)fileStat.st_size;
380 RtlSecondsSince1970ToTime( fileStat.st_mtime, &time );
381 fileTime.dwLowDateTime = time.u.LowPart;
382 fileTime.dwHighDateTime = time.u.HighPart;
383 FileTimeToDosDateTime(&fileTime, &pIDLData->u.file.uFileDate, &pIDLData->u.file.uFileTime);
384 pIDLData->u.file.uFileAttribs = 0;
385 if (S_ISDIR(fileStat.st_mode)) pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_DIRECTORY;
386 if (pszComponent[0] == '.') pIDLData->u.file.uFileAttribs |= FILE_ATTRIBUTE_HIDDEN;
387 memcpy(pIDLData->u.file.szNames, pszComponent, cComponentLen);
389 return pszComponent + cComponentLen;
392 /******************************************************************************
393 * UNIXFS_path_to_pidl [Internal]
396 * pUnixFolder [I] If path is relative, pUnixFolder represents the base path
397 * path [I] An absolute unix or dos path or a path relativ to pUnixFolder
398 * ppidl [O] The corresponding ITEMIDLIST. Release with SHFree/ILFree
402 * Failure: FALSE, invalid params or out of memory
405 * pUnixFolder also carries the information if the path is expected to be unix or dos.
407 static BOOL UNIXFS_path_to_pidl(UnixFolder *pUnixFolder, const WCHAR *path, LPITEMIDLIST *ppidl) {
409 int cSubDirs, cPidlLen, cPathLen;
410 char *pSlash, szCompletePath[FILENAME_MAX], *pNextPathElement;
411 BOOL bParentIsFS = dwRootAttr & SFGAO_FILESYSTEM;
413 TRACE("pUnixFolder=%p, path=%s, ppidl=%p\n", pUnixFolder, debugstr_w(path), ppidl);
418 /* Build an absolute path and let pNextPathElement point to the interesting
419 * relative sub-path. We need the absolute path to call 'stat', but the pidl
420 * will only contain the relative part.
422 if ((pUnixFolder->m_dwPathMode == PATHMODE_DOS) && (path[1] == ':'))
424 /* Absolute dos path. Convert to unix */
425 if (!UNIXFS_get_unix_path(path, szCompletePath))
427 pNextPathElement = szCompletePath;
429 else if ((pUnixFolder->m_dwPathMode == PATHMODE_UNIX) && (path[0] == '/'))
431 /* Absolute unix path. Just convert to ANSI. */
432 WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath, FILENAME_MAX, NULL, NULL);
433 pNextPathElement = szCompletePath;
437 /* Relative dos or unix path. Concat with this folder's path */
438 int cBasePathLen = strlen(pUnixFolder->m_pszPath);
439 memcpy(szCompletePath, pUnixFolder->m_pszPath, cBasePathLen);
440 WideCharToMultiByte(CP_ACP, 0, path, -1, szCompletePath + cBasePathLen,
441 FILENAME_MAX - cBasePathLen, NULL, NULL);
442 pNextPathElement = szCompletePath + cBasePathLen - 1;
444 /* If in dos mode, replace '\' with '/' */
445 if (pUnixFolder->m_dwPathMode == PATHMODE_DOS) {
446 char *pBackslash = strchr(pNextPathElement, '\\');
449 pBackslash = strchr(pBackslash, '\\');
454 /* Special case for the root folder. */
455 if (!strcmp(szCompletePath, "/")) {
456 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(sizeof(USHORT));
457 if (!pidl) return FALSE;
458 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
462 /* Remove trailing slash, if present */
463 cPathLen = strlen(szCompletePath);
464 if (szCompletePath[cPathLen-1] == '/')
465 szCompletePath[cPathLen-1] = '\0';
467 if ((szCompletePath[0] != '/') || (pNextPathElement[0] != '/')) {
468 ERR("szCompletePath: %s, pNextPathElment: %s\n", szCompletePath, pNextPathElement);
472 /* At this point, we have an absolute unix path in szCompletePath
473 * and the relative portion of it in pNextPathElement. Both starting with '/'
474 * and _not_ terminated by a '/'. */
475 TRACE("complete path: %s, relative path: %s\n", szCompletePath, pNextPathElement);
477 /* Count the number of sub-directories in the path */
479 pSlash = pNextPathElement;
482 pSlash = strchr(pSlash+1, '/');
485 /* Allocate enough memory to hold the path. The -cSubDirs is for the '/'
486 * characters, which are not stored in the ITEMIDLIST. */
487 cPidlLen = strlen(pNextPathElement) - cSubDirs + cSubDirs * SHITEMID_LEN_FROM_NAME_LEN(0) + sizeof(USHORT);
488 *ppidl = pidl = (LPITEMIDLIST)SHAlloc(cPidlLen);
489 if (!pidl) return FALSE;
491 /* Concatenate the SHITEMIDs of the sub-directories. */
492 while (*pNextPathElement) {
493 pSlash = strchr(pNextPathElement+1, '/');
494 if (pSlash) *pSlash = '\0';
495 pNextPathElement = UNIXFS_build_shitemid(szCompletePath, bParentIsFS, pidl);
496 if (pSlash) *pSlash = '/';
498 if (!pNextPathElement) {
502 if (!bParentIsFS && (LPSTATSTRUCT_FROM_LPSHITEMID(pidl)->sfAttr & SFGAO_FILESYSTEM))
504 pidl = ILGetNext(pidl);
506 pidl->mkid.cb = 0; /* Terminate the ITEMIDLIST */
508 if ((int)pidl-(int)*ppidl+sizeof(USHORT) != cPidlLen) /* We've corrupted the heap :( */
509 ERR("Computed length of pidl incorrect. Please report.\n");
514 /******************************************************************************
515 * UNIXFS_pidl_to_path [Internal]
517 * Construct the unix path that corresponds to a fully qualified ITEMIDLIST
520 * pidl [I] ITEMIDLIST that specifies the absolute location of the folder
521 * path [O] The corresponding unix path as a zero terminated ascii string
525 * Failure: FALSE, pidl doesn't specify a unix path or out of memory
527 static BOOL UNIXFS_pidl_to_path(LPCITEMIDLIST pidl, UnixFolder *pUnixFolder) {
528 LPCITEMIDLIST current = pidl, root;
530 char *pNextDir, szBasePath[FILENAME_MAX] = "/";
532 TRACE("(pidl=%p, pUnixFolder=%p)\n", pidl, pUnixFolder);
535 pUnixFolder->m_pszPath = NULL;
537 /* Find the UnixFolderClass root */
538 while (current->mkid.cb) {
539 if (_ILIsDrive(current) || /* The dos drive, which maps to '/' */
540 (_ILIsSpecialFolder(current) &&
541 (IsEqualIID(&CLSID_UnixFolder, _ILGetGUIDPointer(current)) ||
542 IsEqualIID(&CLSID_UnixDosFolder, _ILGetGUIDPointer(current)))))
546 current = ILGetNext(current);
549 if (current && current->mkid.cb) {
550 root = current = ILGetNext(current);
551 dwPathLen = 2; /* For the '/' prefix and the terminating '\0' */
552 } else if (_ILIsDesktop(pidl) || _ILIsValue(pidl) || _ILIsFolder(pidl)) {
553 /* Path rooted at Desktop */
554 WCHAR wszDesktopPath[MAX_PATH];
555 if (FAILED(SHGetSpecialFolderPathW(0, wszDesktopPath, CSIDL_DESKTOP, FALSE)))
557 if (!UNIXFS_get_unix_path(wszDesktopPath, szBasePath))
559 dwPathLen = strlen(szBasePath) + 1;
562 ERR("Unknown pidl type!\n");
567 /* Determine the path's length bytes */
568 while (current && current->mkid.cb) {
569 dwPathLen += NAME_LEN_FROM_LPSHITEMID(current) + 1; /* For the '/' */
570 current = ILGetNext(current);
574 pUnixFolder->m_pszPath = pNextDir = SHAlloc(dwPathLen);
575 if (!pUnixFolder->m_pszPath) {
576 WARN("SHAlloc failed!\n");
580 strcpy(pNextDir, szBasePath);
581 pNextDir += strlen(szBasePath);
582 while (current && current->mkid.cb) {
583 memcpy(pNextDir, _ILGetTextPointer(current), NAME_LEN_FROM_LPSHITEMID(current));
584 pNextDir += NAME_LEN_FROM_LPSHITEMID(current);
586 current = ILGetNext(current);
590 TRACE("--> %s\n", pUnixFolder->m_pszPath);
594 /******************************************************************************
595 * UNIXFS_build_subfolder_pidls [Internal]
597 * Builds an array of subfolder PIDLs relative to a unix directory
600 * path [I] Name of a unix directory as a zero terminated ascii string
601 * apidl [O] The array of PIDLs
602 * pCount [O] Size of apidl
606 * Failure: FALSE, path is not a valid unix directory or out of memory
609 * The array of PIDLs and each PIDL are allocated with SHAlloc. You'll have
610 * to release each PIDL as well as the array itself with SHFree.
612 static BOOL UNIXFS_build_subfolder_pidls(UnixFolder *pUnixFolder)
614 struct dirent *pDirEntry;
615 struct stat fileStat;
617 DWORD cDirEntries, i;
620 BOOL bParentIsFS = dwRootAttr & SFGAO_FILESYSTEM;
622 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
624 /* For updates: If there already is an initialized list, release it */
625 if (pUnixFolder->m_cSubDirs > 0) {
626 for (i=0; i < pUnixFolder->m_cSubDirs; i++)
627 SHFree(pUnixFolder->m_apidlSubDirs[i]);
628 SHFree(pUnixFolder->m_apidlSubDirs);
631 pUnixFolder->m_apidlSubDirs = NULL;
632 pUnixFolder->m_cSubDirs = 0;
634 if (pUnixFolder->m_pidlLocation) {
635 StatStruct *statStruct =
636 LPSTATSTRUCT_FROM_LPSHITEMID(ILFindLastID(pUnixFolder->m_pidlLocation));
637 if (statStruct) bParentIsFS = statStruct->sfAttr & SFGAO_FILESYSTEM;
640 dir = opendir(pUnixFolder->m_pszPath);
642 WARN("Failed to open directory '%s'.\n", pUnixFolder->m_pszPath);
646 /* Allocate space for fully qualified paths */
647 pszFQPath = SHAlloc(strlen(pUnixFolder->m_pszPath) + PATH_MAX);
649 WARN("SHAlloc failed!\n");
653 /* Count number of directory entries. */
654 for (cDirEntries = 0, pDirEntry = readdir(dir); pDirEntry; pDirEntry = readdir(dir)) {
655 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
656 sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
657 if (!stat(pszFQPath, &fileStat) && (S_ISDIR(fileStat.st_mode) || S_ISREG(fileStat.st_mode))) cDirEntries++;
660 /* If there are no entries, we are done. */
661 if (cDirEntries == 0) {
667 /* Allocate the array of PIDLs */
668 pUnixFolder->m_apidlSubDirs = SHAlloc(cDirEntries * sizeof(LPITEMIDLIST));
669 if (!pUnixFolder->m_apidlSubDirs) {
670 WARN("SHAlloc failed!\n");
674 /* Allocate and initialize one SHITEMID per sub-directory. */
675 for (rewinddir(dir), pDirEntry = readdir(dir), i = 0; pDirEntry; pDirEntry = readdir(dir)) {
678 if (!strcmp(pDirEntry->d_name, ".") || !strcmp(pDirEntry->d_name, "..")) continue;
680 sprintf(pszFQPath, "%s%s", pUnixFolder->m_pszPath, pDirEntry->d_name);
682 sLen = strlen(pDirEntry->d_name);
683 pid = (LPSHITEMID)SHAlloc(SHITEMID_LEN_FROM_NAME_LEN(sLen)+sizeof(USHORT));
685 WARN("SHAlloc failed!\n");
688 if (!UNIXFS_build_shitemid(pszFQPath, bParentIsFS, pid)) {
692 memset(((PBYTE)pid)+pid->cb, 0, sizeof(USHORT));
694 pUnixFolder->m_apidlSubDirs[i++] = (LPITEMIDLIST)pid;
697 pUnixFolder->m_cSubDirs = i;
704 /******************************************************************************
707 * Class whose heap based instances represent unix filesystem directories.
710 static void UnixFolder_Destroy(UnixFolder *pUnixFolder) {
713 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
715 if (pUnixFolder->m_apidlSubDirs)
716 for (i=0; i < pUnixFolder->m_cSubDirs; i++)
717 SHFree(pUnixFolder->m_apidlSubDirs[i]);
718 SHFree(pUnixFolder->m_apidlSubDirs);
719 SHFree(pUnixFolder->m_pszPath);
720 ILFree(pUnixFolder->m_pidlLocation);
724 static HRESULT WINAPI UnixFolder_IShellFolder2_QueryInterface(IShellFolder2 *iface, REFIID riid,
727 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
729 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
731 if (!ppv) return E_INVALIDARG;
733 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IShellFolder, riid) ||
734 IsEqualIID(&IID_IShellFolder2, riid))
736 *ppv = &This->lpIShellFolder2Vtbl;
737 } else if (IsEqualIID(&IID_IPersistFolder2, riid) || IsEqualIID(&IID_IPersistFolder, riid) ||
738 IsEqualIID(&IID_IPersist, riid))
740 *ppv = &This->lpIPersistFolder2Vtbl;
741 } else if (IsEqualIID(&IID_ISFHelper, riid)) {
742 *ppv = &This->lpISFHelperVtbl;
745 return E_NOINTERFACE;
748 IUnknown_AddRef((IUnknown*)*ppv);
752 static ULONG WINAPI UnixFolder_IShellFolder2_AddRef(IShellFolder2 *iface) {
753 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
755 TRACE("(iface=%p)\n", iface);
757 return InterlockedIncrement(&This->m_cRef);
760 static ULONG WINAPI UnixFolder_IShellFolder2_Release(IShellFolder2 *iface) {
761 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
764 TRACE("(iface=%p)\n", iface);
766 cRef = InterlockedDecrement(&This->m_cRef);
769 UnixFolder_Destroy(This);
774 static HRESULT WINAPI UnixFolder_IShellFolder2_ParseDisplayName(IShellFolder2* iface, HWND hwndOwner,
775 LPBC pbcReserved, LPOLESTR lpszDisplayName, ULONG* pchEaten, LPITEMIDLIST* ppidl,
776 ULONG* pdwAttributes)
778 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
781 TRACE("(iface=%p, hwndOwner=%p, pbcReserved=%p, lpszDisplayName=%s, pchEaten=%p, ppidl=%p, "
782 "pdwAttributes=%p) stub\n", iface, hwndOwner, pbcReserved, debugstr_w(lpszDisplayName),
783 pchEaten, ppidl, pdwAttributes);
785 result = UNIXFS_path_to_pidl(This, lpszDisplayName, ppidl);
786 if (result && pdwAttributes && *pdwAttributes)
788 IShellFolder *pParentSF;
789 LPCITEMIDLIST pidlLast;
792 hr = SHBindToParent(This->m_pidlLocation, &IID_IShellFolder, (LPVOID*)&pParentSF, &pidlLast);
793 if (FAILED(hr)) return E_FAIL;
794 IShellFolder_GetAttributesOf(pParentSF, 1, &pidlLast, pdwAttributes);
795 IShellFolder_Release(pParentSF);
798 if (!result) TRACE("FAILED!\n");
799 return result ? S_OK : E_FAIL;
802 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter);
804 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumObjects(IShellFolder2* iface, HWND hwndOwner,
805 SHCONTF grfFlags, IEnumIDList** ppEnumIDList)
807 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
808 IUnknown *newIterator;
811 TRACE("(iface=%p, hwndOwner=%p, grfFlags=%08lx, ppEnumIDList=%p)\n",
812 iface, hwndOwner, grfFlags, ppEnumIDList);
814 if (This->m_cSubDirs == -1)
815 UNIXFS_build_subfolder_pidls(This);
817 newIterator = UnixSubFolderIterator_Construct(This, grfFlags);
818 hr = IUnknown_QueryInterface(newIterator, &IID_IEnumIDList, (void**)ppEnumIDList);
819 IUnknown_Release(newIterator);
824 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToObject(IShellFolder2* iface, LPCITEMIDLIST pidl,
825 LPBC pbcReserved, REFIID riid, void** ppvOut)
827 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
828 IPersistFolder2 *persistFolder;
829 LPITEMIDLIST pidlSubFolder;
832 TRACE("(iface=%p, pidl=%p, pbcReserver=%p, riid=%p, ppvOut=%p)\n",
833 iface, pidl, pbcReserved, riid, ppvOut);
835 if (!pidl || !pidl->mkid.cb)
838 if (This->m_dwPathMode == PATHMODE_DOS)
839 hr = UnixDosFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
841 hr = UnixFolder_Constructor(NULL, &IID_IPersistFolder2, (void**)&persistFolder);
843 if (!SUCCEEDED(hr)) return hr;
844 hr = IPersistFolder_QueryInterface(persistFolder, riid, (void**)ppvOut);
846 pidlSubFolder = ILCombine(This->m_pidlLocation, pidl);
847 IPersistFolder2_Initialize(persistFolder, pidlSubFolder);
848 IPersistFolder2_Release(persistFolder);
849 ILFree(pidlSubFolder);
854 static HRESULT WINAPI UnixFolder_IShellFolder2_BindToStorage(IShellFolder2* This, LPCITEMIDLIST pidl,
855 LPBC pbcReserved, REFIID riid, void** ppvObj)
861 static HRESULT WINAPI UnixFolder_IShellFolder2_CompareIDs(IShellFolder2* iface, LPARAM lParam,
862 LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
864 BOOL isEmpty1, isEmpty2;
866 LPITEMIDLIST firstpidl;
870 TRACE("(iface=%p, lParam=%ld, pidl1=%p, pidl2=%p)\n", iface, lParam, pidl1, pidl2);
872 isEmpty1 = !pidl1 || !pidl1->mkid.cb;
873 isEmpty2 = !pidl2 || !pidl2->mkid.cb;
875 if (isEmpty1 && isEmpty2)
876 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, 0);
878 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
880 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
882 if (_ILIsFolder(pidl1) && !_ILIsFolder(pidl2))
883 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
884 if (!_ILIsFolder(pidl1) && _ILIsFolder(pidl2))
885 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
887 compare = CompareStringA(LOCALE_USER_DEFAULT, NORM_IGNORECASE,
888 _ILGetTextPointer(pidl1), NAME_LEN_FROM_LPSHITEMID(pidl1),
889 _ILGetTextPointer(pidl2), NAME_LEN_FROM_LPSHITEMID(pidl2));
891 if ((compare == CSTR_LESS_THAN) || (compare == CSTR_GREATER_THAN))
892 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)((compare == CSTR_LESS_THAN)?-1:1));
894 if (pidl1->mkid.cb < pidl2->mkid.cb)
895 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)-1);
896 else if (pidl1->mkid.cb > pidl2->mkid.cb)
897 return MAKE_HRESULT(SEVERITY_SUCCESS, 0, (WORD)1);
899 firstpidl = ILCloneFirst(pidl1);
900 pidl1 = ILGetNext(pidl1);
901 pidl2 = ILGetNext(pidl2);
903 hr = IShellFolder2_BindToObject(iface, firstpidl, NULL, &IID_IShellFolder, (LPVOID*)&psf);
905 hr = IShellFolder_CompareIDs(psf, lParam, pidl1, pidl2);
906 IShellFolder2_Release(psf);
913 static HRESULT WINAPI UnixFolder_IShellFolder2_CreateViewObject(IShellFolder2* iface, HWND hwndOwner,
914 REFIID riid, void** ppv)
916 HRESULT hr = E_INVALIDARG;
918 TRACE("(iface=%p, hwndOwner=%p, riid=%p, ppv=%p) stub\n", iface, hwndOwner, riid, ppv);
920 if (!ppv) return E_INVALIDARG;
923 if (IsEqualIID(&IID_IShellView, riid)) {
924 LPSHELLVIEW pShellView;
926 pShellView = IShellView_Constructor((IShellFolder*)iface);
928 hr = IShellView_QueryInterface(pShellView, riid, ppv);
929 IShellView_Release(pShellView);
936 static HRESULT WINAPI UnixFolder_IShellFolder2_GetAttributesOf(IShellFolder2* iface, UINT cidl,
937 LPCITEMIDLIST* apidl, SFGAOF* rgfInOut)
939 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
940 const StatStruct *pStatStruct;
942 TRACE("(iface=%p, cidl=%u, apidl=%p, rgfInOut=%p)\n", iface, cidl, apidl, rgfInOut);
944 if (!rgfInOut || (cidl && !apidl))
948 if (!strcmp(This->m_pszPath, "/")) {
949 *rgfInOut &= dwRootAttr;
951 pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(ILFindLastID(This->m_pidlLocation));
952 if (!pStatStruct) return E_FAIL;
953 *rgfInOut &= pStatStruct->sfAttr;
957 for (i=0; i<cidl; i++) {
958 pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(apidl[i]);
959 if (!pStatStruct) return E_INVALIDARG;
960 *rgfInOut &= pStatStruct->sfAttr;
967 static HRESULT WINAPI UnixFolder_IShellFolder2_GetUIObjectOf(IShellFolder2* iface, HWND hwndOwner,
968 UINT cidl, LPCITEMIDLIST* apidl, REFIID riid, UINT* prgfInOut, void** ppvOut)
970 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
972 TRACE("(iface=%p, hwndOwner=%p, cidl=%d, apidl=%p, riid=%s, prgfInOut=%p, ppv=%p)\n",
973 iface, hwndOwner, cidl, apidl, debugstr_guid(riid), prgfInOut, ppvOut);
975 if (IsEqualIID(&IID_IContextMenu, riid)) {
976 *ppvOut = ISvItemCm_Constructor((IShellFolder*)iface, This->m_pidlLocation, apidl, cidl);
978 } else if (IsEqualIID(&IID_IDataObject, riid)) {
979 *ppvOut = IDataObject_Constructor(hwndOwner, This->m_pidlLocation, apidl, cidl);
981 } else if (IsEqualIID(&IID_IExtractIconA, riid)) {
983 if (cidl != 1) return E_FAIL;
984 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
985 *ppvOut = (LPVOID)IExtractIconA_Constructor(pidl);
988 } else if (IsEqualIID(&IID_IExtractIconW, riid)) {
990 if (cidl != 1) return E_FAIL;
991 pidl = ILCombine(This->m_pidlLocation, apidl[0]);
992 *ppvOut = (LPVOID)IExtractIconW_Constructor(pidl);
995 } else if (IsEqualIID(&IID_IDropTarget, riid)) {
996 FIXME("IDropTarget\n");
998 } else if (IsEqualIID(&IID_IShellLinkW, riid)) {
999 FIXME("IShellLinkW\n");
1001 } else if (IsEqualIID(&IID_IShellLinkA, riid)) {
1002 FIXME("IShellLinkA\n");
1005 FIXME("Unknown interface %s in GetUIObjectOf\n", debugstr_guid(riid));
1006 return E_NOINTERFACE;
1010 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDisplayNameOf(IShellFolder2* iface,
1011 LPCITEMIDLIST pidl, SHGDNF uFlags, STRRET* lpName)
1013 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1016 TRACE("(iface=%p, pidl=%p, uFlags=%lx, lpName=%p)\n", iface, pidl, uFlags, lpName);
1018 if ((GET_SHGDN_FOR(uFlags) & SHGDN_FORPARSING) &&
1019 (GET_SHGDN_RELATION(uFlags) != SHGDN_INFOLDER))
1021 if (!pidl->mkid.cb) {
1022 lpName->uType = STRRET_CSTR;
1023 strcpy(lpName->u.cStr, This->m_pszPath);
1024 if (This->m_dwPathMode == PATHMODE_DOS) {
1025 char path[MAX_PATH];
1026 GetFullPathNameA(lpName->u.cStr, MAX_PATH, path, NULL);
1027 PathRemoveBackslashA(path);
1028 strcpy(lpName->u.cStr, path);
1031 IShellFolder *pSubFolder;
1032 USHORT emptyIDL = 0;
1034 hr = IShellFolder_BindToObject(iface, pidl, NULL, &IID_IShellFolder, (void**)&pSubFolder);
1035 if (!SUCCEEDED(hr)) return hr;
1037 hr = IShellFolder_GetDisplayNameOf(pSubFolder, (LPITEMIDLIST)&emptyIDL, uFlags, lpName);
1038 IShellFolder_Release(pSubFolder);
1041 char *pszFileName = _ILGetTextPointer(pidl);
1042 lpName->uType = STRRET_CSTR;
1043 strcpy(lpName->u.cStr, pszFileName ? pszFileName : "");
1046 TRACE("--> %s\n", lpName->u.cStr);
1051 static HRESULT WINAPI UnixFolder_IShellFolder2_SetNameOf(IShellFolder2* iface, HWND hwnd,
1052 LPCITEMIDLIST pidl, LPCOLESTR lpszName, SHGDNF uFlags, LPITEMIDLIST* ppidlOut)
1054 UnixFolder *This = ADJUST_THIS(UnixFolder, IShellFolder2, iface);
1056 char szSrc[FILENAME_MAX], szDest[FILENAME_MAX];
1057 int cBasePathLen = lstrlenA(This->m_pszPath);
1058 struct stat statDest;
1059 LPITEMIDLIST pidlNew;
1061 TRACE("(iface=%p, hwnd=%p, pidl=%p, lpszName=%s, uFlags=0x%08lx, ppidlOut=%p)\n",
1062 iface, hwnd, pidl, debugstr_w(lpszName), uFlags, ppidlOut);
1064 /* pidl has to contain a single non-empty SHITEMID */
1065 if (_ILIsDesktop(pidl) || !_ILIsPidlSimple(pidl) || !_ILGetTextPointer(pidl))
1066 return E_INVALIDARG;
1071 /* build source path */
1072 memcpy(szSrc, This->m_pszPath, cBasePathLen);
1073 lstrcpyA(szSrc+cBasePathLen, _ILGetTextPointer(pidl));
1075 /* build destination path */
1076 if (uFlags & SHGDN_FORPARSING) { /* absolute path in lpszName */
1077 WideCharToMultiByte(CP_ACP, 0, lpszName, -1, szDest, FILENAME_MAX, NULL, NULL);
1079 memcpy(szDest, This->m_pszPath, cBasePathLen);
1080 WideCharToMultiByte(CP_ACP, 0, lpszName, -1, szDest+cBasePathLen,
1081 FILENAME_MAX-cBasePathLen, NULL, NULL);
1084 TRACE("src=%s dest=%s\n", szSrc, szDest);
1086 /* Fail, if destination does already exist */
1087 if (!stat(szDest, &statDest))
1090 /* Rename the file and inform the shell */
1091 if (!rename(szSrc, szDest) && UNIXFS_path_to_pidl(This, lpszName, &pidlNew)) {
1092 LPITEMIDLIST pidlSrc = ILCombine(This->m_pidlLocation, pidl);
1093 LPITEMIDLIST pidlDest = ILCombine(This->m_pidlLocation, pidlNew);
1094 UNIXFS_build_subfolder_pidls(This); /* Update list of children */
1095 if (_ILIsFolder(pidlNew))
1096 SHChangeNotify(SHCNE_RENAMEFOLDER, SHCNF_IDLIST, pidlSrc, pidlDest);
1098 SHChangeNotify(SHCNE_RENAMEITEM, SHCNF_IDLIST, pidlSrc, pidlDest);
1102 *ppidlOut = pidlNew;
1110 static HRESULT WINAPI UnixFolder_IShellFolder2_EnumSearches(IShellFolder2* iface,
1111 IEnumExtraSearch **ppEnum)
1117 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumn(IShellFolder2* iface,
1118 DWORD dwReserved, ULONG *pSort, ULONG *pDisplay)
1124 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultColumnState(IShellFolder2* iface,
1125 UINT iColumn, SHCOLSTATEF *pcsFlags)
1131 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDefaultSearchGUID(IShellFolder2* iface,
1138 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsEx(IShellFolder2* iface,
1139 LPCITEMIDLIST pidl, const SHCOLUMNID *pscid, VARIANT *pv)
1145 #define SHELLVIEWCOLUMNS 6
1147 static HRESULT WINAPI UnixFolder_IShellFolder2_GetDetailsOf(IShellFolder2* iface,
1148 LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
1150 HRESULT hr = E_FAIL;
1151 struct passwd *pPasswd;
1152 struct group *pGroup;
1153 static const shvheader SFHeader[SHELLVIEWCOLUMNS] = {
1154 {IDS_SHV_COLUMN1, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 15},
1155 {IDS_SHV_COLUMN5, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10},
1156 {IDS_SHV_COLUMN10, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1157 {IDS_SHV_COLUMN11, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 7},
1158 {IDS_SHV_COLUMN2, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 8},
1159 {IDS_SHV_COLUMN4, SHCOLSTATE_TYPE_DATE | SHCOLSTATE_ONBYDEFAULT, LVCFMT_RIGHT, 10}
1162 TRACE("(iface=%p, pidl=%p, iColumn=%d, psd=%p) stub\n", iface, pidl, iColumn, psd);
1164 if (!psd || iColumn >= SHELLVIEWCOLUMNS)
1165 return E_INVALIDARG;
1168 psd->fmt = SFHeader[iColumn].fmt;
1169 psd->cxChar = SFHeader[iColumn].cxChar;
1170 psd->str.uType = STRRET_CSTR;
1171 LoadStringA(shell32_hInstance, SFHeader[iColumn].colnameid, psd->str.u.cStr, MAX_PATH);
1174 StatStruct *pStatStruct = LPSTATSTRUCT_FROM_LPSHITEMID(pidl);
1175 psd->str.u.cStr[0] = '\0';
1176 psd->str.uType = STRRET_CSTR;
1179 hr = IShellFolder2_GetDisplayNameOf(iface, pidl, SHGDN_NORMAL|SHGDN_INFOLDER, &psd->str);
1182 psd->str.u.cStr[0] = S_ISDIR(pStatStruct->st_mode) ? 'd' : '-';
1183 psd->str.u.cStr[1] = (pStatStruct->st_mode & S_IRUSR) ? 'r' : '-';
1184 psd->str.u.cStr[2] = (pStatStruct->st_mode & S_IWUSR) ? 'w' : '-';
1185 psd->str.u.cStr[3] = (pStatStruct->st_mode & S_IXUSR) ? 'x' : '-';
1186 psd->str.u.cStr[4] = (pStatStruct->st_mode & S_IRGRP) ? 'r' : '-';
1187 psd->str.u.cStr[5] = (pStatStruct->st_mode & S_IWGRP) ? 'w' : '-';
1188 psd->str.u.cStr[6] = (pStatStruct->st_mode & S_IXGRP) ? 'x' : '-';
1189 psd->str.u.cStr[7] = (pStatStruct->st_mode & S_IROTH) ? 'r' : '-';
1190 psd->str.u.cStr[8] = (pStatStruct->st_mode & S_IWOTH) ? 'w' : '-';
1191 psd->str.u.cStr[9] = (pStatStruct->st_mode & S_IXOTH) ? 'x' : '-';
1192 psd->str.u.cStr[10] = '\0';
1195 pPasswd = getpwuid(pStatStruct->st_uid);
1196 if (pPasswd) strcpy(psd->str.u.cStr, pPasswd->pw_name);
1199 pGroup = getgrgid(pStatStruct->st_gid);
1200 if (pGroup) strcpy(psd->str.u.cStr, pGroup->gr_name);
1203 _ILGetFileSize(pidl, psd->str.u.cStr, MAX_PATH);
1206 _ILGetFileDate(pidl, psd->str.u.cStr, MAX_PATH);
1214 static HRESULT WINAPI UnixFolder_IShellFolder2_MapColumnToSCID(IShellFolder2* iface, UINT iColumn,
1221 /* VTable for UnixFolder's IShellFolder2 interface.
1223 static const IShellFolder2Vtbl UnixFolder_IShellFolder2_Vtbl = {
1224 UnixFolder_IShellFolder2_QueryInterface,
1225 UnixFolder_IShellFolder2_AddRef,
1226 UnixFolder_IShellFolder2_Release,
1227 UnixFolder_IShellFolder2_ParseDisplayName,
1228 UnixFolder_IShellFolder2_EnumObjects,
1229 UnixFolder_IShellFolder2_BindToObject,
1230 UnixFolder_IShellFolder2_BindToStorage,
1231 UnixFolder_IShellFolder2_CompareIDs,
1232 UnixFolder_IShellFolder2_CreateViewObject,
1233 UnixFolder_IShellFolder2_GetAttributesOf,
1234 UnixFolder_IShellFolder2_GetUIObjectOf,
1235 UnixFolder_IShellFolder2_GetDisplayNameOf,
1236 UnixFolder_IShellFolder2_SetNameOf,
1237 UnixFolder_IShellFolder2_GetDefaultSearchGUID,
1238 UnixFolder_IShellFolder2_EnumSearches,
1239 UnixFolder_IShellFolder2_GetDefaultColumn,
1240 UnixFolder_IShellFolder2_GetDefaultColumnState,
1241 UnixFolder_IShellFolder2_GetDetailsEx,
1242 UnixFolder_IShellFolder2_GetDetailsOf,
1243 UnixFolder_IShellFolder2_MapColumnToSCID
1246 static HRESULT WINAPI UnixFolder_IPersistFolder2_QueryInterface(IPersistFolder2* This, REFIID riid,
1249 return UnixFolder_IShellFolder2_QueryInterface(
1250 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This), riid, ppvObject);
1253 static ULONG WINAPI UnixFolder_IPersistFolder2_AddRef(IPersistFolder2* This)
1255 return UnixFolder_IShellFolder2_AddRef(
1256 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1259 static ULONG WINAPI UnixFolder_IPersistFolder2_Release(IPersistFolder2* This)
1261 return UnixFolder_IShellFolder2_Release(
1262 (IShellFolder2*)ADJUST_THIS(UnixFolder, IPersistFolder2, This));
1265 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetClassID(IPersistFolder2* This, CLSID* pClassID)
1271 static HRESULT WINAPI UnixFolder_IPersistFolder2_Initialize(IPersistFolder2* iface, LPCITEMIDLIST pidl)
1273 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1275 TRACE("(iface=%p, pidl=%p)\n", iface, pidl);
1277 This->m_pidlLocation = ILClone(pidl);
1281 if (!UNIXFS_pidl_to_path(pidl, This))
1284 if (!strcmp(This->m_pszPath, "/")) {
1285 struct stat statRoot;
1286 if (stat(This->m_pszPath, &statRoot)) return E_FAIL;
1287 dwRootAttr = SFGAO_FOLDER|SFGAO_HASSUBFOLDER|SFGAO_FILESYSANCESTOR;
1288 if (UNIXFS_is_dos_device(&statRoot)) dwRootAttr |= SFGAO_FILESYSTEM;
1294 static HRESULT WINAPI UnixFolder_IPersistFolder2_GetCurFolder(IPersistFolder2* iface, LPITEMIDLIST* ppidl)
1296 UnixFolder *This = ADJUST_THIS(UnixFolder, IPersistFolder2, iface);
1298 TRACE ("(iface=%p, ppidl=%p)\n", iface, ppidl);
1302 *ppidl = ILClone (This->m_pidlLocation);
1306 /* VTable for UnixFolder's IPersistFolder interface.
1308 static const IPersistFolder2Vtbl UnixFolder_IPersistFolder2_Vtbl = {
1309 UnixFolder_IPersistFolder2_QueryInterface,
1310 UnixFolder_IPersistFolder2_AddRef,
1311 UnixFolder_IPersistFolder2_Release,
1312 UnixFolder_IPersistFolder2_GetClassID,
1313 UnixFolder_IPersistFolder2_Initialize,
1314 UnixFolder_IPersistFolder2_GetCurFolder
1317 static HRESULT WINAPI UnixFolder_ISFHelper_QueryInterface(ISFHelper* iface, REFIID riid,
1320 return UnixFolder_IShellFolder2_QueryInterface(
1321 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface), riid, ppvObject);
1324 static ULONG WINAPI UnixFolder_ISFHelper_AddRef(ISFHelper* iface)
1326 return UnixFolder_IShellFolder2_AddRef(
1327 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1330 static ULONG WINAPI UnixFolder_ISFHelper_Release(ISFHelper* iface)
1332 return UnixFolder_IShellFolder2_Release(
1333 (IShellFolder2*)ADJUST_THIS(UnixFolder, ISFHelper, iface));
1336 static HRESULT WINAPI UnixFolder_ISFHelper_GetUniqueName(ISFHelper* iface, LPSTR lpName, UINT uLen)
1338 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1341 LPITEMIDLIST pidlElem;
1344 static const char szNewFolder[] = "New Folder";
1346 TRACE("(iface=%p, lpName=%p, uLen=%u)\n", iface, lpName, uLen);
1348 if (uLen < sizeof(szNewFolder)+3)
1349 return E_INVALIDARG;
1351 hr = IShellFolder2_EnumObjects(STATIC_CAST(IShellFolder2, This), 0,
1352 SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN, &pEnum);
1353 if (SUCCEEDED(hr)) {
1354 lstrcpyA(lpName, szNewFolder);
1355 IEnumIDList_Reset(pEnum);
1357 while ((IEnumIDList_Next(pEnum, 1, &pidlElem, &dwFetched) == S_OK) && (dwFetched == 1)) {
1358 if (!strcasecmp(_ILGetTextPointer(pidlElem), lpName)) {
1359 IEnumIDList_Reset(pEnum);
1360 sprintf(lpName, "%s %d", szNewFolder, i++);
1367 IEnumIDList_Release(pEnum);
1372 static HRESULT WINAPI UnixFolder_ISFHelper_AddFolder(ISFHelper* iface, HWND hwnd, LPCSTR pszName,
1373 LPITEMIDLIST* ppidlOut)
1375 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1376 char szNewDir[FILENAME_MAX];
1378 TRACE("(iface=%p, hwnd=%p, pszName=%s, ppidlOut=%p)\n", iface, hwnd, pszName, ppidlOut);
1383 lstrcpyA(szNewDir, This->m_pszPath);
1384 lstrcatA(szNewDir, pszName);
1386 if (mkdir(szNewDir, 0755)) {
1387 char szMessage[256 + FILENAME_MAX];
1388 char szCaption[256];
1390 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_DENIED, szCaption, sizeof(szCaption));
1391 sprintf(szMessage, szCaption, szNewDir);
1392 LoadStringA(shell32_hInstance, IDS_CREATEFOLDER_CAPTION, szCaption, sizeof(szCaption));
1393 MessageBoxA(hwnd, szMessage, szCaption, MB_OK | MB_ICONEXCLAMATION);
1397 LPITEMIDLIST pidlRelative;
1398 WCHAR wszName[MAX_PATH];
1400 /* Update the folder's children */
1401 UNIXFS_build_subfolder_pidls(This);
1403 /* Inform the shell */
1404 MultiByteToWideChar(CP_ACP, 0, pszName, -1, wszName, MAX_PATH);
1405 if (UNIXFS_path_to_pidl(This, wszName, &pidlRelative)) {
1406 LPITEMIDLIST pidlAbsolute = ILCombine(This->m_pidlLocation, pidlRelative);
1408 *ppidlOut = pidlRelative;
1410 ILFree(pidlRelative);
1411 SHChangeNotify(SHCNE_MKDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1412 ILFree(pidlAbsolute);
1418 static HRESULT WINAPI UnixFolder_ISFHelper_DeleteItems(ISFHelper* iface, UINT cidl,
1419 LPCITEMIDLIST* apidl)
1421 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1422 char szAbsolute[FILENAME_MAX], *pszRelative;
1423 LPITEMIDLIST pidlAbsolute;
1427 TRACE("(iface=%p, cidl=%d, apidl=%p)\n", iface, cidl, apidl);
1429 lstrcpyA(szAbsolute, This->m_pszPath);
1430 pszRelative = szAbsolute + lstrlenA(szAbsolute);
1432 for (i=0; i<cidl && SUCCEEDED(hr); i++) {
1433 lstrcpyA(pszRelative, _ILGetTextPointer(apidl[i]));
1434 pidlAbsolute = ILCombine(This->m_pidlLocation, apidl[i]);
1435 if (_ILIsFolder(apidl[i])) {
1436 if (rmdir(szAbsolute)) {
1439 SHChangeNotify(SHCNE_RMDIR, SHCNF_IDLIST, pidlAbsolute, NULL);
1441 } else if (_ILIsValue(apidl[i])) {
1442 if (unlink(szAbsolute)) {
1445 SHChangeNotify(SHCNE_DELETE, SHCNF_IDLIST, pidlAbsolute, NULL);
1448 ILFree(pidlAbsolute);
1450 UNIXFS_build_subfolder_pidls(This);
1455 static HRESULT WINAPI UnixFolder_ISFHelper_CopyItems(ISFHelper* iface, IShellFolder *psfFrom,
1456 UINT cidl, LPCITEMIDLIST *apidl)
1458 UnixFolder *This = ADJUST_THIS(UnixFolder, ISFHelper, iface);
1462 char szAbsoluteDst[FILENAME_MAX], *pszRelativeDst;
1464 TRACE("(iface=%p, psfFrom=%p, cidl=%d, apidl=%p): semi-stub\n", iface, psfFrom, cidl, apidl);
1466 if (!psfFrom || !cidl || !apidl)
1467 return E_INVALIDARG;
1469 /* All source items have to be filesystem items. */
1470 dwAttributes = SFGAO_FILESYSTEM;
1471 hr = IShellFolder_GetAttributesOf(psfFrom, cidl, apidl, &dwAttributes);
1472 if (FAILED(hr) || !(dwAttributes & SFGAO_FILESYSTEM))
1473 return E_INVALIDARG;
1475 lstrcpyA(szAbsoluteDst, This->m_pszPath);
1476 pszRelativeDst = szAbsoluteDst + strlen(szAbsoluteDst);
1478 for (i=0; i<cidl; i++) {
1479 WCHAR wszSrc[MAX_PATH];
1480 char szSrc[FILENAME_MAX];
1483 /* Build the unix path of the current source item. */
1484 if (FAILED(IShellFolder_GetDisplayNameOf(psfFrom, apidl[i], SHGDN_FORPARSING, &strret)))
1486 if (FAILED(StrRetToBufW(&strret, apidl[i], wszSrc, MAX_PATH)))
1488 if (!UNIXFS_get_unix_path(wszSrc, szSrc))
1491 /* Build the unix path of the current destination item */
1492 lstrcpyA(pszRelativeDst, _ILGetTextPointer(apidl[i]));
1494 FIXME("Would copy %s to %s. Not yet implemented.\n", szSrc, szAbsoluteDst);
1499 /* VTable for UnixFolder's ISFHelper interface
1501 static const ISFHelperVtbl UnixFolder_ISFHelper_Vtbl = {
1502 UnixFolder_ISFHelper_QueryInterface,
1503 UnixFolder_ISFHelper_AddRef,
1504 UnixFolder_ISFHelper_Release,
1505 UnixFolder_ISFHelper_GetUniqueName,
1506 UnixFolder_ISFHelper_AddFolder,
1507 UnixFolder_ISFHelper_DeleteItems,
1508 UnixFolder_ISFHelper_CopyItems
1511 /******************************************************************************
1512 * Unix[Dos]Folder_Constructor [Internal]
1515 * pUnkOuter [I] Outer class for aggregation. Currently ignored.
1516 * riid [I] Interface asked for by the client.
1517 * ppv [O] Pointer to an riid interface to the UnixFolder object.
1520 * Those are the only functions exported from shfldr_unixfs.c. They are called from
1521 * shellole.c's default class factory and thus have to exhibit a LPFNCREATEINSTANCE
1522 * compatible signature.
1524 * The UnixDosFolder_Constructor sets the dwPathMode member to PATHMODE_DOS. This
1525 * means that paths are converted from dos to unix and back at the interfaces.
1527 static HRESULT CreateUnixFolder(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv, DWORD dwPathMode) {
1528 HRESULT hr = E_FAIL;
1529 UnixFolder *pUnixFolder = SHAlloc((ULONG)sizeof(UnixFolder));
1532 pUnixFolder->lpIShellFolder2Vtbl = &UnixFolder_IShellFolder2_Vtbl;
1533 pUnixFolder->lpIPersistFolder2Vtbl = &UnixFolder_IPersistFolder2_Vtbl;
1534 pUnixFolder->lpISFHelperVtbl = &UnixFolder_ISFHelper_Vtbl;
1535 pUnixFolder->m_cRef = 0;
1536 pUnixFolder->m_pszPath = NULL;
1537 pUnixFolder->m_apidlSubDirs = NULL;
1538 pUnixFolder->m_cSubDirs = -1;
1539 pUnixFolder->m_dwPathMode = dwPathMode;
1541 UnixFolder_IShellFolder2_AddRef(STATIC_CAST(IShellFolder2, pUnixFolder));
1542 hr = UnixFolder_IShellFolder2_QueryInterface(STATIC_CAST(IShellFolder2, pUnixFolder), riid, ppv);
1543 UnixFolder_IShellFolder2_Release(STATIC_CAST(IShellFolder2, pUnixFolder));
1548 HRESULT WINAPI UnixFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1549 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1550 return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_UNIX);
1553 HRESULT WINAPI UnixDosFolder_Constructor(IUnknown *pUnkOuter, REFIID riid, LPVOID *ppv) {
1554 TRACE("(pUnkOuter=%p, riid=%p, ppv=%p)\n", pUnkOuter, riid, ppv);
1555 return CreateUnixFolder(pUnkOuter, riid, ppv, PATHMODE_DOS);
1558 /******************************************************************************
1559 * UnixSubFolderIterator
1561 * Class whose heap based objects represent iterators over the sub-directories
1562 * of a given UnixFolder object.
1565 /* UnixSubFolderIterator object layout and typedef.
1567 typedef struct _UnixSubFolderIterator {
1568 const IEnumIDListVtbl *lpIEnumIDListVtbl;
1570 UnixFolder *m_pUnixFolder;
1573 } UnixSubFolderIterator;
1575 static void UnixSubFolderIterator_Destroy(UnixSubFolderIterator *iterator) {
1576 TRACE("(iterator=%p)\n", iterator);
1578 UnixFolder_IShellFolder2_Release((IShellFolder2*)iterator->m_pUnixFolder);
1582 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_QueryInterface(IEnumIDList* iface,
1583 REFIID riid, void** ppv)
1585 TRACE("(iface=%p, riid=%p, ppv=%p)\n", iface, riid, ppv);
1587 if (!ppv) return E_INVALIDARG;
1589 if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IEnumIDList, riid)) {
1593 return E_NOINTERFACE;
1596 IEnumIDList_AddRef(iface);
1600 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_AddRef(IEnumIDList* iface)
1602 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1604 TRACE("(iface=%p)\n", iface);
1606 return InterlockedIncrement(&This->m_cRef);
1609 static ULONG WINAPI UnixSubFolderIterator_IEnumIDList_Release(IEnumIDList* iface)
1611 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1614 TRACE("(iface=%p)\n", iface);
1616 cRef = InterlockedDecrement(&This->m_cRef);
1619 UnixSubFolderIterator_Destroy(This);
1624 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Next(IEnumIDList* iface, ULONG celt,
1625 LPITEMIDLIST* rgelt, ULONG* pceltFetched)
1627 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1630 TRACE("(iface=%p, celt=%ld, rgelt=%p, pceltFetched=%p)\n", iface, celt, rgelt, pceltFetched);
1632 for (i=0; (i < celt) && (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs); This->m_cIdx++) {
1633 LPITEMIDLIST pCurrent = This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx];
1634 if (UNIXFS_is_pidl_of_type(pCurrent, This->m_fFilter)) {
1635 rgelt[i] = ILClone(pCurrent);
1643 return (i == 0) ? S_FALSE : S_OK;
1646 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Skip(IEnumIDList* iface, ULONG celt)
1648 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1651 TRACE("(iface=%p, celt=%ld)\n", iface, celt);
1653 for (i=0; i < celt; i++) {
1654 while (This->m_cIdx < This->m_pUnixFolder->m_cSubDirs &&
1655 !UNIXFS_is_pidl_of_type(This->m_pUnixFolder->m_apidlSubDirs[This->m_cIdx], This->m_fFilter))
1662 if (This->m_cIdx > This->m_pUnixFolder->m_cSubDirs) {
1663 This->m_cIdx = This->m_pUnixFolder->m_cSubDirs;
1670 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Reset(IEnumIDList* iface)
1672 UnixSubFolderIterator *This = ADJUST_THIS(UnixSubFolderIterator, IEnumIDList, iface);
1674 TRACE("(iface=%p)\n", iface);
1681 static HRESULT WINAPI UnixSubFolderIterator_IEnumIDList_Clone(IEnumIDList* This,
1682 IEnumIDList** ppenum)
1688 /* VTable for UnixSubFolderIterator's IEnumIDList interface.
1690 static const IEnumIDListVtbl UnixSubFolderIterator_IEnumIDList_Vtbl = {
1691 UnixSubFolderIterator_IEnumIDList_QueryInterface,
1692 UnixSubFolderIterator_IEnumIDList_AddRef,
1693 UnixSubFolderIterator_IEnumIDList_Release,
1694 UnixSubFolderIterator_IEnumIDList_Next,
1695 UnixSubFolderIterator_IEnumIDList_Skip,
1696 UnixSubFolderIterator_IEnumIDList_Reset,
1697 UnixSubFolderIterator_IEnumIDList_Clone
1700 static IUnknown *UnixSubFolderIterator_Construct(UnixFolder *pUnixFolder, SHCONTF fFilter) {
1701 UnixSubFolderIterator *iterator;
1703 TRACE("(pUnixFolder=%p)\n", pUnixFolder);
1705 iterator = SHAlloc((ULONG)sizeof(UnixSubFolderIterator));
1706 iterator->lpIEnumIDListVtbl = &UnixSubFolderIterator_IEnumIDList_Vtbl;
1707 iterator->m_cRef = 0;
1708 iterator->m_cIdx = 0;
1709 iterator->m_pUnixFolder = pUnixFolder;
1710 iterator->m_fFilter = fFilter;
1712 UnixSubFolderIterator_IEnumIDList_AddRef((IEnumIDList*)iterator);
1713 UnixFolder_IShellFolder2_AddRef((IShellFolder2*)pUnixFolder);
1715 return (IUnknown*)iterator;