flat_uri.c

Go to the documentation of this file.
00001 /*
00002  * $Id$
00003  *
00004  * Copyright (C) 2008 iptelorg GmbH
00005  * Written by Jan Janak <jan@iptel.org>
00006  *
00007  * This file is part of SER, a free SIP server.
00008  *
00009  * SER is free software; you can redistribute it and/or modify it under the
00010  * terms of the GNU General Public License as published by the Free Software
00011  * Foundation; either version 2 of the License, or (at your option) any later
00012  * version.
00013  *
00014  * SER is distributed in the hope that it will be useful, but WITHOUT ANY
00015  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00016  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
00017  * details.
00018  *
00019  * You should have received a copy of the GNU General Public License along
00020  * with this program; if not, write to the Free Software Foundation, Inc., 
00021  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022  */
00023 
00032 #include "flat_uri.h"
00033 
00034 #include "../../mem/mem.h"
00035 #include "../../ut.h"
00036 
00037 #include <stdlib.h>
00038 #include <string.h>
00039 
00040 
00041 static void flat_uri_free(db_uri_t* uri, struct flat_uri* payload)
00042 {
00043         if (payload == NULL) return;
00044         if (payload->path.s) free(payload->path.s);
00045         db_drv_free(&payload->drv);
00046         pkg_free(payload);
00047 }
00048 
00049 
00050 int flat_uri(db_uri_t* uri)
00051 {
00052         struct flat_uri* furi;
00053 
00054         if ((furi = (struct flat_uri*)pkg_malloc(sizeof(*furi))) == NULL) {
00055                 ERR("flatstore: No memory left\n");
00056                 return -1;
00057         }
00058         memset(furi, '\0', sizeof(*furi));
00059         if (db_drv_init(&furi->drv, flat_uri_free) < 0) goto error;
00060 
00061         if ((furi->path.s = get_abs_pathname(NULL, &uri->body)) == NULL) {
00062                 ERR("flatstore: Error while obtaining absolute pathname for '%.*s'\n",
00063                         STR_FMT(&uri->body));
00064                 goto error;
00065         }
00066         furi->path.len = strlen(furi->path.s);
00067 
00068         DB_SET_PAYLOAD(uri, furi);
00069         return 0;
00070 
00071  error:
00072         if (furi) {
00073                 if (furi->path.s) pkg_free(furi->path.s);
00074                 db_drv_free(&furi->drv);
00075                 pkg_free(furi);
00076         }
00077         return -1;      
00078 }
00079