diff options
| -rw-r--r-- | examples/VFS/Makefile.in | 10 | ||||
| -rw-r--r-- | examples/VFS/recycle.c | 582 | 
2 files changed, 454 insertions, 138 deletions
diff --git a/examples/VFS/Makefile.in b/examples/VFS/Makefile.in index 2ffd23b853..46e1a90263 100644 --- a/examples/VFS/Makefile.in +++ b/examples/VFS/Makefile.in @@ -6,11 +6,8 @@ LDSHFLAGS	= -shared  srcdir		= @builddir@  FLAGS		=  $(CFLAGS) -Iinclude -I$(srcdir)/include -I$(srcdir)/ubiqx -I$(srcdir)/smbwrapper  -I. $(CPPFLAGS) -I$(srcdir) -VFS_OBJS	= audit.so recycle.so netatalk.so - -# Default target - -default: $(VFS_OBJS) +# Auto target +default: $(patsubst %.c,%.so,$(wildcard *.c))  # Pattern rules @@ -24,5 +21,4 @@ default: $(VFS_OBJS)  clean:  	rm -rf .libs -	rm -f core *~ *% *.bak \ -	$(VFS_OBJ) $(VFS_OBJS) +	rm -f core *~ *% *.bak *.o *.so diff --git a/examples/VFS/recycle.c b/examples/VFS/recycle.c index 0a261c20df..bcf5f898cc 100644 --- a/examples/VFS/recycle.c +++ b/examples/VFS/recycle.c @@ -1,227 +1,547 @@ -/*  - * Auditing VFS module for samba.  Log selected file operations to syslog - * facility. +/* + * Recycle bin VFS module for Samba.   *   * Copyright (C) 2001, Brandon Stone, Amherst College, <bbstone@amherst.edu>.   * Copyright (C) 2002, Jeremy Allison - modified to make a VFS module.   * Copyright (C) 2002, Alexander Bokovoy - cascaded VFS adoption, + * Copyright (C) 2002, Juergen Hasch - added some options. + * Copyright (C) 2002, Simo Sorce   *   * This program is free software; you can redistribute it and/or modify   * it under the terms of the GNU General Public License as published by   * the Free Software Foundation; either version 2 of the License, or   * (at your option) any later version. - *   + *   * This program is distributed in the hope that it will be useful,   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * GNU General Public License for more details. - *   + *   * You should have received a copy of the GNU General Public License   * along with this program; if not, write to the Free Software   * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.   */ -#include "config.h" -#include <stdio.h> -#include <sys/stat.h> -#ifdef HAVE_UTIME_H -#include <utime.h> -#endif -#ifdef HAVE_DIRENT_H -#include <dirent.h> -#endif -#include <syslog.h> -#ifdef HAVE_FCNTL_H -#include <fcntl.h> -#endif -#include <errno.h> -#include <string.h> -#include <includes.h> -#include <vfs.h> -  -/* VFS operations */ +#include "includes.h" + +#define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0) + +static int vfs_recycle_debug_level = DBGC_VFS; + +#undef DBGC_CLASS +#define DBGC_CLASS vfs_recycle_debug_level +static const char *delimiter = "|";		/* delimiter for options */ + +/* One per connection */ + +typedef struct recycle_bin_struct +{ +	TALLOC_CTX *ctx; +	char	*repository;		/* name of the recycle bin directory */ +	BOOL	keep_directories;	/* keep directory structure of deleted file in recycle bin */ +	BOOL	versions;		/* create versions of deleted files with identical name */ +	BOOL	touch;			/* touch access date of deleted file */ +	char	*exclude;		/* which files to exclude */ +	char	*exclude_dir;		/* which directories to exclude */ +	char	*noversions;		/* which files to exclude from versioning */ +	SMB_OFF_T maxsize;		/* maximum file size to be saved */ +} recycle_bin_struct; + +static BOOL checkparam(char *haystack,char *needle); + +/* VFS operations */  static struct vfs_ops default_vfs_ops;   /* For passthrough operation */ -static struct smb_vfs_handle_struct *recycle_handle; -static int recycle_unlink(connection_struct *, const char *); +  static int recycle_connect(struct connection_struct *conn, const char *service, const char *user);  static void recycle_disconnect(struct connection_struct *conn); +static int recycle_unlink(connection_struct *, const char *); + +#define VFS_OP(x) ((void *) x)  static vfs_op_tuple recycle_ops[] = {  	/* Disk operations */ - -	{recycle_connect,	SMB_VFS_OP_CONNECT,	SMB_VFS_LAYER_OPAQUE}, -	{recycle_disconnect,	SMB_VFS_OP_DISCONNECT,	SMB_VFS_LAYER_OPAQUE}, +	{VFS_OP(recycle_connect),	SMB_VFS_OP_CONNECT,	SMB_VFS_LAYER_TRANSPARENT}, +	{VFS_OP(recycle_disconnect),	SMB_VFS_OP_DISCONNECT,	SMB_VFS_LAYER_TRANSPARENT},  	/* File operations */ -	 -	{recycle_unlink,	SMB_VFS_OP_UNLINK,	SMB_VFS_LAYER_OPAQUE}, +	{VFS_OP(recycle_unlink),	SMB_VFS_OP_UNLINK,	SMB_VFS_LAYER_TRANSPARENT}, -	{NULL,			SMB_VFS_OP_NOOP,	SMB_VFS_LAYER_NOOP} +	{NULL,				SMB_VFS_OP_NOOP,	SMB_VFS_LAYER_NOOP}  }; -/* VFS initialisation function.  Return initialised vfs_op_tuple array back to SAMBA. */ - -vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops, -			struct smb_vfs_handle_struct *vfs_handle) +/** + * VFS initialisation function. + * + * @retval initialised vfs_op_tuple array + **/ +vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops)  { +	DEBUG(10, ("Initializing VFS module recycle\n"));  	*vfs_version = SMB_VFS_INTERFACE_VERSION;  	memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops)); - -	DEBUG(5,("vfs_init: for recycle!\n")); -	 -	/* Remember vfs_id for storing private information at connect */ -	recycle_handle = vfs_handle; +	vfs_recycle_debug_level = debug_add_class("recycle.bin"); +	if (vfs_recycle_debug_level == -1) { +		vfs_recycle_debug_level = DBGC_VFS; +		DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n")); +	} else { +		DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level)); +	}  	return recycle_ops;  } -/* VFS finalization function. */ +/** + * VFS finalization function. + * + **/  void vfs_done(connection_struct *conn)  { -	DEBUG(5,("vfs_done_recycle: called for connection %p\n",conn)); +	DEBUG(10,("Called for connection %d\n", SNUM(conn)));  }  static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)  { -	fstring recycle_bin; +	TALLOC_CTX *ctx = NULL; +	recycle_bin_struct *recbin; +	char *servicename; +	char *tmp_str; -	DEBUG(4,("recycle_connect: called for service %s as user %s\n", service, user)); +	DEBUG(10, ("Called for service %s (%d) as user %s\n", service, SNUM(conn), user)); -	fstrcpy(recycle_bin, (const char *)lp_parm_string(lp_servicename(SNUM(conn)),"vfs","recycle bin")); -	if (!*recycle_bin) { -		DEBUG(0,("recycle_connect: No options listed (vfs:recycle bin).\n" )); -		return 0; /* No options. */ +	if (!(ctx = talloc_init_named("recycle bin"))) { +		DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n")); +		return 0;  	} -	 -	standard_sub_conn(conn,recycle_bin,sizeof(fstring)); +	recbin = talloc(ctx,sizeof(recycle_bin_struct)); +	if ( recbin == NULL) { +		DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n")); +		return -1; +	} +	recbin->ctx = ctx; + +	/* Set defaults */ +	recbin->repository = talloc_strdup(ctx, ".recycle"); +	ALLOC_CHECK(recbin->repository, error); +	recbin->keep_directories = False; +	recbin->versions = False; +	recbin->touch = False; +	recbin->exclude = ""; +	recbin->exclude_dir = ""; +	recbin->noversions = ""; +	recbin->maxsize = 0; + +	/* parse configuration options */ +	servicename = talloc_strdup(recbin->ctx, lp_servicename(SNUM(conn))); +	DEBUG(10, ("servicename = %s\n",servicename)); +	if ((tmp_str = lp_parm_string(servicename, "recycle.bin", "repository")) != NULL) { +		recbin->repository = talloc_sub_conn(ctx, conn, tmp_str); +		ALLOC_CHECK(recbin->repository, error); +		trim_string(recbin->repository, "/", "/"); +		DEBUG(5, ("recycle.bin: repository = %s\n", recbin->repository)); +	} +	if ((tmp_str = lp_parm_string(servicename, "recycle.bin", "mode")) != NULL) { +		if (checkparam(tmp_str, "KEEP_DIRECTORIES") == True) +			recbin->keep_directories = True; +		if (checkparam(tmp_str, "VERSIONS") == True) +			recbin->versions = True; +		if (checkparam(tmp_str, "TOUCH") == True) +			recbin->touch = True; +		DEBUG(5, ("recycle.bin: mode = %s\n", tmp_str)); +	} +	if ((tmp_str = lp_parm_string(servicename, "recycle.bin", "maxsize")) != NULL) { +		recbin->maxsize = strtoul(tmp_str, NULL, 10); +		if (recbin->maxsize == 0) { +			recbin->maxsize = -1; +			DEBUG(5, ("recycle.bin: maxsize = -infinite-\n")); +		} else { +			DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin->maxsize)); +		} +	} +	if ((tmp_str = lp_parm_string(servicename, "recycle.bin", "exclude")) != NULL) { +		recbin->exclude = talloc_strdup(ctx, tmp_str); +		ALLOC_CHECK(recbin->exclude, error); +		DEBUG(5, ("recycle.bin: exclude = %s\n", recbin->exclude)); +	} +	if ((tmp_str = lp_parm_string(servicename,"recycle.bin", "exclude_dir")) != NULL) { +		recbin->exclude_dir = talloc_strdup(ctx, tmp_str); +		ALLOC_CHECK(recbin->exclude_dir, error); +		DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin->exclude_dir)); +	} +	if ((tmp_str = lp_parm_string(servicename,"recycle.bin", "noversions")) != NULL) { +		recbin->noversions = talloc_strdup(ctx, tmp_str); +		ALLOC_CHECK(recbin->noversions, error); +		DEBUG(5, ("recycle.bin: noversions = %s\n", recbin->noversions)); +	} -	DEBUG(3,("recycle_connect: recycle name is %s\n", recycle_bin )); +	conn->vfs_private = (void *)recbin; +	return default_vfs_ops.connect(conn, service, user); -	recycle_handle->data = (void *)strdup(recycle_bin); -	return 0; +error: +	talloc_destroy(ctx); +	return -1;  }  static void recycle_disconnect(struct connection_struct *conn)  { -	SAFE_FREE(recycle_handle->data); +	DEBUG(10, ("Disconnecting VFS module recycle bin\n")); +	if (conn->vfs_private) { +		talloc_destroy(((recycle_bin_struct *)conn->vfs_private)->ctx); +		conn->vfs_private = NULL; +	} +	default_vfs_ops.disconnect(conn);  } -static BOOL recycle_XXX_exist(connection_struct *conn, const char *dname, BOOL isdir) +static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)  {  	SMB_STRUCT_STAT st; -	if (default_vfs_ops.stat(conn,dname,&st) != 0) -		return(False); +	if (default_vfs_ops.stat(conn, dname, &st) == 0) { +		if (S_ISDIR(st.st_mode)) { +			return True; +		} +	} -	if (isdir) -		return S_ISDIR(st.st_mode) ? True : False; -	else -		return S_ISREG(st.st_mode) ? True : False; -} - -static BOOL recycle_directory_exist(connection_struct *conn, const char *dname) -{ -	return recycle_XXX_exist(conn, dname, True); +	return False;  }  static BOOL recycle_file_exist(connection_struct *conn, const char *fname)  { -	return recycle_XXX_exist(conn, fname, False); +	SMB_STRUCT_STAT st; + +	if (default_vfs_ops.stat(conn, fname, &st) == 0) { +		if (S_ISREG(st.st_mode)) { +			return True; +		} +	} + +	return False;  } +/** + * Return file size + * @param conn connection + * @param fname file name + * @return size in bytes + **/  static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)  {  	SMB_STRUCT_STAT st; +	if (default_vfs_ops.stat(conn, fname, &st) != 0) { +		DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno))); +		return (SMB_OFF_T)0; +	} +	return(st.st_size); +} -	if (default_vfs_ops.stat(conn,fname,&st) != 0) -		return (SMB_OFF_T)-1; +/** + * Create directory tree + * @param conn connection + * @param dname Directory tree to be created + * @return Returns True for success + **/ +static BOOL recycle_create_dir(connection_struct *conn, const char *dname) +{ +	int len; +	mode_t mode; +	char *new_dir = NULL; +	char *tmp_str = NULL; +	char *token; +	char *tok_str; +	BOOL ret = False; + +	mode = S_IREAD | S_IWRITE | S_IEXEC; + +	tmp_str = strdup(dname); +	ALLOC_CHECK(tmp_str, done); +	tok_str = tmp_str; + +	len = strlen(dname); +	new_dir = (char *)malloc(len + 1); +	ALLOC_CHECK(new_dir, done); +	*new_dir = '\0'; + +	/* Create directory tree if neccessary */ +	for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) { +		safe_strcat(new_dir, token, len); +		if (recycle_directory_exist(conn, new_dir)) +			DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir)); +		else { +			DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir)); +			if (default_vfs_ops.mkdir(conn, new_dir, mode) != 0) { +				DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir, strerror(errno))); +				ret = False; +				goto done; +			} +		} +		safe_strcat(new_dir, "/", len); +		} -	return(st.st_size); +	ret = True; +done: +	SAFE_FREE(tmp_str); +	SAFE_FREE(new_dir); +	return ret;  } -/******************************************************************** - Check if file should be recycled -*********************************************************************/ +/** + * Check if needle is contained exactly in haystack + * @param haystack list of parameters separated by delimimiter character + * @param needle string to be matched exactly to haystack + * @return True if found + **/ +static BOOL checkparam(char *haystack, char *needle) +{ +	char *token; +	char *tok_str; +	char *tmp_str; +	BOOL ret = False; + +	if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) { +		return False; +	} + +	tmp_str = strdup(haystack); +	ALLOC_CHECK(tmp_str, done); +	token = tok_str = tmp_str; + +	for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) { +		if(strcmp(token, needle) == 0) { +			ret = True; +			goto done; +		} +	} +done: +	SAFE_FREE(tmp_str); +	return ret; +} + +/** + * Check if needle is contained in haystack, * and ? patterns are resolved + * @param haystack list of parameters separated by delimimiter character + * @param needle string to be matched exectly to haystack including pattern matching + * @return True if found + **/ +static BOOL matchparam(char *haystack, char *needle) +{ +	char *token; +	char *tok_str; +	char *tmp_str; +	BOOL ret = False; + +	if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) { +		return False; +	} + +	tmp_str = strdup(haystack); +	ALLOC_CHECK(tmp_str, done); +	token = tok_str = tmp_str; + +	for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) { +		if (!unix_wild_match(token, needle)) { +			ret = True; +			goto done; +		} +	} +done: +	SAFE_FREE(tmp_str); +	return ret; +} + +/** + * Touch access date + **/ +static void recycle_touch(connection_struct *conn, const char *fname) +{ +	SMB_STRUCT_STAT st; +	struct utimbuf tb; +	time_t currtime; + +	if (default_vfs_ops.stat(conn, fname, &st) != 0) { +		DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno))); +		return; +	} +	currtime = time(&currtime); +	tb.actime = currtime; +	tb.modtime = st.st_mtime; + +	if (default_vfs_ops.utime(conn, fname, &tb) == -1 ) +		DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname, strerror(errno))); +	} +/** + * Check if file should be recycled + **/  static int recycle_unlink(connection_struct *conn, const char *inname)  { -	fstring recycle_bin; -	pstring fname; -	char *base, *ext; -	pstring bin; -	int i=1, len, addlen; -	int dir_mask=0770; -	SMB_BIG_UINT dfree,dsize,bsize; +	recycle_bin_struct *recbin; +	char *file_name = NULL; +	char *path_name = NULL; +       	char *temp_name = NULL; +	char *final_name = NULL; +	char *base; +	int i; +	SMB_BIG_UINT dfree, dsize, bsize; +	SMB_OFF_T file_size, space_avail; +	BOOL exist; +	int rc; + +	file_name = strdup(inname); +	ALLOC_CHECK(file_name, done); + +	if (conn->vfs_private) +		recbin = (recycle_bin_struct *)conn->vfs_private; +	else { +		DEBUG(0, ("Recycle bin not initialized!\n")); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done; +	} -	*recycle_bin = '\0'; -	pstrcpy(fname, inname); +	if(!recbin->repository || *(recbin->repository) == '\0') { +		DEBUG(3, ("Recycle path not set, purging %s...\n", file_name)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done; +	} -	if (recycle_handle->data) -		fstrcpy(recycle_bin, (const char *)recycle_handle->data); +	/* we don't recycle the recycle bin... */ +	if (strncmp(file_name, recbin->repository, strlen(recbin->repository)) == 0) { +		DEBUG(3, ("File is within recycling bin, unlinking ...\n")); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done; +	} -	if(!*recycle_bin) { -		DEBUG(1, ("recycle bin: share parameter not set, purging %s...\n", fname)); -		return default_vfs_ops.unlink(conn,fname); +	file_size = recycle_get_file_size(conn, file_name); +	/* it is wrong to purge filenames only because they are empty imho +	 *   --- simo +	 * +	if(fsize == 0) { +		DEBUG(3, ("File %s is empty, purging...\n", file_name)); +		rc = default_vfs_ops.unlink(conn,file_name); +		goto done; +	} +	 */ + +	/* FIXME: this is wrong, we should check the hole size of the recycle bin is +	 * not greater then maxsize, not the size of the single file, also it is better +	 * to remove older files +	 */ +	if(recbin->maxsize > 0 && file_size > recbin->maxsize) { +		DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done;  	} -	if(recycle_get_file_size(conn, fname) == 0) { -		DEBUG(3, ("recycle bin: file %s is empty, purging...\n", fname)); -		return default_vfs_ops.unlink(conn,fname); +	/* FIXME: this is wrong: moving files with rename does not change the disk space +	 * allocation +	 * +	space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L; +	DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size)); +	if(space_avail < file_size) { +		DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done; +	} +	 */ + +	/* extract filename and path */ +	path_name = (char *)malloc(PATH_MAX); +	ALLOC_CHECK(path_name, done); +	*path_name = '\0'; +	safe_strcpy(path_name, file_name, PATH_MAX); +	base = strrchr(path_name, '/'); +	if (base == NULL) { +		base = file_name; +		safe_strcpy(path_name, "/", PATH_MAX); +	} +	else { +		*base = '\0'; +		base++;  	} -	base = strrchr(fname, '/'); -	pstrcpy(bin, recycle_bin); -	pstrcat(bin, "/"); +	DEBUG(10, ("recycle.bin: fname = %s\n", file_name));	/* original filename with path */ +	DEBUG(10, ("recycle.bin: fpath = %s\n", path_name));	/* original path */ +	DEBUG(10, ("recycle.bin: base = %s\n", base));		/* filename without path */ -	if(base == NULL) { -		ext = strrchr(fname, '.'); -		pstrcat(bin, fname); -	} else { -		ext = strrchr(base, '.'); -		pstrcat(bin, base+1); +	if (matchparam(recbin->exclude, base)) { +		DEBUG(3, ("recycle.bin: file %s is excluded \n", base)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done;  	} -	DEBUG(4, ("recycle bin: base %s, ext %s, fname %s, bin %s\n", base, ext, fname, bin)); -	if(strcmp(fname,bin) == 0) { -		DEBUG(3, ("recycle bin: file %s exists, purging...\n", fname)); -		return default_vfs_ops.unlink(conn,fname); +	/* FIXME: this check will fail if we have more than one level of directories, +	 * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 ....  +	 * 	---simo +	 */ +	if (checkparam(recbin->exclude_dir, path_name)) { +		DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done;  	} -	len = strlen(bin); -	if ( ext != NULL) -		len = len - strlen(ext); +	temp_name = (char *)malloc(PATH_MAX); +	ALLOC_CHECK(temp_name, done); +	safe_strcpy(temp_name, recbin->repository, PATH_MAX); -	addlen = sizeof(pstring)-len-1; -	while(recycle_file_exist(conn,bin)) { -		slprintf(bin+len, addlen, " (Copy #%d)", i++); -		pstrcat(bin, ext); +	/* see if we need to recreate the original directory structure in the recycle bin */ +	if (recbin->keep_directories == True) { +		safe_strcat(temp_name, "/", PATH_MAX); +		safe_strcat(temp_name, path_name, PATH_MAX);  	} -	DEBUG(3, ("recycle bin: moving source=%s to  dest=%s\n", fname, bin)); -	default_vfs_ops.disk_free(conn,".",True,&bsize,&dfree,&dsize); -	if((unsigned int)dfree > 0) { -		int ret; -		if(!recycle_directory_exist(conn,recycle_bin)) { -			DEBUG(3, ("recycle bin: directory %s nonexistant, creating...\n", recycle_bin)); -			if (default_vfs_ops.mkdir(conn,recycle_bin,dir_mask) == -1) { -				DEBUG(0, ("recycle bin: unable to create directory %s. Error was %s\n", -					recycle_bin, strerror(errno) )); -			} +	exist = recycle_directory_exist(conn, temp_name); +	if (exist) { +		DEBUG(10, ("recycle.bin: Directory already exists\n")); +	} else { +		DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name)); +		if (recycle_create_dir(conn, temp_name) == False) { +			DEBUG(3, ("Could not create directory, purging %s...\n", file_name)); +			rc = default_vfs_ops.unlink(conn, file_name); +			goto done;  		} -		DEBUG(3, ("recycle bin: move %s -> %s\n", fname, bin)); +	} -		ret = default_vfs_ops.rename(conn, fname, bin); -		if (ret == -1) { -			DEBUG(1, ("recycle bin: move error %d (%s)\n", errno, strerror(errno) )); -			DEBUG(0, ("recycle bin: move failed, purging...\n")); -			return default_vfs_ops.unlink(conn,fname); +	safe_strcat(temp_name, "/", PATH_MAX); +	safe_strcat(temp_name, base, PATH_MAX); +	DEBUG(10, ("recycle.bin: recycled file name%s\n", temp_name));		/* new filename with path */ + +	/* check if we should delete file from recycle bin */ +	if (recycle_file_exist(conn, temp_name)) { +		if (recbin->versions == False || matchparam(recbin->noversions, base) == True) { +			DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", temp_name)); +			if (default_vfs_ops.unlink(conn, temp_name) != 0) { +				DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno))); +			}  		} -		return ret; -	} else {  -		DEBUG(1, ("recycle bin: move failed, purging...\n")); -		return default_vfs_ops.unlink(conn,fname);  	} + +	/* rename file we move to recycle bin */ +	i = 1; +	final_name = (char *)malloc(PATH_MAX); +	ALLOC_CHECK(final_name, done); +	final_name = safe_strcpy(final_name, temp_name, PATH_MAX); +	while (recycle_file_exist(conn, final_name)) { +		snprintf(final_name, PATH_MAX, "Copy #%d of ", i++); +		safe_strcat(final_name, temp_name, PATH_MAX); +	} + +	DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name, final_name)); +	rc = default_vfs_ops.rename(conn, file_name, final_name); +	if (rc != 0) { +		DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name)); +		rc = default_vfs_ops.unlink(conn, file_name); +		goto done; +	} + +	/* touch access date of moved file */ +	if (recbin->touch == True ) +		recycle_touch(conn, final_name); + +done: +	SAFE_FREE(file_name); +	SAFE_FREE(path_name); +	SAFE_FREE(temp_name); +	SAFE_FREE(final_name); +	return rc;  }  | 
