shell32: Indentation fix.
[wine] / dlls / shell32 / trash.c
1 /*
2  * The freedesktop.org Trash, implemented using the 0.7 spec version
3  * (see http://www.ramendik.ru/docs/trashspec.html)
4  *
5  * Copyright (C) 2006 Mikolaj Zalewski
6  * Copyright 2011 Jay Yang
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
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
33
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
50
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"
60
61 WINE_DEFAULT_DEBUG_CHANNEL(trash);
62
63 #ifdef HAVE_CORESERVICES_CORESERVICES_H
64
65 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
66 {
67     char *unix_path;
68     OSStatus status;
69     FSRef ref;
70     FSCatalogInfo catalogInfo;
71
72     TRACE("(%s)\n", debugstr_w(wszPath));
73     if (!(unix_path = wine_get_unix_file_name(wszPath)))
74         return FALSE;
75
76     status = FSPathMakeRef((UInt8*)unix_path, &ref, NULL);
77     HeapFree(GetProcessHeap(), 0, unix_path);
78     if (status == noErr)
79         status = FSGetCatalogInfo(&ref, kFSCatInfoVolume, &catalogInfo, NULL,
80                                   NULL, NULL);
81     if (status == noErr)
82         status = FSFindFolder(catalogInfo.volume, kTrashFolderType,
83                               kCreateFolder, &ref);
84
85     return (status == noErr);
86 }
87
88 BOOL TRASH_TrashFile(LPCWSTR wszPath)
89 {
90     char *unix_path;
91     OSStatus status;
92
93     TRACE("(%s)\n", debugstr_w(wszPath));
94     if (!(unix_path = wine_get_unix_file_name(wszPath)))
95         return FALSE;
96
97     status = FSPathMoveObjectToTrashSync(unix_path, NULL, kFSFileOperationSkipPreflight);
98
99     HeapFree(GetProcessHeap(), 0, unix_path);
100     return (status == noErr);
101 }
102
103 HRESULT TRASH_EnumItems(LPITEMIDLIST **pidls, int *count)
104 {
105     FIXME("stub!\n");
106     return E_NOTIMPL;
107 }
108
109 HRESULT TRASH_UnpackItemID(LPCSHITEMID id, WIN32_FIND_DATAW *data)
110 {
111     FIXME("stub!\n");
112     return E_NOTIMPL;
113 }
114
115 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl)
116 {
117     FIXME("stub!\n");
118     return E_NOTIMPL;
119 }
120
121 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
122 {
123     FIXME("stub!\n");
124     return E_NOTIMPL;
125 }
126
127 #else /* HAVE_CORESERVICES_CORESERVICES_H */
128
129 static CRITICAL_SECTION TRASH_Creating;
130 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug =
131 {
132     0, 0, &TRASH_Creating,
133     { &TRASH_Creating_Debug.ProcessLocksList,
134       &TRASH_Creating_Debug.ProcessLocksList},
135     0, 0, { (DWORD_PTR)__FILE__ ": TRASH_Creating"}
136 };
137 static CRITICAL_SECTION TRASH_Creating = { &TRASH_Creating_Debug, -1, 0, 0, 0, 0 };
138
139 static const char trashinfo_suffix[] = ".trashinfo";
140 static const char trashinfo_header[] = "[Trash Info]\n";
141 static const char trashinfo_group[] = "Trash Info";
142
143 typedef struct
144 {
145     char *info_dir;
146     char *files_dir;
147     dev_t device;
148 } TRASH_BUCKET;
149
150 static TRASH_BUCKET *home_trash=NULL;
151
152 static char *init_home_dir(const char *subpath)
153 {
154     char *path = XDG_BuildPath(XDG_DATA_HOME, subpath);
155     if (path == NULL) return NULL;
156     if (!XDG_MakeDirs(path))
157     {
158         ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path), errno);
159         SHFree(path);
160         path=NULL;
161     }
162     return path;
163 }
164
165 static TRASH_BUCKET *TRASH_CreateHomeBucket(void)
166 {
167     TRASH_BUCKET *bucket;
168     struct stat trash_stat;
169     char *trash_path = NULL;
170     
171     bucket = SHAlloc(sizeof(TRASH_BUCKET));
172     if (bucket == NULL)
173     {
174         errno = ENOMEM;
175         goto error;
176     }
177     memset(bucket, 0, sizeof(*bucket));
178     bucket->info_dir = init_home_dir("Trash/info/");
179     if (bucket->info_dir == NULL) goto error;
180     bucket->files_dir = init_home_dir("Trash/files/");
181     if (bucket->files_dir == NULL) goto error;
182     
183     trash_path = XDG_BuildPath(XDG_DATA_HOME, "Trash/");
184     if (stat(trash_path, &trash_stat) == -1)
185         goto error;
186     bucket->device = trash_stat.st_dev;
187     SHFree(trash_path);
188     return bucket;
189 error:
190     SHFree(trash_path);
191     if (bucket)
192     {
193         SHFree(bucket->info_dir);
194         SHFree(bucket->files_dir);
195     }
196     SHFree(bucket);
197     return NULL;
198 }
199
200 static BOOL TRASH_EnsureInitialized(void)
201 {
202     if (home_trash == NULL)
203     {
204         EnterCriticalSection(&TRASH_Creating);
205         if (home_trash == NULL)
206             home_trash = TRASH_CreateHomeBucket();
207         LeaveCriticalSection(&TRASH_Creating);
208     }
209
210     if (home_trash == NULL)
211     {
212         ERR("Couldn't initialize home trash (errno=%d)\n", errno);
213         return FALSE;
214     }
215     return TRUE;
216 }
217
218 static BOOL file_good_for_bucket(const TRASH_BUCKET *pBucket, const struct stat *file_stat)
219 {
220     if (pBucket->device != file_stat->st_dev)
221         return FALSE;
222     return TRUE;
223 }
224
225 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
226 {
227     struct stat file_stat;
228     char *unix_path;
229     
230     TRACE("(%s)\n", debugstr_w(wszPath));
231     if (!TRASH_EnsureInitialized()) return FALSE;
232     if (!(unix_path = wine_get_unix_file_name(wszPath)))
233         return FALSE;
234     if (lstat(unix_path, &file_stat)==-1)
235     {
236         HeapFree(GetProcessHeap(), 0, unix_path);
237         return FALSE;
238     }
239     HeapFree(GetProcessHeap(), 0, unix_path);
240     return file_good_for_bucket(home_trash, &file_stat);
241 }
242
243 /*
244  * Try to create a single .trashinfo file. Return TRUE if successful, else FALSE
245  */
246 static BOOL try_create_trashinfo_file(const char *info_dir, const char *file_name,
247     const char *original_file_name)
248 {
249     SYSTEMTIME curr_time;
250     char datebuf[200];
251     char *path = SHAlloc(strlen(info_dir)+strlen(file_name)+strlen(trashinfo_suffix)+1);
252     int writer = -1;
253     
254     if (path==NULL) return FALSE;
255     wsprintfA(path, "%s%s%s", info_dir, file_name, trashinfo_suffix);
256     TRACE("Trying to create '%s'\n", path);
257     writer = open(path, O_CREAT|O_WRONLY|O_TRUNC|O_EXCL, 0600);
258     if (writer==-1) goto error;
259     
260     write(writer, trashinfo_header, strlen(trashinfo_header));
261     if (!XDG_WriteDesktopStringEntry(writer, "Path", XDG_URLENCODE, original_file_name))
262         goto error;
263
264     GetLocalTime( &curr_time );
265     wnsprintfA(datebuf, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
266                curr_time.wYear, curr_time.wMonth, curr_time.wDay,
267                curr_time.wHour, curr_time.wMinute, curr_time.wSecond);
268     if (!XDG_WriteDesktopStringEntry(writer, "DeletionDate", 0, datebuf))
269         goto error;
270     close(writer);
271     SHFree(path);
272     return TRUE;
273
274 error:
275     if (writer != -1)
276     {
277         close(writer);
278         unlink(path);
279     }
280     SHFree(path);
281     return FALSE;
282 }
283
284 /*
285  * Try to create a .trashinfo file. This function will make several attempts with
286  * different filenames. It will return the filename that succeeded or NULL if a file
287  * couldn't be created.
288  */
289 static char *create_trashinfo(const char *info_dir, const char *file_path)
290 {
291     const char *base_name;
292     char *filename_buffer;
293     ULONG seed = GetTickCount();
294     int i;
295
296     errno = ENOMEM;       /* out-of-memory is the only case when errno isn't set */
297     base_name = strrchr(file_path, '/');
298     if (base_name == NULL)
299         base_name = file_path;
300     else
301         base_name++;
302
303     filename_buffer = SHAlloc(strlen(base_name)+9+1);
304     if (filename_buffer == NULL)
305         return NULL;
306     lstrcpyA(filename_buffer, base_name);
307     if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
308         return filename_buffer;
309     for (i=0; i<30; i++)
310     {
311         sprintf(filename_buffer, "%s-%d", base_name, i+1);
312         if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
313             return filename_buffer;
314     }
315     
316     for (i=0; i<1000; i++)
317     {
318         sprintf(filename_buffer, "%s-%08x", base_name, RtlRandom(&seed));
319         if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
320             return filename_buffer;
321     }
322     
323     WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno);
324     SHFree(filename_buffer);
325     return NULL;
326 }
327
328 static void remove_trashinfo_file(const char *info_dir, const char *base_name)
329 {
330     char *filename_buffer;
331     
332     filename_buffer = SHAlloc(lstrlenA(info_dir)+lstrlenA(base_name)+lstrlenA(trashinfo_suffix)+1);
333     if (filename_buffer == NULL) return;
334     sprintf(filename_buffer, "%s%s%s", info_dir, base_name, trashinfo_suffix);
335     unlink(filename_buffer);
336     SHFree(filename_buffer);
337 }
338
339 static BOOL TRASH_MoveFileToBucket(TRASH_BUCKET *pBucket, const char *unix_path)
340 {
341     struct stat file_stat;
342     char *trash_file_name = NULL;
343     char *trash_path = NULL;
344     BOOL ret = TRUE;
345
346     if (lstat(unix_path, &file_stat)==-1)
347         return FALSE;
348     if (!file_good_for_bucket(pBucket, &file_stat))
349         return FALSE;
350         
351     trash_file_name = create_trashinfo(pBucket->info_dir, unix_path);
352     if (trash_file_name == NULL)
353         return FALSE;
354         
355     trash_path = SHAlloc(strlen(pBucket->files_dir)+strlen(trash_file_name)+1);
356     if (trash_path == NULL) goto error;
357     lstrcpyA(trash_path, pBucket->files_dir);
358     lstrcatA(trash_path, trash_file_name);
359     
360     if (rename(unix_path, trash_path)==0)
361     {
362         TRACE("rename succeeded\n");
363         goto cleanup;
364     }
365     
366     /* TODO: try to manually move the file */
367     ERR("Couldn't move file\n");
368 error:
369     ret = FALSE;
370     remove_trashinfo_file(pBucket->info_dir, trash_file_name);
371 cleanup:
372     SHFree(trash_file_name);
373     SHFree(trash_path);
374     return ret;
375 }
376
377 BOOL TRASH_TrashFile(LPCWSTR wszPath)
378 {
379     char *unix_path;
380     BOOL result;
381     
382     TRACE("(%s)\n", debugstr_w(wszPath));
383     if (!TRASH_EnsureInitialized()) return FALSE;
384     if (!(unix_path = wine_get_unix_file_name(wszPath)))
385         return FALSE;
386     result = TRASH_MoveFileToBucket(home_trash, unix_path);
387     HeapFree(GetProcessHeap(), 0, unix_path);
388     return result;
389 }
390
391 /*
392  * The item ID of a trashed element is built as follows:
393  *  NUL byte                    - in most PIDLs the first byte is the type so we keep it constant
394  *  WIN32_FIND_DATAW structure  - with data about original file attributes
395  *  bucket name                 - currently only an empty string meaning the home bucket is supported
396  *  trash file name             - a NUL-terminated string
397  */
398 static HRESULT TRASH_CreateSimplePIDL(LPCSTR filename, const WIN32_FIND_DATAW *data, LPITEMIDLIST *pidlOut)
399 {
400     LPITEMIDLIST pidl = SHAlloc(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1+2);
401     *pidlOut = NULL;
402     if (pidl == NULL)
403         return E_OUTOFMEMORY;
404     pidl->mkid.cb = (USHORT)(2+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1);
405     pidl->mkid.abID[0] = 0;
406     memcpy(pidl->mkid.abID+1, data, sizeof(WIN32_FIND_DATAW));
407     pidl->mkid.abID[1+sizeof(WIN32_FIND_DATAW)] = 0;
408     lstrcpyA((LPSTR)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1), filename);
409     *(USHORT *)(pidl->mkid.abID+1+sizeof(WIN32_FIND_DATAW)+1+lstrlenA(filename)+1) = 0;
410     *pidlOut = pidl;
411     return S_OK;
412 }
413
414 /***********************************************************************
415  *      TRASH_UnpackItemID [Internal]
416  *
417  * DESCRIPTION:
418  * Extract the information stored in an Item ID. The WIN32_FIND_DATA contains
419  * the information about the original file. The data->ftLastAccessTime contains
420  * the deletion time
421  *
422  * PARAMETER(S):
423  * [I] id : the ID of the item
424  * [O] data : the WIN32_FIND_DATA of the original file. Can be NULL is not needed
425  */                 
426 HRESULT TRASH_UnpackItemID(LPCSHITEMID id, WIN32_FIND_DATAW *data)
427 {
428     if (id->cb < 2+1+sizeof(WIN32_FIND_DATAW)+2)
429         return E_INVALIDARG;
430     if (id->abID[0] != 0 || id->abID[1+sizeof(WIN32_FIND_DATAW)] != 0)
431         return E_INVALIDARG;
432     if (memchr(id->abID+1+sizeof(WIN32_FIND_DATAW)+1, 0, id->cb-(2+1+sizeof(WIN32_FIND_DATAW)+1)) == NULL)
433         return E_INVALIDARG;
434
435     if (data != NULL)
436         *data = *(const WIN32_FIND_DATAW *)(id->abID+1);
437     return S_OK;
438 }
439
440 static HRESULT TRASH_GetDetails(const TRASH_BUCKET *bucket, LPCSTR filename, WIN32_FIND_DATAW *data)
441 {
442     LPSTR path = NULL;
443     XDG_PARSED_FILE *parsed = NULL;
444     char *original_file_name = NULL;
445     char *deletion_date = NULL;
446     int fd = -1;
447     struct stat stats;
448     HRESULT ret = S_FALSE;
449     LPWSTR original_dos_name;
450     int suffix_length = lstrlenA(trashinfo_suffix);
451     int filename_length = lstrlenA(filename);
452     int files_length = lstrlenA(bucket->files_dir);
453     int path_length = max(lstrlenA(bucket->info_dir), files_length);
454     
455     path = SHAlloc(path_length + filename_length + 1);
456     if (path == NULL) return E_OUTOFMEMORY;
457     wsprintfA(path, "%s%s", bucket->files_dir, filename);
458     path[path_length + filename_length - suffix_length] = 0;  /* remove the '.trashinfo' */    
459     if (lstat(path, &stats) == -1)
460     {
461         ERR("Error accessing data file for trashinfo %s (errno=%d)\n", filename, errno);
462         goto failed;
463     }
464     
465     wsprintfA(path, "%s%s", bucket->info_dir, filename);
466     fd = open(path, O_RDONLY);
467     if (fd == -1)
468     {
469         ERR("Couldn't open trashinfo file %s (errno=%d)\n", path, errno);
470         goto failed;
471     }
472     
473     parsed = XDG_ParseDesktopFile(fd);
474     if (parsed == NULL)
475     {
476         ERR("Parse error in trashinfo file %s\n", path);
477         goto failed;
478     }
479     
480     original_file_name = XDG_GetStringValue(parsed, trashinfo_group, "Path", XDG_URLENCODE);
481     if (original_file_name == NULL)
482     {
483         ERR("No 'Path' entry in trashinfo file\n");
484         goto failed;
485     }
486     
487     ZeroMemory(data, sizeof(*data));
488     data->nFileSizeHigh = (DWORD)((LONGLONG)stats.st_size>>32);
489     data->nFileSizeLow = stats.st_size & 0xffffffff;
490     RtlSecondsSince1970ToTime(stats.st_mtime, (LARGE_INTEGER *)&data->ftLastWriteTime);
491     
492     original_dos_name = wine_get_dos_file_name(original_file_name);
493     if (original_dos_name != NULL)
494     {
495         lstrcpynW(data->cFileName, original_dos_name, MAX_PATH);
496         SHFree(original_dos_name);
497     }
498     else
499     {
500         /* show only the file name */
501         char *file = strrchr(original_file_name, '/');
502         if (file == NULL)
503             file = original_file_name;
504         MultiByteToWideChar(CP_UNIXCP, 0, file, -1, data->cFileName, MAX_PATH);
505     }
506     
507     deletion_date = XDG_GetStringValue(parsed, trashinfo_group, "DeletionDate", 0);
508     if (deletion_date)
509     {
510         struct tm del_time;
511         time_t del_secs;
512         
513         sscanf(deletion_date, "%d-%d-%dT%d:%d:%d",
514             &del_time.tm_year, &del_time.tm_mon, &del_time.tm_mday,
515             &del_time.tm_hour, &del_time.tm_min, &del_time.tm_sec);
516         del_time.tm_year -= 1900;
517         del_time.tm_mon--;
518         del_secs = mktime(&del_time);
519         
520         RtlSecondsSince1970ToTime(del_secs, (LARGE_INTEGER *)&data->ftLastAccessTime);
521     }
522     
523     ret = S_OK;
524 failed:
525     SHFree(path);
526     SHFree(original_file_name);
527     SHFree(deletion_date);
528     if (fd != -1)
529         close(fd);
530     XDG_FreeParsedFile(parsed);
531     return ret;
532 }
533
534 static INT CALLBACK free_item_callback(void *item, void *lParam)
535 {
536     SHFree(item);
537     return TRUE;
538 }
539
540 static HDPA enum_bucket_trashinfos(const TRASH_BUCKET *bucket, int *count)
541 {
542     HDPA ret = DPA_Create(32);
543     struct dirent *entry;
544     DIR *dir = NULL;
545     
546     errno = ENOMEM;
547     *count = 0;
548     if (ret == NULL) goto failed;
549     dir = opendir(bucket->info_dir);
550     if (dir == NULL) goto failed;
551     while ((entry = readdir(dir)) != NULL)
552     {
553         LPSTR filename;
554         int namelen = lstrlenA(entry->d_name);
555         int suffixlen = lstrlenA(trashinfo_suffix);
556         if (namelen <= suffixlen ||
557                 lstrcmpA(entry->d_name+namelen-suffixlen, trashinfo_suffix) != 0)
558             continue;
559
560         filename = StrDupA(entry->d_name);
561         if (filename == NULL)
562             goto failed;
563         if (DPA_InsertPtr(ret, DPA_APPEND, filename) == -1)
564         {
565             SHFree(filename);
566             goto failed;
567         }
568         (*count)++;
569     }
570     closedir(dir);
571     return ret;
572 failed:
573     if (dir) closedir(dir);
574     if (ret)
575         DPA_DestroyCallback(ret, free_item_callback, NULL);
576     return NULL;
577 }
578
579 HRESULT TRASH_EnumItems(LPITEMIDLIST **pidls, int *count)
580 {
581     int ti_count;
582     int pos=0, i;
583     HRESULT err = E_OUTOFMEMORY;
584     HDPA tinfs;
585     
586     if (!TRASH_EnsureInitialized()) return E_FAIL;
587     tinfs = enum_bucket_trashinfos(home_trash, &ti_count);
588     if (tinfs == NULL) return E_FAIL;
589     *pidls = SHAlloc(sizeof(LPITEMIDLIST)*ti_count);
590     if (!*pidls) goto failed;
591     for (i=0; i<ti_count; i++)
592     {
593         WIN32_FIND_DATAW data;
594         LPCSTR filename;
595         
596         filename = DPA_GetPtr(tinfs, i);
597         if (FAILED(err = TRASH_GetDetails(home_trash, filename, &data)))
598             goto failed;
599         if (err == S_FALSE)
600             continue;
601         if (FAILED(err = TRASH_CreateSimplePIDL(filename, &data, &(*pidls)[pos])))
602             goto failed;
603         pos++;
604     }
605     *count = pos;
606     DPA_DestroyCallback(tinfs, free_item_callback, NULL);
607     return S_OK;
608 failed:
609     if (*pidls != NULL)
610     {
611         int j;
612         for (j=0; j<pos; j++)
613             SHFree((*pidls)[j]);
614         SHFree(*pidls);
615     }
616     DPA_DestroyCallback(tinfs, free_item_callback, NULL);
617     
618     return err;
619 }
620
621 HRESULT TRASH_RestoreItem(LPCITEMIDLIST pidl){
622     int suffix_length = strlen(trashinfo_suffix);
623     LPCSHITEMID id = &(pidl->mkid);
624     const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
625     const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
626     char *restore_path;
627     WIN32_FIND_DATAW data;
628     char *file_path;
629
630     TRACE("(%p)\n",pidl);
631     if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
632     {
633         ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
634         return E_INVALIDARG;
635     }
636     TRASH_UnpackItemID(id,&data);
637     restore_path = wine_get_unix_file_name(data.cFileName);
638     file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
639     sprintf(file_path,"%s%s",home_trash->files_dir,filename);
640     file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
641     if(!rename(file_path,restore_path))
642     {
643             sprintf(file_path,"%s%s",home_trash->info_dir,filename);
644             if(unlink(file_path))
645                 WARN("failed to delete the trashinfo file %s\n",filename);
646     }
647     else
648         WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
649     SHFree(file_path);
650     HeapFree(GetProcessHeap(), 0, restore_path);
651     return S_OK;
652 }
653
654 HRESULT TRASH_EraseItem(LPCITEMIDLIST pidl)
655 {
656     int suffix_length = strlen(trashinfo_suffix);
657
658     LPCSHITEMID id = &(pidl->mkid);
659     const char *bucket_name = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW));
660     const char *filename = (const char*)(id->abID+1+sizeof(WIN32_FIND_DATAW)+strlen(bucket_name)+1);
661     char *file_path;
662
663     TRACE("(%p)\n",pidl);
664     if(strcmp(filename+strlen(filename)-suffix_length,trashinfo_suffix))
665     {
666         ERR("pidl at %p is not a valid recycle bin entry\n",pidl);
667         return E_INVALIDARG;
668     }
669     file_path = SHAlloc(max(strlen(home_trash->files_dir),strlen(home_trash->info_dir))+strlen(filename)+1);
670     sprintf(file_path,"%s%s",home_trash->info_dir,filename);
671     if(unlink(file_path))
672         WARN("failed to delete the trashinfo file %s\n",filename);
673     sprintf(file_path,"%s%s",home_trash->files_dir,filename);
674     file_path[strlen(home_trash->files_dir)+strlen(filename)-suffix_length] = '\0';
675     if(unlink(file_path))
676         WARN("could not erase %s from the trash (errno=%i)\n",filename,errno);
677     SHFree(file_path);
678     return S_OK;
679 }
680
681 #endif /* HAVE_CORESERVICES_CORESERVICES_H */