2 Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2009
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
22 #include <sys/types.h>
23 #include <sys/socket.h>
25 #include <libsmbclient.h>
28 #define discard_const(ptr) ((void *)((intptr_t)(ptr)))
30 static char *smb_domain;
31 static char *smb_user;
32 static char *smb_password;
33 static char *smb_server;
34 static char *smb_share;
36 typedef struct _data_t {
41 typedef struct _smb_handle_t {
45 /* a tree to store all open handles, indexed by path */
46 typedef struct _tree_t {
49 struct _tree_t *parent;
51 struct _tree_t *right;
54 static tree_t *open_files;
57 static tree_t *find_path(tree_t *tree, const char *path)
65 i = strcmp(path, tree->path.dptr);
70 return find_path(tree->left, path);
73 return find_path(tree->right, path);
76 static smb_handle_t *lookup_path(const char *path)
80 t = find_path(open_files, path);
88 static void free_node(tree_t *t)
90 free(discard_const(t->path.dptr));
94 static void delete_path(const char *path)
98 t = find_path(open_files, path);
103 /* we have a left child */
107 for(tmp_tree=t->left;tmp_tree->right;tmp_tree=tmp_tree->right)
109 tmp_tree->right = t->right;
111 t->right->parent = tmp_tree;
114 if (t->parent == NULL) {
115 open_files = tmp_tree;
116 tmp_tree->parent = NULL;
121 if (t->parent->left == t) {
122 t->parent->left = t->left;
124 t->left->parent = t->parent;
130 t->parent->right = t->left;
132 t->left->parent = t->parent;
138 /* we only have a right child */
142 for(tmp_tree=t->right;tmp_tree->left;tmp_tree=tmp_tree->left)
144 tmp_tree->left = t->left;
146 t->left->parent = tmp_tree;
149 if (t->parent == NULL) {
150 open_files = tmp_tree;
151 tmp_tree->parent = NULL;
156 if (t->parent->left == t) {
157 t->parent->left = t->right;
159 t->right->parent = t->parent;
165 t->parent->right = t->right;
167 t->right->parent = t->parent;
173 /* we are a leaf node */
174 if (t->parent == NULL) {
177 if (t->parent->left == t) {
178 t->parent->left = NULL;
180 t->parent->right = NULL;
187 static void insert_path(const char *path, smb_handle_t *hnd)
193 tmp_t = find_path(open_files, path);
198 t = malloc(sizeof(tree_t));
200 fprintf(stderr, "MALLOC failed to allocate tree_t in insert_fhandle\n");
204 t->path.dptr = strdup(path);
205 if (t->path.dptr == NULL) {
206 fprintf(stderr, "STRDUP failed to allocate key in insert_fhandle\n");
209 t->path.dsize = strlen(path);
217 if (open_files == NULL) {
224 i = strcmp(t->path.dptr, tmp_t->path.dptr);
226 tmp_t->handle = t->handle;
227 free(discard_const(t->path.dptr));
232 if (tmp_t->left == NULL) {
240 if (tmp_t->right == NULL) {
245 tmp_t = tmp_t->right;
257 static void failed(struct child_struct *child)
260 fprintf(stderr, "ERROR: child %d failed at line %d\n", child->id, child->line);
264 static int check_status(int ret, const char *status)
266 if (!strcmp(status, "*")) {
270 if ((!strcmp(status, "SUCCESS")) && (ret == 0)) {
274 if ((!strcmp(status, "ERROR")) && (ret != 0)) {
282 void smb_auth_fn(const char *server, const char *share, char *wrkgrp, int wrkgrplen, char *user, int userlen, char *passwd, int passwdlen)
287 if (smb_domain != NULL) {
288 strncpy(wrkgrp, smb_domain, wrkgrplen - 1); wrkgrp[wrkgrplen - 1] = 0;
290 strncpy(user, smb_user, userlen - 1); user[userlen - 1] = 0;
291 strncpy(passwd, smb_password, passwdlen - 1); passwd[passwdlen - 1] = 0;
294 static int smb_init(void)
301 if (options.smb_share == NULL) {
302 fprintf(stderr, "You must specify --smb-share=<share> with the \"smb\" backend.\n");
305 if (options.smb_share[0] != '/' || options.smb_share[1] != '/') {
306 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
309 smb_server = strdup(options.smb_share+2);
310 tmp = index(smb_server, '/');
312 fprintf(stderr, "--smb-share Must be of the form //SERVER/SHARE[/PATH]\n");
319 if (options.smb_user == NULL) {
320 fprintf(stderr, "You must specify --smb-user=[<domain>/]<user>%%<password> with the \"smb\" backend.\n");
324 smb_domain = strdup(options.smb_user);
325 tmp = index(smb_domain, '/');
327 smb_user = smb_domain;
333 tmp = index(smb_user, '%');
337 smb_password = tmp+1;
341 ctx = smbc_new_context();
343 fprintf(stderr, "Could not allocate SMB Context\n");
347 smbc_setDebug(ctx, 0);
348 smbc_setFunctionAuthData(ctx, smb_auth_fn);
350 if (!smbc_init_context(ctx)) {
351 smbc_free_context(ctx, 0);
352 fprintf(stderr, "failed to initialize context\n");
355 smbc_set_context(ctx);
357 asprintf(&str, "smb://%s/%s", smb_server, smb_share);
358 ret = smbc_opendir(str);
362 fprintf(stderr, "Failed to access //%s/%s\n", smb_server, smb_share);
366 smbc_free_context(ctx, 1);
370 static void smb_setup(struct child_struct *child)
372 struct smb_child *ctx;
374 ctx = malloc(sizeof(struct smb_child));
376 fprintf(stderr, "Failed to malloc child ctx\n");
381 ctx->ctx = smbc_new_context();
382 if (ctx->ctx == NULL) {
383 fprintf(stderr, "Could not allocate SMB Context\n");
387 smbc_setDebug(ctx->ctx, 0);
388 smbc_setFunctionAuthData(ctx->ctx, smb_auth_fn);
390 if (!smbc_init_context(ctx->ctx)) {
391 smbc_free_context(ctx->ctx, 0);
392 fprintf(stderr, "failed to initialize context\n");
395 smbc_setOptionUrlEncodeReaddirEntries(ctx->ctx, True);
396 smbc_set_context(ctx->ctx);
399 static void smb_mkdir(struct dbench_op *op)
407 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
409 ret = smbc_mkdir(str, 0777);
412 if (check_status(ret, op->status)) {
413 fprintf(stderr, "[%d] MKDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
418 static void smb_rmdir(struct dbench_op *op)
425 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, dir);
426 ret = smbc_rmdir(str);
429 if (check_status(ret, op->status)) {
430 fprintf(stderr, "[%d] RMDIR \"%s\" failed - expected %s, got %d\n", op->child->line, dir, op->status, ret);
435 static void smb_open(struct dbench_op *op)
442 if (op->params[0] & 0x01) {
445 if (op->params[0] & 0x02) {
448 if (op->params[0] & 0x04) {
451 if (op->params[0] & 0x08) {
454 if (op->params[0] & 0x10) {
457 if (op->params[0] & 0x20) {
460 if (op->params[0] & 0x40) {
464 file = op->fname + 2;
465 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, file);
467 hnd.fd = smbc_open(str, flags, 0777);
470 if (check_status(hnd.fd<0?-1:0, op->status)) {
471 fprintf(stderr, "[%d] OPEN \"%s\" failed\n", op->child->line, file);
476 insert_path(file, &hnd);
480 static void smb_close(struct dbench_op *op)
486 file = op->fname + 2;
488 hnd = lookup_path(file);
490 fprintf(stderr, "[%d] CLOSE \"%s\" failed. This file is not open.\n", op->child->line, file);
495 ret = smbc_close(hnd->fd);
498 if (check_status(ret, op->status)) {
499 fprintf(stderr, "[%d] CLOSE \"%s\" failed\n", op->child->line, file);
505 static void smb_write(struct dbench_op *op)
514 offset = op->params[0];
515 length = op->params[1];
516 if (length > 65536) {
520 file = op->fname + 2;
522 hnd = lookup_path(file);
524 fprintf(stderr, "[%d] WRITE \"%s\" failed. This file is not open.\n", op->child->line, file);
530 smbc_lseek(hnd->fd, offset, SEEK_SET);
531 ret = smbc_write(hnd->fd, garbage, length);
533 if (check_status(ret==(int)length?0:-1, op->status)) {
534 fprintf(stderr, "[%d] WRITE \"%s\" failed\n", op->child->line, file);
538 op->child->bytes += length;
541 static void smb_read(struct dbench_op *op)
550 offset = op->params[0];
551 length = op->params[1];
552 if (length > 65536) {
556 file = op->fname + 2;
558 hnd = lookup_path(file);
560 fprintf(stderr, "[%d] READ \"%s\" failed. This file is not open.\n", op->child->line, file);
566 smbc_lseek(hnd->fd, offset, SEEK_SET);
567 ret = smbc_read(hnd->fd, garbage, length);
569 if (check_status(ret==(int)length?0:-1, op->status)) {
570 fprintf(stderr, "[%d] READ \"%s\" failed\n", op->child->line, file);
574 op->child->bytes += length;
578 static void smb_unlink(struct dbench_op *op)
584 path = op->fname + 2;
585 asprintf(&str, "smb://%s/%s/%s", smb_server, smb_share, path);
587 ret = smbc_unlink(str);
590 if (check_status(ret, op->status)) {
591 fprintf(stderr, "[%d] UNLINK \"%s\" failed\n", op->child->line, path);
597 static void recursive_delete_tree(struct dbench_op *op, const char *url)
600 struct smbc_dirent *dirent;
602 dir = smbc_opendir(url);
604 fprintf(stderr, "[%d] Deltree \"%s\" failed\n", op->child->line, url);
607 while((dirent = smbc_readdir(dir))) {
610 asprintf(&path, "%s/%s", url, dirent->name);
611 if (!strcmp(dirent->name, ".")) {
614 if (!strcmp(dirent->name, "..")) {
617 if (dirent->smbc_type == SMBC_DIR) {
618 recursive_delete_tree(op, path);
630 static void smb_readdir(struct dbench_op *op)
636 path = op->fname + 2;
637 asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
639 dir = smbc_opendir(url);
642 fprintf(stderr, "[%d] READDIR \"%s\" failed\n", op->child->line, url);
648 static void smb_deltree(struct dbench_op *op)
653 path = op->fname + 2;
654 asprintf(&url, "smb://%s/%s/%s", smb_server, smb_share, path);
655 recursive_delete_tree(op, url);
659 static void smb_cleanup(struct child_struct *child)
661 struct smb_child *ctx = child->private;
663 struct dbench_op fake_op;
665 asprintf(&url, "smb://%s/%s", smb_server, smb_share);
666 recursive_delete_tree(&fake_op, url);
669 smbc_free_context(ctx->ctx, 1);
675 static struct backend_op ops[] = {
676 { "Deltree", smb_deltree },
677 { "CLOSE", smb_close },
678 { "MKDIR", smb_mkdir },
679 { "OPEN", smb_open },
680 { "READ", smb_read },
681 { "READDIR", smb_readdir },
682 { "RMDIR", smb_rmdir },
683 { "UNLINK", smb_unlink },
684 { "WRITE", smb_write },
688 struct nb_operations smb_ops = {
689 .backend_name = "smbbench",
692 .cleanup = smb_cleanup,