summaryrefslogtreecommitdiffstats
path: root/xlators/features/trash/src/trash.c
blob: ad8d1a7e3fd8197ebdc34bbd2105be66e7e16c81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
/*
  Copyright (c) 2006-2009 Gluster, Inc. <http://www.gluster.com>
  This file is part of GlusterFS.

  GlusterFS 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 3 of the License,
  or (at your option) any later version.

  GlusterFS 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, see
  <http://www.gnu.org/licenses/>.
*/

#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif

#include "trash.h"

int32_t
trash_unlink_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct stat *buf,
                         struct stat *preoldparent, struct stat *postoldparent,
                         struct stat *prenewparent, struct stat *postnewparent);

void
trash_local_wipe (trash_local_t *local)
{
        if (!local)
                goto out;

        loc_wipe (&local->loc);
        loc_wipe (&local->newloc);

        if (local->fd)
                fd_unref (local->fd);

        if (local->newfd)
                fd_unref (local->newfd);

        FREE (local);
out:
        return;
}

int32_t
trash_common_unwind_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno,
                         struct stat *preparent, struct stat *postparent)
{
        TRASH_STACK_UNWIND (frame, op_ret, op_errno, preparent, postparent);
        return 0;
}

int32_t
trash_unlink_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct stat *stbuf, struct stat *preparent,
                        struct stat *postparent)
{
        trash_local_t *local       = NULL;
        char          *tmp_str     = NULL;
        char          *tmp_path    = NULL;
        char          *tmp_dirname = NULL;
        char          *dir_name    = NULL;
        int32_t        count       = 0;
        int32_t        loop_count  = 0;
        int            i           = 0;
        loc_t          tmp_loc     = {0,};

        local   = frame->local;
        tmp_str = strdup (local->newpath);
        if (!tmp_str) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
        }
        loop_count = local->loop_count;

        if ((op_ret == -1) &&  (op_errno == ENOENT)) {
                tmp_dirname = strchr (tmp_str, '/');
                while (tmp_dirname) {
                        count = tmp_dirname - tmp_str;
                        if (count == 0)
                                count = 1;
                        i++;
                        if (i > loop_count)
                                break;
                        tmp_dirname = strchr (tmp_str + count + 1, '/');
                }
                tmp_path = strndup (local->newpath, count);
                if (!tmp_path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }

                tmp_loc.path = tmp_path;

                /* TODO:create the directory with proper permissions */
                STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_path,
                                   this->children->xlator,
                                   this->children->xlator->fops->mkdir,
                                   &tmp_loc, 0755);

                goto out;
        }

        if (op_ret == 0) {
                dir_name = dirname (tmp_str);
                if (strcmp((char*)cookie, dir_name) == 0) {
                        tmp_loc.path = local->newpath;
                        STACK_WIND (frame, trash_unlink_rename_cbk,
                                    this->children->xlator,
                                    this->children->xlator->fops->rename,
                                    &local->loc, &tmp_loc);
                        goto out;
                }
        }

        LOCK (&frame->lock);
        {
                loop_count = ++local->loop_count;
        }
        UNLOCK (&frame->lock);
        tmp_dirname = strchr (tmp_str, '/');
        while (tmp_dirname) {
                count = tmp_dirname - tmp_str;
                if (count == 0)
                        count = 1;
                i++;
                if ((i > loop_count) || (count > PATH_MAX))
                        break;
                tmp_dirname = strchr (tmp_str + count + 1, '/');
        }
        tmp_path = strndup (local->newpath, count);
        if (!tmp_path) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
        }
        tmp_loc.path = tmp_path;

        STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_path,
                           this->children->xlator,
                           this->children->xlator->fops->mkdir,
                           &tmp_loc, 0755);

out:
        free (cookie);
        free (tmp_str);

        return 0;
}

int32_t
trash_rename_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct stat *stbuf, struct stat *preparent,
                        struct stat *postparent);

int32_t
trash_unlink_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct stat *buf,
                         struct stat *preoldparent, struct stat *postoldparent,
                         struct stat *prenewparent, struct stat *postnewparent)
{
        trash_local_t   *local      = NULL;
        trash_private_t *priv       = NULL;
        char            *tmp_str    = NULL;
        char            *dir_name   = NULL;
        char            *tmp_cookie = NULL;
        loc_t            tmp_loc    = {0,};

