Debian Codegraph @

xlators/playground/rot-13/src/rot-13.c

raw symbols SCIP
1 /*
2 Copyright (c) 2006-2012 Red Hat, Inc. <http://www.redhat.com>
3 This file is part of GlusterFS.
4
5 This file is licensed to you under your choice of the GNU Lesser
6 General Public License, version 3 or any later version (LGPLv3 or
7 later), or the GNU General Public License, version 2 (GPLv2), in all
8 cases as published by the Free Software Foundation.
9 */
10 #include <ctype.h>
11 #include <sys/uio.h>
12
13 #include <glusterfs/glusterfs.h>
14 #include <glusterfs/xlator.h>
15 #include <glusterfs/logging.h>
16
17 #include "rot-13.h"
18
19 /*
20 * This is a rot13 ``encryption'' xlator. It rot13's data when
21 * writing to disk and rot13's it back when reading it.
22 * This xlator is meant as an example, NOT FOR PRODUCTION
23 * USE ;) (hence no error-checking)
24 */
25
26 void
27 rot13(char *buf, int len)
28 {
29 int i;
30 for (i = 0; i < len; i++) {
31 if (buf[i] >= 'a' && buf[i] <= 'z')
32 buf[i] = 'a' + ((buf[i] - 'a' + 13) % 26);
33 else if (buf[i] >= 'A' && buf[i] <= 'Z')
34 buf[i] = 'A' + ((buf[i] - 'A' + 13) % 26);
35 }
36 }
37
38 void
39 rot13_iovec(struct iovec *vector, int count)
40 {
41 int i;
42 for (i = 0; i < count; i++) {
43 rot13(vector[i].iov_base, vector[i].iov_len);
44 }
45 }
46
47 int32_t
48 rot13_readv_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
49 int32_t op_ret, int32_t op_errno, struct iovec *vector,
50 int32_t count, struct iatt *stbuf, struct iobref *iobref,
51 dict_t *xdata)
52 {
53 rot_13_private_t *priv = (rot_13_private_t *)this->private;
54
55 if (priv->decrypt_read)
56 rot13_iovec(vector, count);
57
58 STACK_UNWIND_STRICT(readv, frame, op_ret, op_errno, vector, count, stbuf,
59 iobref, xdata);
60 return 0;
61 }
62
63 int32_t
64 rot13_readv(call_frame_t *frame, xlator_t *this, fd_t *fd, size_t size,
65 off_t offset, uint32_t flags, dict_t *xdata)
66 {
67 STACK_WIND(frame, rot13_readv_cbk, FIRST_CHILD(this),
68 FIRST_CHILD(this)->fops->readv, fd, size, offset, flags, xdata);
69 return 0;
70 }
71
72 int32_t
73 rot13_writev_cbk(call_frame_t *frame, void *cookie, xlator_t *this,
74 int32_t op_ret, int32_t op_errno, struct iatt *prebuf,
75 struct iatt *postbuf, dict_t *xdata)
76 {
77 STACK_UNWIND_STRICT(writev, frame, op_ret, op_errno, prebuf, postbuf,
78 xdata);
79 return 0;
80 }
81
82 int32_t
83 rot13_writev(call_frame_t *frame, xlator_t *this, fd_t *fd,
84 struct iovec *vector, int32_t count, off_t offset, uint32_t flags,
85 struct iobref *iobref, dict_t *xdata)
86 {
87 rot_13_private_t *priv = (rot_13_private_t *)this->private;
88 if (priv->encrypt_write)
89 rot13_iovec(vector, count);
90
91 STACK_WIND(frame, rot13_writev_cbk, FIRST_CHILD(this),
92 FIRST_CHILD(this)->fops->writev, fd, vector, count, offset,
93 flags, iobref, xdata);
94 return 0;
95 }
96
97 int32_t
98 init(xlator_t *this)
99 {
100 data_t *data = NULL;
101 rot_13_private_t *priv = NULL;
102
103 if (!this->children || this->children->next) {
104 gf_log("rot13", GF_LOG_ERROR,
105 "FATAL: rot13 should have exactly one child");
106 return -1;
107 }
108
109 if (!this->parents) {
110 gf_log(this->name, GF_LOG_WARNING, "dangling volume. check volfile ");
111 }
112
113 priv = GF_CALLOC(sizeof(rot_13_private_t), 1, 0);
114 if (!priv)
115 return -1;
116
117 priv->decrypt_read = 1;
118 priv->encrypt_write = 1;
119
120 data = dict_get(this->options, "encrypt-write");
121 if (data) {
122 if (gf_string2boolean(data->data, &priv->encrypt_write) == -1) {
123 gf_log(this->name, GF_LOG_ERROR,
124 "encrypt-write takes only boolean options");
125 GF_FREE(priv);
126 return -1;
127 }
128 }
129
130 data = dict_get(this->options, "decrypt-read");
131 if (data) {
132 if (gf_string2boolean(data->data, &priv->decrypt_read) == -1) {
133 gf_log(this->name, GF_LOG_ERROR,
134 "decrypt-read takes only boolean options");
135 GF_FREE(priv);
136 return -1;
137 }
138 }
139
140 this->private = priv;
141 gf_log("rot13", GF_LOG_DEBUG, "rot13 xlator loaded");
142 return 0;
143 }
144
145 void
146 fini(xlator_t *this)
147 {
148 rot_13_private_t *priv = this->private;
149
150 if (!priv)
151 return;
152 this->private = NULL;
153 GF_FREE(priv);
154
155 return;
156 }
157
158 struct xlator_fops fops = {.readv = rot13_readv, .writev = rot13_writev};
159
160 struct xlator_cbks cbks;
161
162 struct volume_options options[] = {
163 {.key = {"encrypt-write"}, .type = GF_OPTION_TYPE_BOOL},
164 {.key = {"decrypt-read"}, .type = GF_OPTION_TYPE_BOOL},
165 {.key = {NULL}},
166 };