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
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #ifdef HAVE_SYS_STAT_H
26 # include <sys/stat.h>
28 #include <sys/types.h>
48 #include "wine/debug.h"
49 #include "shell32_main.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(trash
);
54 static CRITICAL_SECTION TRASH_Creating
;
55 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug
=
57 0, 0, &TRASH_Creating
,
58 { &TRASH_Creating_Debug
.ProcessLocksList
,
59 &TRASH_Creating_Debug
.ProcessLocksList
},
60 0, 0, { (DWORD_PTR
)__FILE__
": TRASH_Creating"}
62 static CRITICAL_SECTION TRASH_Creating
= { &TRASH_Creating_Debug
, -1, 0, 0, 0, 0 };
64 static const char trashinfo_suffix
[] = ".trashinfo";
65 static const char trashinfo_header
[] = "[Trash Info]\n";
66 static const char trashinfo_group
[] = "Trash Info";
75 static TRASH_BUCKET
*home_trash
=NULL
;
77 static char *init_home_dir(const char *subpath
)
79 char *path
= XDG_BuildPath(XDG_DATA_HOME
, subpath
);
80 if (path
== NULL
) return NULL
;
81 if (!XDG_MakeDirs(path
))
83 ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path
), errno
);
90 static TRASH_BUCKET
*TRASH_CreateHomeBucket(void)
93 struct stat trash_stat
;
94 char *trash_path
= NULL
;
96 bucket
= SHAlloc(sizeof(TRASH_BUCKET
));
102 memset(bucket
, 0, sizeof(*bucket
));
103 bucket
->info_dir
= init_home_dir("Trash/info/");
104 if (bucket
->info_dir
== NULL
) goto error
;
105 bucket
->files_dir
= init_home_dir("Trash/files/");
106 if (bucket
->files_dir
== NULL
) goto error
;
108 trash_path
= XDG_BuildPath(XDG_DATA_HOME
, "Trash/");
109 if (stat(trash_path
, &trash_stat
) == -1)
111 bucket
->device
= trash_stat
.st_dev
;
118 SHFree(bucket
->info_dir
);
119 SHFree(bucket
->files_dir
);
125 static BOOL
TRASH_EnsureInitialized(void)
127 if (home_trash
== NULL
)
129 EnterCriticalSection(&TRASH_Creating
);
130 if (home_trash
== NULL
)
131 home_trash
= TRASH_CreateHomeBucket();
132 LeaveCriticalSection(&TRASH_Creating
);
135 if (home_trash
== NULL
)
137 ERR("Couldn't initialize home trash (errno=%d)\n", errno
);
143 static BOOL
file_good_for_bucket(const TRASH_BUCKET
*pBucket
, const struct stat
*file_stat
)
145 if (pBucket
->device
!= file_stat
->st_dev
)
150 BOOL
TRASH_CanTrashFile(LPCWSTR wszPath
)
152 struct stat file_stat
;
155 TRACE("(%s)\n", debugstr_w(wszPath
));
156 if (!TRASH_EnsureInitialized()) return FALSE
;
157 if (!(unix_path
= wine_get_unix_file_name(wszPath
)))
159 if (lstat(unix_path
, &file_stat
)==-1)
161 HeapFree(GetProcessHeap(), 0, unix_path
);
164 HeapFree(GetProcessHeap(), 0, unix_path
);
165 return file_good_for_bucket(home_trash
, &file_stat
);
169 * Try to create a single .trashinfo file. Return TRUE if successful, else FALSE
171 static BOOL
try_create_trashinfo_file(const char *info_dir
, const char *file_name
,
172 const char *original_file_name
)
174 SYSTEMTIME curr_time
;
176 char *path
= SHAlloc(strlen(info_dir
)+strlen(file_name
)+strlen(trashinfo_suffix
)+1);
179 if (path
==NULL
) return FALSE
;
180 wsprintfA(path
, "%s%s%s", info_dir
, file_name
, trashinfo_suffix
);
181 TRACE("Trying to create '%s'\n", path
);
182 writer
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_EXCL
, 0600);
183 if (writer
==-1) goto error
;
185 write(writer
, trashinfo_header
, strlen(trashinfo_header
));
186 if (!XDG_WriteDesktopStringEntry(writer
, "Path", XDG_URLENCODE
, original_file_name
))
189 GetLocalTime( &curr_time
);
190 wnsprintfA(datebuf
, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
191 curr_time
.wYear
, curr_time
.wMonth
, curr_time
.wDay
,
192 curr_time
.wHour
, curr_time
.wMinute
, curr_time
.wSecond
);
193 if (!XDG_WriteDesktopStringEntry(writer
, "DeletionDate", 0, datebuf
))
210 * Try to create a .trashinfo file. This function will make several attempts with
211 * different filenames. It will return the filename that succeded or NULL if a file
212 * couldn't be created.
214 static char *create_trashinfo(const char *info_dir
, const char *file_path
)
216 const char *base_name
;
217 char *filename_buffer
;
218 ULONG seed
= GetTickCount();
221 errno
= ENOMEM
; /* out-of-memory is the only case when errno isn't set */
222 base_name
= strrchr(file_path
, '/');
223 if (base_name
== NULL
)
224 base_name
= file_path
;
228 filename_buffer
= SHAlloc(strlen(base_name
)+9+1);
229 if (filename_buffer
== NULL
)
231 lstrcpyA(filename_buffer
, base_name
);
232 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
233 return filename_buffer
;
236 sprintf(filename_buffer
, "%s-%d", base_name
, i
+1);
237 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
238 return filename_buffer
;
241 for (i
=0; i
<1000; i
++)
243 sprintf(filename_buffer
, "%s-%08x", base_name
, RtlRandom(&seed
));
244 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
245 return filename_buffer
;
248 WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno
);
249 SHFree(filename_buffer
);
253 static void remove_trashinfo_file(const char *info_dir
, const char *base_name
)
255 char *filename_buffer
;
257 filename_buffer
= SHAlloc(lstrlenA(info_dir
)+lstrlenA(base_name
)+lstrlenA(trashinfo_suffix
)+1);
258 if (filename_buffer
== NULL
) return;
259 sprintf(filename_buffer
, "%s%s%s", info_dir
, base_name
, trashinfo_suffix
);
260 unlink(filename_buffer
);
261 SHFree(filename_buffer
);
264 static BOOL
TRASH_MoveFileToBucket(TRASH_BUCKET
*pBucket
, const char *unix_path
)
266 struct stat file_stat
;
267 char *trash_file_name
= NULL
;
268 char *trash_path
= NULL
;
271 if (lstat(unix_path
, &file_stat
)==-1)
273 if (!file_good_for_bucket(pBucket
, &file_stat
))
276 trash_file_name
= create_trashinfo(pBucket
->info_dir
, unix_path
);
277 if (trash_file_name
== NULL
)
280 trash_path
= SHAlloc(strlen(pBucket
->files_dir
)+strlen(trash_file_name
)+1);
281 if (trash_path
== NULL
) goto error
;
282 lstrcpyA(trash_path
, pBucket
->files_dir
);
283 lstrcatA(trash_path
, trash_file_name
);
285 if (rename(unix_path
, trash_path
)==0)
287 TRACE("rename succeded\n");
291 /* TODO: try to manually move the file */
292 ERR("Couldn't move file\n");
295 remove_trashinfo_file(pBucket
->info_dir
, trash_file_name
);
297 SHFree(trash_file_name
);
302 BOOL
TRASH_TrashFile(LPCWSTR wszPath
)
307 TRACE("(%s)\n", debugstr_w(wszPath
));
308 if (!TRASH_EnsureInitialized()) return FALSE
;
309 if (!(unix_path
= wine_get_unix_file_name(wszPath
)))
311 result
= TRASH_MoveFileToBucket(home_trash
, unix_path
);
312 HeapFree(GetProcessHeap(), 0, unix_path
);
317 * The item ID of a trashed element is built as follows:
318 * NUL byte - in most PIDLs the first byte is the type so we keep it constant
319 * WIN32_FIND_DATAW structure - with data about original file attributes
320 * bucket name - currently only an empty string meaning the home bucket is supported
321 * trash file name - a NUL-terminated string
323 static HRESULT
TRASH_CreateSimplePIDL(LPCSTR filename
, const WIN32_FIND_DATAW
*data
, LPITEMIDLIST
*pidlOut
)
325 LPITEMIDLIST pidl
= SHAlloc(2+1+sizeof(WIN32_FIND_DATAW
)+1+lstrlenA(filename
)+1+2);
328 return E_OUTOFMEMORY
;
329 pidl
->mkid
.cb
= (USHORT
)(2+1+sizeof(WIN32_FIND_DATAW
)+1+lstrlenA(filename
)+1);
330 pidl
->mkid
.abID
[0] = 0;
331 memcpy(pidl
->mkid
.abID
+1, data
, sizeof(WIN32_FIND_DATAW
));
332 pidl
->mkid
.abID
[1+sizeof(WIN32_FIND_DATAW
)] = 0;
333 lstrcpyA((LPSTR
)(pidl
->mkid
.abID
+1+sizeof(WIN32_FIND_DATAW
)+1), filename
);
334 *(USHORT
*)(pidl
->mkid
.abID
+1+sizeof(WIN32_FIND_DATAW
)+1+lstrlenA(filename
)+1) = 0;
339 /***********************************************************************
340 * TRASH_UnpackItemID [Internal]
343 * Extract the information stored in an Item ID. The WIN32_FIND_DATA contains
344 * the information about the original file. The data->ftLastAccessTime contains
348 * [I] id : the ID of the item
349 * [O] data : the WIN32_FIND_DATA of the original file. Can be NULL is not needed
351 HRESULT
TRASH_UnpackItemID(LPCSHITEMID id
, WIN32_FIND_DATAW
*data
)
353 if (id
->cb
< 2+1+sizeof(WIN32_FIND_DATAW
)+2)
355 if (id
->abID
[0] != 0 || id
->abID
[1+sizeof(WIN32_FIND_DATAW
)] != 0)
357 if (memchr(id
->abID
+1+sizeof(WIN32_FIND_DATAW
)+1, 0, id
->cb
-(2+1+sizeof(WIN32_FIND_DATAW
)+1)) == NULL
)
361 *data
= *(WIN32_FIND_DATAW
*)(id
->abID
+1);
365 static HRESULT
TRASH_GetDetails(const TRASH_BUCKET
*bucket
, LPCSTR filename
, WIN32_FIND_DATAW
*data
)
368 XDG_PARSED_FILE
*parsed
= NULL
;
369 char *original_file_name
= NULL
;
370 char *deletion_date
= NULL
;
373 HRESULT ret
= S_FALSE
;
374 LPWSTR original_dos_name
;
375 int suffix_length
= lstrlenA(trashinfo_suffix
);
376 int filename_length
= lstrlenA(filename
);
377 int files_length
= lstrlenA(bucket
->files_dir
);
378 int path_length
= max(lstrlenA(bucket
->info_dir
), files_length
);
380 path
= SHAlloc(path_length
+ filename_length
+ 1);
381 if (path
== NULL
) return E_OUTOFMEMORY
;
382 wsprintfA(path
, "%s%s", bucket
->files_dir
, filename
);
383 path
[path_length
+ filename_length
- suffix_length
] = 0; /* remove the '.trashinfo' */
384 if (lstat(path
, &stats
) == -1)
386 ERR("Error accessing data file for trashinfo %s (errno=%d)\n", filename
, errno
);
390 wsprintfA(path
, "%s%s", bucket
->info_dir
, filename
);
391 fd
= open(path
, O_RDONLY
);
394 ERR("Couldn't open trashinfo file %s (errno=%d)\n", path
, errno
);
398 parsed
= XDG_ParseDesktopFile(fd
);
401 ERR("Parse error in trashinfo file %s\n", path
);
405 original_file_name
= XDG_GetStringValue(parsed
, trashinfo_group
, "Path", XDG_URLENCODE
);
406 if (original_file_name
== NULL
)
408 ERR("No 'Path' entry in trashinfo file\n");
412 ZeroMemory(data
, sizeof(*data
));
413 data
->nFileSizeHigh
= (DWORD
)((LONGLONG
)stats
.st_size
>>32);
414 data
->nFileSizeLow
= stats
.st_size
& 0xffffffff;
415 RtlSecondsSince1970ToTime(stats
.st_mtime
, (LARGE_INTEGER
*)&data
->ftLastWriteTime
);
417 original_dos_name
= wine_get_dos_file_name(original_file_name
);
418 if (original_dos_name
!= NULL
)
420 lstrcpynW(data
->cFileName
, original_dos_name
, MAX_PATH
);
421 SHFree(original_dos_name
);
425 /* show only the file name */
426 char *filename
= strrchr(original_file_name
, '/');
427 if (filename
== NULL
)
428 filename
= original_file_name
;
429 MultiByteToWideChar(CP_UNIXCP
, 0, filename
, -1, data
->cFileName
, MAX_PATH
);
432 deletion_date
= XDG_GetStringValue(parsed
, trashinfo_group
, "DeletionDate", 0);
438 sscanf(deletion_date
, "%d-%d-%dT%d:%d:%d",
439 &del_time
.tm_year
, &del_time
.tm_mon
, &del_time
.tm_mday
,
440 &del_time
.tm_hour
, &del_time
.tm_min
, &del_time
.tm_sec
);
441 del_time
.tm_year
-= 1900;
443 del_secs
= mktime(&del_time
);
445 RtlSecondsSince1970ToTime(del_secs
, (LARGE_INTEGER
*)&data
->ftLastAccessTime
);
451 SHFree(original_file_name
);
452 SHFree(deletion_date
);
455 XDG_FreeParsedFile(parsed
);
459 static INT CALLBACK
free_item_callback(void *item
, void *lParam
)
465 static HDPA
enum_bucket_trashinfos(const TRASH_BUCKET
*bucket
, int *count
)
467 HDPA ret
= DPA_Create(32);
468 struct dirent
*entry
;
473 if (ret
== NULL
) goto failed
;
474 dir
= opendir(bucket
->info_dir
);
475 if (dir
== NULL
) goto failed
;
476 while ((entry
= readdir(dir
)) != NULL
)
479 int namelen
= lstrlenA(entry
->d_name
);
480 int suffixlen
= lstrlenA(trashinfo_suffix
);
481 if (namelen
<= suffixlen
||
482 lstrcmpA(entry
->d_name
+namelen
-suffixlen
, trashinfo_suffix
) != 0)
485 filename
= StrDupA(entry
->d_name
);
486 if (filename
== NULL
)
488 if (DPA_InsertPtr(ret
, DPA_APPEND
, filename
) == -1)
498 if (dir
) closedir(dir
);
500 DPA_DestroyCallback(ret
, free_item_callback
, NULL
);
504 HRESULT
TRASH_EnumItems(LPITEMIDLIST
**pidls
, int *count
)
508 HRESULT err
= E_OUTOFMEMORY
;
511 if (!TRASH_EnsureInitialized()) return E_FAIL
;
512 tinfs
= enum_bucket_trashinfos(home_trash
, &ti_count
);
513 if (tinfs
== NULL
) return E_FAIL
;
514 *pidls
= SHAlloc(sizeof(LPITEMIDLIST
)*ti_count
);
515 if (!*pidls
) goto failed
;
516 for (i
=0; i
<ti_count
; i
++)
518 WIN32_FIND_DATAW data
;
521 filename
= DPA_GetPtr(tinfs
, i
);
522 if (FAILED(err
= TRASH_GetDetails(home_trash
, filename
, &data
)))
526 if (FAILED(err
= TRASH_CreateSimplePIDL(filename
, &data
, &(*pidls
)[pos
])))
531 DPA_DestroyCallback(tinfs
, free_item_callback
, NULL
);
537 for (j
=0; j
<pos
; j
++)
541 DPA_DestroyCallback(tinfs
, free_item_callback
, NULL
);