ntdll: Use MAP_SHARED also for read-only mappings.
[wine.git] / dlls / shell32 / trash.c
blob6b3d1ca156449a8f296a53009cea55bef99c1a9f
1 /*
2 * The freedesktop.org Trash, implemented using the 0.7 spec version
3 * (see http://www.ramendik.ru/docs/trashspec.html)
5 * Copyright (C) 2006 Mikolaj Zalewski
6 * Copyright 2011 Jay Yang
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #ifdef HAVE_CORESERVICES_CORESERVICES_H
26 #define GetCurrentThread MacGetCurrentThread
27 #define LoadResource MacLoadResource
28 #include <CoreServices/CoreServices.h>
29 #undef GetCurrentThread
30 #undef LoadResource
31 #undef DPRINTF
32 #endif
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <errno.h>
38 #include <time.h>
39 #ifdef HAVE_SYS_STAT_H
40 # include <sys/stat.h>
41 #endif
42 #include <sys/types.h>
43 #include <stdlib.h>
44 #ifdef HAVE_UNISTD_H
45 # include <unistd.h>
46 #endif
47 #ifdef HAVE_DIRENT_H
48 # include <dirent.h>
49 #endif
51 #include "windef.h"
52 #include "winbase.h"
53 #include "winerror.h"
54 #include "winreg.h"
55 #include "shlwapi.h"
56 #include "winternl.h"
57 #include "wine/debug.h"
58 #include "shell32_main.h"
59 #include "xdg.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(trash);
64 * The item ID of a trashed element is built as follows:
65 * NUL byte - in most PIDLs the first byte is the type so we keep it constant
66 * WIN32_FIND_DATAW structure - with data about original file attributes
67 * bucket name - currently only an empty string meaning the home bucket is supported
68 * trash file name - a NUL-terminated string
70 static HRESULT TRASH_CreateSimplePIDL(LPCSTR filename, const WIN32_FIND_DATAW *data, LPITEMIDLIST *pidlOut)
72 LPITEMIDLIST pidl = SHAlloc(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1+2);
73 *pidlOut = NULL;
74 if (pidl == NULL)
75 return E_OUTOFMEMORY;
76 pidl->mkid.cb = (USHORT)(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1);
77 pidl->mkid.abID[0] = 0;
78 memcpy(pidl->mkid.abID+1, data, sizeof(WIN32_FIND_DATAW));
79 pidl->mkid.abID[1+sizeof(WIN32_FIND_DATAW)] = 0;
80 lstrcpyA((LPSTR)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1), filename);
81 *(USHORT *)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1) = 0;
82 *pidlOut = pidl;
83 return S_OK;
86 /***********************************************************************
87 * TRASH_UnpackItemID [Internal]
89 * DESCRIPTION:
90 * Extract the information stored in an Item ID. The WIN32_FIND_DATA contains
91 * the information about the original file. The data->ftLastAccessTime contains
92 * the deletion time
94 * PARAMETER(S):
95 * [I] id : the ID of the item
96 * [O] data : the WIN32_FIND_DATA of the original file. Can be NULL is not needed
98 HRESULT TRASH_UnpackItemID(LPCSHITEMID id, WIN32_FIND_DATAW *data)
100 if (id->cb < 2+1+sizeof(WIN32_FIND_DATAW)+2)
101 return E_INVALIDARG;
102 if (id->abID[0] != 0 || id->abID[1+sizeof(WIN32_FIND_DATAW)] != 0)
103 return E_INVALIDARG;
104 if (memchr(id->abID+1+sizeof(WIN32_FIND_DATAW)+1, 0, id->cb-(2+1+sizeof(WIN32_FIND_DATAW)+1)) == NULL)
105 return E_INVALIDARG;
107 if (data != NULL)
108 *data = *(const WIN32_FIND_DATAW *)(id->abID+1);
109 return S_OK;
112 #ifdef HAVE_CORESERVICES_CORESERVICES_H
114 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
116 char *unix_path;
117 OSStatus status;
118 FSRef ref;
119 FSCatalogInfo catalogInfo;
121 TRACE("(%s)\n", debugstr_w(wszPath));
122 if (!(unix_path = wine_get_unix_file_name(wszPath)))
123 return FALSE;
125 status = FSPathMakeRef((UInt8*)unix_path, &ref, NULL);
126 HeapFree(GetProcessHeap(), 0, unix_path);
127 if (status == noErr)
128 status = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &catalogInfo, NULL,
129 NULL, NULL);
130 if (status == noErr)
131 status = FSFindFolder(catalogInfo.volume, kTrashFolderType,
132 kCreateFolder, &ref);
134 return (status == noErr);
137 BOOL TRASH_TrashFile(LPCWSTR wszPath)
139 char *unix_path;
140 OSStatus status;
142 TRACE("(%s)\n", debugstr_w(wszPath));
143 if (!(unix_path = wine_get_unix_file_name(wszPath)))
144 return FALSE;
146 status = FSPathMoveObjectToTrashSync(unix_path, NULL, kFSFileOperationSkipPreflight);
148 HeapFree(GetProcessHeap(), 0, unix_path);
149 return (status == noErr);
152 /* TODO:
153 * - set file deletion time
154 * - set original file location
156 HRESULT TRASH_GetDetails(const char *trash_path, const char *name, WIN32_FIND_DATAW *data)
158 static int once;
160 int trash_path_length = lstrlenA(trash_path);
161 int name_length = lstrlenA(name);
162 char *path = SHAlloc(trash_path_length+1+name_length+1);
163 struct stat stats;
164 int ret;
166 if(!once++) FIXME("semi-stub\n");
168 if(!path) return E_OUTOFMEMORY;
169 memcpy(path, trash_path, trash_path_length);
170 path[trash_path_length] = '/';
171 memcpy(path+trash_path_length+1, name, name_length+1);
173 ret = lstat(path, &stats);
174 HeapFree(GetProcessHeap(), 0, path);
175 if(ret == -1) return S_FALSE;
176 memset(data, 0, sizeof(*data));
177 data->nFileSizeHigh = stats.st_size>>32;
178 data->nFileSizeLow = stats.st_size & 0xffffffff;
179 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER*)&data->ftLastWriteTime);
181 if(!MultiByteToWideChar(CP_UNIXCP, 0, name, -1, data->cFileName, MAX_PATH))
182 return S_FALSE;
183 return S_OK;
186 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
188 WCHAR volume_path[MAX_PATH];
189 char *unix_path, trash_path[MAX_PATH];
190 FSCatalogInfo catalog_info;
191 OSStatus status;
192 FSRef ref;
193 struct dirent *entry;
194 DIR *dir;
195 LPITEMIDLIST *ret;
196 int i=0, ret_size=8;
197 HRESULT hr;
199 TRACE("(%s %p %p)\n", debugstr_w(path), pidls, count);
201 if(!path) {
202 FIXME("All trashes enumeration not supported\n");
203 volume_path[0] = 'C';
204 volume_path[1] = ':';
205 volume_path[2] = 0;
206 } else {
207 if(!GetVolumePathNameW(path, volume_path, MAX_PATH))
208 return E_FAIL;
211 if(!(unix_path = wine_get_unix_file_name(volume_path)))
212 return E_OUTOFMEMORY;
214 status = FSPathMakeRef((UInt8*)unix_path, &ref, NULL);
215 HeapFree(GetProcessHeap(), 0, unix_path);
216 if(status != noErr) return E_FAIL;
217 status = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &catalog_info, NULL, NULL, NULL);
218 if(status != noErr) return E_FAIL;
219 status = FSFindFolder(catalog_info.volume, kTrashFolderType, kCreateFolder, &ref);
220 if(status != noErr) return E_FAIL;
221 status = FSRefMakePath(&ref, (UInt8*)trash_path, MAX_PATH);
222 if(status != noErr) return E_FAIL;
224 if(!(dir = opendir(trash_path))) return E_FAIL;
225 ret = HeapAlloc(GetProcessHeap(), 0, ret_size * sizeof(*ret));
226 if(!ret) {
227 closedir(dir);
228 return E_OUTOFMEMORY;
230 while((entry = readdir(dir))) {
231 WIN32_FIND_DATAW data;
233 if(!lstrcmpA(entry->d_name, ".") || !lstrcmpA(entry->d_name, ".." )
234 || !lstrcmpA(entry->d_name, ".DS_Store"))
235 continue;
237 if(i == ret_size) {
238 LPITEMIDLIST *resized;
239 ret_size *= 2;
240 resized = HeapReAlloc(GetProcessHeap(), 0, ret, ret_size * sizeof(*ret));
241 if(!resized) hr = E_OUTOFMEMORY;
242 else ret = resized;
245 if(SUCCEEDED(hr)) {
246 hr = TRASH_GetDetails(trash_path, entry->d_name, &data);
247 if(hr == S_FALSE) continue;
249 if(SUCCEEDED(hr))
250 hr = TRASH_CreateSimplePIDL(entry->d_name, &data, ret+i);
251 if(FAILED(hr)) {
252 while(i>0) SHFree(ret+(--i));
253 HeapFree(GetProcessHeap(), 0, ret);
254 closedir(dir);
255 return hr;
257 i++;
259 closedir(dir);
261 *pidls = SHAlloc(sizeof(**pidls) * i);
262 if(!*pidls) {
263 while(i>0) SHFree(ret+(--i));
264 HeapFree(GetProcessHeap(), 0, ret);
265 return E_OUTOFMEMORY;
267 *count = i;
268 for(i--; i>=0; i--) (*pidls)[i] = ret[i];
269 HeapFree(GetProcessHeap(), 0, ret);
270 return S_OK;
273 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl)
275 FIXME("stub!\n");
276 return E_NOTIMPL;
279 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
281 FIXME("stub!\n");
282 return E_NOTIMPL;
285 #else /* HAVE_CORESERVICES_CORESERVICES_H */
287 static CRITICAL_SECTION TRASH_Creating;
288 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug =
290 0, 0, &TRASH_Creating,
291 { &TRASH_Creating_Debug.ProcessLocksList,
292 &TRASH_Creating_Debug.ProcessLocksList},
293 0, 0, { (DWORD_PTR)__FILE__ ": TRASH_Creating"}
295 static CRITICAL_SECTION TRASH_Creating = { &TRASH_Creating_Debug, -1, 0, 0, 0, 0 };
297 static const char trashinfo_suffix[] = ".trashinfo";
298 static const char trashinfo_header[] = "[Trash Info]\n";
299 static const char trashinfo_group[] = "Trash Info";
301 typedef struct
303 char *info_dir;
304 char *files_dir;
305 dev_t device;
306 } TRASH_BUCKET;
308 static TRASH_BUCKET *home_trash=NULL;
310 static char *init_home_dir(const char *subpath)
312 char *path = XDG_BuildPath(XDG_DATA_HOME, subpath);
313 if (path == NULL) return NULL;
314 if (!XDG_MakeDirs(path))
316 ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path), errno);
317 SHFree(path);
318 path=NULL;
320 return path;
323 static TRASH_BUCKET *TRASH_CreateHomeBucket(void)
325 TRASH_BUCKET *bucket;
326 struct stat trash_stat;
327 char *trash_path = NULL;
329 bucket = SHAlloc(sizeof(TRASH_BUCKET));
330 if (bucket == NULL)
332 errno = ENOMEM;
333 goto error;
335 memset(bucket, 0, sizeof(*bucket));
336 bucket->info_dir = init_home_dir("Trash/info/");
337 if (bucket->info_dir == NULL) goto error;
338 bucket->files_dir = init_home_dir("Trash/files/");
339 if (bucket->files_dir == NULL) goto error;
341 trash_path = XDG_BuildPath(XDG_DATA_HOME, "Trash/");
342 if (stat(trash_path, &trash_stat) == -1)
343 goto error;
344 bucket->device = trash_stat.st_dev;
345 SHFree(trash_path);
346 return bucket;
347 error:
348 SHFree(trash_path);
349 if (bucket)
351 SHFree(bucket->info_dir);
352 SHFree(bucket->files_dir);
354 SHFree(bucket);
355 return NULL;
358 static BOOL TRASH_EnsureInitialized(void)
360 if (home_trash == NULL)
362 EnterCriticalSection(&TRASH_Creating);
363 if (home_trash == NULL)
364 home_trash = TRASH_CreateHomeBucket();
365 LeaveCriticalSection(&TRASH_Creating);
368 if (home_trash == NULL)
370 ERR("Couldn't initialize home trash (errno=%d)\n", errno);
371 return FALSE;
373 return TRUE;
376 static BOOL file_good_for_bucket(const TRASH_BUCKET *pBucket, const struct stat *file_stat)
378 if (pBucket->device != file_stat->st_dev)
379 return FALSE;
380 return TRUE;
383 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
385 struct stat file_stat;
386 char *unix_path;
388 TRACE("(%s)\n", debugstr_w(wszPath));
389 if (!TRASH_EnsureInitialized()) return FALSE;
390 if (!(unix_path = wine_get_unix_file_name(wszPath)))
391 return FALSE;
392 if (lstat(unix_path, &file_stat)==-1)
394 HeapFree(GetProcessHeap(), 0, unix_path);
395 return FALSE;
397 HeapFree(GetProcessHeap(), 0, unix_path);
398 return file_good_for_bucket(home_trash, &file_stat);
402 * Try to create a single .trashinfo file. Return TRUE if successful, else FALSE
404 static BOOL try_create_trashinfo_file(const char *info_dir, const char *file_name,
405 const char *original_file_name)
407 SYSTEMTIME curr_time;
408 char datebuf[200];
409 char *path = SHAlloc(strlen(info_dir)+strlen(file_name)+strlen(trashinfo_suffix)+1);
410 int writer = -1;
412 if (path==NULL) return FALSE;
413 wsprintfA(path, "%s%s%s", info_dir, file_name, trashinfo_suffix);
414 TRACE("Trying to create '%s'\n", path);
415 writer = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0600);
416 if (writer==-1) goto error;
418 write(writer, trashinfo_header, strlen(trashinfo_header));
419 if (!XDG_WriteDesktopStringEntry(writer, "Path", XDG_URLENCODE, original_file_name))
420 goto error;
422 GetLocalTime( &curr_time );
423 wnsprintfA(datebuf, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
424 curr_time.wYear, curr_time.wMonth, curr_time.wDay,
425 curr_time.wHour, curr_time.wMinute, curr_time.wSecond);
426 if (!XDG_WriteDesktopStringEntry(writer, "DeletionDate", 0, datebuf))
427 goto error;
428 close(writer);
429 SHFree(path);
430 return TRUE;
432 error:
433 if (writer != -1)
435 close(writer);
436 unlink(path);
438 SHFree(path);
439 return FALSE;
443 * Try to create a .trashinfo file. This function will make several attempts with
444 * different filenames. It will return the filename that succeeded or NULL if a file
445 * couldn't be created.
447 static char *create_trashinfo(const char *info_dir, const char *file_path)
449 const char *base_name;
450 char *filename_buffer;
451 ULONG seed = GetTickCount();
452 int i;
454 errno = ENOMEM; /* out-of-memory is the only case when errno isn't set */
455 base_name = strrchr(file_path, '/');
456 if (base_name == NULL)
457 base_name = file_path;
458 else
459 base_name++;
461 filename_buffer = SHAlloc(strlen(base_name)+9+1);
462 if (filename_buffer == NULL)
463 return NULL;
464 lstrcpyA(filename_buffer, base_name);
465 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
466 return filename_buffer;
467 for (i=0; i<30; i++)
469 sprintf(filename_buffer, "%s-%d", base_name, i+1);
470 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
471 return filename_buffer;
474 for (i=0; i<1000; i++)
476 sprintf(filename_buffer, "%s-%08x", base_name, RtlRandom(&seed));
477 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
478 return filename_buffer;
481 WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno);
482 SHFree(filename_buffer);
483 return NULL;
486 static void remove_trashinfo_file(const char *info_dir, const char *base_name)
488 char *filename_buffer;
490 filename_buffer = SHAlloc(lstrlenA(info_dir)+lstrlenA(base_name)+lstrlenA(trashinfo_suffix)+1);
491 if (filename_buffer == NULL) return;
492 sprintf(filename_buffer, "%s%s%s", info_dir, base_name, trashinfo_suffix);
493 unlink(filename_buffer);
494 SHFree(filename_buffer);
497 static BOOL TRASH_MoveFileToBucket(TRASH_BUCKET *pBucket, const char *unix_path)
499 struct stat file_stat;
500 char *trash_file_name = NULL;
501 char *trash_path = NULL;
502 BOOL ret = TRUE;
504 if (lstat(unix_path, &file_stat)==-1)
505 return FALSE;
506 if (!file_good_for_bucket(pBucket, &file_stat))
507 return FALSE;
509 trash_file_name = create_trashinfo(pBucket->info_dir, unix_path);
510 if (trash_file_name == NULL)
511 return FALSE;
513 trash_path = SHAlloc(strlen(pBucket->files_dir)+strlen(trash_file_name)+1);
514 if (trash_path == NULL) goto error;
515 lstrcpyA(trash_path, pBucket->files_dir);
516 lstrcatA(trash_path, trash_file_name);
518 if (rename(unix_path, trash_path)==0)
520 TRACE("rename succeeded\n");
521 goto cleanup;
524 /* TODO: try to manually move the file */
525 ERR("Couldn't move file\n");
526 error:
527 ret = FALSE;
528 remove_trashinfo_file(pBucket->info_dir, trash_file_name);
529 cleanup:
530 SHFree(trash_file_name);
531 SHFree(trash_path);
532 return ret;
535 BOOL TRASH_TrashFile(LPCWSTR wszPath)
537 char *unix_path;
538 BOOL result;
540 TRACE("(%s)\n", debugstr_w(wszPath));
541 if (!TRASH_EnsureInitialized()) return FALSE;
542 if (!(unix_path = wine_get_unix_file_name(wszPath)))
543 return FALSE;
544 result = TRASH_MoveFileToBucket(home_trash, unix_path);
545 HeapFree(GetProcessHeap(), 0, unix_path);
546 return result;
549 static HRESULT TRASH_GetDetails(const TRASH_BUCKET *bucket, LPCSTR filename, WIN32_FIND_DATAW *data)
551 LPSTR path = NULL;
552 XDG_PARSED_FILE *parsed = NULL;
553 char *original_file_name = NULL;
554 char *deletion_date = NULL;
555 int fd = -1;
556 struct stat stats;
557 HRESULT ret = S_FALSE;
558 LPWSTR original_dos_name;
559 int suffix_length = lstrlenA(trashinfo_suffix);
560 int filename_length = lstrlenA(filename);
561 int files_length = lstrlenA(bucket->files_dir);
562 int path_length = max(lstrlenA(bucket->info_dir), files_length);
564 path = SHAlloc(path_length + filename_length + 1);
565 if (path == NULL) return E_OUTOFMEMORY;
566 wsprintfA(path, "%s%s", bucket->files_dir, filename);
567 path[path_length + filename_length - suffix_length] = 0; /* remove the '.trashinfo' */
568 if (lstat(path, &stats) == -1)
570 ERR("Error accessing data file for trashinfo %s (errno=%d)\n", filename, errno);
571 goto failed;
574 wsprintfA(path, "%s%s", bucket->info_dir, filename);
575 fd = open(path, O_RDONLY);
576 if (fd == -1)
578 ERR("Couldn't open trashinfo file %s (errno=%d)\n", path, errno);
579 goto failed;
582 parsed = XDG_ParseDesktopFile(fd);
583 if (parsed == NULL)
585 ERR("Parse error in trashinfo file %s\n", path);
586 goto failed;
589 original_file_name = XDG_GetStringValue(parsed, trashinfo_group, "Path", XDG_URLENCODE);
590 if (original_file_name == NULL)
592 ERR("No 'Path' entry in trashinfo file\n");
593 goto failed;
596 ZeroMemory(data, sizeof(*data));
597 data->nFileSizeHigh = (DWORD)((LONGLONG)stats.st_size>>32);
598 data->nFileSizeLow = stats.st_size & 0xffffffff;
599 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER *)&data->ftLastWriteTime);
601 original_dos_name = wine_get_dos_file_name(original_file_name);
602 if (original_dos_name != NULL)
604 lstrcpynW(data->cFileName, original_dos_name, MAX_PATH);
605 SHFree(original_dos_name);
607 else
609 /* show only the file name */
610 char *file = strrchr(original_file_name, '/');
611 if (file == NULL)
612 file = original_file_name;
613 MultiByteToWideChar(CP_UNIXCP, 0, file, -1, data->cFileName, MAX_PATH);
616 deletion_date = XDG_GetStringValue(parsed, trashinfo_group, "DeletionDate", 0);
617 if (deletion_date)
619 struct tm del_time;
620 time_t del_secs;
622 sscanf(deletion_date, "%d-%d-%dT%d:%d:%d",
623 &del_time.tm_year, &del_time.tm_mon, &del_time.tm_mday,
624 &del_time.tm_hour, &del_time.tm_min, &del_time.tm_sec);
625 del_time.tm_year -= 1900;
626 del_time.tm_mon--;
627 del_time.tm_isdst = -1;
628 del_secs = mktime(&del_time);
630 RtlSecondsSince1970ToTime(del_secs, (LARGE_INTEGER *)&data->ftLastAccessTime);
633 ret = S_OK;
634 failed:
635 SHFree(path);
636 SHFree(original_file_name);
637 SHFree(deletion_date);
638 if (fd != -1)
639 close(fd);
640 XDG_FreeParsedFile(parsed);
641 return ret;
644 static INT CALLBACK free_item_callback(void *item, void *lParam)
646 SHFree(item);
647 return TRUE;
650 static HDPA enum_bucket_trashinfos(const TRASH_BUCKET *bucket, int *count)
652 HDPA ret = DPA_Create(32);
653 struct dirent *entry;
654 DIR *dir = NULL;
656 errno = ENOMEM;
657 *count = 0;
658 if (ret == NULL) goto failed;
659 dir = opendir(bucket->info_dir);
660 if (dir == NULL) goto failed;
661 while ((entry = readdir(dir)) != NULL)
663 LPSTR filename;
664 int namelen = lstrlenA(entry->d_name);
665 int suffixlen = lstrlenA(trashinfo_suffix);
666 if (namelen <= suffixlen ||
667 lstrcmpA(entry->d_name+namelen-suffixlen, trashinfo_suffix) != 0)
668 continue;
670 filename = StrDupA(entry->d_name);
671 if (filename == NULL)
672 goto failed;
673 if (DPA_InsertPtr(ret, DPA_APPEND, filename) == -1)
675 LocalFree(filename);
676 goto failed;
678 (*count)++;
680 closedir(dir);
681 return ret;
682 failed:
683 if (dir) closedir(dir);
684 if (ret)
685 DPA_DestroyCallback(ret, free_item_callback, NULL);
686 return NULL;
689 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
691 int ti_count;
692 int pos=0, i;
693 HRESULT err = E_OUTOFMEMORY;
694 HDPA tinfs;
696 if(path)
697 FIXME("Ignoring path = %s\n", debugstr_w(path));
699 if (!TRASH_EnsureInitialized()) return E_FAIL;
700 tinfs = enum_bucket_trashinfos(home_trash, &ti_count);
701 if (tinfs == NULL) return E_FAIL;
702 *pidls = SHAlloc(sizeof(LPITEMIDLIST)*ti_count);
703 if (!*pidls) goto failed;
704 for (i=0; i<ti_count; i++)
706 WIN32_FIND_DATAW data;
707 LPCSTR filename;
709 filename = DPA_GetPtr(tinfs, i);
710 if (FAILED(err = TRASH_GetDetails(home_trash, filename, &data)))
711 goto failed;
712 if (err == S_FALSE)
713 continue;
714 if (FAILED(err = TRASH_CreateSimplePIDL(filename, &data, &(*pidls)[pos])))
715 goto failed;
716 pos++;
718 *count = pos;
719 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
720 return S_OK;
721 failed:
722 if (*pidls != NULL)
724 int j;
725 for (j=0; j<pos; j++)
726 SHFree((*pidls)[j]);
727 SHFree(*pidls);
729 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
731 return err;
734 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl){
735 int suffix_length = strlen(trashinfo_suffix);
736 LPCSHITEMID id = &(pidl->mkid);
737 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
738 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
739 char *restore_path;
740 WIN32_FIND_DATAW data;
741 char *file_path;
743 TRACE("(%p)\n",pidl);
744 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
746 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
747 return E_INVALIDARG;
749 TRASH_UnpackItemID(id,&data);
750 restore_path = wine_get_unix_file_name(data.cFileName);
751 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
752 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
753 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
754 if(!rename(file_path,restore_path))
756 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
757 if(unlink(file_path))
758 WARN("failed to delete the trashinfo file %s\n",filename);
760 else
761 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
762 SHFree(file_path);
763 HeapFree(GetProcessHeap(), 0, restore_path);
764 return S_OK;
767 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
769 int suffix_length = strlen(trashinfo_suffix);
771 LPCSHITEMID id = &(pidl->mkid);
772 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
773 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
774 char *file_path;
776 TRACE("(%p)\n",pidl);
777 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
779 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
780 return E_INVALIDARG;
782 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
783 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
784 if(unlink(file_path))
785 WARN("failed to delete the trashinfo file %s\n",filename);
786 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
787 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
788 if(unlink(file_path))
789 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
790 SHFree(file_path);
791 return S_OK;
794 #endif /* HAVE_CORESERVICES_CORESERVICES_H */