        priv  = this->private;
        local = frame->local;

        if ((op_ret == -1) && (op_errno == ENOENT)) {
                tmp_str = strdup (local->newpath);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }
                dir_name = dirname (tmp_str);

                tmp_loc.path = dir_name;

                tmp_cookie = strdup (dir_name);
                if (!tmp_cookie) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }
                /* TODO: create the directory with proper permissions */
                STACK_WIND_COOKIE (frame, trash_unlink_mkdir_cbk, tmp_cookie,
                                   FIRST_CHILD(this),
                                   FIRST_CHILD(this)->fops->mkdir,
                                   &tmp_loc, 0755);

                free (tmp_str);

                return 0;
        }

        if ((op_ret == -1) && (op_errno == ENOTDIR)) {

                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists, cannot keep the copy, deleting",
                        local->newpath);

                STACK_WIND (frame, trash_common_unwind_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->unlink, &local->loc);

                return 0;
        }

        if ((op_ret == -1) && (op_errno == EISDIR)) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists as directory, cannot keep copy, "
                        "deleting", local->newpath);

                STACK_WIND (frame, trash_common_unwind_cbk,
                            FIRST_CHILD(this),
                            FIRST_CHILD(this)->fops->unlink, &local->loc);
                return 0;
        }

        /* All other cases, unlink should return success */
        TRASH_STACK_UNWIND (frame, 0, op_errno, &local->preparent,
                            &local->postparent);

        return 0;
}


int
trash_common_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct stat *stbuf,
                         struct stat *preoldparent, struct stat *postoldparent,
                         struct stat *prenewparent, struct stat *postnewparent)
{
        TRASH_STACK_UNWIND (frame, op_ret, op_errno, stbuf, preoldparent,
                            postoldparent, prenewparent, postnewparent);
        return 0;
}


int32_t
trash_unlink_stat_cbk (call_frame_t *frame,  void *cookie, xlator_t *this,
                       int32_t op_ret, int32_t op_errno, struct stat *buf)
{
        trash_private_t *priv    = NULL;
        trash_local_t   *local   = NULL;
        loc_t            new_loc = {0,};

        priv  = this->private;
        local = frame->local;

        if (-1 == op_ret) {
                gf_log (this->name, GF_LOG_DEBUG, "%s: %s",
                        local->loc.path, strerror (op_errno));
                goto fail;
        }

        if ((buf->st_size == 0) ||
            (buf->st_size > priv->max_trash_file_size)) {
                /* if the file is too big or zero, just unlink it */

                if (buf->st_size > priv->max_trash_file_size) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file size too big (%"GF_PRI_SIZET") to "
                                "move into trash directory",
                                local->loc.path, buf->st_size);
                }

                STACK_WIND (frame, trash_common_unwind_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->unlink, &local->loc);
                return 0;
        }

        new_loc.path = local->newpath;

        STACK_WIND (frame, trash_unlink_rename_cbk,
                    this->children->xlator,
                    this->children->xlator->fops->rename,
                    &local->loc, &new_loc);

        return 0;

fail:
        TRASH_STACK_UNWIND (frame, op_ret, op_errno, buf,
                            NULL, NULL, NULL, NULL);

        return 0;

}

int32_t
trash_rename_rename_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, struct stat *buf,
                         struct stat *preoldparent, struct stat *postoldparent,
                         struct stat *prenewparent, struct stat *postnewparent)
{
        trash_local_t *local    = NULL;
        char          *tmp_str  = NULL;
        char          *dir_name = NULL;
        char          *tmp_path = NULL;
        loc_t          tmp_loc  = {0,};

        local = frame->local;
        if ((op_ret == -1) && (op_errno == ENOENT)) {
                tmp_str  = strdup (local->newpath);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }
                dir_name = dirname (tmp_str);

