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 tcon_context
*conn
, const char *service
, const char *user
);
68 static void recycle_disconnect(struct tcon_context
*conn
);
69 static int recycle_unlink(struct tcon_context
*, 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 vfs_op_tuple
*vfs_init(int *vfs_version
, 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 *vfs_version
= SMB_VFS_INTERFACE_VERSION
;
97 memcpy(&default_vfs_ops
, def_vfs_ops
, sizeof(struct vfs_ops
));
98 vfs_recycle_debug_level
= debug_add_class("vfs_recycle_bin");
99 if (vfs_recycle_debug_level
== -1) {
100 vfs_recycle_debug_level
= DBGC_VFS
;
101 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
103 DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level
));
106 recycle_bin_private_handle
= vfs_handle
;
107 if (!(mem_ctx
= talloc_init("recycle bin data"))) {
108 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
112 recycle_bin_private_handle
->data
= talloc(mem_ctx
, sizeof(recycle_bin_private_data
));
113 if (recycle_bin_private_handle
->data
== NULL
) {
114 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
117 ((recycle_bin_private_data
*)(recycle_bin_private_handle
->data
))->mem_ctx
= mem_ctx
;
118 ((recycle_bin_private_data
*)(recycle_bin_private_handle
->data
))->conns
= NULL
;
124 * VFS finalization function.
129 recycle_bin_private_data
*recdata
;
130 recycle_bin_connections
*recconn
;
132 DEBUG(10, ("Unloading/Cleaning VFS module recycle bin\n"));
134 if (recycle_bin_private_handle
)
135 recdata
= (recycle_bin_private_data
*)(recycle_bin_private_handle
->data
);
137 DEBUG(0, ("Recycle bin not initialized!\n"));
142 if (recdata
->conns
) {
143 recconn
= recdata
->conns
;
145 talloc_destroy(recconn
->data
->mem_ctx
);
146 recconn
= recconn
->next
;
149 if (recdata
->mem_ctx
) {
150 talloc_destroy(recdata
->mem_ctx
);
156 static int recycle_connect(struct tcon_context
*conn
, const char *service
, const char *user
)
158 TALLOC_CTX
*ctx
= NULL
;
159 recycle_bin_struct
*recbin
;
160 recycle_bin_connections
*recconn
;
161 recycle_bin_connections
*recconnbase
;
162 recycle_bin_private_data
*recdata
;
165 DEBUG(10, ("Called for service %s (%d) as user %s\n", service
, SNUM(conn
), user
));
167 if (recycle_bin_private_handle
)
168 recdata
= (recycle_bin_private_data
*)(recycle_bin_private_handle
->data
);
170 DEBUG(0, ("Recycle bin not initialized!\n"));
174 if (!(ctx
= talloc_init("recycle bin connection"))) {
175 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
179 recbin
= talloc(ctx
, sizeof(recycle_bin_struct
));
180 if (recbin
== NULL
) {
181 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
184 recbin
->mem_ctx
= ctx
;
187 recbin
->repository
= talloc_strdup(recbin
->mem_ctx
, ".recycle");
188 ALLOC_CHECK(recbin
->repository
, error
);
189 recbin
->keep_dir_tree
= False
;
190 recbin
->versions
= False
;
191 recbin
->touch
= False
;
192 recbin
->exclude
= "";
193 recbin
->exclude_dir
= "";
194 recbin
->noversions
= "";
197 /* parse configuration options */
198 if ((tmp_str
= lp_parm_string(SNUM(conn
), "vfs_recycle_bin", "repository")) != NULL
) {
199 recbin
->repository
= talloc_sub_conn(recbin
->mem_ctx
, conn
, tmp_str
);
200 ALLOC_CHECK(recbin
->repository
, error
);
201 trim_string(recbin
->repository
, "/", "/");
202 DEBUG(5, ("recycle.bin: repository = %s\n", recbin
->repository
));
205 recbin
->keep_dir_tree
= lp_parm_bool(SNUM(conn
), "vfs_recycle_bin", "keeptree", False
);
206 DEBUG(5, ("recycle.bin: keeptree = %d\n", recbin
->keep_dir_tree
));
208 recbin
->versions
= lp_parm_bool(SNUM(conn
), "vfs_recycle_bin", "versions", False
);
209 DEBUG(5, ("recycle.bin: versions = %d\n", recbin
->versions
));
211 recbin
->touch
= lp_parm_bool(SNUM(conn
), "vfs_recycle_bin", "touch", False
);
212 DEBUG(5, ("recycle.bin: touch = %d\n", recbin
->touch
));
214 recbin
->maxsize
= lp_parm_ulong(SNUM(conn
), "vfs_recycle_bin", "maxsize");
215 if (recbin
->maxsize
== 0) {
216 recbin
->maxsize
= -1;
217 DEBUG(5, ("recycle.bin: maxsize = -infinite-\n"));
219 DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin
->maxsize
));
222 if ((tmp_str
= lp_parm_string(SNUM(conn
), "vfs_recycle_bin", "exclude")) != NULL
) {
223 recbin
->exclude
= talloc_strdup(recbin
->mem_ctx
, tmp_str
);
224 ALLOC_CHECK(recbin
->exclude
, error
);
225 DEBUG(5, ("recycle.bin: exclude = %s\n", recbin
->exclude
));
227 if ((tmp_str
= lp_parm_string(SNUM(conn
), "vfs_recycle_bin", "exclude_dir")) != NULL
) {
228 recbin
->exclude_dir
= talloc_strdup(recbin
->mem_ctx
, tmp_str
);
229 ALLOC_CHECK(recbin
->exclude_dir
, error
);
230 DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin
->exclude_dir
));
232 if ((tmp_str
= lp_parm_string(SNUM(conn
), "vfs_recycle_bin", "noversions")) != NULL
) {
233 recbin
->noversions
= talloc_strdup(recbin
->mem_ctx
, tmp_str
);
234 ALLOC_CHECK(recbin
->noversions
, error
);
235 DEBUG(5, ("recycle.bin: noversions = %s\n", recbin
->noversions
));
238 recconn
= talloc(recdata
->mem_ctx
, sizeof(recycle_bin_connections
));
239 if (recconn
== NULL
) {
240 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
243 recconn
->conn
= SNUM(conn
);
244 recconn
->data
= recbin
;
245 recconn
->next
= NULL
;
246 if (recdata
->conns
) {
247 recconnbase
= recdata
->conns
;
248 while (recconnbase
->next
!= NULL
) recconnbase
= recconnbase
->next
;
249 recconnbase
->next
= recconn
;
251 recdata
->conns
= recconn
;
253 return default_vfs_ops
.connect(conn
, service
, user
);
260 static void recycle_disconnect(struct tcon_context
*conn
)
262 recycle_bin_private_data
*recdata
;
263 recycle_bin_connections
*recconn
;
265 DEBUG(10, ("Disconnecting VFS module recycle bin\n"));
267 if (recycle_bin_private_handle
)
268 recdata
= (recycle_bin_private_data
*)(recycle_bin_private_handle
->data
);
270 DEBUG(0, ("Recycle bin not initialized!\n"));
275 if (recdata
->conns
) {
276 if (recdata
->conns
->conn
== SNUM(conn
)) {
277 talloc_destroy(recdata
->conns
->data
->mem_ctx
);
278 recdata
->conns
= recdata
->conns
->next
;
280 recconn
= recdata
->conns
;
281 while (recconn
->next
) {
282 if (recconn
->next
->conn
== SNUM(conn
)) {
283 talloc_destroy(recconn
->next
->data
->mem_ctx
);
284 recconn
->next
= recconn
->next
->next
;
287 recconn
= recconn
->next
;
292 default_vfs_ops
.disconnect(conn
);
295 static BOOL
recycle_directory_exist(struct tcon_context
*conn
, const char *dname
)
299 if (default_vfs_ops
.stat(conn
, dname
, &st
) == 0) {
300 if (S_ISDIR(st
.st_mode
)) {
308 static BOOL
recycle_file_exist(struct tcon_context
*conn
, const char *fname
)
312 if (default_vfs_ops
.stat(conn
, fname
, &st
) == 0) {
313 if (S_ISREG(st
.st_mode
)) {
323 * @param conn connection
324 * @param fname file name
325 * @return size in bytes
327 static SMB_OFF_T
recycle_get_file_size(struct tcon_context
*conn
, const char *fname
)
330 if (default_vfs_ops
.stat(conn
, fname
, &st
) != 0) {
331 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname
, strerror(errno
)));
338 * Create directory tree
339 * @param conn connection
340 * @param dname Directory tree to be created
341 * @return Returns True for success
343 static BOOL
recycle_create_dir(struct tcon_context
*conn
, const char *dname
)
347 char *new_dir
= NULL
;
348 char *tmp_str
= NULL
;
353 mode
= S_IREAD
| S_IWRITE
| S_IEXEC
;
355 tmp_str
= strdup(dname
);
356 ALLOC_CHECK(tmp_str
, done
);
360 new_dir
= (char *)malloc(len
+ 1);
361 ALLOC_CHECK(new_dir
, done
);
364 /* Create directory tree if neccessary */
365 for(token
= strtok(tok_str
, "/"); token
; token
= strtok(NULL
, "/")) {
366 safe_strcat(new_dir
, token
, len
);
367 if (recycle_directory_exist(conn
, new_dir
))
368 DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir
));
370 DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir
));
371 if (default_vfs_ops
.mkdir(conn
, new_dir
, mode
) != 0) {
372 DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir
, strerror(errno
)));
377 safe_strcat(new_dir
, "/", len
);
388 * Check if needle is contained exactly in haystack
389 * @param haystack list of parameters separated by delimimiter character
390 * @param needle string to be matched exactly to haystack
391 * @return True if found
393 static BOOL
checkparam(const char *haystack
, const char *needle
)
400 if (haystack
== NULL
|| strlen(haystack
) == 0 || needle
== NULL
|| strlen(needle
) == 0) {
404 tmp_str
= strdup(haystack
);
405 ALLOC_CHECK(tmp_str
, done
);
406 token
= tok_str
= tmp_str
;
408 for(token
= strtok(tok_str
, delimiter
); token
; token
= strtok(NULL
, delimiter
)) {
409 if(strcmp(token
, needle
) == 0) {
420 * Check if needle is contained in haystack, * and ? patterns are resolved
421 * @param haystack list of parameters separated by delimimiter character
422 * @param needle string to be matched exectly to haystack including pattern matching
423 * @return True if found
425 static BOOL
matchparam(const char *haystack
, const char *needle
)
432 if (haystack
== NULL
|| strlen(haystack
) == 0 || needle
== NULL
|| strlen(needle
) == 0) {
436 tmp_str
= strdup(haystack
);
437 ALLOC_CHECK(tmp_str
, done
);
438 token
= tok_str
= tmp_str
;
440 for(token
= strtok(tok_str
, delimiter
); token
; token
= strtok(NULL
, delimiter
)) {
441 if (!unix_wild_match(token
, needle
)) {
454 static void recycle_touch(struct tcon_context
*conn
, const char *fname
)
460 if (default_vfs_ops
.stat(conn
, fname
, &st
) != 0) {
461 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname
, strerror(errno
)));
464 currtime
= time(&currtime
);
465 tb
.actime
= currtime
;
466 tb
.modtime
= st
.st_mtime
;
468 if (default_vfs_ops
.utime(conn
, fname
, &tb
) == -1 )
469 DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname
, strerror(errno
)));
473 * Check if file should be recycled
475 static int recycle_unlink(struct tcon_context
*conn
, const char *file_name
)
477 recycle_bin_private_data
*recdata
;
478 recycle_bin_connections
*recconn
;
479 recycle_bin_struct
*recbin
;
480 char *path_name
= NULL
;
481 char *temp_name
= NULL
;
482 char *final_name
= NULL
;
485 /* SMB_BIG_UINT dfree, dsize, bsize; */
486 SMB_OFF_T file_size
; /* space_avail; */
491 if (recycle_bin_private_handle
) {
492 recdata
= (recycle_bin_private_data
*)(recycle_bin_private_handle
->data
);
494 if (recdata
->conns
) {
495 recconn
= recdata
->conns
;
496 while (recconn
&& recconn
->conn
!= SNUM(conn
)) recconn
= recconn
->next
;
497 if (recconn
!= NULL
) {
498 recbin
= recconn
->data
;
503 if (recbin
== NULL
) {
504 DEBUG(0, ("Recycle bin not initialized!\n"));
505 rc
= default_vfs_ops
.unlink(conn
, file_name
);
509 if(!recbin
->repository
|| *(recbin
->repository
) == '\0') {
510 DEBUG(3, ("Recycle path not set, purging %s...\n", file_name
));
511 rc
= default_vfs_ops
.unlink(conn
, file_name
);
515 /* we don't recycle the recycle bin... */
516 if (strncmp(file_name
, recbin
->repository
, strlen(recbin
->repository
)) == 0) {
517 DEBUG(3, ("File is within recycling bin, unlinking ...\n"));
518 rc
= default_vfs_ops
.unlink(conn
, file_name
);
522 file_size
= recycle_get_file_size(conn
, file_name
);
523 /* it is wrong to purge filenames only because they are empty imho
527 DEBUG(3, ("File %s is empty, purging...\n", file_name));
528 rc = default_vfs_ops.unlink(conn,file_name);
533 /* FIXME: this is wrong, we should check the hole size of the recycle bin is
534 * not greater then maxsize, not the size of the single file, also it is better
535 * to remove older files
537 if(recbin
->maxsize
> 0 && file_size
> recbin
->maxsize
) {
538 DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name
));
539 rc
= default_vfs_ops
.unlink(conn
, file_name
);
543 /* FIXME: this is wrong: moving files with rename does not change the disk space
546 space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L;
547 DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
548 if(space_avail < file_size) {
549 DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name));
550 rc = default_vfs_ops.unlink(conn, file_name);
555 /* extract filename and path */
556 path_name
= (char *)malloc(PATH_MAX
);
557 ALLOC_CHECK(path_name
, done
);
559 safe_strcpy(path_name
, file_name
, PATH_MAX
- 1);
560 base
= strrchr(path_name
, '/');
563 safe_strcpy(path_name
, "/", PATH_MAX
- 1);
569 DEBUG(10, ("recycle.bin: fname = %s\n", file_name
)); /* original filename with path */
570 DEBUG(10, ("recycle.bin: fpath = %s\n", path_name
)); /* original path */
571 DEBUG(10, ("recycle.bin: base = %s\n", base
)); /* filename without path */
573 if (matchparam(recbin
->exclude
, base
)) {
574 DEBUG(3, ("recycle.bin: file %s is excluded \n", base
));
575 rc
= default_vfs_ops
.unlink(conn
, file_name
);
579 /* FIXME: this check will fail if we have more than one level of directories,
580 * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 ....
583 if (checkparam(recbin
->exclude_dir
, path_name
)) {
584 DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name
));
585 rc
= default_vfs_ops
.unlink(conn
, file_name
);
589 temp_name
= (char *)strdup(recbin
->repository
);
590 ALLOC_CHECK(temp_name
, done
);
592 /* see if we need to recreate the original directory structure in the recycle bin */
593 if (recbin
->keep_dir_tree
== True
) {
594 safe_strcat(temp_name
, "/", PATH_MAX
- 1);
595 safe_strcat(temp_name
, path_name
, PATH_MAX
- 1);
598 exist
= recycle_directory_exist(conn
, temp_name
);
600 DEBUG(10, ("recycle.bin: Directory already exists\n"));
602 DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name
));
603 if (recycle_create_dir(conn
, temp_name
) == False
) {
604 DEBUG(3, ("Could not create directory, purging %s...\n", file_name
));
605 rc
= default_vfs_ops
.unlink(conn
, file_name
);
611 asprintf(&final_name
, "%s/%s", temp_name
, base
);
612 ALLOC_CHECK(final_name
, done
);
613 DEBUG(10, ("recycle.bin: recycled file name%s\n", temp_name
)); /* new filename with path */
615 /* check if we should delete file from recycle bin */
616 if (recycle_file_exist(conn
, final_name
)) {
617 if (recbin
->versions
== False
|| matchparam(recbin
->noversions
, base
) == True
) {
618 DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", final_name
));
619 if (default_vfs_ops
.unlink(conn
, final_name
) != 0) {
620 DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno
)));
625 /* rename file we move to recycle bin */
627 while (recycle_file_exist(conn
, final_name
)) {
628 snprintf(final_name
, PATH_MAX
, "%s/Copy #%d of %s", temp_name
, i
++, base
);
631 DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name
, final_name
));
632 rc
= default_vfs_ops
.rename(conn
, file_name
, final_name
);
634 DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno
, strerror(errno
), file_name
, final_name
));
635 rc
= default_vfs_ops
.unlink(conn
, file_name
);
639 /* touch access date of moved file */
640 if (recbin
->touch
== True
)
641 recycle_touch(conn
, final_name
);
644 SAFE_FREE(path_name
);
645 SAFE_FREE(temp_name
);
646 SAFE_FREE(final_name
);