iccvid: Use the ARRAY_SIZE() macro.
[wine.git] / dlls / shell32 / trash.c
blobfc75c17dd28df14c2510e25f05b99cab5b12b2ad
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 #include <stdarg.h>
26 #include <stdio.h>
27 #include <fcntl.h>
28 #include <errno.h>
29 #include <time.h>
30 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_SYS_MOUNT_H
35 # include <sys/mount.h>
36 #endif
37 #include <stdlib.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #ifdef HAVE_DIRENT_H
42 # include <dirent.h>
43 #endif
44 #ifdef HAVE_PWD_H
45 # include <pwd.h>
46 #endif
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winerror.h"
51 #include "winreg.h"
52 #include "shlwapi.h"
53 #include "winternl.h"
54 #include "wine/debug.h"
55 #include "shell32_main.h"
56 #include "xdg.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(trash);
61 * The item ID of a trashed element is built as follows:
62 * NUL byte - in most PIDLs the first byte is the type so we keep it constant
63 * WIN32_FIND_DATAW structure - with data about original file attributes
64 * bucket name - currently only an empty string meaning the home bucket is supported
65 * trash file name - a NUL-terminated string
67 static HRESULT TRASH_CreateSimplePIDL(LPCSTR filename, const WIN32_FIND_DATAW *data, LPITEMIDLIST *pidlOut)
69 LPITEMIDLIST pidl = SHAlloc(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1+2);
70 *pidlOut = NULL;
71 if (pidl == NULL)
72 return E_OUTOFMEMORY;
73 pidl->mkid.cb = (USHORT)(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1);
74 pidl->mkid.abID[0] = 0;
75 memcpy(pidl->mkid.abID+1, data, sizeof(WIN32_FIND_DATAW));
76 pidl->mkid.abID[1+sizeof(WIN32_FIND_DATAW)] = 0;
77 lstrcpyA((LPSTR)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1), filename);
78 *(USHORT *)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1) = 0;
79 *pidlOut = pidl;
80 return S_OK;
83 /***********************************************************************
84 * TRASH_UnpackItemID [Internal]
86 * DESCRIPTION:
87 * Extract the information stored in an Item ID. The WIN32_FIND_DATA contains
88 * the information about the original file. The data->ftLastAccessTime contains
89 * the deletion time
91 * PARAMETER(S):
92 * [I] id : the ID of the item
93 * [O] data : the WIN32_FIND_DATA of the original file. Can be NULL is not needed
95 HRESULT TRASH_UnpackItemID(LPCSHITEMID id, WIN32_FIND_DATAW *data)
97 if (id->cb < 2+1+sizeof(WIN32_FIND_DATAW)+2)
98 return E_INVALIDARG;
99 if (id->abID[0] != 0 || id->abID[1+sizeof(WIN32_FIND_DATAW)] != 0)
100 return E_INVALIDARG;
101 if (memchr(id->abID+1+sizeof(WIN32_FIND_DATAW)+1, 0, id->cb-(2+1+sizeof(WIN32_FIND_DATAW)+1)) == NULL)
102 return E_INVALIDARG;
104 if (data != NULL)
105 *data = *(const WIN32_FIND_DATAW *)(id->abID+1);
106 return S_OK;
109 #ifdef __APPLE__
111 static char *TRASH_GetTrashPath(const char *unix_path, const char **base_name)
113 struct statfs stfs, home_stfs;
114 struct passwd *user = getpwuid(geteuid());
115 char *trash_path;
116 size_t name_size;
118 if (statfs(unix_path, &stfs) == -1)
119 return NULL;
120 if (statfs(user->pw_dir, &home_stfs) == -1)
121 return NULL;
123 *base_name = strrchr(unix_path, '/');
124 if (!*base_name)
125 *base_name = unix_path;
126 else
127 ++*base_name;
128 name_size = lstrlenA(*base_name);
130 /* If the file exists on the same volume as the user's home directory,
131 * we can use the User domain Trash folder. Otherwise, we have to use
132 * <volume>/.Trashes/<uid>.
134 if (memcmp(&stfs.f_fsid, &home_stfs.f_fsid, sizeof(fsid_t)) == 0)
136 size_t home_size = lstrlenA(user->pw_dir);
137 trash_path = heap_alloc(home_size + sizeof("/.Trash/") + name_size);
138 if (!trash_path)
139 return NULL;
140 memcpy(trash_path, user->pw_dir, home_size);
141 memcpy(trash_path+home_size, "/.Trash", sizeof("/.Trash"));
143 else
145 size_t vol_size = lstrlenA(stfs.f_mntonname);
146 /* 10 for the maximum length of a 32-bit integer + 1 for the \0 */
147 size_t trash_size = vol_size + sizeof("/.Trashes/") + 10 + 1 + name_size + 1;
148 trash_path = heap_alloc(trash_size);
149 if (!trash_path)
150 return NULL;
151 snprintf(trash_path, trash_size, "%s/.Trashes/%u", stfs.f_mntonname, geteuid());
153 return trash_path;
156 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
158 char *unix_path, *trash_path;
159 const char *base_name;
160 BOOL can_trash;
161 struct stat st;
163 TRACE("(%s)\n", debugstr_w(wszPath));
165 if (!(unix_path = wine_get_unix_file_name(wszPath)))
166 return FALSE;
167 if (!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
169 heap_free(unix_path);
170 return FALSE;
172 can_trash = stat(trash_path, &st) == 0;
173 if (!can_trash && errno == ENOENT)
175 /* Recursively create the Trash folder. */
176 char *p = trash_path;
177 if (*p == '/') ++p;
178 while ((p = strchr(p, '/')) != NULL)
180 *p = '\0';
181 if (mkdir(trash_path, 0755) == -1 && errno != EEXIST)
183 heap_free(unix_path);
184 heap_free(trash_path);
185 return FALSE;
187 *p++ = '/';
189 can_trash = TRUE;
191 heap_free(unix_path);
192 heap_free(trash_path);
193 return can_trash;
196 BOOL TRASH_TrashFile(LPCWSTR wszPath)
198 char *unix_path, *trash_path;
199 const char *base_name;
200 int res;
202 TRACE("(%s)\n", debugstr_w(wszPath));
203 if (!(unix_path = wine_get_unix_file_name(wszPath)))
204 return FALSE;
205 if (!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
207 heap_free(unix_path);
208 return FALSE;
211 lstrcatA(trash_path, "/");
212 lstrcatA(trash_path, base_name);
214 res = rename(unix_path, trash_path);
216 heap_free(unix_path);
217 heap_free(trash_path);
218 return (res != -1);
221 /* TODO:
222 * - set file deletion time
223 * - set original file location
225 HRESULT TRASH_GetDetails(const char *trash_path, const char *name, WIN32_FIND_DATAW *data)
227 static int once;
229 int trash_path_length = lstrlenA(trash_path);
230 int name_length = lstrlenA(name);
231 char *path = SHAlloc(trash_path_length+1+name_length+1);
232 struct stat stats;
233 int ret;
235 if(!once++) FIXME("semi-stub\n");
237 if(!path) return E_OUTOFMEMORY;
238 memcpy(path, trash_path, trash_path_length);
239 path[trash_path_length] = '/';
240 memcpy(path+trash_path_length+1, name, name_length+1);
242 ret = lstat(path, &stats);
243 heap_free(path);
244 if(ret == -1) return S_FALSE;
245 memset(data, 0, sizeof(*data));
246 data->nFileSizeHigh = stats.st_size>>32;
247 data->nFileSizeLow = stats.st_size & 0xffffffff;
248 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER*)&data->ftLastWriteTime);
250 if(!MultiByteToWideChar(CP_UNIXCP, 0, name, -1, data->cFileName, MAX_PATH))
251 return S_FALSE;
252 return S_OK;
255 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
257 WCHAR volume_path[MAX_PATH];
258 char *unix_path, *trash_path;
259 const char *base_name;
260 struct dirent *entry;
261 DIR *dir;
262 LPITEMIDLIST *ret;
263 int i=0, ret_size=8;
264 HRESULT hr;
266 TRACE("(%s %p %p)\n", debugstr_w(path), pidls, count);
268 if(!path) {
269 FIXME("All trashes enumeration not supported\n");
270 volume_path[0] = 'C';
271 volume_path[1] = ':';
272 volume_path[2] = 0;
273 } else {
274 if(!GetVolumePathNameW(path, volume_path, MAX_PATH))
275 return E_FAIL;
278 if(!(unix_path = wine_get_unix_file_name(volume_path)))
279 return E_OUTOFMEMORY;
281 if(!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
282 return E_OUTOFMEMORY;
284 if(!(dir = opendir(trash_path))) return E_FAIL;
285 ret = heap_alloc(ret_size * sizeof(*ret));
286 if(!ret) {
287 closedir(dir);
288 return E_OUTOFMEMORY;
290 while((entry = readdir(dir))) {
291 WIN32_FIND_DATAW data;
293 if(!lstrcmpA(entry->d_name, ".") || !lstrcmpA(entry->d_name, ".." )
294 || !lstrcmpA(entry->d_name, ".DS_Store"))
295 continue;
297 if(i == ret_size) {
298 LPITEMIDLIST *resized;
299 ret_size *= 2;
300 resized = heap_realloc(ret, ret_size * sizeof(*ret));
301 if(!resized) hr = E_OUTOFMEMORY;
302 else ret = resized;
305 if(SUCCEEDED(hr)) {
306 hr = TRASH_GetDetails(trash_path, entry->d_name, &data);
307 if(hr == S_FALSE) continue;
309 if(SUCCEEDED(hr))
310 hr = TRASH_CreateSimplePIDL(entry->d_name, &data, ret+i);
311 if(FAILED(hr)) {
312 while(i>0) SHFree(ret+(--i));
313 heap_free(ret);
314 closedir(dir);
315 return hr;
317 i++;
319 closedir(dir);
321 *pidls = SHAlloc(sizeof(**pidls) * i);
322 if(!*pidls) {
323 while(i>0) SHFree(ret+(--i));
324 heap_free(ret);
325 return E_OUTOFMEMORY;
327 *count = i;
328 for(i--; i>=0; i--) (*pidls)[i] = ret[i];
329 heap_free(ret);
330 return S_OK;
333 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl)
335 FIXME("stub!\n");
336 return E_NOTIMPL;
339 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
341 FIXME("stub!\n");
342 return E_NOTIMPL;
345 #else /* __APPLE__ */
347 static CRITICAL_SECTION TRASH_Creating;
348 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug =
350 0, 0, &TRASH_Creating,
351 { &TRASH_Creating_Debug.ProcessLocksList,
352 &TRASH_Creating_Debug.ProcessLocksList},
353 0, 0, { (DWORD_PTR)__FILE__ ": TRASH_Creating"}
355 static CRITICAL_SECTION TRASH_Creating = { &TRASH_Creating_Debug, -1, 0, 0, 0, 0 };
357 static const char trashinfo_suffix[] = ".trashinfo";
358 static const char trashinfo_header[] = "[Trash Info]\n";
359 static const char trashinfo_group[] = "Trash Info";
361 typedef struct
363 char *info_dir;
364 char *files_dir;
365 dev_t device;
366 } TRASH_BUCKET;
368 static TRASH_BUCKET *home_trash=NULL;
370 static char *init_home_dir(const char *subpath)
372 char *path = XDG_BuildPath(XDG_DATA_HOME, subpath);
373 if (path == NULL) return NULL;
374 if (!XDG_MakeDirs(path))
376 ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path), errno);
377 SHFree(path);
378 path=NULL;
380 return path;
383 static TRASH_BUCKET *TRASH_CreateHomeBucket(void)
385 TRASH_BUCKET *bucket;
386 struct stat trash_stat;
387 char *trash_path = NULL;
389 bucket = SHAlloc(sizeof(TRASH_BUCKET));
390 if (bucket == NULL)
392 errno = ENOMEM;
393 goto error;
395 memset(bucket, 0, sizeof(*bucket));
396 bucket->info_dir = init_home_dir("Trash/info/");
397 if (bucket->info_dir == NULL) goto error;
398 bucket->files_dir = init_home_dir("Trash/files/");
399 if (bucket->files_dir == NULL) goto error;
401 trash_path = XDG_BuildPath(XDG_DATA_HOME, "Trash/");
402 if (stat(trash_path, &trash_stat) == -1)
403 goto error;
404 bucket->device = trash_stat.st_dev;
405 SHFree(trash_path);
406 return bucket;
407 error:
408 SHFree(trash_path);
409 if (bucket)
411 SHFree(bucket->info_dir);
412 SHFree(bucket->files_dir);
414 SHFree(bucket);
415 return NULL;
418 static BOOL TRASH_EnsureInitialized(void)
420 if (home_trash == NULL)
422 EnterCriticalSection(&TRASH_Creating);
423 if (home_trash == NULL)
424 home_trash = TRASH_CreateHomeBucket();
425 LeaveCriticalSection(&TRASH_Creating);
428 if (home_trash == NULL)
430 ERR("Couldn't initialize home trash (errno=%d)\n", errno);
431 return FALSE;
433 return TRUE;
436 static BOOL file_good_for_bucket(const TRASH_BUCKET *pBucket, const struct stat *file_stat)
438 if (pBucket->device != file_stat->st_dev)
439 return FALSE;
440 return TRUE;
443 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
445 struct stat file_stat;
446 char *unix_path;
447 int ret;
449 TRACE("(%s)\n", debugstr_w(wszPath));
450 if (!TRASH_EnsureInitialized()) return FALSE;
451 if (!(unix_path = wine_get_unix_file_name(wszPath)))
452 return FALSE;
453 ret = lstat(unix_path, &file_stat);
454 heap_free(unix_path);
455 if (ret == -1)
456 return FALSE;
457 return file_good_for_bucket(home_trash, &file_stat);
461 * Try to create a single .trashinfo file. Return TRUE if successful, else FALSE
463 static BOOL try_create_trashinfo_file(const char *info_dir, const char *file_name,
464 const char *original_file_name)
466 SYSTEMTIME curr_time;
467 char datebuf[200];
468 char *path = SHAlloc(strlen(info_dir)+strlen(file_name)+strlen(trashinfo_suffix)+1);
469 int writer = -1;
471 if (path==NULL) return FALSE;
472 wsprintfA(path, "%s%s%s", info_dir, file_name, trashinfo_suffix);
473 TRACE("Trying to create '%s'\n", path);
474 writer = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0600);
475 if (writer==-1) goto error;
477 write(writer, trashinfo_header, strlen(trashinfo_header));
478 if (!XDG_WriteDesktopStringEntry(writer, "Path", XDG_URLENCODE, original_file_name))
479 goto error;
481 GetLocalTime( &curr_time );
482 wnsprintfA(datebuf, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
483 curr_time.wYear, curr_time.wMonth, curr_time.wDay,
484 curr_time.wHour, curr_time.wMinute, curr_time.wSecond);
485 if (!XDG_WriteDesktopStringEntry(writer, "DeletionDate", 0, datebuf))
486 goto error;
487 close(writer);
488 SHFree(path);
489 return TRUE;
491 error:
492 if (writer != -1)
494 close(writer);
495 unlink(path);
497 SHFree(path);
498 return FALSE;
502 * Try to create a .trashinfo file. This function will make several attempts with
503 * different filenames. It will return the filename that succeeded or NULL if a file
504 * couldn't be created.
506 static char *create_trashinfo(const char *info_dir, const char *file_path)
508 const char *base_name;
509 char *filename_buffer;
510 ULONG seed = GetTickCount();
511 int i;
513 errno = ENOMEM; /* out-of-memory is the only case when errno isn't set */
514 base_name = strrchr(file_path, '/');
515 if (base_name == NULL)
516 base_name = file_path;
517 else
518 base_name++;
520 filename_buffer = SHAlloc(strlen(base_name)+9+1);
521 if (filename_buffer == NULL)
522 return NULL;
523 lstrcpyA(filename_buffer, base_name);
524 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
525 return filename_buffer;
526 for (i=0; i<30; i++)
528 sprintf(filename_buffer, "%s-%d", base_name, i+1);
529 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
530 return filename_buffer;
533 for (i=0; i<1000; i++)
535 sprintf(filename_buffer, "%s-%08x", base_name, RtlRandom(&seed));
536 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
537 return filename_buffer;
540 WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno);
541 SHFree(filename_buffer);
542 return NULL;
545 static void remove_trashinfo_file(const char *info_dir, const char *base_name)
547 char *filename_buffer;
549 filename_buffer = SHAlloc(lstrlenA(info_dir)+lstrlenA(base_name)+lstrlenA(trashinfo_suffix)+1);
550 if (filename_buffer == NULL) return;
551 sprintf(filename_buffer, "%s%s%s", info_dir, base_name, trashinfo_suffix);
552 unlink(filename_buffer);
553 SHFree(filename_buffer);
556 static BOOL TRASH_MoveFileToBucket(TRASH_BUCKET *pBucket, const char *unix_path)
558 struct stat file_stat;
559 char *trash_file_name = NULL;
560 char *trash_path = NULL;
561 BOOL ret = TRUE;
563 if (lstat(unix_path, &file_stat)==-1)
564 return FALSE;
565 if (!file_good_for_bucket(pBucket, &file_stat))
566 return FALSE;
568 trash_file_name = create_trashinfo(pBucket->info_dir, unix_path);
569 if (trash_file_name == NULL)
570 return FALSE;
572 trash_path = SHAlloc(strlen(pBucket->files_dir)+strlen(trash_file_name)+1);
573 if (trash_path == NULL) goto error;
574 lstrcpyA(trash_path, pBucket->files_dir);
575 lstrcatA(trash_path, trash_file_name);
577 if (rename(unix_path, trash_path)==0)
579 TRACE("rename succeeded\n");
580 goto cleanup;
583 /* TODO: try to manually move the file */
584 ERR("Couldn't move file\n");
585 error:
586 ret = FALSE;
587 remove_trashinfo_file(pBucket->info_dir, trash_file_name);
588 cleanup:
589 SHFree(trash_file_name);
590 SHFree(trash_path);
591 return ret;
594 BOOL TRASH_TrashFile(LPCWSTR wszPath)
596 char *unix_path;
597 BOOL result;
599 TRACE("(%s)\n", debugstr_w(wszPath));
600 if (!TRASH_EnsureInitialized()) return FALSE;
601 if (!(unix_path = wine_get_unix_file_name(wszPath)))
602 return FALSE;
603 result = TRASH_MoveFileToBucket(home_trash, unix_path);
604 heap_free(unix_path);
605 return result;
608 static HRESULT TRASH_GetDetails(const TRASH_BUCKET *bucket, LPCSTR filename, WIN32_FIND_DATAW *data)
610 LPSTR path = NULL;
611 XDG_PARSED_FILE *parsed = NULL;
612 char *original_file_name = NULL;
613 char *deletion_date = NULL;
614 int fd = -1;
615 struct stat stats;
616 HRESULT ret = S_FALSE;
617 LPWSTR original_dos_name;
618 int suffix_length = lstrlenA(trashinfo_suffix);
619 int filename_length = lstrlenA(filename);
620 int files_length = lstrlenA(bucket->files_dir);
621 int path_length = max(lstrlenA(bucket->info_dir), files_length);
623 path = SHAlloc(path_length + filename_length + 1);
624 if (path == NULL) return E_OUTOFMEMORY;
625 wsprintfA(path, "%s%s", bucket->files_dir, filename);
626 path[path_length + filename_length - suffix_length] = 0; /* remove the '.trashinfo' */
627 if (lstat(path, &stats) == -1)
629 ERR("Error accessing data file for trashinfo %s (errno=%d)\n", filename, errno);
630 goto failed;
633 wsprintfA(path, "%s%s", bucket->info_dir, filename);
634 fd = open(path, O_RDONLY);
635 if (fd == -1)
637 ERR("Couldn't open trashinfo file %s (errno=%d)\n", path, errno);
638 goto failed;
641 parsed = XDG_ParseDesktopFile(fd);
642 if (parsed == NULL)
644 ERR("Parse error in trashinfo file %s\n", path);
645 goto failed;
648 original_file_name = XDG_GetStringValue(parsed, trashinfo_group, "Path", XDG_URLENCODE);
649 if (original_file_name == NULL)
651 ERR("No 'Path' entry in trashinfo file\n");
652 goto failed;
655 ZeroMemory(data, sizeof(*data));
656 data->nFileSizeHigh = (DWORD)((LONGLONG)stats.st_size>>32);
657 data->nFileSizeLow = stats.st_size & 0xffffffff;
658 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER *)&data->ftLastWriteTime);
660 original_dos_name = wine_get_dos_file_name(original_file_name);
661 if (original_dos_name != NULL)
663 lstrcpynW(data->cFileName, original_dos_name, MAX_PATH);
664 SHFree(original_dos_name);
666 else
668 /* show only the file name */
669 char *file = strrchr(original_file_name, '/');
670 if (file == NULL)
671 file = original_file_name;
672 MultiByteToWideChar(CP_UNIXCP, 0, file, -1, data->cFileName, MAX_PATH);
675 deletion_date = XDG_GetStringValue(parsed, trashinfo_group, "DeletionDate", 0);
676 if (deletion_date)
678 struct tm del_time;
679 time_t del_secs;
681 sscanf(deletion_date, "%d-%d-%dT%d:%d:%d",
682 &del_time.tm_year, &del_time.tm_mon, &del_time.tm_mday,
683 &del_time.tm_hour, &del_time.tm_min, &del_time.tm_sec);
684 del_time.tm_year -= 1900;
685 del_time.tm_mon--;
686 del_time.tm_isdst = -1;
687 del_secs = mktime(&del_time);
689 RtlSecondsSince1970ToTime(del_secs, (LARGE_INTEGER *)&data->ftLastAccessTime);
692 ret = S_OK;
693 failed:
694 SHFree(path);
695 SHFree(original_file_name);
696 SHFree(deletion_date);
697 if (fd != -1)
698 close(fd);
699 XDG_FreeParsedFile(parsed);
700 return ret;
703 static INT CALLBACK free_item_callback(void *item, void *lParam)
705 SHFree(item);
706 return TRUE;
709 static HDPA enum_bucket_trashinfos(const TRASH_BUCKET *bucket, int *count)
711 HDPA ret = DPA_Create(32);
712 struct dirent *entry;
713 DIR *dir = NULL;
715 errno = ENOMEM;
716 *count = 0;
717 if (ret == NULL) goto failed;
718 dir = opendir(bucket->info_dir);
719 if (dir == NULL) goto failed;
720 while ((entry = readdir(dir)) != NULL)
722 LPSTR filename;
723 int namelen = lstrlenA(entry->d_name);
724 int suffixlen = lstrlenA(trashinfo_suffix);
725 if (namelen <= suffixlen ||
726 lstrcmpA(entry->d_name+namelen-suffixlen, trashinfo_suffix) != 0)
727 continue;
729 filename = StrDupA(entry->d_name);
730 if (filename == NULL)
731 goto failed;
732 if (DPA_InsertPtr(ret, DPA_APPEND, filename) == -1)
734 LocalFree(filename);
735 goto failed;
737 (*count)++;
739 closedir(dir);
740 return ret;
741 failed:
742 if (dir) closedir(dir);
743 if (ret)
744 DPA_DestroyCallback(ret, free_item_callback, NULL);
745 return NULL;
748 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
750 int ti_count;
751 int pos=0, i;
752 HRESULT err = E_OUTOFMEMORY;
753 HDPA tinfs;
755 if(path)
756 FIXME("Ignoring path = %s\n", debugstr_w(path));
758 if (!TRASH_EnsureInitialized()) return E_FAIL;
759 tinfs = enum_bucket_trashinfos(home_trash, &ti_count);
760 if (tinfs == NULL) return E_FAIL;
761 *pidls = SHAlloc(sizeof(LPITEMIDLIST)*ti_count);
762 if (!*pidls) goto failed;
763 for (i=0; i<ti_count; i++)
765 WIN32_FIND_DATAW data;
766 LPCSTR filename;
768 filename = DPA_GetPtr(tinfs, i);
769 if (FAILED(err = TRASH_GetDetails(home_trash, filename, &data)))
770 goto failed;
771 if (err == S_FALSE)
772 continue;
773 if (FAILED(err = TRASH_CreateSimplePIDL(filename, &data, &(*pidls)[pos])))
774 goto failed;
775 pos++;
777 *count = pos;
778 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
779 return S_OK;
780 failed:
781 if (*pidls != NULL)
783 int j;
784 for (j=0; j<pos; j++)
785 SHFree((*pidls)[j]);
786 SHFree(*pidls);
788 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
790 return err;
793 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl){
794 int suffix_length = strlen(trashinfo_suffix);
795 LPCSHITEMID id = &(pidl->mkid);
796 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
797 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
798 char *restore_path;
799 WIN32_FIND_DATAW data;
800 char *file_path;
802 TRACE("(%p)\n",pidl);
803 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
805 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
806 return E_INVALIDARG;
808 TRASH_UnpackItemID(id,&data);
809 restore_path = wine_get_unix_file_name(data.cFileName);
810 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
811 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
812 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
813 if(!rename(file_path,restore_path))
815 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
816 if(unlink(file_path))
817 WARN("failed to delete the trashinfo file %s\n",filename);
819 else
820 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
821 SHFree(file_path);
822 heap_free(restore_path);
823 return S_OK;
826 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
828 int suffix_length = strlen(trashinfo_suffix);
830 LPCSHITEMID id = &(pidl->mkid);
831 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
832 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
833 char *file_path;
835 TRACE("(%p)\n",pidl);
836 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
838 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
839 return E_INVALIDARG;
841 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
842 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
843 if(unlink(file_path))
844 WARN("failed to delete the trashinfo file %s\n",filename);
845 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
846 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
847 if(unlink(file_path))
848 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
849 SHFree(file_path);
850 return S_OK;
853 #endif /* __APPLE__ */