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
37 #include "wine/debug.h"
38 #include "shell32_main.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(trash
);
43 static CRITICAL_SECTION TRASH_Creating
;
44 static CRITICAL_SECTION_DEBUG TRASH_Creating_Debug
=
46 0, 0, &TRASH_Creating
,
47 { &TRASH_Creating_Debug
.ProcessLocksList
,
48 &TRASH_Creating_Debug
.ProcessLocksList
},
49 0, 0, { (DWORD_PTR
)__FILE__
": TRASH_Creating"}
51 static CRITICAL_SECTION TRASH_Creating
= { &TRASH_Creating_Debug
, -1, 0, 0, 0, 0 };
53 static const char trashinfo_suffix
[] = ".trashinfo";
54 static const char trashinfo_header
[] = "[Trash Info]\n";
63 static TRASH_BUCKET
*home_trash
=NULL
;
65 static char *init_home_dir(const char *subpath
)
67 char *path
= XDG_BuildPath(XDG_DATA_HOME
, subpath
);
68 if (path
== NULL
) return NULL
;
69 if (!XDG_MakeDirs(path
))
71 ERR("Couldn't create directory %s (errno=%d). Trash won't be available\n", debugstr_a(path
), errno
);
78 static TRASH_BUCKET
*TRASH_CreateHomeBucket(void)
81 struct stat trash_stat
;
82 char *trash_path
= NULL
;
84 bucket
= SHAlloc(sizeof(TRASH_BUCKET
));
90 memset(bucket
, 0, sizeof(*bucket
));
91 bucket
->info_dir
= init_home_dir("Trash/info/");
92 if (bucket
->info_dir
== NULL
) goto error
;
93 bucket
->files_dir
= init_home_dir("Trash/files/");
94 if (bucket
->files_dir
== NULL
) goto error
;
96 trash_path
= XDG_BuildPath(XDG_DATA_HOME
, "Trash/");
97 if (stat(trash_path
, &trash_stat
) == -1)
99 bucket
->device
= trash_stat
.st_dev
;
106 SHFree(bucket
->info_dir
);
107 SHFree(bucket
->files_dir
);
113 static BOOL
TRASH_EnsureInitialized(void)
115 if (home_trash
== NULL
)
117 EnterCriticalSection(&TRASH_Creating
);
118 if (home_trash
== NULL
)
119 home_trash
= TRASH_CreateHomeBucket();
120 LeaveCriticalSection(&TRASH_Creating
);
123 if (home_trash
== NULL
)
125 ERR("Couldn't initialize home trash (errno=%d)\n", errno
);
131 static BOOL
file_good_for_bucket(TRASH_BUCKET
*pBucket
, struct stat
*file_stat
)
133 if (pBucket
->device
!= file_stat
->st_dev
)
138 BOOL
TRASH_CanTrashFile(LPCWSTR wszPath
)
140 struct stat file_stat
;
143 TRACE("(%s)\n", debugstr_w(wszPath
));
144 if (!TRASH_EnsureInitialized()) return FALSE
;
145 if (!(unix_path
= wine_get_unix_file_name(wszPath
)))
147 if (lstat(unix_path
, &file_stat
)==-1)
149 HeapFree(GetProcessHeap(), 0, unix_path
);
152 HeapFree(GetProcessHeap(), 0, unix_path
);
153 return file_good_for_bucket(home_trash
, &file_stat
);
157 * Try to create a single .trashinfo file. Return TRUE if successfull, else FALSE
159 static BOOL
try_create_trashinfo_file(const char *info_dir
, const char *file_name
,
160 const char *original_file_name
)
163 time_t curr_time_secs
;
165 char *path
= SHAlloc(strlen(info_dir
)+strlen(file_name
)+strlen(trashinfo_suffix
)+1);
168 if (path
==NULL
) return FALSE
;
169 wsprintfA(path
, "%s%s%s", info_dir
, file_name
, trashinfo_suffix
);
170 TRACE("Trying to create '%s'\n", path
);
171 writer
= open(path
, O_CREAT
|O_WRONLY
|O_TRUNC
|O_EXCL
, 0600);
172 if (writer
==-1) goto error
;
174 write(writer
, trashinfo_header
, strlen(trashinfo_header
));
175 if (!XDG_WriteDesktopStringEntry(writer
, "Path", XDG_URLENCODE
, original_file_name
))
178 time(&curr_time_secs
);
179 localtime_r(&curr_time_secs
, &curr_time
);
180 wnsprintfA(datebuf
, 200, "%04d-%02d-%02dT%02d:%02d:%02d",
181 curr_time
.tm_year
+1900,
187 if (!XDG_WriteDesktopStringEntry(writer
, "DeletionDate", 0, datebuf
))
204 * Try to create a .trashinfo file. This function will make several attempts with
205 * different filenames. It will return the filename that succeded or NULL if a file
206 * couldn't be created.
208 static char *create_trashinfo(const char *info_dir
, const char *file_path
)
210 const char *base_name
;
211 char *filename_buffer
;
212 unsigned int seed
= (unsigned int)time(NULL
);
215 errno
= ENOMEM
; /* out-of-memory is the only case when errno isn't set */
216 base_name
= strrchr(file_path
, '/');
217 if (base_name
== NULL
)
218 base_name
= file_path
;
222 filename_buffer
= SHAlloc(strlen(base_name
)+9+1);
223 if (filename_buffer
== NULL
)
225 lstrcpyA(filename_buffer
, base_name
);
226 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
227 return filename_buffer
;
230 sprintf(filename_buffer
, "%s-%d", base_name
, i
+1);
231 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
232 return filename_buffer
;
235 for (i
=0; i
<1000; i
++)
237 sprintf(filename_buffer
, "%s-%08x", base_name
, rand_r(&seed
));
238 if (try_create_trashinfo_file(info_dir
, filename_buffer
, file_path
))
239 return filename_buffer
;
242 WARN("Couldn't create trashinfo after 1031 tries (errno=%d)\n", errno
);
243 SHFree(filename_buffer
);
247 void remove_trashinfo_file(const char *info_dir
, const char *base_name
)
249 char *filename_buffer
;
251 filename_buffer
= SHAlloc(lstrlenA(info_dir
)+lstrlenA(base_name
)+lstrlenA(trashinfo_suffix
)+1);
252 if (filename_buffer
== NULL
) return;
253 sprintf(filename_buffer
, "%s%s%s", info_dir
, base_name
, trashinfo_suffix
);
254 unlink(filename_buffer
);
255 SHFree(filename_buffer
);
258 static BOOL
TRASH_MoveFileToBucket(TRASH_BUCKET
*pBucket
, const char *unix_path
)
260 struct stat file_stat
;
261 char *trash_file_name
= NULL
;
262 char *trash_path
= NULL
;
265 if (lstat(unix_path
, &file_stat
)==-1)
267 if (!file_good_for_bucket(pBucket
, &file_stat
))
270 trash_file_name
= create_trashinfo(pBucket
->info_dir
, unix_path
);
271 if (trash_file_name
== NULL
)
274 trash_path
= SHAlloc(strlen(pBucket
->files_dir
)+strlen(trash_file_name
)+1);
275 if (trash_path
== NULL
) goto error
;
276 lstrcpyA(trash_path
, pBucket
->files_dir
);
277 lstrcatA(trash_path
, trash_file_name
);
279 if (rename(unix_path
, trash_path
)==0)
281 TRACE("rename succeded\n");
285 /* TODO: try to manually move the file */
286 ERR("Couldn't move file\n");
289 remove_trashinfo_file(pBucket
->info_dir
, trash_file_name
);
291 SHFree(trash_file_name
);
296 BOOL
TRASH_TrashFile(LPCWSTR wszPath
)
301 TRACE("(%s)\n", debugstr_w(wszPath
));
302 if (!TRASH_EnsureInitialized()) return FALSE
;
303 if (!(unix_path
= wine_get_unix_file_name(wszPath
)))
305 result
= TRASH_MoveFileToBucket(home_trash
, unix_path
);
306 HeapFree(GetProcessHeap(), 0, unix_path
);