                /* check for the errno, if its ENOENT create directory and call
                 * rename later
                 */
                tmp_path = strdup (dir_name);
                if (!tmp_path) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }
                tmp_loc.path = tmp_path;

                /* TODO: create the directory with proper permissions */
                STACK_WIND_COOKIE (frame, trash_rename_mkdir_cbk, tmp_path,
                                   this->children->xlator,
                                   this->children->xlator->fops->mkdir,
                                   &tmp_loc, 0755);

                free (tmp_str);
                return 0;
        }

        if ((op_ret == -1) && (op_errno == ENOTDIR)) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists, cannot keep the dest entry(%s): "
                        "renaming", local->newpath, local->origpath);
        } else if ((op_ret == -1) && (op_errno == EISDIR)) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "target(%s) exists as a directory, cannot keep the "
                        "copy (%s), renaming", local->newpath, local->origpath);
        }

        STACK_WIND (frame, trash_common_rename_cbk,
                    this->children->xlator,
                    this->children->xlator->fops->rename, &local->loc,
                    &local->newloc);

        return 0;
}


int32_t
trash_rename_mkdir_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                        int32_t op_ret, int32_t op_errno, inode_t *inode,
                        struct stat *stbuf, struct stat *preparent,
                        struct stat *postparent)
{
        trash_local_t *local = NULL;
        char          *tmp_str = NULL;
        char          *tmp_path = NULL;
        char          *tmp_dirname = NULL;
        char          *dir_name = NULL;
        int32_t        count = 0;
        loc_t          tmp_loc = {0,};

        local   = frame->local;
        tmp_str = strdup (local->newpath);
        if (!tmp_str) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
        }

        if ((op_ret == -1) && (op_errno == ENOENT)) {
                tmp_dirname = strchr (tmp_str, '/');
                while (tmp_dirname) {
                        count = tmp_dirname - tmp_str;
                        if (count == 0)
                                count = 1;

                        tmp_dirname = strchr (tmp_str + count + 1, '/');

                        tmp_path = strndup (local->newpath, count);
                        if (!tmp_path) {
                                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        }

                        tmp_loc.path = tmp_path;

                        /* TODO: create the directory with proper permissions */
                        STACK_WIND_COOKIE (frame, trash_rename_mkdir_cbk,
                                           tmp_path,  this->children->xlator,
                                           this->children->xlator->fops->mkdir,
                                           &tmp_loc, 0755);
                }

                goto out;
        }

        dir_name = dirname (tmp_str);
        if (strcmp ((char*)cookie, dir_name) == 0) {
                tmp_loc.path = local->newpath;

                STACK_WIND (frame, trash_rename_rename_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->rename,
                            &local->newloc, &tmp_loc);
        }

out:
        free (cookie); /* strdup (dir_name) was sent here :) */
        free (tmp_str);

        return 0;
}

int32_t
trash_rename_lookup_cbk (call_frame_t *frame, void *cookie, xlator_t *this,
                         int32_t op_ret, int32_t op_errno, inode_t *inode,
                         struct stat *buf, dict_t *xattr,
                         struct stat *postparent)
{
        trash_private_t *priv = NULL;
        trash_local_t   *local = NULL;
        loc_t            tmp_loc = {0,};

        local = frame->local;
        priv  = this->private;

        if (op_ret == -1) {
                STACK_WIND (frame, trash_common_rename_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->rename,
                            &local->loc, &local->newloc);
                return 0;
        }
        if ((buf->st_size == 0) ||
            (buf->st_size > priv->max_trash_file_size)) {
                /* if the file is too big or zero, just unlink it */

                if (buf->st_size > priv->max_trash_file_size) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file size too big (%"GF_PRI_SIZET") to "
                                "move into trash directory",
                                local->newloc.path, buf->st_size);
                }

                STACK_WIND (frame, trash_common_rename_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->rename,
                            &local->loc, &local->newloc);
                return 0;
        }

        tmp_loc.path = local->newpath;

        STACK_WIND (frame, trash_rename_rename_cbk,
                    this->children->xlator,
                    this->children->xlator->fops->rename,
                    &local->newloc, &tmp_loc);

        return 0;
}


