make vfs recycle use vfs facilities correctly!
[amitay/samba.git] / source3 / modules / vfs_recycle.c
1 /*
2  * Recycle bin VFS module for Samba.
3  *
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
9  *
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.
14  *
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.
19  *
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.
23  */
24
25 #include "includes.h"
26
27 #define ALLOC_CHECK(ptr, label) do { if ((ptr) == NULL) { DEBUG(0, ("recycle.bin: out of memory!\n")); errno = ENOMEM; goto label; } } while(0)
28
29 static int vfs_recycle_debug_level = DBGC_VFS;
30
31 #undef DBGC_CLASS
32 #define DBGC_CLASS vfs_recycle_debug_level
33
34 static const char *delimiter = "|";             /* delimiter for options */
35
36 /* One per connection */
37
38 typedef struct recycle_bin_struct
39 {
40         TALLOC_CTX *mem_ctx;
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 */
49 } recycle_bin_struct;
50
51 typedef struct recycle_bin_connections {
52         int conn;
53         recycle_bin_struct *data;
54         struct recycle_bin_connections *next;
55 } recycle_bin_connections;
56
57 typedef struct recycle_bin_private_data {
58         TALLOC_CTX *mem_ctx;
59         recycle_bin_connections *conns;
60 } recycle_bin_private_data;
61
62 struct smb_vfs_handle_struct *recycle_bin_private_handle;
63
64 /* VFS operations */
65 static struct vfs_ops default_vfs_ops;   /* For passthrough operation */
66
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 *);
70
71 #define VFS_OP(x) ((void *) x)
72
73 static vfs_op_tuple recycle_ops[] = {
74
75         /* Disk operations */
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},
78
79         /* File operations */
80         {VFS_OP(recycle_unlink),        SMB_VFS_OP_UNLINK,      SMB_VFS_LAYER_TRANSPARENT},
81
82         {NULL,                          SMB_VFS_OP_NOOP,        SMB_VFS_LAYER_NOOP}
83 };
84
85 static BOOL check_bool_param(const char *value)
86 {
87         if (strwicmp(value, "yes") == 0 ||
88             strwicmp(value, "true") == 0 ||
89             strwicmp(value, "1") == 0)
90                 return True;
91
92         return False;
93 }
94
95 /**
96  * VFS initialisation function.
97  *
98  * @retval initialised vfs_op_tuple array
99  **/
100 vfs_op_tuple *vfs_init(int *vfs_version, struct vfs_ops *def_vfs_ops,
101                         struct smb_vfs_handle_struct *vfs_handle)
102 {
103         TALLOC_CTX *mem_ctx = NULL;
104
105         DEBUG(10, ("Initializing VFS module recycle\n"));
106         *vfs_version = SMB_VFS_INTERFACE_VERSION;
107         memcpy(&default_vfs_ops, def_vfs_ops, sizeof(struct vfs_ops));
108         vfs_recycle_debug_level = debug_add_class("vfs_recycle_bin");
109         if (vfs_recycle_debug_level == -1) {
110                 vfs_recycle_debug_level = DBGC_VFS;
111                 DEBUG(0, ("vfs_recycle: Couldn't register custom debugging class!\n"));
112         } else {
113                 DEBUG(0, ("vfs_recycle: Debug class number of 'vfs_recycle': %d\n", vfs_recycle_debug_level));
114         }
115
116         recycle_bin_private_handle = vfs_handle;
117         if (!(mem_ctx = talloc_init_named("recycle bin data"))) {
118                 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
119                 return NULL;
120         }
121
122         recycle_bin_private_handle->data = talloc(mem_ctx, sizeof(recycle_bin_private_data));
123         if (recycle_bin_private_handle->data == NULL) {
124                 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
125                 return NULL;
126         }
127         ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->mem_ctx = mem_ctx;
128         ((recycle_bin_private_data *)(recycle_bin_private_handle->data))->conns = NULL;
129
130         return recycle_ops;
131 }
132
133 /**
134  * VFS finalization function.
135  *
136  **/
137 void vfs_done(void)
138 {
139         recycle_bin_private_data *recdata;
140         recycle_bin_connections *recconn;
141
142         DEBUG(10, ("Unloading/Cleaning VFS module recycle bin\n"));
143
144         if (recycle_bin_private_handle)
145                 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
146         else {
147                 DEBUG(0, ("Recycle bin not initialized!\n"));
148                 return;
149         }
150
151         if (recdata) {
152                 if (recdata->conns) {
153                         recconn = recdata->conns;
154                         while (recconn) {
155                                 talloc_destroy(recconn->data->mem_ctx);
156                                 recconn = recconn->next;
157                         }
158                 }
159                 if (recdata->mem_ctx) {
160                         talloc_destroy(recdata->mem_ctx);
161                 }
162                 recdata = NULL;
163         }
164 }
165
166 static int recycle_connect(struct connection_struct *conn, const char *service, const char *user)
167 {
168         TALLOC_CTX *ctx = NULL;
169         recycle_bin_struct *recbin;
170         recycle_bin_connections *recconn;
171         recycle_bin_connections *recconnbase;
172         recycle_bin_private_data *recdata;
173         char *servicename;
174         char *tmp_str;
175
176         DEBUG(10, ("Called for service %s (%d) as user %s\n", service, SNUM(conn), user));
177
178         if (recycle_bin_private_handle)
179                 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
180         else {
181                 DEBUG(0, ("Recycle bin not initialized!\n"));
182                 return -1;
183         }
184
185         if (!(ctx = talloc_init_named("recycle bin connection"))) {
186                 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
187                 return -1;
188         }
189
190         recbin = talloc(ctx, sizeof(recycle_bin_struct));
191         if (recbin == NULL) {
192                 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
193                 return -1;
194         }
195         recbin->mem_ctx = ctx;
196
197         /* Set defaults */
198         recbin->repository = talloc_strdup(recbin->mem_ctx, ".recycle");
199         ALLOC_CHECK(recbin->repository, error);
200         recbin->keep_dir_tree = False;
201         recbin->versions = False;
202         recbin->touch = False;
203         recbin->exclude = "";
204         recbin->exclude_dir = "";
205         recbin->noversions = "";
206         recbin->maxsize = 0;
207
208         /* parse configuration options */
209         servicename = talloc_strdup(recbin->mem_ctx, lp_servicename(SNUM(conn)));
210         DEBUG(10, ("servicename = %s\n",servicename));
211         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "repository")) != NULL) {
212                 recbin->repository = talloc_sub_conn(recbin->mem_ctx, conn, tmp_str);
213                 ALLOC_CHECK(recbin->repository, error);
214                 trim_string(recbin->repository, "/", "/");
215                 DEBUG(5, ("recycle.bin: repository = %s\n", recbin->repository));
216         }
217         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "keeptree")) != NULL) {
218                 if (check_bool_param(tmp_str) == True)
219                         recbin->keep_dir_tree = True;
220                 DEBUG(5, ("recycle.bin: keeptree = %s\n", tmp_str));
221         }
222         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "versions")) != NULL) {
223                 if (check_bool_param(tmp_str) == True)
224                         recbin->versions = True;
225                 DEBUG(5, ("recycle.bin: versions = %s\n", tmp_str));
226         }
227         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "touch")) != NULL) {
228                 if (check_bool_param(tmp_str) == True)
229                         recbin->touch = True;
230                 DEBUG(5, ("recycle.bin: touch = %s\n", tmp_str));
231         }
232         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "maxsize")) != NULL) {
233                 recbin->maxsize = strtoul(tmp_str, NULL, 10);
234                 if (recbin->maxsize == 0) {
235                         recbin->maxsize = -1;
236                         DEBUG(5, ("recycle.bin: maxsize = -infinite-\n"));
237                 } else {
238                         DEBUG(5, ("recycle.bin: maxsize = %ld\n", (long int)recbin->maxsize));
239                 }
240         }
241         if ((tmp_str = lp_parm_string(servicename, "vfs_recycle_bin", "exclude")) != NULL) {
242                 recbin->exclude = talloc_strdup(recbin->mem_ctx, tmp_str);
243                 ALLOC_CHECK(recbin->exclude, error);
244                 DEBUG(5, ("recycle.bin: exclude = %s\n", recbin->exclude));
245         }
246         if ((tmp_str = lp_parm_string(servicename,"vfs_recycle_bin", "exclude_dir")) != NULL) {
247                 recbin->exclude_dir = talloc_strdup(recbin->mem_ctx, tmp_str);
248                 ALLOC_CHECK(recbin->exclude_dir, error);
249                 DEBUG(5, ("recycle.bin: exclude_dir = %s\n", recbin->exclude_dir));
250         }
251         if ((tmp_str = lp_parm_string(servicename,"vfs_recycle_bin", "noversions")) != NULL) {
252                 recbin->noversions = talloc_strdup(recbin->mem_ctx, tmp_str);
253                 ALLOC_CHECK(recbin->noversions, error);
254                 DEBUG(5, ("recycle.bin: noversions = %s\n", recbin->noversions));
255         }
256
257         recconn = talloc(recdata->mem_ctx, sizeof(recycle_bin_connections));
258         if (recconn == NULL) {
259                 DEBUG(0, ("Failed to allocate memory in VFS module recycle_bin\n"));
260                 goto error;
261         }
262         recconn->conn = SNUM(conn);
263         recconn->data = recbin;
264         recconn->next = NULL;
265         if (recdata->conns) {
266                 recconnbase = recdata->conns;
267                 while (recconnbase->next != NULL) recconnbase = recconnbase->next;
268                 recconnbase->next = recconn;
269         } else {
270                 recdata->conns = recconn;
271         }
272         return default_vfs_ops.connect(conn, service, user);
273
274 error:
275         talloc_destroy(ctx);
276         return -1;
277 }
278
279 static void recycle_disconnect(struct connection_struct *conn)
280 {
281         recycle_bin_private_data *recdata;
282         recycle_bin_connections *recconn;
283
284         DEBUG(10, ("Disconnecting VFS module recycle bin\n"));
285
286         if (recycle_bin_private_handle)
287                 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
288         else {
289                 DEBUG(0, ("Recycle bin not initialized!\n"));
290                 return;
291         }
292
293         if (recdata) {
294                 if (recdata->conns) {
295                         if (recdata->conns->conn == SNUM(conn)) {
296                                 talloc_destroy(recdata->conns->data->mem_ctx);
297                                 recdata->conns = recdata->conns->next;
298                         } else {
299                                 recconn = recdata->conns;
300                                 while (recconn->next) {
301                                         if (recconn->next->conn == SNUM(conn)) {
302                                                 talloc_destroy(recconn->next->data->mem_ctx);
303                                                 recconn->next = recconn->next->next;
304                                                 break;
305                                         }
306                                         recconn = recconn->next;
307                                 }
308                         }
309                 }
310         }
311         default_vfs_ops.disconnect(conn);
312 }
313
314 static BOOL recycle_directory_exist(connection_struct *conn, const char *dname)
315 {
316         SMB_STRUCT_STAT st;
317
318         if (default_vfs_ops.stat(conn, dname, &st) == 0) {
319                 if (S_ISDIR(st.st_mode)) {
320                         return True;
321                 }
322         }
323
324         return False;
325 }
326
327 static BOOL recycle_file_exist(connection_struct *conn, const char *fname)
328 {
329         SMB_STRUCT_STAT st;
330
331         if (default_vfs_ops.stat(conn, fname, &st) == 0) {
332                 if (S_ISREG(st.st_mode)) {
333                         return True;
334                 }
335         }
336
337         return False;
338 }
339
340 /**
341  * Return file size
342  * @param conn connection
343  * @param fname file name
344  * @return size in bytes
345  **/
346 static SMB_OFF_T recycle_get_file_size(connection_struct *conn, const char *fname)
347 {
348         SMB_STRUCT_STAT st;
349         if (default_vfs_ops.stat(conn, fname, &st) != 0) {
350                 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
351                 return (SMB_OFF_T)0;
352         }
353         return(st.st_size);
354 }
355
356 /**
357  * Create directory tree
358  * @param conn connection
359  * @param dname Directory tree to be created
360  * @return Returns True for success
361  **/
362 static BOOL recycle_create_dir(connection_struct *conn, const char *dname)
363 {
364         int len;
365         mode_t mode;
366         char *new_dir = NULL;
367         char *tmp_str = NULL;
368         char *token;
369         char *tok_str;
370         BOOL ret = False;
371
372         mode = S_IREAD | S_IWRITE | S_IEXEC;
373
374         tmp_str = strdup(dname);
375         ALLOC_CHECK(tmp_str, done);
376         tok_str = tmp_str;
377
378         len = strlen(dname);
379         new_dir = (char *)malloc(len + 1);
380         ALLOC_CHECK(new_dir, done);
381         *new_dir = '\0';
382
383         /* Create directory tree if neccessary */
384         for(token = strtok(tok_str, "/"); token; token = strtok(NULL, "/")) {
385                 safe_strcat(new_dir, token, len);
386                 if (recycle_directory_exist(conn, new_dir))
387                         DEBUG(10, ("recycle.bin: dir %s already exists\n", new_dir));
388                 else {
389                         DEBUG(5, ("recycle.bin: creating new dir %s\n", new_dir));
390                         if (default_vfs_ops.mkdir(conn, new_dir, mode) != 0) {
391                                 DEBUG(1,("recycle.bin: mkdir failed for %s with error: %s\n", new_dir, strerror(errno)));
392                                 ret = False;
393                                 goto done;
394                         }
395                 }
396                 safe_strcat(new_dir, "/", len);
397                 }
398
399         ret = True;
400 done:
401         SAFE_FREE(tmp_str);
402         SAFE_FREE(new_dir);
403         return ret;
404 }
405
406 /**
407  * Check if needle is contained exactly in haystack
408  * @param haystack list of parameters separated by delimimiter character
409  * @param needle string to be matched exactly to haystack
410  * @return True if found
411  **/
412 static BOOL checkparam(const char *haystack, const char *needle)
413 {
414         char *token;
415         char *tok_str;
416         char *tmp_str;
417         BOOL ret = False;
418
419         if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
420                 return False;
421         }
422
423         tmp_str = strdup(haystack);
424         ALLOC_CHECK(tmp_str, done);
425         token = tok_str = tmp_str;
426
427         for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
428                 if(strcmp(token, needle) == 0) {
429                         ret = True;
430                         goto done;
431                 }
432         }
433 done:
434         SAFE_FREE(tmp_str);
435         return ret;
436 }
437
438 /**
439  * Check if needle is contained in haystack, * and ? patterns are resolved
440  * @param haystack list of parameters separated by delimimiter character
441  * @param needle string to be matched exectly to haystack including pattern matching
442  * @return True if found
443  **/
444 static BOOL matchparam(const char *haystack, const char *needle)
445 {
446         char *token;
447         char *tok_str;
448         char *tmp_str;
449         BOOL ret = False;
450
451         if (haystack == NULL || strlen(haystack) == 0 || needle == NULL || strlen(needle) == 0) {
452                 return False;
453         }
454
455         tmp_str = strdup(haystack);
456         ALLOC_CHECK(tmp_str, done);
457         token = tok_str = tmp_str;
458
459         for(token = strtok(tok_str, delimiter); token; token = strtok(NULL, delimiter)) {
460                 if (!unix_wild_match(token, needle)) {
461                         ret = True;
462                         goto done;
463                 }
464         }
465 done:
466         SAFE_FREE(tmp_str);
467         return ret;
468 }
469
470 /**
471  * Touch access date
472  **/
473 static void recycle_touch(connection_struct *conn, const char *fname)
474 {
475         SMB_STRUCT_STAT st;
476         struct utimbuf tb;
477         time_t currtime;
478
479         if (default_vfs_ops.stat(conn, fname, &st) != 0) {
480                 DEBUG(0,("recycle.bin: stat for %s returned %s\n", fname, strerror(errno)));
481                 return;
482         }
483         currtime = time(&currtime);
484         tb.actime = currtime;
485         tb.modtime = st.st_mtime;
486
487         if (default_vfs_ops.utime(conn, fname, &tb) == -1 )
488                 DEBUG(0, ("recycle.bin: touching %s failed, reason = %s\n", fname, strerror(errno)));
489         }
490
491 /**
492  * Check if file should be recycled
493  **/
494 static int recycle_unlink(connection_struct *conn, const char *inname)
495 {
496         recycle_bin_private_data *recdata;
497         recycle_bin_connections *recconn;
498         recycle_bin_struct *recbin;
499         char *file_name = NULL;
500         char *path_name = NULL;
501         char *temp_name = NULL;
502         char *final_name = NULL;
503         char *base;
504         int i;
505 /*      SMB_BIG_UINT dfree, dsize, bsize;       */
506         SMB_OFF_T file_size; /* space_avail;    */
507         BOOL exist;
508         int rc = -1;
509
510         file_name = strdup(inname);
511         ALLOC_CHECK(file_name, done);
512
513         recbin = NULL;
514         if (recycle_bin_private_handle) {
515                 recdata = (recycle_bin_private_data *)(recycle_bin_private_handle->data);
516                 if (recdata) {
517                         if (recdata->conns) {
518                                 recconn = recdata->conns;
519                                 while (recconn && recconn->conn != SNUM(conn)) recconn = recconn->next;
520                                 if (recconn != NULL) {
521                                         recbin = recconn->data;
522                                 }
523                         }
524                 }
525         }
526         if (recbin == NULL) {
527                 DEBUG(0, ("Recycle bin not initialized!\n"));
528                 rc = default_vfs_ops.unlink(conn, file_name);
529                 goto done;
530         }
531
532         if(!recbin->repository || *(recbin->repository) == '\0') {
533                 DEBUG(3, ("Recycle path not set, purging %s...\n", file_name));
534                 rc = default_vfs_ops.unlink(conn, file_name);
535                 goto done;
536         }
537
538         /* we don't recycle the recycle bin... */
539         if (strncmp(file_name, recbin->repository, strlen(recbin->repository)) == 0) {
540                 DEBUG(3, ("File is within recycling bin, unlinking ...\n"));
541                 rc = default_vfs_ops.unlink(conn, file_name);
542                 goto done;
543         }
544
545         file_size = recycle_get_file_size(conn, file_name);
546         /* it is wrong to purge filenames only because they are empty imho
547          *   --- simo
548          *
549         if(fsize == 0) {
550                 DEBUG(3, ("File %s is empty, purging...\n", file_name));
551                 rc = default_vfs_ops.unlink(conn,file_name);
552                 goto done;
553         }
554          */
555
556         /* FIXME: this is wrong, we should check the hole size of the recycle bin is
557          * not greater then maxsize, not the size of the single file, also it is better
558          * to remove older files
559          */
560         if(recbin->maxsize > 0 && file_size > recbin->maxsize) {
561                 DEBUG(3, ("File %s exceeds maximum recycle size, purging... \n", file_name));
562                 rc = default_vfs_ops.unlink(conn, file_name);
563                 goto done;
564         }
565
566         /* FIXME: this is wrong: moving files with rename does not change the disk space
567          * allocation
568          *
569         space_avail = default_vfs_ops.disk_free(conn, ".", True, &bsize, &dfree, &dsize) * 1024L;
570         DEBUG(5, ("space_avail = %Lu, file_size = %Lu\n", space_avail, file_size));
571         if(space_avail < file_size) {
572                 DEBUG(3, ("Not enough diskspace, purging file %s\n", file_name));
573                 rc = default_vfs_ops.unlink(conn, file_name);
574                 goto done;
575         }
576          */
577
578         /* extract filename and path */
579         path_name = (char *)malloc(PATH_MAX);
580         ALLOC_CHECK(path_name, done);
581         *path_name = '\0';
582         safe_strcpy(path_name, file_name, PATH_MAX);
583         base = strrchr(path_name, '/');
584         if (base == NULL) {
585                 base = file_name;
586                 safe_strcpy(path_name, "/", PATH_MAX);
587         }
588         else {
589                 *base = '\0';
590                 base++;
591         }
592
593         DEBUG(10, ("recycle.bin: fname = %s\n", file_name));    /* original filename with path */
594         DEBUG(10, ("recycle.bin: fpath = %s\n", path_name));    /* original path */
595         DEBUG(10, ("recycle.bin: base = %s\n", base));          /* filename without path */
596
597         if (matchparam(recbin->exclude, base)) {
598                 DEBUG(3, ("recycle.bin: file %s is excluded \n", base));
599                 rc = default_vfs_ops.unlink(conn, file_name);
600                 goto done;
601         }
602
603         /* FIXME: this check will fail if we have more than one level of directories,
604          * we shoud check for every level 1, 1/2, 1/2/3, 1/2/3/4 .... 
605          *      ---simo
606          */
607         if (checkparam(recbin->exclude_dir, path_name)) {
608                 DEBUG(3, ("recycle.bin: directory %s is excluded \n", path_name));
609                 rc = default_vfs_ops.unlink(conn, file_name);
610                 goto done;
611         }
612
613         temp_name = (char *)malloc(PATH_MAX);
614         ALLOC_CHECK(temp_name, done);
615         safe_strcpy(temp_name, recbin->repository, PATH_MAX);
616
617         /* see if we need to recreate the original directory structure in the recycle bin */
618         if (recbin->keep_dir_tree == True) {
619                 safe_strcat(temp_name, "/", PATH_MAX);
620                 safe_strcat(temp_name, path_name, PATH_MAX);
621         }
622
623         exist = recycle_directory_exist(conn, temp_name);
624         if (exist) {
625                 DEBUG(10, ("recycle.bin: Directory already exists\n"));
626         } else {
627                 DEBUG(10, ("recycle.bin: Creating directory %s\n", temp_name));
628                 if (recycle_create_dir(conn, temp_name) == False) {
629                         DEBUG(3, ("Could not create directory, purging %s...\n", file_name));
630                         rc = default_vfs_ops.unlink(conn, file_name);
631                         goto done;
632                 }
633         }
634
635         final_name = (char *)malloc(PATH_MAX);
636         ALLOC_CHECK(final_name, done);
637         snprintf(final_name, PATH_MAX, "%s/%s", temp_name, base);
638         DEBUG(10, ("recycle.bin: recycled file name%s\n", temp_name));          /* new filename with path */
639
640         /* check if we should delete file from recycle bin */
641         if (recycle_file_exist(conn, final_name)) {
642                 if (recbin->versions == False || matchparam(recbin->noversions, base) == True) {
643                         DEBUG(3, ("recycle.bin: Removing old file %s from recycle bin\n", final_name));
644                         if (default_vfs_ops.unlink(conn, final_name) != 0) {
645                                 DEBUG(1, ("recycle.bin: Error deleting old file: %s\n", strerror(errno)));
646                         }
647                 }
648         }
649
650         /* rename file we move to recycle bin */
651         i = 1;
652         while (recycle_file_exist(conn, final_name)) {
653                 snprintf(final_name, PATH_MAX, "%s/Copy #%d of %s", temp_name, i++, base);
654         }
655
656         DEBUG(10, ("recycle.bin: Moving %s to %s\n", file_name, final_name));
657         rc = default_vfs_ops.rename(conn, file_name, final_name);
658         if (rc != 0) {
659                 DEBUG(3, ("recycle.bin: Move error %d (%s), purging file %s (%s)\n", errno, strerror(errno), file_name, final_name));
660                 rc = default_vfs_ops.unlink(conn, file_name);
661                 goto done;
662         }
663
664         /* touch access date of moved file */
665         if (recbin->touch == True )
666                 recycle_touch(conn, final_name);
667
668 done:
669         SAFE_FREE(file_name);
670         SAFE_FREE(path_name);
671         SAFE_FREE(temp_name);
672         SAFE_FREE(final_name);
673         return rc;
674 }