ntdll: Add a helper for platform-specific threading initialization.
[wine.git] / dlls / shell32 / trash.c
blob93b4ee4588e6b06a50385350eaa26e3bb21b4c37
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"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <time.h>
31 #include <sys/types.h>
32 #ifdef HAVE_SYS_STAT_H
33 # include <sys/stat.h>
34 #endif
35 #ifdef HAVE_SYS_MOUNT_H
36 # include <sys/mount.h>
37 #endif
38 #include <stdlib.h>
39 #ifdef HAVE_UNISTD_H
40 # include <unistd.h>
41 #endif
42 #ifdef HAVE_DIRENT_H
43 # include <dirent.h>
44 #endif
45 #ifdef HAVE_PWD_H
46 # include <pwd.h>
47 #endif
49 #include "windef.h"
50 #include "winbase.h"
51 #include "winerror.h"
52 #include "winreg.h"
53 #include "shlwapi.h"
54 #include "winternl.h"
55 #include "wine/debug.h"
56 #include "shell32_main.h"
57 #include "xdg.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(trash);
62 * The item ID of a trashed element is built as follows:
63 * NUL byte - in most PIDLs the first byte is the type so we keep it constant
64 * WIN32_FIND_DATAW structure - with data about original file attributes
65 * bucket name - currently only an empty string meaning the home bucket is supported
66 * trash file name - a NUL-terminated string
68 static HRESULT TRASH_CreateSimplePIDL(LPCSTR filename, const WIN32_FIND_DATAW *data, LPITEMIDLIST *pidlOut)
70 LPITEMIDLIST pidl = SHAlloc(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1+2);
71 *pidlOut = NULL;
72 if (pidl == NULL)
73 return E_OUTOFMEMORY;
74 pidl->mkid.cb = (USHORT)(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1);
75 pidl->mkid.abID[0] = 0;
76 memcpy(pidl->mkid.abID+1, data, sizeof(WIN32_FIND_DATAW));
77 pidl->mkid.abID[1+sizeof(WIN32_FIND_DATAW)] = 0;
78 lstrcpyA((LPSTR)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1), filename);
79 *(USHORT *)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1) = 0;
80 *pidlOut = pidl;
81 return S_OK;
84 /***********************************************************************
85 * TRASH_UnpackItemID [Internal]
87 * DESCRIPTION:
88 * Extract the information stored in an Item ID. The WIN32_FIND_DATA contains
89 * the information about the original file. The data->ftLastAccessTime contains
90 * the deletion time
92 * PARAMETER(S):
93 * [I] id : the ID of the item
94 * [O] data : the WIN32_FIND_DATA of the original file. Can be NULL is not needed
96 HRESULT TRASH_UnpackItemID(LPCSHITEMID id, WIN32_FIND_DATAW *data)
98 if (id->cb < 2+1+sizeof(WIN32_FIND_DATAW)+2)
99 return E_INVALIDARG;
100 if (id->abID[0] != 0 || id->abID[1+sizeof(WIN32_FIND_DATAW)] != 0)
101 return E_INVALIDARG;
102 if (memchr(id->abID+1+sizeof(WIN32_FIND_DATAW)+1, 0, id->cb-(2+1+sizeof(WIN32_FIND_DATAW)+1)) == NULL)
103 return E_INVALIDARG;
105 if (data != NULL)
106 *data = *(const WIN32_FIND_DATAW *)(id->abID+1);
107 return S_OK;
110 #ifdef __APPLE__
112 static char *TRASH_GetTrashPath(const char *unix_path, const char **base_name)
114 struct statfs stfs, home_stfs;
115 struct passwd *user = getpwuid(geteuid());
116 char *trash_path;
117 size_t name_size;
119 if (statfs(unix_path, &stfs) == -1)
120 return NULL;
121 if (statfs(user->pw_dir, &home_stfs) == -1)
122 return NULL;
124 *base_name = strrchr(unix_path, '/');
125 if (!*base_name)
126 *base_name = unix_path;
127 else
128 ++*base_name;
129 name_size = lstrlenA(*base_name);
131 /* If the file exists on the same volume as the user's home directory,
132 * we can use the User domain Trash folder. Otherwise, we have to use
133 * <volume>/.Trashes/<uid>.
135 if (memcmp(&stfs.f_fsid, &home_stfs.f_fsid, sizeof(fsid_t)) == 0)
137 size_t home_size = lstrlenA(user->pw_dir);
138 trash_path = heap_alloc(home_size + sizeof("/.Trash/") + name_size);
139 if (!trash_path)
140 return NULL;
141 memcpy(trash_path, user->pw_dir, home_size);
142 memcpy(trash_path+home_size, "/.Trash", sizeof("/.Trash"));
144 else
146 size_t vol_size = lstrlenA(stfs.f_mntonname);
147 /* 10 for the maximum length of a 32-bit integer + 1 for the \0 */
148 size_t trash_size = vol_size + sizeof("/.Trashes/") + 10 + 1 + name_size + 1;
149 trash_path = heap_alloc(trash_size);
150 if (!trash_path)
151 return NULL;
152 snprintf(trash_path, trash_size, "%s/.Trashes/%u", stfs.f_mntonname, geteuid());
154 return trash_path;
157 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
159 char *unix_path, *trash_path;
160 const char *base_name;
161 BOOL can_trash;
162 struct stat st;
164 TRACE("(%s)\n", debugstr_w(wszPath));
166 if (!(unix_path = wine_get_unix_file_name(wszPath)))
167 return FALSE;
168 if (!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
170 heap_free(unix_path);
171 return FALSE;
173 can_trash = stat(trash_path, &st) == 0;
174 if (!can_trash && errno == ENOENT)
176 /* Recursively create the Trash folder. */
177 char *p = trash_path;
178 if (*p == '/') ++p;
179 while ((p = strchr(p, '/')) != NULL)
181 *p = '\0';
182 if (mkdir(trash_path, 0755) == -1 && errno != EEXIST)
184 heap_free(unix_path);
185 heap_free(trash_path);
186 return FALSE;
188 *p++ = '/';
190 can_trash = TRUE;
192 heap_free(unix_path);
193 heap_free(trash_path);
194 return can_trash;
197 BOOL TRASH_TrashFile(LPCWSTR wszPath)
199 char *unix_path, *trash_path;
200 const char *base_name;
201 int res;
203 TRACE("(%s)\n", debugstr_w(wszPath));
204 if (!(unix_path = wine_get_unix_file_name(wszPath)))
205 return FALSE;
206 if (!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
208 heap_free(unix_path);
209 return FALSE;
212 lstrcatA(trash_path, "/");
213 lstrcatA(trash_path, base_name);
215 res = rename(unix_path, trash_path);
217 heap_free(unix_path);
218 heap_free(trash_path);
219 return (res != -1);
222 /* TODO:
223 * - set file deletion time
224 * - set original file location
226 HRESULT TRASH_GetDetails(const char *trash_path, const char *name, WIN32_FIND_DATAW *data)
228 static int once;
230 int trash_path_length = lstrlenA(trash_path);
231 int name_length = lstrlenA(name);
232 char *path = SHAlloc(trash_path_length+1+name_length+1);
233 struct stat stats;
234 int ret;
236 if(!once++) FIXME("semi-stub\n");
238 if(!path) return E_OUTOFMEMORY;
239 memcpy(path, trash_path, trash_path_length);
240 path[trash_path_length] = '/';
241 memcpy(path+trash_path_length+1, name, name_length+1);
243 ret = lstat(path, &stats);
244 heap_free(path);
245 if(ret == -1) return S_FALSE;
246 memset(data, 0, sizeof(*data));
247 data->nFileSizeHigh = stats.st_size>>32;
248 data->nFileSizeLow = stats.st_size & 0xffffffff;
249 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER*)&data->ftLastWriteTime);
251 if(!MultiByteToWideChar(CP_UNIXCP, 0, name, -1, data->cFileName, MAX_PATH))
252 return S_FALSE;
253 return S_OK;
256 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
258 WCHAR volume_path[MAX_PATH];
259 char *unix_path, *trash_path;
260 const char *base_name;
261 struct dirent *entry;
262 DIR *dir;
263 LPITEMIDLIST *ret;
264 int i=0, ret_size=8;
265 HRESULT hr;
267 TRACE("(%s %p %p)\n", debugstr_w(path), pidls, count);
269 if(!path) {
270 FIXME("All trashes enumeration not supported\n");
271 volume_path[0] = 'C';
272 volume_path[1] = ':';
273 volume_path[2] = 0;
274 } else {
275 if(!GetVolumePathNameW(path, volume_path, MAX_PATH))
276 return E_FAIL;
279 if(!(unix_path = wine_get_unix_file_name(volume_path)))
280 return E_OUTOFMEMORY;
282 if(!(trash_path = TRASH_GetTrashPath(unix_path, &base_name)))
283 return E_OUTOFMEMORY;
285 if(!(dir = opendir(trash_path))) return E_FAIL;
286 ret = heap_alloc(ret_size * sizeof(*ret));
287 if(!ret) {
288 closedir(dir);
289 return E_OUTOFMEMORY;
291 while((entry = readdir(dir))) {
292 WIN32_FIND_DATAW data;
294 if(!lstrcmpA(entry->d_name, ".") || !lstrcmpA(entry->d_name, ".." )
295 || !lstrcmpA(entry->d_name, ".DS_Store"))
296 continue;
298 if(i == ret_size) {
299 LPITEMIDLIST *resized;
300 ret_size *= 2;
301 resized = heap_realloc(ret, ret_size * sizeof(*ret));
302 if(!resized) hr = E_OUTOFMEMORY;
303 else ret = resized;
306 if(SUCCEEDED(hr)) {
307 hr = TRASH_GetDetails(trash_path, entry->d_name, &data);
308 if(hr == S_FALSE) continue;
310 if(SUCCEEDED(hr))
311 hr = TRASH_CreateSimplePIDL(entry->d_name, &data, ret+i);
312 if(FAILED(hr)) {
313 while(i>0) SHFree(ret+(--i));
314 heap_free(ret);
315 closedir(dir);
316 return hr;
318 i++;
320 closedir(dir);
322 *pidls = SHAlloc(sizeof(**pidls) * i);
323 if(!*pidls) {
324 while(i>0) SHFree(ret+(--i));
325 heap_free(ret);
326 return E_OUTOFMEMORY;
328 *count = i;
329 for(i--; i>=0; i--) (*pidls)[i] = ret[i];
330 heap_free(ret);
331 return S_OK;
334 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl)
336 FIXME("stub!\n");
337 return E_NOTIMPL;
340 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
342 FIXME("stub!\n");
343 return E_NOTIMPL;
346 #else /* __APPLE__ */
348 static CRITICAL_SECTION TRASH_Creating;
349 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug =
351 0, 0, &TRASH_Creating,
352 { &TRASH_Creating_Debug.ProcessLocksList,
353 &TRASH_Creating_Debug.ProcessLocksList},
354 0, 0, { (DWORD_PTR)__FILE__ ": TRASH_Creating"}
356 static CRITICAL_SECTION TRASH_Creating = { &TRASH_Creating_Debug, -1, 0, 0, 0, 0 };
358 static const char trashinfo_suffix[] = ".trashinfo";
359 static const char trashinfo_header[] = "[Trash Info]\n";
360 static const char trashinfo_group[] = "Trash Info";
362 typedef struct
364 char *info_dir;
365 char *files_dir;
366 dev_t device;
367 } TRASH_BUCKET;
369 static TRASH_BUCKET *home_trash=NULL;
371 static char *init_home_dir(const char *subpath)
373 char *path = XDG_BuildPath(XDG_DATA_HOME, subpath);
374 if (path == NULL) return NULL;
375 if (!XDG_MakeDirs(path))
377 ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path), errno);
378 SHFree(path);
379 path=NULL;
381 return path;
384 static TRASH_BUCKET *TRASH_CreateHomeBucket(void)
386 TRASH_BUCKET *bucket;
387 struct stat trash_stat;
388 char *trash_path = NULL;
390 bucket = SHAlloc(sizeof(TRASH_BUCKET));
391 if (bucket == NULL)
393 errno = ENOMEM;
394 goto error;
396 memset(bucket, 0, sizeof(*bucket));
397 bucket->info_dir = init_home_dir("Trash/info/");
398 if (bucket->info_dir == NULL) goto error;
399 bucket->files_dir = init_home_dir("Trash/files/");
400 if (bucket->files_dir == NULL) goto error;
402 trash_path = XDG_BuildPath(XDG_DATA_HOME, "Trash/");
403 if (stat(trash_path, &trash_stat) == -1)
404 goto error;
405 bucket->device = trash_stat.st_dev;
406 SHFree(trash_path);
407 return bucket;
408 error:
409 SHFree(trash_path);
410 if (bucket)
412 SHFree(bucket->info_dir);
413 SHFree(bucket->files_dir);
415 SHFree(bucket);
416 return NULL;
419 static BOOL TRASH_EnsureInitialized(void)
421 if (home_trash == NULL)
423 EnterCriticalSection(&TRASH_Creating);
424 if (home_trash == NULL)
425 home_trash = TRASH_CreateHomeBucket();
426 LeaveCriticalSection(&TRASH_Creating);
429 if (home_trash == NULL)
431 ERR("Couldn't initialize home trash (errno=%d)\n", errno);
432 return FALSE;
434 return TRUE;
437 static BOOL file_good_for_bucket(const TRASH_BUCKET *pBucket, const struct stat *file_stat)
439 if (pBucket->device != file_stat->st_dev)
440 return FALSE;
441 return TRUE;
444 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
446 struct stat file_stat;
447 char *unix_path;
448 int ret;
450 TRACE("(%s)\n", debugstr_w(wszPath));
451 if (!TRASH_EnsureInitialized()) return FALSE;
452 if (!(unix_path = wine_get_unix_file_name(wszPath)))
453 return FALSE;
454 ret = lstat(unix_path, &file_stat);
455 heap_free(unix_path);
456 if (ret == -1)
457 return FALSE;
458 return file_good_for_bucket(home_trash, &file_stat);
462 * Try to create a single .trashinfo file. Return TRUE if successful, else FALSE
464 static BOOL try_create_trashinfo_file(const char *info_dir, const char *file_name,
465 const char *original_file_name)
467 SYSTEMTIME curr_time;
468 char datebuf[200];
469 char *path = SHAlloc(strlen(info_dir)+strlen(file_name)+strlen(trashinfo_suffix)+1);
470 int writer = -1;
472 if (path==NULL) return FALSE;
473 wsprintfA(path, "%s%s%s", info_dir, file_name, trashinfo_suffix);
474 TRACE("Trying to create '%s'\n", path);
475 writer = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0600);
476 if (writer==-1) goto error;
478 write(writer, trashinfo_header, strlen(trashinfo_header));
479 if (!XDG_WriteDesktopStringEntry(writer, "Path", XDG_URLENCODE, original_file_name))
480 goto error;
482 GetLocalTime( &curr_time );
483 wnsprintfA(datebuf, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
484 curr_time.wYear, curr_time.wMonth, curr_time.wDay,
485 curr_time.wHour, curr_time.wMinute, curr_time.wSecond);
486 if (!XDG_WriteDesktopStringEntry(writer, "DeletionDate", 0, datebuf))
487 goto error;
488 close(writer);
489 SHFree(path);
490 return TRUE;
492 error:
493 if (writer != -1)
495 close(writer);
496 unlink(path);
498 SHFree(path);
499 return FALSE;
503 * Try to create a .trashinfo file. This function will make several attempts with
504 * different filenames. It will return the filename that succeeded or NULL if a file
505 * couldn't be created.
507 static char *create_trashinfo(const char *info_dir, const char *file_path)
509 const char *base_name;
510 char *filename_buffer;
511 ULONG seed = GetTickCount();
512 int i;
514 errno = ENOMEM; /* out-of-memory is the only case when errno isn't set */
515 base_name = strrchr(file_path, '/');
516 if (base_name == NULL)
517 base_name = file_path;
518 else
519 base_name++;
521 filename_buffer = SHAlloc(strlen(base_name)+9+1);
522 if (filename_buffer == NULL)
523 return NULL;
524 lstrcpyA(filename_buffer, base_name);
525 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
526 return filename_buffer;
527 for (i=0; i<30; i++)
529 sprintf(filename_buffer, "%s-%d", base_name, i+1);
530 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
531 return filename_buffer;
534 for (i=0; i<1000; i++)
536 sprintf(filename_buffer, "%s-%08x", base_name, RtlRandom(&seed));
537 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
538 return filename_buffer;
541 WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno);
542 SHFree(filename_buffer);
543 return NULL;
546 static void remove_trashinfo_file(const char *info_dir, const char *base_name)
548 char *filename_buffer;
550 filename_buffer = SHAlloc(lstrlenA(info_dir)+lstrlenA(base_name)+lstrlenA(trashinfo_suffix)+1);
551 if (filename_buffer == NULL) return;
552 sprintf(filename_buffer, "%s%s%s", info_dir, base_name, trashinfo_suffix);
553 unlink(filename_buffer);
554 SHFree(filename_buffer);
557 static BOOL TRASH_MoveFileToBucket(TRASH_BUCKET *pBucket, const char *unix_path)
559 struct stat file_stat;
560 char *trash_file_name = NULL;
561 char *trash_path = NULL;
562 BOOL ret = TRUE;
564 if (lstat(unix_path, &file_stat)==-1)
565 return FALSE;
566 if (!file_good_for_bucket(pBucket, &file_stat))
567 return FALSE;
569 trash_file_name = create_trashinfo(pBucket->info_dir, unix_path);
570 if (trash_file_name == NULL)
571 return FALSE;
573 trash_path = SHAlloc(strlen(pBucket->files_dir)+strlen(trash_file_name)+1);
574 if (trash_path == NULL) goto error;
575 lstrcpyA(trash_path, pBucket->files_dir);
576 lstrcatA(trash_path, trash_file_name);
578 if (rename(unix_path, trash_path)==0)
580 TRACE("rename succeeded\n");
581 goto cleanup;
584 /* TODO: try to manually move the file */
585 ERR("Couldn't move file\n");
586 error:
587 ret = FALSE;
588 remove_trashinfo_file(pBucket->info_dir, trash_file_name);
589 cleanup:
590 SHFree(trash_file_name);
591 SHFree(trash_path);
592 return ret;
595 BOOL TRASH_TrashFile(LPCWSTR wszPath)
597 char *unix_path;
598 BOOL result;
600 TRACE("(%s)\n", debugstr_w(wszPath));
601 if (!TRASH_EnsureInitialized()) return FALSE;
602 if (!(unix_path = wine_get_unix_file_name(wszPath)))
603 return FALSE;
604 result = TRASH_MoveFileToBucket(home_trash, unix_path);
605 heap_free(unix_path);
606 return result;
609 static HRESULT TRASH_GetDetails(const TRASH_BUCKET *bucket, LPCSTR filename, WIN32_FIND_DATAW *data)
611 LPSTR path = NULL;
612 XDG_PARSED_FILE *parsed = NULL;
613 char *original_file_name = NULL;
614 char *deletion_date = NULL;
615 int fd = -1;
616 struct stat stats;
617 HRESULT ret = S_FALSE;
618 LPWSTR original_dos_name;
619 int suffix_length = lstrlenA(trashinfo_suffix);
620 int filename_length = lstrlenA(filename);
621 int files_length = lstrlenA(bucket->files_dir);
622 int path_length = max(lstrlenA(bucket->info_dir), files_length);
624 path = SHAlloc(path_length + filename_length + 1);
625 if (path == NULL) return E_OUTOFMEMORY;
626 wsprintfA(path, "%s%s", bucket->files_dir, filename);
627 path[path_length + filename_length - suffix_length] = 0; /* remove the '.trashinfo' */
628 if (lstat(path, &stats) == -1)
630 ERR("Error accessing data file for trashinfo %s (errno=%d)\n", filename, errno);
631 goto failed;
634 wsprintfA(path, "%s%s", bucket->info_dir, filename);
635 fd = open(path, O_RDONLY);
636 if (fd == -1)
638 ERR("Couldn't open trashinfo file %s (errno=%d)\n", path, errno);
639 goto failed;
642 parsed = XDG_ParseDesktopFile(fd);
643 if (parsed == NULL)
645 ERR("Parse error in trashinfo file %s\n", path);
646 goto failed;
649 original_file_name = XDG_GetStringValue(parsed, trashinfo_group, "Path", XDG_URLENCODE);
650 if (original_file_name == NULL)
652 ERR("No 'Path' entry in trashinfo file\n");
653 goto failed;
656 ZeroMemory(data, sizeof(*data));
657 data->nFileSizeHigh = (DWORD)((LONGLONG)stats.st_size>>32);
658 data->nFileSizeLow = stats.st_size & 0xffffffff;
659 RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER *)&data->ftLastWriteTime);
661 original_dos_name = wine_get_dos_file_name(original_file_name);
662 if (original_dos_name != NULL)
664 lstrcpynW(data->cFileName, original_dos_name, MAX_PATH);
665 SHFree(original_dos_name);
667 else
669 /* show only the file name */
670 char *file = strrchr(original_file_name, '/');
671 if (file == NULL)
672 file = original_file_name;
673 MultiByteToWideChar(CP_UNIXCP, 0, file, -1, data->cFileName, MAX_PATH);
676 deletion_date = XDG_GetStringValue(parsed, trashinfo_group, "DeletionDate", 0);
677 if (deletion_date)
679 struct tm del_time;
680 time_t del_secs;
682 sscanf(deletion_date, "%d-%d-%dT%d:%d:%d",
683 &del_time.tm_year, &del_time.tm_mon, &del_time.tm_mday,
684 &del_time.tm_hour, &del_time.tm_min, &del_time.tm_sec);
685 del_time.tm_year -= 1900;
686 del_time.tm_mon--;
687 del_time.tm_isdst = -1;
688 del_secs = mktime(&del_time);
690 RtlSecondsSince1970ToTime(del_secs, (LARGE_INTEGER *)&data->ftLastAccessTime);
693 ret = S_OK;
694 failed:
695 SHFree(path);
696 SHFree(original_file_name);
697 SHFree(deletion_date);
698 if (fd != -1)
699 close(fd);
700 XDG_FreeParsedFile(parsed);
701 return ret;
704 static INT CALLBACK free_item_callback(void *item, void *lParam)
706 SHFree(item);
707 return TRUE;
710 static HDPA enum_bucket_trashinfos(const TRASH_BUCKET *bucket, int *count)
712 HDPA ret = DPA_Create(32);
713 struct dirent *entry;
714 DIR *dir = NULL;
716 errno = ENOMEM;
717 *count = 0;
718 if (ret == NULL) goto failed;
719 dir = opendir(bucket->info_dir);
720 if (dir == NULL) goto failed;
721 while ((entry = readdir(dir)) != NULL)
723 LPSTR filename;
724 int namelen = lstrlenA(entry->d_name);
725 int suffixlen = lstrlenA(trashinfo_suffix);
726 if (namelen <= suffixlen ||
727 lstrcmpA(entry->d_name+namelen-suffixlen, trashinfo_suffix) != 0)
728 continue;
730 filename = StrDupA(entry->d_name);
731 if (filename == NULL)
732 goto failed;
733 if (DPA_InsertPtr(ret, DPA_APPEND, filename) == -1)
735 LocalFree(filename);
736 goto failed;
738 (*count)++;
740 closedir(dir);
741 return ret;
742 failed:
743 if (dir) closedir(dir);
744 if (ret)
745 DPA_DestroyCallback(ret, free_item_callback, NULL);
746 return NULL;
749 HRESULT TRASH_EnumItems(const WCHAR *path, LPITEMIDLIST **pidls, int *count)
751 int ti_count;
752 int pos=0, i;
753 HRESULT err = E_OUTOFMEMORY;
754 HDPA tinfs;
756 if(path)
757 FIXME("Ignoring path = %s\n", debugstr_w(path));
759 if (!TRASH_EnsureInitialized()) return E_FAIL;
760 tinfs = enum_bucket_trashinfos(home_trash, &ti_count);
761 if (tinfs == NULL) return E_FAIL;
762 *pidls = SHAlloc(sizeof(LPITEMIDLIST)*ti_count);
763 if (!*pidls) goto failed;
764 for (i=0; i<ti_count; i++)
766 WIN32_FIND_DATAW data;
767 LPCSTR filename;
769 filename = DPA_GetPtr(tinfs, i);
770 if (FAILED(err = TRASH_GetDetails(home_trash, filename, &data)))
771 goto failed;
772 if (err == S_FALSE)
773 continue;
774 if (FAILED(err = TRASH_CreateSimplePIDL(filename, &data, &(*pidls)[pos])))
775 goto failed;
776 pos++;
778 *count = pos;
779 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
780 return S_OK;
781 failed:
782 if (*pidls != NULL)
784 int j;
785 for (j=0; j<pos; j++)
786 SHFree((*pidls)[j]);
787 SHFree(*pidls);
789 DPA_DestroyCallback(tinfs, free_item_callback, NULL);
791 return err;
794 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl){
795 int suffix_length = strlen(trashinfo_suffix);
796 LPCSHITEMID id = &(pidl->mkid);
797 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
798 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
799 char *restore_path;
800 WIN32_FIND_DATAW data;
801 char *file_path;
803 TRACE("(%p)\n",pidl);
804 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
806 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
807 return E_INVALIDARG;
809 TRASH_UnpackItemID(id,&data);
810 restore_path = wine_get_unix_file_name(data.cFileName);
811 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
812 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
813 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
814 if(!rename(file_path,restore_path))
816 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
817 if(unlink(file_path))
818 WARN("failed to delete the trashinfo file %s\n",filename);
820 else
821 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
822 SHFree(file_path);
823 heap_free(restore_path);
824 return S_OK;
827 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
829 int suffix_length = strlen(trashinfo_suffix);
831 LPCSHITEMID id = &(pidl->mkid);
832 const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
833 const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
834 char *file_path;
836 TRACE("(%p)\n",pidl);
837 if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
839 ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
840 return E_INVALIDARG;
842 file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
843 sprintf(file_path,"%s%s",home_trash->info_dir,filename);
844 if(unlink(file_path))
845 WARN("failed to delete the trashinfo file %s\n",filename);
846 sprintf(file_path,"%s%s",home_trash->files_dir,filename);
847 file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
848 if(unlink(file_path))
849 WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
850 SHFree(file_path);
851 return S_OK;
854 #endif /* __APPLE__ */