int32_t
trash_rename (call_frame_t *frame, xlator_t *this, loc_t *oldloc,
              loc_t *newloc)
{
        trash_elim_pattern_t  *trav  = NULL;
        trash_private_t *priv = NULL;
        trash_local_t   *local = NULL;
        struct tm       *tm = NULL;
        char             timestr[256] = {0,};
        time_t           utime = 0;
        int32_t          match = 0;

        priv = this->private;
        if (priv->eliminate) {
                trav = priv->eliminate;
                while (trav) {
                        if (fnmatch(trav->pattern, newloc->name, 0) == 0) {
                                match++;
                                break;
                        }
                        trav = trav->next;
                }
        }

        if ((strncmp (oldloc->path, priv->trash_dir,
                      strlen (priv->trash_dir)) == 0) || match) {
                /* Trying to rename from the trash dir,
                   do the actual rename */
                STACK_WIND (frame, trash_common_rename_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->rename,
                            oldloc, newloc);

                return 0;
        }

        local = CALLOC (1, sizeof (trash_local_t));
        if (!local) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                TRASH_STACK_UNWIND (frame, -1, ENOMEM,
                                    NULL, NULL, NULL, NULL, NULL);
                return 0;
        }

        frame->local = local;
        loc_copy (&local->loc, oldloc);

        loc_copy (&local->newloc, newloc);

        strcpy (local->origpath, newloc->path);
        strcpy (local->newpath, priv->trash_dir);
        strcat (local->newpath, newloc->path);

        {
                /* append timestamp to file name */
                /* TODO: can we make it optional? */
                utime = time (NULL);
                tm    = localtime (&utime);
                strftime (timestr, 256, ".%Y-%m-%d-%H%M%S", tm);
                strcat (local->newpath, timestr);
        }

        /* Send a lookup call on newloc, to ensure we are not
           overwriting */
        STACK_WIND (frame, trash_rename_lookup_cbk,
                    this->children->xlator,
                    this->children->xlator->fops->lookup, newloc, 0);

        return 0;
}

int32_t
trash_unlink (call_frame_t *frame, xlator_t *this, loc_t *loc)
{
        trash_elim_pattern_t  *trav  = NULL;
        trash_private_t *priv = NULL;
        trash_local_t   *local = NULL;
        struct tm       *tm = NULL;
        char             timestr[256] = {0,};
        time_t           utime = 0;
        int32_t          match = 0;

        priv = this->private;

        if (priv->eliminate) {
                trav = priv->eliminate;
                while (trav) {
                        if (fnmatch(trav->pattern, loc->name, 0) == 0) {
                                match++;
                                break;
                        }
                        trav = trav->next;
                }
        }

        if ((strncmp (loc->path, priv->trash_dir,
                      strlen (priv->trash_dir)) == 0) || (match)) {
                if (match) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "%s: file matches eliminate pattern, "
                                "not moved to trash", loc->name);
                } else {
                        /* unlink from the trash-dir, not keeping any copy */
                        ;
                }

                STACK_WIND (frame, trash_common_unwind_cbk,
                            this->children->xlator,
                            this->children->xlator->fops->unlink, loc);
                return 0;
        }

        local = CALLOC (1, sizeof (trash_local_t));
        if (!local) {
                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                TRASH_STACK_UNWIND (frame, -1, ENOMEM, NULL, NULL);
                return 0;
        }
        frame->local = local;
        loc_copy (&local->loc, loc);

        strcpy (local->origpath, loc->path);
        strcpy (local->newpath, priv->trash_dir);
        strcat (local->newpath, loc->path);

        {
                /* append timestamp to file name */
                /* TODO: can we make it optional? */
                utime = time (NULL);
                tm    = localtime (&utime);
                strftime (timestr, 256, ".%Y-%m-%d-%H%M%S", tm);
                strcat (local->newpath, timestr);
        }

        LOCK_INIT (&frame->lock);

        STACK_WIND (frame, trash_unlink_stat_cbk,
                    this->children->xlator,
                    this->children->xlator->fops->stat, loc);

        return 0;
}

/**
 * trash_init -
 */
