flat_con.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <string.h>
00026 #include <errno.h>
00027 #include "../../mem/mem.h"
00028 #include "../../dprint.h"
00029 #include "../../ut.h"
00030 #include "flatstore_mod.h"
00031 #include "flat_con.h"
00032
00033 #define FILE_SUFFIX ".log"
00034 #define FILE_SUFFIX_LEN (sizeof(FILE_SUFFIX) - 1)
00035
00036
00037 static char* get_name(struct flat_id* id)
00038 {
00039 char* buf;
00040 int buf_len;
00041 char* num, *ptr;
00042 int num_len;
00043 int total_len;
00044
00045 buf_len=pathmax();
00046 if (!id) {
00047 LM_ERR("invalid parameter value\n");
00048 return 0;
00049 }
00050 total_len=id->dir.len+1 +id->table.len+1 +
00051 FILE_SUFFIX_LEN+1 ;
00052 if (buf_len<total_len){
00053 LM_ERR("the path is too long (%d and PATHMAX is %d)\n",
00054 total_len, buf_len);
00055 return 0;
00056 }
00057
00058 buf=pkg_malloc(buf_len);
00059 if (buf==0){
00060 LM_ERR("pkg memory allocation failure\n");
00061 return 0;
00062 }
00063
00064 ptr = buf;
00065
00066 memcpy(ptr, id->dir.s, id->dir.len);
00067 ptr += id->dir.len;
00068 *ptr++ = '/';
00069
00070 memcpy(ptr, id->table.s, id->table.len);
00071 ptr += id->table.len;
00072
00073 *ptr++ = '_';
00074
00075 num = int2str(flat_pid, &num_len);
00076 if (buf_len<(total_len+num_len)){
00077 LM_ERR("the path is too long (%d and PATHMAX is"
00078 " %d)\n", total_len+num_len, buf_len);
00079 pkg_free(buf);
00080 return 0;
00081 }
00082 memcpy(ptr, num, num_len);
00083 ptr += num_len;
00084
00085 memcpy(ptr, FILE_SUFFIX, FILE_SUFFIX_LEN);
00086 ptr += FILE_SUFFIX_LEN;
00087
00088 *ptr = '\0';
00089 return buf;
00090 }
00091
00092
00093 struct flat_con* flat_new_connection(struct flat_id* id)
00094 {
00095 char* fn;
00096
00097 struct flat_con* res;
00098
00099 if (!id) {
00100 LM_ERR("invalid parameter value\n");
00101 return 0;
00102 }
00103
00104 res = (struct flat_con*)pkg_malloc(sizeof(struct flat_con));
00105 if (!res) {
00106 LM_ERR("no pkg memory left\n");
00107 return 0;
00108 }
00109
00110 memset(res, 0, sizeof(struct flat_con));
00111 res->ref = 1;
00112
00113 res->id = id;
00114
00115 fn = get_name(id);
00116 if (fn==0){
00117 LM_ERR("get_name() failed\n");
00118 return 0;
00119 }
00120
00121 res->file = fopen(fn, "a");
00122 pkg_free(fn);
00123 if (!res->file) {
00124 LM_ERR(" %s\n", strerror(errno));
00125 pkg_free(res);
00126 return 0;
00127 }
00128
00129 return res;
00130 }
00131
00132
00133
00134
00135
00136 void flat_free_connection(struct flat_con* con)
00137 {
00138 if (!con) return;
00139 if (con->id) free_flat_id(con->id);
00140 if (con->file) {
00141 fclose(con->file);
00142 }
00143 pkg_free(con);
00144 }
00145
00146
00147
00148
00149
00150 int flat_reopen_connection(struct flat_con* con)
00151 {
00152 char* fn;
00153
00154 if (!con) {
00155 LM_ERR("invalid parameter value\n");
00156 return -1;
00157 }
00158
00159 if (con->file) {
00160 fclose(con->file);
00161 con->file = 0;
00162
00163 fn = get_name(con->id);
00164 if (fn == 0) {
00165 LM_ERR("failed to get_name\n");
00166 return -1;
00167 }
00168
00169 con->file = fopen(fn, "a");
00170 pkg_free(fn);
00171
00172 if (!con->file) {
00173 LM_ERR("invalid parameter value\n");
00174 return -1;
00175 }
00176 }
00177
00178 return 0;
00179 }