Commit 02872e4995d6a8782088b2cf7fa5adbfee7de50e

Authored by Georg Hopp
1 parent 9230978b

add first user class and persistence experiments. refs #36

... ... @@ -65,5 +65,7 @@ AC_CONFIG_FILES([Makefile
65 65 src/stream/Makefile
66 66 src/tree/Makefile
67 67 src/application/Makefile
  68 + src/storage/Makefile
  69 + src/user/Makefile
68 70 tests/Makefile])
69 71 AC_OUTPUT
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#ifndef __STORAGE_H__
  24 +#define __STORAGE_H__
  25 +
  26 +#include <gdbm.h>
  27 +#include <sys/types.h>
  28 +
  29 +#include "class.h"
  30 +
  31 +
  32 +CLASS(Storage) {
  33 + GDBM_FILE gdbm;
  34 + char * db_name;
  35 +};
  36 +
  37 +void storagePut(Storage, char *, size_t, char *, size_t);
  38 +void storageGet(Storage, char *, size_t, char **, size_t *);
  39 +
  40 +#endif // __STORAGE_H__
  41 +
  42 +// vim: set ts=4 sw=4:
  43 +
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#ifndef __USER_H__
  24 +#define __USER_H__
  25 +
  26 +#include <sys/types.h>
  27 +
  28 +#include "class.h"
  29 +#include "storage.h"
  30 +
  31 +
  32 +CLASS(User) {
  33 + char * email;
  34 + char * firstname;
  35 + char * surname;
  36 +
  37 + size_t * nemail;
  38 + size_t * nfirstname;
  39 + size_t * nsurname;
  40 +};
  41 +
  42 +User userLoad(User, Storage);
  43 +void userSave(User, Storage);
  44 +
  45 +// void userSerialize(User, void **, size_t *);
  46 +// void userDeserialize(User, void *, size_t);
  47 +
  48 +#endif // __USER_H__
  49 +
  50 +// vim: set ts=4 sw=4:
  51 +
... ...
... ... @@ -12,6 +12,8 @@ UTILS = utils/hash.c \
12 12 utils/mime_type.c
13 13
14 14 LIBS = ./application/libapplication.a \
  15 + ./user/libuser.a \
  16 + ./storage/libstorage.a \
15 17 ./http/libhttp.a \
16 18 ./auth/libauth.a \
17 19 ./cbuf/libcbuf.a \
... ... @@ -32,8 +34,9 @@ bin_PROGRAMS = taskrambler
32 34
33 35 taskrambler_SOURCES = taskrambler.c $(IFACE) $(UTILS)
34 36 taskrambler_CFLAGS = $(CFLAGS) -Wall -DPWD=\"$(PWD)\" -I ../include/# $(COVERAGE_CFLAGS)
35   -taskrambler_LDADD = $(LIBS) -lrt -lssl -lldap
  37 +taskrambler_LDADD = $(LIBS) -lrt -lssl -lldap -lgdbm
36 38 #taskrambler_LDFLAGS = $(COVERAGE_LDFLAGS)
37 39
38 40 SUBDIRS = asset auth cbuf class hash queue http \
39   - logger server session socket stream tree application
  41 + logger server session socket stream tree application \
  42 + storage user