int32_t
init (xlator_t *this)
{
        int32_t                ret   = 0;
        data_t                *data  = NULL;
        trash_private_t       *_priv = NULL;
        trash_elim_pattern_t  *trav  = NULL;
        char                  *tmp_str = NULL;
        char                  *strtokptr = NULL;
        char                  *component = NULL;
        char                   trash_dir[PATH_MAX] = {0,};

        /* Create .trashcan directory in init */
        if (!this->children || this->children->next) {
                gf_log (this->name, GF_LOG_ERROR,
                        "not configured with exactly one child. exiting");
                return -1;
        }

        if (!this->parents) {
                gf_log (this->name, GF_LOG_WARNING,
                        "dangling volume. check volfile ");
        }

        _priv = CALLOC (1, sizeof (*_priv));
        if (!_priv) {
                gf_log (this->name, GF_LOG_ERROR, "out of memory");
                return -1;
        }

        data = dict_get (this->options, "trash-dir");
        if (!data) {
                gf_log (this->name, GF_LOG_NORMAL,
                        "no option specified for 'trash-dir', "
                        "using \"/.trashcan/\"");
                _priv->trash_dir = strdup ("/.trashcan");
        } else {
                /* Need a path with '/' as the first char, if not
                   given, append it */
                if (data->data[0] == '/') {
                        _priv->trash_dir = strdup (data->data);
                } else {
                        /* TODO: Make sure there is no ".." in the path */
                        strcpy (trash_dir, "/");
                        strcat (trash_dir, data->data);
                        _priv->trash_dir = strdup (trash_dir);
                }
        }

        data = dict_get (this->options, "eliminate-pattern");
        if (!data) {
                gf_log (this->name, GF_LOG_TRACE,
                        "no option specified for 'eliminate', using NULL");
        } else {
                tmp_str = strdup (data->data);
                if (!tmp_str) {
                        gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                }

                /*  Match Filename to option specified in eliminate. */
                component = strtok_r (tmp_str, "|", &strtokptr);
                while (component) {
                        trav = CALLOC (1, sizeof (*trav));
                        if (!trav) {
                                gf_log (this->name, GF_LOG_DEBUG, "out of memory");
                        }
                        trav->pattern = component;
                        trav->next = _priv->eliminate;
                        _priv->eliminate = trav;

                        component = strtok_r (NULL, "|", &strtokptr);
                }
        }

        /* TODO: do gf_string2sizet () */
        data = dict_get (this->options, "max-trashable-file-size");
        if (!data) {
                gf_log (this->name, GF_LOG_DEBUG,
                        "no option specified for 'max-trashable-file-size', "
                        "using default = %lld MB",
                        GF_DEFAULT_MAX_FILE_SIZE / GF_UNIT_MB);
                _priv->max_trash_file_size = GF_DEFAULT_MAX_FILE_SIZE;
        } else {
                ret = gf_string2bytesize (data->data,
                                          &_priv->max_trash_file_size);
                if( _priv->max_trash_file_size > GF_ALLOWED_MAX_FILE_SIZE ) {
                        gf_log (this->name, GF_LOG_DEBUG,
                                "Size specified for max-size(in MB) is too "
                                "large so using 1GB as max-size (NOT IDEAL)");
                        _priv->max_trash_file_size = GF_ALLOWED_MAX_FILE_SIZE;
                }
                gf_log (this->name, GF_LOG_DEBUG, "%"GF_PRI_SIZET" max-size",
                        _priv->max_trash_file_size);
        }

        this->private = (void *)_priv;
        return 0;
}

void
fini (xlator_t *this)
{
        trash_private_t *priv = NULL;

        priv = this->private;
        if (priv)
                FREE (priv);

        return;
}

struct xlator_fops fops = {
        .unlink = trash_unlink,
        .rename = trash_rename,
};

struct xlator_mops mops = {
};

struct xlator_cbks cbks = {
};

struct volume_options options[] = {
        { .key  = { "trash-directory" },
          .type = GF_OPTION_TYPE_PATH,
        },
        { .key  = { "eliminate-pattern" },
          .type = GF_OPTION_TYPE_STR,
        },
        { .key  = { "max-trashable-file-size" },
          .type = GF_OPTION_TYPE_SIZET,
        },
        { .key  = {NULL} },
};