2 * Recycle bin VFS module for Samba.
4 * Copyright (C) 2001, Brandon Stone, Amherst College, <bbstone@amherst.edu>.
5 * Copyright (C) 2002, Jeremy Allison - modified to make a VFS module.
6 * Copyright (C) 2002, Alexander Bokovoy - cascaded VFS adoption,
7 * Copyright (C) 2002, Juergen Hasch - added some options.
8 * Copyright (C) 2002, Simo Sorce
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
29 static int vfs_recycle_debug_level = DBGC_VFS;
32 #define DBGC_CLASS vfs_recycle_debug_level
34 static const char *delimiter = "|"; /* delimiter for options */
36 /* One per connection */
38 typedef struct recycle_bin_struct
41 char *repository; /* name of the recycle bin directory */
42 BOOL keep_dir_tree; /* keep directory structure of deleted file in recycle bin */
43 BOOL versions; /* create versions of deleted files with identical name */
44 BOOL touch; /* touch access date of deleted file */
45 char *exclude; /* which files to exclude */
46 char *exclude_dir; /* which directories to exclude */
47 char *noversions; /* which files to exclude from versioning */
48 SMB_OFF_T maxsize; /* maximum file size to be saved */
51 typedef struct recycle_bin_connections {
53 recycle_bin_struct *data;
54 struct recycle_bin_connections *next;
55 } recycle_bin_connections;
57 typedef struct recycle_bin_private_data {
59 recycle_bin_connections *conns;
60 } recycle_bin_private_data;
62 struct smb_vfs_handle_struct *recycle_bin_private_handle;
65 static struct vfs_ops default_vfs_ops; /* For passthrough operation */
67 static int recycle_connect(struct connection_struct *conn, const char *service, const char *user);
68 static void recycle_disconnect(struct connection_struct *conn);
69 static int recycle_unlink(connection_struct *, const char *);
71 #define VFS_OP(x) ((void *) x)
73 static vfs_op_tuple recycle_ops[] = {
76 {VFS_OP(recycle_connect), SMB_VFS_OP_CONNECT, SMB_VFS_LAYER_TRANSPARENT},
77 {VFS_OP(recycle_disconnect), SMB_VFS_OP_DISCONNECT, SMB_VFS_LAYER_TRANSPARENT},
80 {VFS_OP(recycle_unlink), SMB_VFS_OP_UNLINK, SMB_VFS_LAYER_TRANSPARENT},
82 {NULL, SMB_VFS_OP_NOOP, SMB_VFS_LAYER_NOOP}
86 * VFS initialisation function.
88 * @retval initialised vfs_op_tuple array
90 static vfs_op_tuple *recycle_init(const struct vfs_ops *def_vfs_ops,
91 struct smb_vfs_handle_struct *vfs_handle)
93 TALLOC_CTX *mem_ctx = NULL;
95 DEBUG(10, ("Initializing VFS module recycle\n"));
96 memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
97 vfs_recycle_debug_level = debug_add_class("vfs_recycle_bin");
98 if (vfs_recycle_debug_level == -1) {
99 vfs_recycle_debug_level = DBGC_VFS;
100 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
102 DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level));
105 recycle_bin_private_handle = vfs_handle;
106 if (!(mem_ctx = talloc_init("recycle bin data"))) {
107 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
111 recycle_bin_private_handle->data = talloc(mem_ctx, sizeof(recycle_bin_private_data));
112 if (recycle_bin_private_handle->data == NULL) {
113 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
116 ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->mem_ctx = mem_ctx;
117 ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->conns = NULL;
122 static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)
124 TALLOC_CTX *ctx = NULL;
125 recycle_bin_struct *recbin;
126 recycle_bin_connections *recconn;
127 recycle_bin_connections *recconnbase;
128 recycle_bin_private_data *recdata;
131 DEBUG(10, ("Called for service %s (%d) as user %s\n", service, SNUM(conn), user));
133 if (recycle_bin_private_handle)
134 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
136 DEBUG(0, ("Recycle bin not initialized!\n"));
140 if (!(ctx = talloc_init("recycle bin connection"))) {
141 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
145 recbin = talloc(ctx, sizeof(recycle_bin_struct));
146 if (recbin == NULL) {
147 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
150 recbin->mem_ctx = ctx;
153 recbin->repository = talloc_strdup(recbin->mem_ctx, ".recycle");
154 ALLOC_CHECK(recbin->repository, error);
155 recbin->keep_dir_tree = False;
156 recbin->versions = False;
157 recbin->touch = False;
158 recbin->exclude = "";
159 recbin->exclude_dir = "";
160 recbin->noversions = "";
163 /* parse configuration options */
164 if ((tmp_str = lp_parm_string(SNUM(conn), "vfs_recycle_bin", "repository")) != NULL) {
165 recbin->repository = talloc_sub_conn(recbin->mem_ctx, conn, tmp_str);
166 ALLOC_CHECK(recbin->repository, error);
167 trim_string(recbin->repository, "/", "/");
168 DEBUG(5, ("recycle.bin: repository = %s\n", recbin->repository));
171 recbin->keep_dir_tree = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "keeptree");
172 DEBUG(5, ("recycle.bin: keeptree = %d\n", recbin->keep_dir_tree));
174 recbin->versions = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "versions");
175 DEBUG(5, ("recycle.bin: versions = %d\n", recbin->versions));
177 recbin->touch = lp_parm_bool(SNUM(conn), "vfs_recycle_bin", "touch");
178 DEBUG(5, ("recycle.bin: touch = %d\n", recbin->touch));
180 recbin->maxsize = lp_parm_ulong(SNUM(conn), "vfs_recycle_bin", "maxsize");
181 if (recbin->maxsize == 0) {
182 recbin->maxsize = -1;
183 DEBUG(5, ("recycle.bin: maxsize = -infinite-\n"));
185 DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin->maxsize));
188 if ((tmp_str = lp_parm_string(SNUM(conn), "vfs_recycle_bin", "exclude")) != NULL) {
189 recbin->exclude = talloc_strdup(recbin->mem_ctx, tmp_str);
190 ALLOC_CHECK(recbin->exclude, error);
191 DEBUG(5, ("recycle.bin: exclude = %s\n", recbin->exclude));
193 if ((tmp_str = lp_parm_string(SNUM(conn), "vfs_recycle_bin", "exclude_dir")) != NULL) {
194 recbin->exclude_dir = talloc_strdup(recbin->mem_ctx, tmp_str);
195 ALLOC_CHECK(recbin->exclude_dir, error);
196 DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin->exclude_dir));
198 if ((tmp_str = lp_parm_string(SNUM(conn), "vfs_recycle_bin", "noversions")) != NULL) {
199 recbin->noversions = talloc_strdup(recbin->mem_ctx, tmp_str);
200 ALLOC_CHECK(recbin->noversions, error);
201 DEBUG(5, ("recycle.bin: noversions = %s\n", recbin->noversions));
204 recconn = talloc(recdata->mem_ctx, sizeof(recycle_bin_connections));
205 if (recconn == NULL) {
206 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
209 recconn->conn = SNUM(conn);
210 recconn->data = recbin;
211 recconn->next = NULL;
212 if (recdata->conns) {
213 recconnbase = recdata->conns;
214 while (recconnbase->next != NULL) recconnbase = recconnbase->next;
215 recconnbase->next = recconn;
217 recdata->conns = recconn;
219 return default_vfs_ops.connect(conn, service, user);
226 static void recycle_disconnect(struct connection_struct *conn)
228 recycle_bin_private_data *recdata;
229 recycle_bin_connections *recconn;
231 DEBUG(10, ("Disconnecting VFS module recycle bin\n"));
233 if (recycle_bin_private_handle)
234 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
236 DEBUG(0, ("Recycle bin not initialized!\n"));
241 if (recdata->conns) {
242 if (recdata->conns->conn == SNUM(conn)) {
243 talloc_destroy(recdata->conns->data->mem_ctx);
244 recdata->conns = recdata->conns->next;
246 recconn = recdata->conns;
247 while (recconn->next) {
248 if (recconn->next->conn == SNUM(conn)) {
249 talloc_destroy(recconn->next->data->mem_ctx);
250 recconn->next = recconn->next->next;
253 recconn = recconn->next;
258 default_vfs_ops.disconnect(conn);
261 static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)
265 if (default_vfs_ops.stat(conn, dname, &st) == 0) {
266 if (S_ISDIR(st.st_mode)) {
274 static BOOL recycle_file_exist(connection_struct *conn, const char *fname)
278 if (default_vfs_ops.stat(conn, fname, &st) == 0) {
279 if (S_ISREG(st.st_mode)) {
289 * @param conn connection
290 * @param fname file name
291 * @return size in bytes
293 static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)
296 if (default_vfs_ops.stat(conn, fname, &st) != 0) {
297 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
304 * Create directory tree
305 * @param conn connection
306 * @param dname Directory tree to be created
307 * @return Returns True for success
309 static BOOL recycle_create_dir(connection_struct *conn, const char *dname)
313 char *new_dir = NULL;
314 char *tmp_str = NULL;
319 mode = S_IREAD | S_IWRITE | S_IEXEC;
321 tmp_str = strdup(dname);
322 ALLOC_CHECK(tmp_str, done);
326 new_dir = (char *)malloc(len + 1);
327 ALLOC_CHECK(new_dir, done);
330 /* Create directory tree if neccessary */
331 for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) {
332 safe_strcat(new_dir, token, len);
333 if (recycle_directory_exist(conn, new_dir))
334 DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir));
336 DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir));
337 if (default_vfs_ops.mkdir(conn, new_dir, mode) != 0) {
338 DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
343 safe_strcat(new_dir, "/", len);
354 * Check if needle is contained exactly in haystack
355 * @param haystack list of parameters separated by delimimiter character
356 * @param needle string to be matched exactly to haystack
357 * @return True if found
359 static BOOL checkparam(const char *haystack, const char *needle)
366 if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
370 tmp_str = strdup(haystack);
371 ALLOC_CHECK(tmp_str, done);
372 token = tok_str = tmp_str;
374 for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
375 if(strcmp(token, needle) == 0) {
386 * Check if needle is contained in haystack, * and ? patterns are resolved
387 * @param haystack list of parameters separated by delimimiter character
388 * @param needle string to be matched exectly to haystack including pattern matching
389 * @return True if found
391 static BOOL matchparam(const char *haystack, const char *needle)
398 if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
402 tmp_str = strdup(haystack);
403 ALLOC_CHECK(tmp_str, done);
404 token = tok_str = tmp_str;
406 for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
407 if (!unix_wild_match(token, needle)) {
420 static void recycle_touch(connection_struct *conn, const char *fname)
426 if (default_vfs_ops.stat(conn, fname, &st) != 0) {
427 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
430 currtime = time(&currtime);
431 tb.actime = currtime;
432 tb.modtime = st.st_mtime;
434 if (default_vfs_ops.utime(conn, fname, &tb) == -1 )
435 DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname, strerror(errno)));
439 * Check if file should be recycled
441 static int recycle_unlink(connection_struct *conn, const char *file_name)
443 recycle_bin_private_data *recdata;
444 recycle_bin_connections *recconn;
445 recycle_bin_struct *recbin;
446 char *path_name = NULL;
447 char *temp_name = NULL;
448 char *final_name = NULL;
451 /* SMB_BIG_UINT dfree, dsize, bsize; */
452 SMB_OFF_T file_size; /* space_avail; */
457 if (recycle_bin_private_handle) {
458 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
460 if (recdata->conns) {
461 recconn = recdata->conns;
462 while (recconn && recconn->conn != SNUM(conn)) recconn = recconn->next;
463 if (recconn != NULL) {
464 recbin = recconn->data;
469 if (recbin == NULL) {
470 DEBUG(0, ("Recycle bin not initialized!\n"));
471 rc = default_vfs_ops.unlink(conn, file_name);
475 if(!recbin->repository || *(recbin->repository) == '\0') {
476 DEBUG(3, ("Recycle path not set, purging %s...\n", file_name));
477 rc = default_vfs_ops.unlink(conn, file_name);
481 /* we don't recycle the recycle bin... */
482 if (strncmp(file_name, recbin->repository, strlen(recbin->repository)) == 0) {
483 DEBUG(3, ("File is within recycling bin, unlinking ...\n"));
484 rc = default_vfs_ops.unlink(conn, file_name);
488 file_size = recycle_get_file_size(conn, file_name);
489 /* it is wrong to purge filenames only because they are empty imho
493 DEBUG(3, ("File %s is empty, purging...\n", file_name));
494 rc = default_vfs_ops.unlink(conn,file_name);
499 /* FIXME: this is wrong, we should check the hole size of the recycle bin is
500 * not greater then maxsize, not the size of the single file, also it is better
501 * to remove older files
503 if(recbin->maxsize > 0 && file_size > recbin->maxsize) {
504 DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name));
505 rc = default_vfs_ops.unlink(conn, file_name);
509 /* FIXME: this is wrong: moving files with rename does not change the disk space
512 space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L;
513 DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
514 if(space_avail < file_size) {
515 DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name));
516 rc = default_vfs_ops.unlink(conn, file_name);
521 /* extract filename and path */
522 path_name = (char *)malloc(PATH_MAX);
523 ALLOC_CHECK(path_name, done);
525 safe_strcpy(path_name, file_name, PATH_MAX - 1);
526 base = strrchr(path_name, '/');
529 safe_strcpy(path_name, "/", PATH_MAX - 1);
535 DEBUG(10, ("recycle.bin: fname = %s\n", file_name)); /* original filename with path */
536 DEBUG(10, ("recycle.bin: fpath = %s\n", path_name)); /* original path */
537 DEBUG(10, ("recycle.bin: base = %s\n", base)); /* filename without path */
539 if (matchparam(recbin->exclude, base)) {
540 DEBUG(3, ("recycle.bin: file %s is excluded \n", base));
541 rc = default_vfs_ops.unlink(conn, file_name);
545 /* FIXME: this check will fail if we have more than one level of directories,
546 * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 ....
549 if (checkparam(recbin->exclude_dir, path_name)) {
550 DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name));
551 rc = default_vfs_ops.unlink(conn, file_name);
555 temp_name = (char *)malloc(PATH_MAX);
556 ALLOC_CHECK(temp_name, done);
557 safe_strcpy(temp_name, recbin->repository, PATH_MAX - 1);
560 /* see if we need to recreate the original directory structure in the recycle bin */
561 if (recbin->keep_dir_tree == True) {
562 safe_strcat(temp_name, "/", PATH_MAX - 1);
563 safe_strcat(temp_name, path_name, PATH_MAX - 1);
566 exist = recycle_directory_exist(conn, temp_name);
568 DEBUG(10, ("recycle.bin: Directory already exists\n"));
570 DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name));
571 if (recycle_create_dir(conn, temp_name) == False) {
572 DEBUG(3, ("Could not create directory, purging %s...\n", file_name));
573 rc = default_vfs_ops.unlink(conn, file_name);
578 asprintf(&final_name, "%s/%s", temp_name, base);
579 ALLOC_CHECK(final_name, done);
580 DEBUG(10, ("recycle.bin: recycled file name: %s\n", temp_name)); /* new filename with path */
582 /* check if we should delete file from recycle bin */
583 if (recycle_file_exist(conn, final_name)) {
584 if (recbin->versions == False || matchparam(recbin->noversions, base) == True) {
585 DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", final_name));
586 if (default_vfs_ops.unlink(conn, final_name) != 0) {
587 DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno)));
592 /* rename file we move to recycle bin */
594 while (recycle_file_exist(conn, final_name)) {
595 snprintf(final_name, PATH_MAX, "%s/Copy #%d of %s", temp_name, i++, base);
598 DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name, final_name));
599 rc = default_vfs_ops.rename(conn, file_name, final_name);
601 DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name));
602 rc = default_vfs_ops.unlink(conn, file_name);
606 /* touch access date of moved file */
607 if (recbin->touch == True )
608 recycle_touch(conn, final_name);
611 SAFE_FREE(path_name);
612 SAFE_FREE(temp_name);
613 SAFE_FREE(final_name);
617 int vfs_recycle_init(void)
619 return smb_register_vfs("recycle", recycle_init, SMB_VFS_INTERFACE_VERSION);