... ...
  1 +ACLOCAL_AMFLAGS = -I m4
  2 +AUTOMAKE_OPTIONS = subdir-objects
  3 +
  4 +noinst_LIBRARIES = libstorage.a
  5 +
  6 +libstorage_a_SOURCES = storage.c get.c put.c
  7 +libstorage_a_CFLAGS = $(CFLAGS) -Wall -I ../../include/
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <gdbm.h>
  24 +#include <string.h>
  25 +#include <stdlib.h>
  26 +#include <sys/types.h>
  27 +
  28 +#include "storage.h"
  29 +#include "class.h"
  30 +
  31 +#include "utils/memory.h"
  32 +
  33 +void
  34 +storageGet(
  35 + Storage this,
  36 + char * _key, size_t nkey,
  37 + char ** data, size_t * ndata)
  38 +{
  39 + datum key = {_key, nkey};
  40 + datum value;
  41 +
  42 + value = gdbm_fetch(this->gdbm, key);
  43 +
  44 + if (NULL != value.dptr) {
  45 + *ndata = value.dsize;
  46 + *data = memMalloc(value.dsize);
  47 + memcpy(*data, value.dptr, value.dsize);
  48 +
  49 + free(value.dptr);
  50 + }
  51 +}
  52 +
  53 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <gdbm.h>
  24 +#include <string.h>
  25 +#include <sys/types.h>
  26 +
  27 +#include "storage.h"
  28 +#include "class.h"
  29 +
  30 +#include "utils/memory.h"
  31 +
  32 +void
  33 +storagePut(Storage this, char * _key, size_t nkey, char * data, size_t ndata)
  34 +{
  35 + datum key = {_key, nkey};
  36 + datum value = {data, ndata};
  37 +
  38 + gdbm_store(this->gdbm, key, value, GDBM_REPLACE);
  39 +}
  40 +
  41 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <gdbm.h>
  24 +#include <string.h>
  25 +#include <sys/stat.h>
  26 +
  27 +#include "storage.h"
  28 +#include "class.h"
  29 +
  30 +#include "utils/memory.h"
  31 +
  32 +static
  33 +int
  34 +storageCtor(void * _this, va_list * params)
  35 +{
  36 + Storage this = _this;
  37 + char * db_name = va_arg(* params, void *);
  38 +
  39 + this->db_name = memMalloc(strlen(db_name) + 1);
  40 + strcpy(this->db_name, db_name);
  41 +
  42 + this->gdbm = gdbm_open(
  43 + this->db_name,
  44 + 0,
  45 + GDBM_WRCREAT,
  46 + S_IRUSR | S_IWUSR,
  47 + NULL);
  48 +
  49 + if (NULL == this->gdbm) {
  50 + return -1;
  51 + }
  52 +
  53 + return 0;
  54 +}
  55 +
  56 +static
  57 +void
  58 +storageDtor(void * _this)
  59 +{
  60 + Storage this = _this;
  61 +
  62 + if (NULL != this->db_name) MEM_FREE(this->db_name);
  63 + if (NULL != this->gdbm) gdbm_close(this->gdbm);
  64 +}
  65 +
  66 +INIT_IFACE(Class, storageCtor, storageDtor, NULL);
  67 +CREATE_CLASS(Storage, NULL, IFACE(Class));
  68 +
  69 +// vim: set ts=4 sw=4:
... ...
  1 +ACLOCAL_AMFLAGS = -I m4
  2 +AUTOMAKE_OPTIONS = subdir-objects
  3 +
  4 +noinst_LIBRARIES = libuser.a
  5 +
  6 +libuser_a_SOURCES = user.c load.c save.c
  7 +libuser_a_CFLAGS = $(CFLAGS) -Wall -I ../../include/
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <sys/types.h>
  24 +#include <string.h>
  25 +
  26 +#include "user.h"
  27 +#include "storage.h"
  28 +#include "class.h"
  29 +
  30 +#include "utils/memory.h"
  31 +
  32 +
  33 +User
  34 +userLoad(User this, Storage storage)
  35 +{
  36 + char * storage_data;
  37 + size_t nstorage_data;
  38 +
  39 + size_t * user_data_sizes;
  40 +
  41 + storageGet(
  42 + storage,
  43 + this->email, *this->nemail,
  44 + &storage_data, &nstorage_data);
  45 +
  46 + if (NULL == storage_data) {
  47 + return NULL;
  48 + }
  49 +
  50 + user_data_sizes =
  51 + (size_t *)(storage_data + nstorage_data - 3 * sizeof(size_t));
  52 +
  53 + this->nemail = user_data_sizes;
  54 + this->nfirstname = user_data_sizes + 1;
  55 + this->nsurname = user_data_sizes + 2;
  56 +
  57 + this->email = storage_data;
  58 + this->firstname = this->email + *this->nemail + 1;
  59 + this->surname = this->firstname + *this->nfirstname + 1;
  60 +
  61 + return this;
  62 +}
  63 +
  64 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include <sys/types.h>
  24 +#include <string.h>
  25 +
  26 +#include "user.h"
  27 +#include "storage.h"
  28 +#include "class.h"
  29 +
  30 +#include "utils/memory.h"
  31 +
  32 +
  33 +void
  34 +userSave(User this, Storage storage)
  35 +{
  36 + size_t storage_size =
  37 + *this->nemail + 1 +
  38 + *this->nfirstname + 1 +
  39 + *this->nsurname + 1 +
  40 + 3 * sizeof(size_t);
  41 +
  42 + storagePut(
  43 + storage,
  44 + this->email, *this->nemail,
  45 + this->email, storage_size);
  46 +}
  47 +
  48 +// vim: set ts=4 sw=4:
... ...
  1 +/**
  2 + * \file
  3 + *
  4 + * \author Georg Hopp
  5 + *
  6 + * \copyright
  7 + * Copyright © 2012 Georg Hopp
  8 + *
  9 + * This program is free software: you can redistribute it and/or modify
  10 + * it under the terms of the GNU General Public License as published by
  11 + * the Free Software Foundation, either version 3 of the License, or
  12 + * (at your option) any later version.
  13 + *
  14 + * This program is distributed in the hope that it will be useful,
  15 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17 + * GNU General Public License for more details.
  18 + *
  19 + * You should have received a copy of the GNU General Public License
  20 + * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21 + */
  22 +
  23 +#include "user.h"
  24 +#include "storage.h"
  25 +#include "class.h"
  26 +
  27 +#include "utils/memory.h"
  28 +
  29 +
  30 +static
  31 +int
  32 +userCtor(void * _this, va_list * params)
  33 +{
  34 + User this = _this;
  35 +
  36 + char * email = va_arg(* params, char *);
  37 +
  38 + if (NULL != email) {
  39 + size_t nemail = va_arg(* params, size_t);
  40 + char * firstname = va_arg(* params, char *);
  41 + size_t nfirstname = va_arg(* params, size_t);
  42 + char * surname = va_arg(* params, char *);
  43 + size_t nsurname = va_arg(* params, size_t);
  44 +
  45 + size_t storage_size =
  46 + nemail + 1 +
  47 + nfirstname + 1 +
  48 + nsurname + 1 +
  49 + 3 * sizeof(size_t);
  50 +
  51 + this->email = memMalloc(storage_size);
  52 + memcpy(this->email, email, nemail);
  53 + this->email[nemail] = '\0';
  54 +
  55 + this->firstname = this->email + nemail + 1;
  56 + memcpy(this->firstname, firstname, nfirstname);
  57 + this->firstname[nfirstname] = '\0';
  58 +
  59 + this->surname = this->firstname + nfirstname + 1;
  60 + memcpy(this->surname, surname, nsurname);
  61 + this->surname[nsurname] = '\0';
  62 +
  63 + this->nemail = (size_t *)(this->surname + nsurname + 1);
  64 + *this->nemail = nemail;
  65 +
  66 + this->nfirstname = this->nemail + 1;
  67 + *this->nfirstname = nfirstname;
  68 +
  69 + this->nsurname = this->nfirstname + 1;
  70 + *this->nsurname = nsurname;
  71 + }
  72 +
  73 + return 0;
  74 +}
  75 +
  76 +static
  77 +void
  78 +userDtor(void * _this)
  79 +{
  80 + User this = _this;
  81 +
  82 + if (NULL != this->email) {
  83 + MEM_FREE(this->email);
  84 + }
  85 +}
  86 +
  87 +INIT_IFACE(Class, userCtor, userDtor, NULL);
  88 +CREATE_CLASS(User, NULL, IFACE(Class));
  89 +
  90 +// vim: set ts=4 sw=4:
... ...
  1 +#include <stdio.h>
  2 +#include <stdlib.h>
  3 +#include <gdbm.h>
  4 +#include <errno.h>
  5 +#include <string.h>
  6 +
  7 +#include "class.h"
  8 +#include "commons.h"
  9 +#include "user.h"
  10 +#include "storage.h"
  11 +
  12 +#include "utils/memory.h"
  13 +
  14 +int
  15 +main(int argc, char * argv[])
  16 +{
  17 + Storage users = new(Storage, CSTRA("./users.db"));
  18 + User user;
  19 + User georg;
  20 + //User steffi;
  21 + size_t nuser;
  22 +
  23 + if (NULL == users) {
  24 + fprintf(stderr, "%s\n", gdbm_strerror(gdbm_errno));
  25 + fprintf(stderr, "%s\n", strerror(errno));
  26 + return 1;
  27 + }
  28 +
  29 + georg = new(User,
  30 + CSTRA("georg@steffers.org"),
  31 + CSTRA("Georg"),
  32 + CSTRA("Hopp"));
  33 +
  34 + userSave(georg, users);
  35 + delete(georg);
  36 +
  37 + user = new(User, NULL);
  38 + user->email = "georg@steffers.org";
  39 + nuser = sizeof("georg@steffers.org") - 1;
  40 + user->nemail = &nuser;
  41 +
  42 + if (NULL == userLoad(user, users)) {
  43 + fprintf(stderr, "can't find user");
  44 + } else {
  45 + puts("found user:");
  46 + puts(user->email);
  47 + puts(user->firstname);
  48 + puts(user->surname);
  49 + }
  50 +
  51 + delete(user);
  52 + delete(users);
  53 +
  54 + memCleanup();
  55 +
  56 + return 0;
  57 +}
  58 +
  59 +// vim: set et ts=4 sw=4:
... ...
Please register or login to post a comment