shell32: Send directories and non-regular files to the trash.
[wine/multimedia.git] / dlls / shell32 / trash.c
blob2e96bfb0223c778e827cf7256a8c938ceef6ce24
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
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
22 #include <stdarg.h>
23 #include <sys/stat.h>
24 #include <stdlib.h>
25 #include <unistd.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winerror.h"
30 #include "winreg.h"
31 #include "shlwapi.h"
33 #include <stdio.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <time.h>
37 #include "wine/debug.h"
38 #include "shell32_main.h"
39 #include "xdg.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";
56 typedef struct
58 char *info_dir;
59 char *files_dir;
60 dev_t device;
61 } TRASH_BUCKET;
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);
72 SHFree(path);
73 path=NULL;
75 return path;
78 static TRASH_BUCKET *TRASH_CreateHomeBucket(void)
80 TRASH_BUCKET *bucket;
81 struct stat trash_stat;
82 char *trash_path = NULL;
84 bucket = SHAlloc(sizeof(TRASH_BUCKET));
85 if (bucket == NULL)
87 errno = ENOMEM;
88 goto error;
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)
98 goto error;
99 bucket->device = trash_stat.st_dev;
100 SHFree(trash_path);
101 return bucket;
102 error:
103 SHFree(trash_path);
104 if (bucket)
106 SHFree(bucket->info_dir);
107 SHFree(bucket->files_dir);
109 SHFree(bucket);
110 return NULL;
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);
126 return FALSE;
128 return TRUE;
131 static BOOL file_good_for_bucket(TRASH_BUCKET *pBucket, struct stat *file_stat)
133 if (pBucket->device != file_stat->st_dev)
134 return FALSE;
135 return TRUE;
138 BOOL TRASH_CanTrashFile(LPCWSTR wszPath)
140 struct stat file_stat;
141 char *unix_path;
143 TRACE("(%s)\n", debugstr_w(wszPath));
144 if (!TRASH_EnsureInitialized()) return FALSE;
145 if (!(unix_path = wine_get_unix_file_name(wszPath)))
146 return FALSE;
147 if (lstat(unix_path, &file_stat)==-1)
149 HeapFree(GetProcessHeap(), 0, unix_path);
150 return FALSE;
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)
162 struct tm curr_time;
163 time_t curr_time_secs;
164 char datebuf[200];
165 char *path = SHAlloc(strlen(info_dir)+strlen(file_name)+strlen(trashinfo_suffix)+1);
166 int writer = -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))
176 goto error;
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,
182 curr_time.tm_mon+1,
183 curr_time.tm_mday,
184 curr_time.tm_hour,
185 curr_time.tm_min,
186 curr_time.tm_sec);
187 if (!XDG_WriteDesktopStringEntry(writer, "DeletionDate", 0, datebuf))
188 goto error;
189 close(writer);
190 SHFree(path);
191 return TRUE;
193 error:
194 if (writer != -1)
196 close(writer);
197 unlink(path);
199 SHFree(path);
200 return FALSE;
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);
213 int i;
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;
219 else
220 base_name++;
222 filename_buffer = SHAlloc(strlen(base_name)+9+1);
223 if (filename_buffer == NULL)
224 return NULL;
225 lstrcpyA(filename_buffer, base_name);
226 if (try_create_trashinfo_file(info_dir, filename_buffer, file_path))
227 return filename_buffer;
228 for (i=0; i<30; i++)
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);
244 return NULL;
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;
263 BOOL ret = TRUE;
265 if (lstat(unix_path, &file_stat)==-1)
266 return FALSE;
267 if (!file_good_for_bucket(pBucket, &file_stat))
268 return FALSE;
270 trash_file_name = create_trashinfo(pBucket->info_dir, unix_path);
271 if (trash_file_name == NULL)
272 return FALSE;
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");
282 goto cleanup;
285 /* TODO: try to manually move the file */
286 ERR("Couldn't move file\n");
287 error:
288 ret = FALSE;
289 remove_trashinfo_file(pBucket->info_dir, trash_file_name);
290 cleanup:
291 SHFree(trash_file_name);
292 SHFree(trash_path);
293 return ret;
296 BOOL TRASH_TrashFile(LPCWSTR wszPath)
298 char *unix_path;
299 BOOL result;
301 TRACE("(%s)\n", debugstr_w(wszPath));
302 if (!TRASH_EnsureInitialized()) return FALSE;
303 if (!(unix_path = wine_get_unix_file_name(wszPath)))
304 return FALSE;
305 result = TRASH_MoveFileToBucket(home_trash, unix_path);
306 HeapFree(GetProcessHeap(), 0, unix_path);
307 return result;