Definition in file bin.c.
#include <stdio.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include "bin.h"
Go to the source code of this file.
Defines | |
| #define | BIN_DEBUG 0 |
| Whether to print debug message while encoding/decoding. | |
| #define | BIN_DECODE_CHECKS 1 |
| Whether to do sanity checks on the available data when decoding If you are crazy about start-up performance you can disable this. | |
Functions | |
| int | bin_alloc (bin_data *x, int max_len) |
| int | bin_realloc (bin_data *x, int delta) |
| int | bin_expand (bin_data *x, int delta) |
| void | bin_free (bin_data *x) |
| void | bin_print (bin_data *x) |
| simple print function | |
| int | bin_encode_char (bin_data *x, char k) |
| Append a char of 1 byte. | |
| int | bin_decode_char (bin_data *x, char *c) |
| Decode of 1 char. | |
| int | bin_encode_uchar (bin_data *x, unsigned char k) |
| Append an unsigned char of 1 byte. | |
| int | bin_decode_uchar (bin_data *x, unsigned char *c) |
| Decode of 1 unsigned char. | |
| int | bin_encode_short (bin_data *x, short k) |
| Append the a short. | |
| int | bin_decode_short (bin_data *x, short *v) |
| Decode of a short. | |
| int | bin_encode_ushort (bin_data *x, unsigned short k) |
| Append the an unsigned short. | |
| int | bin_decode_ushort (bin_data *x, unsigned short *v) |
| Decode of a short. | |
| int | bin_encode_int (bin_data *x, int k) |
| Append an integer. | |
| int | bin_decode_int (bin_data *x, int *v) |
| Decode an integer. | |
| int | bin_encode_uint (bin_data *x, unsigned int k) |
| Append an unsigned integer. | |
| int | bin_decode_uint (bin_data *x, unsigned int *v) |
| Decode an unsigned integer. | |
| int | bin_encode_time_t (bin_data *x, time_t k) |
| Append a time_t structure. | |
| int | bin_decode_time_t (bin_data *x, time_t *v) |
| Decode an unsigned integer. | |
| int | bin_encode_str (bin_data *x, str *s) |
| Append a string. | |
| int | bin_decode_str (bin_data *x, str *s) |
| Decode of a str string. | |
| int | bin_encode_dlg_t (bin_data *x, dlg_t *d) |
| Encode a dlg_t into a binary form. | |
| int | bin_decode_dlg_t (bin_data *x, dlg_t **d) |
| Decode a dlg_t from a binary data structure. | |
| int | bin_dump_to_file (bin_data *x, char *location, char *prepend_fname) |
| Writes the binary data to a snapshot file. | |
| int | bin_load_from_file (bin_data *x, char *location, char *prepend_fname) |
| Reloads the snapshot from the link to the last time-stamped-file. | |
| int | bin_dump (bin_data *x, int mode, char *location, char *prepend_fname) |
| Dump a binary data to certain location. | |
| int | bin_load (bin_data *x, int mode, char *location, char *prepend_fname) |
| Reloads the bin_data from certain location. | |
Variables | |
| dlg_func_t | dialogb |
| Structure with pointers to dialog funcs. | |
| int | bin_files_keep_count = 3 |
| how many old snapshots to keep | |
| #define BIN_DEBUG 0 |
| #define BIN_DECODE_CHECKS 1 |
| int bin_alloc | ( | bin_data * | x, | |
| int | max_len | |||
| ) | [inline] |
Definition at line 79 of file bin.c.
References BIN_ALLOC_METHOD, _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00080 { 00081 x->s = (char*)BIN_ALLOC_METHOD(max_len); 00082 if (!x->s){ 00083 LOG(L_ERR,"ERR:"M_NAME":bin_alloc: Error allocating %d bytes.\n",max_len); 00084 x->len=0; 00085 x->max=0; 00086 return 0; 00087 } 00088 x->len=0; 00089 x->max=max_len; 00090 return 1; 00091 }
| int bin_realloc | ( | bin_data * | x, | |
| int | delta | |||
| ) | [inline] |
Definition at line 93 of file bin.c.
References BIN_REALLOC_METHOD, M_NAME, _bin_data::max, NULL, and _bin_data::s.
00094 { 00095 #if BIN_DEBUG 00096 LOG(L_INFO,"INFO:"M_NAME":bin_realloc: realloc %p from %d to + %d\n",x->s,x->max,delta); 00097 #endif 00098 x->s=BIN_REALLOC_METHOD(x->s,x->max + delta); 00099 if (x->s==NULL){ 00100 LOG(L_ERR,"ERR:"M_NAME":bin_realloc: No more memory to expand %d with %d \n",x->max,delta); 00101 return 0; 00102 } 00103 x->max += delta; 00104 return 1; 00105 }
| int bin_expand | ( | bin_data * | x, | |
| int | delta | |||
| ) | [inline] |
Definition at line 107 of file bin.c.
References BIN_REALLOC_METHOD, _bin_data::len, M_NAME, _bin_data::max, NULL, and _bin_data::s.
00108 { 00109 if (x->max-x->len>=delta) return 1; 00110 #if BIN_DEBUG 00111 LOG(L_INFO,"INFO:"M_NAME":bin_realloc: realloc %p from %d to + %d\n",x->s,x->max,delta); 00112 #endif 00113 x->s=BIN_REALLOC_METHOD(x->s,x->max + delta); 00114 if (x->s==NULL){ 00115 LOG(L_ERR,"ERR:"M_NAME":bin_realloc: No more memory to expand %d with %d \n",x->max,delta); 00116 return 0; 00117 } 00118 x->max += delta; 00119 return 1; 00120 }
| void bin_free | ( | bin_data * | x | ) | [inline] |
Definition at line 122 of file bin.c.
References BIN_FREE_METHOD, _bin_data::len, _bin_data::max, and _bin_data::s.
00123 { 00124 BIN_FREE_METHOD(x->s); 00125 x->s=0;x->len=0;x->max=0; 00126 }
| void bin_print | ( | bin_data * | x | ) | [inline] |
simple print function
Definition at line 131 of file bin.c.
References _bin_data::len, _bin_data::max, and _bin_data::s.
00132 { 00133 int i,j,w=16; 00134 char c; 00135 fprintf(stderr,"----------------------------------\nBinary form %d (max %d) bytes:\n",x->len,x->max); 00136 for(i=0;i<x->len;i+=w){ 00137 fprintf(stderr,"%04X> ",i); 00138 for(j=0;j<w;j++){ 00139 if (i+j<x->len) fprintf(stderr,"%02X ",(unsigned char)x->s[i+j]); 00140 else fprintf(stderr," "); 00141 } 00142 printf("\t"); 00143 for(j=0;j<w;j++)if (i+j<x->len){ 00144 if (x->s[i+j]>32) c=x->s[i+j]; 00145 else c = '.'; 00146 fprintf(stderr,"%c",c); 00147 }else fprintf(stderr," "); 00148 fprintf(stderr,"\n"); 00149 } 00150 fprintf(stderr,"\n---------------------------------\n"); 00151 }
| int bin_encode_char | ( | bin_data * | x, | |
| char | k | |||
| ) | [inline] |
Append a char of 1 byte.
Definition at line 167 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00168 { 00169 if (!bin_expand(x,1)) return 0; 00170 x->s[x->len++]= k; 00171 #if BIN_DEBUG 00172 LOG(L_INFO,"INFO:"M_NAME":bin_encode_char: [%d]:[%.02x] new len %04x\n",k,x->s[x->len-1],x->len); 00173 #endif 00174 return 1; 00175 }
| int bin_decode_char | ( | bin_data * | x, | |
| char * | c | |||
| ) | [inline] |
Decode of 1 char.
Definition at line 179 of file bin.c.
References _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00180 { 00181 #if BIN_DECODE_CHECKS 00182 if (x->max+1 > x->len) return 0; 00183 #endif 00184 *c = x->s[x->max]; 00185 x->max += 1; 00186 #if BIN_DEBUG 00187 LOG(L_INFO,"INFO:"M_NAME":bin_decode_char: [%d] new pos %04x\n",*c,x->max); 00188 #endif 00189 return 1; 00190 }
| int bin_encode_uchar | ( | bin_data * | x, | |
| unsigned char | k | |||
| ) | [inline] |
Append an unsigned char of 1 byte.
Definition at line 198 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00199 { 00200 if (!bin_expand(x,1)) return 0; 00201 x->s[x->len++]= k; 00202 #if BIN_DEBUG 00203 LOG(L_INFO,"INFO:"M_NAME":bin_encode_uchar: [%u]:[%.02x] new len %04x\n",k,x->s[x->len-1],x->len); 00204 #endif 00205 return 1; 00206 }
| int bin_decode_uchar | ( | bin_data * | x, | |
| unsigned char * | c | |||
| ) | [inline] |
Decode of 1 unsigned char.
Definition at line 210 of file bin.c.
References _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00211 { 00212 #if BIN_DECODE_CHECKS 00213 if (x->max+1 > x->len) return 0; 00214 #endif 00215 *c = x->s[x->max]; 00216 x->max += 1; 00217 #if BIN_DEBUG 00218 LOG(L_INFO,"INFO:"M_NAME":bin_decode_uchar: [%u] new pos %04x\n",*c,x->max); 00219 #endif 00220 return 1; 00221 }
| int bin_encode_short | ( | bin_data * | x, | |
| short | k | |||
| ) | [inline] |
Append the a short.
Definition at line 232 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00233 { 00234 if (!bin_expand(x,2)) return 0; 00235 x->s[x->len++]=k & 0x00FF; 00236 x->s[x->len++]=(k & 0xFF00) >> 8; 00237 #if BIN_DEBUG 00238 LOG(L_INFO,"INFO:"M_NAME":bin_encode_short: [%d]:[%.02x %.02x] new len %04x\n",k,x->s[x->len-2],x->s[x->len-1],x->len); 00239 #endif 00240 return 1; 00241 }
| int bin_decode_short | ( | bin_data * | x, | |
| short * | v | |||
| ) | [inline] |
Decode of a short.
Definition at line 245 of file bin.c.
References _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00246 { 00247 #if BIN_DECODE_CHECKS 00248 if (x->max+2 > x->len) return 0; 00249 #endif 00250 *v = (unsigned char)x->s[x->max ] | 00251 (unsigned char)x->s[x->max+1]<<8; 00252 x->max += 2; 00253 #if BIN_DEBUG 00254 LOG(L_INFO,"INFO:"M_NAME":bin_decode_short: [%d] new pos %04x\n",*v,x->max); 00255 #endif 00256 return 1; 00257 }
| int bin_encode_ushort | ( | bin_data * | x, | |
| unsigned short | k | |||
| ) | [inline] |
Append the an unsigned short.
Definition at line 263 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00264 { 00265 if (!bin_expand(x,2)) return 0; 00266 x->s[x->len++]=k & 0x00FF; 00267 x->s[x->len++]=(k & 0xFF00) >> 8; 00268 #if BIN_DEBUG 00269 LOG(L_INFO,"INFO:"M_NAME":bin_encode_ushort: [%u]:[%.02x %.02x] new len %04x\n",k,x->s[x->len-2],x->s[x->len-1],x->len); 00270 #endif 00271 return 1; 00272 }
| int bin_decode_ushort | ( | bin_data * | x, | |
| unsigned short * | v | |||
| ) | [inline] |
Decode of a short.
Definition at line 276 of file bin.c.
References _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00277 { 00278 #if BIN_DECODE_CHECKS 00279 if (x->max+2 > x->len) return 0; 00280 #endif 00281 *v = (unsigned char)x->s[x->max ] | 00282 (unsigned char)x->s[x->max+1]<<8; 00283 x->max += 2; 00284 #if BIN_DEBUG 00285 LOG(L_INFO,"INFO:"M_NAME":bin_decode_ushort: [%u] new pos %04x\n",*v,x->max); 00286 #endif 00287 return 1; 00288 }
| int bin_encode_int | ( | bin_data * | x, | |
| int | k | |||
| ) | [inline] |
Append an integer.
Definition at line 294 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00295 { 00296 int len = sizeof(int),i; 00297 if (!bin_expand(x,len)) return 0; 00298 for(i=0;i<len;i++){ 00299 x->s[x->len++]= k & 0xFF; 00300 k = k>>8; 00301 } 00302 #if BIN_DEBUG 00303 switch(len){ 00304 case 4: 00305 LOG(L_INFO,"INFO:"M_NAME":bin_encode_int: [%d]:[%.02x %.02x %.02x %.02x] new len %04x\n",k, 00306 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1],x->len); 00307 break; 00308 case 8: 00309 LOG(L_INFO,"INFO:"M_NAME":bin_encode_int: [%d]:[%.02x %.02x %.02x %.02x%.02x %.02x %.02x %.02x] new len %04x\n",k, 00310 x->s[x->len-8],x->s[x->len-7],x->s[x->len-6],x->s[x->len-5], 00311 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1], 00312 x->len); 00313 break; 00314 } 00315 #endif 00316 return 1; 00317 }
| int bin_decode_int | ( | bin_data * | x, | |
| int * | v | |||
| ) | [inline] |
Decode an integer.
Definition at line 321 of file bin.c.
References BIN_DEBUG, _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00322 { 00323 int len = sizeof(int),i; 00324 #if BIN_DECODE_CHECKS 00325 if (x->max+len > x->len) return 0; 00326 #endif 00327 *v = 0; 00328 for(i=0;i<len;i++) 00329 *v = *v | ((unsigned char)x->s[x->max++] <<(8*i)); 00330 #if BIN_DEBUG 00331 LOG(L_INFO,"INFO:"M_NAME":bin_decode_int: [%d] new pos %04x\n",*v,x->max); 00332 #endif 00333 return 1; 00334 }
| int bin_encode_uint | ( | bin_data * | x, | |
| unsigned int | k | |||
| ) | [inline] |
Append an unsigned integer.
Definition at line 341 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00342 { 00343 int len = sizeof(unsigned int),i; 00344 if (!bin_expand(x,len)) return 0; 00345 for(i=0;i<len;i++){ 00346 x->s[x->len++]= k & 0xFF; 00347 k = k>>8; 00348 } 00349 #if BIN_DEBUG 00350 switch(len){ 00351 case 4: 00352 LOG(L_INFO,"INFO:"M_NAME":bin_encode_uint: [%u]:[%.02x %.02x %.02x %.02x] new len %04x\n",k, 00353 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1],x->len); 00354 break; 00355 case 8: 00356 LOG(L_INFO,"INFO:"M_NAME":bin_encode_uint: [%u]:[%.02x %.02x %.02x %.02x%.02x %.02x %.02x %.02x] new len %04x\n",k, 00357 x->s[x->len-8],x->s[x->len-7],x->s[x->len-6],x->s[x->len-5], 00358 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1], 00359 x->len); 00360 break; 00361 } 00362 #endif 00363 return 1; 00364 }
| int bin_decode_uint | ( | bin_data * | x, | |
| unsigned int * | v | |||
| ) | [inline] |
Decode an unsigned integer.
Definition at line 368 of file bin.c.
References BIN_DEBUG, _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00369 { 00370 int len = sizeof(unsigned int),i; 00371 #if BIN_DECODE_CHECKS 00372 if (x->max+len > x->len) return 0; 00373 #endif 00374 *v = 0; 00375 for(i=0;i<len;i++) 00376 *v = *v | ((unsigned char)x->s[x->max++] <<(8*i)); 00377 #if BIN_DEBUG 00378 LOG(L_INFO,"INFO:"M_NAME":bin_decode_uint: [%u] new pos %04x\n",*v,x->max); 00379 #endif 00380 return 1; 00381 }
| int bin_encode_time_t | ( | bin_data * | x, | |
| time_t | k | |||
| ) | [inline] |
Append a time_t structure.
Definition at line 386 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00387 { 00388 int len = sizeof(time_t),i; 00389 if (!bin_expand(x,len)) return 0; 00390 for(i=0;i<len;i++){ 00391 x->s[x->len++]= k & 0xFF; 00392 k = k>>8; 00393 } 00394 #if BIN_DEBUG 00395 switch(len){ 00396 case 4: 00397 LOG(L_INFO,"INFO:"M_NAME":bin_encode_time_t: [%u]:[%.02x %.02x %.02x %.02x] new len %04x\n",(unsigned int)k, 00398 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1],x->len); 00399 break; 00400 case 8: 00401 LOG(L_INFO,"INFO:"M_NAME":bin_encode_time_t: [%u]:[%.02x %.02x %.02x %.02x%.02x %.02x %.02x %.02x] new len %04x\n",(unsigned int)k, 00402 x->s[x->len-8],x->s[x->len-7],x->s[x->len-6],x->s[x->len-5], 00403 x->s[x->len-4],x->s[x->len-3],x->s[x->len-2],x->s[x->len-1], 00404 x->len); 00405 break; 00406 } 00407 #endif 00408 return 1; 00409 }
| int bin_decode_time_t | ( | bin_data * | x, | |
| time_t * | v | |||
| ) | [inline] |
Decode an unsigned integer.
Definition at line 413 of file bin.c.
References BIN_DEBUG, _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00414 { 00415 int len = sizeof(time_t),i; 00416 #if BIN_DECODE_CHECKS 00417 if (x->max+len > x->len) return 0; 00418 #endif 00419 *v = 0; 00420 for(i=0;i<len;i++) 00421 *v = *v | ((unsigned char)x->s[x->max++] <<(8*i)); 00422 #if BIN_DEBUG 00423 LOG(L_INFO,"INFO:"M_NAME":bin_decode_time_t: [%u] new pos %04x\n",(unsigned int) *v,x->max); 00424 #endif 00425 return 1; 00426 }
| int bin_encode_str | ( | bin_data * | x, | |
| str * | s | |||
| ) | [inline] |
Append a string.
Definition at line 432 of file bin.c.
References bin_expand(), _bin_data::len, M_NAME, and _bin_data::s.
00433 { 00434 if (!bin_expand(x,2+s->len)) return 0; 00435 if (s->len>65535) 00436 LOG(L_ERR,"ERROR:"M_NAME":bin_encode_str: Possible loss of characters in encoding (string > 65535bytes) %d bytes \n",s->len); 00437 x->s[x->len++]=s->len & 0x000000FF; 00438 x->s[x->len++]=(s->len & 0x0000FF00)>>8; 00439 memcpy(x->s+x->len,s->s,s->len); 00440 x->len+=s->len; 00441 #if BIN_DEBUG 00442 LOG(L_INFO,"INFO:"M_NAME":bin_encode_str : [%d]:[%.02x %.02x]:[%.*s] new len %04x\n",s->len, 00443 x->s[x->len-s->len-2],x->s[x->len-s->len-1],s->len,s->s,x->len); 00444 #endif 00445 return 1; 00446 }
| int bin_decode_str | ( | bin_data * | x, | |
| str * | s | |||
| ) | [inline] |
Decode of a str string.
Definition at line 450 of file bin.c.
References _bin_data::len, M_NAME, _bin_data::max, and _bin_data::s.
00451 { 00452 #if BIN_DECODE_CHECKS 00453 if (x->max+2 > x->len) return 0; 00454 #endif 00455 s->len = (unsigned char)x->s[x->max ] | 00456 (unsigned char)x->s[x->max+1]<<8; 00457 x->max +=2; 00458 if (x->max+s->len>x->len) return 0; 00459 s->s = x->s + x->max; 00460 x->max += s->len; 00461 #if BIN_DEBUG 00462 LOG(L_INFO,"INFO:"M_NAME":bin_decode_str : [%d]:[%.*s] new pos %04x\n",s->len,s->len,s->s,x->max); 00463 #endif 00464 return 1; 00465 }
| int bin_encode_dlg_t | ( | bin_data * | x, | |
| dlg_t * | d | |||
| ) |
Encode a dlg_t into a binary form.
| x | - binary data to append to | |
| d | - the dlg_t to encode |
Definition at line 482 of file bin.c.
References bin_encode_str(), dialogb, and M_NAME.
00483 { 00484 str s={0,0}; 00485 if (d){ 00486 if (dialogb.dlg2str(d,&s)!=0) goto error; 00487 } 00488 if (!bin_encode_str(x,&s)) goto error; 00489 str_free_content(&s); 00490 return 1; 00491 error: 00492 LOG(L_ERR,"ERR:"M_NAME":bin_encode_dlg_t: Error while encoding.\n"); 00493 if (s.s) str_free_content(&s); 00494 return 0; 00495 }
| int bin_decode_dlg_t | ( | bin_data * | x, | |
| dlg_t ** | d | |||
| ) |
Decode a dlg_t from a binary data structure.
| x | - binary data to decode from | |
| dlg | - the dlg_t ** to write to |
Definition at line 503 of file bin.c.
References bin_decode_str(), dialogb, M_NAME, and _bin_data::max.
00504 { 00505 int len; 00506 str s; 00507 00508 if (!bin_decode_str(x,&s)) goto error; 00509 00510 if (!s.len) { 00511 *d = 0; 00512 return 1; 00513 } 00514 00515 len = sizeof(dlg_t); 00516 *d = (dlg_t*) shm_malloc(len); 00517 if (!*d) { 00518 LOG(L_ERR,"ERR:"M_NAME":bin_decode_dlg_t: Error allocating %d bytes.\n",len); 00519 goto error; 00520 } 00521 memset(*d,0,len); 00522 if (dialogb.str2dlg(&s,*d)!=0) goto error; 00523 00524 return 1; 00525 error: 00526 LOG(L_ERR,"ERR:"M_NAME":bin_decode_dlg_t: Error while decoding (at %d (%04x)).\n",x->max,x->max); 00527 if (*d) { 00528 shm_free(*d); 00529 } 00530 return 0; 00531 }
| int bin_dump_to_file | ( | bin_data * | x, | |
| char * | location, | |||
| char * | prepend_fname | |||
| ) |
Writes the binary data to a snapshot file.
The file names contain the time of the dump. If a file was dumped just partialy it will contain a ".part" in the name. A link to the last complete file is created each time. Old files are deleted and only scscf_persistency_keep_count time-stamped-files are kept.
| x | - the binary data to write | |
| prepend_fname | - what to prepend to the file_name |
Definition at line 553 of file bin.c.
References bin_files_keep_count, errno, _bin_data::len, M_NAME, and _bin_data::s.
00554 { 00555 char c_part[256],c_time[256],c_last[256]; 00556 time_t now; 00557 FILE *f; 00558 int k; 00559 00560 now = time(0); 00561 sprintf(c_part,"%s/%s_%.10u.bin.part",location,prepend_fname,(unsigned int)now); 00562 sprintf(c_time,"%s/%s_%.10u.bin",location,prepend_fname,(unsigned int)now); 00563 sprintf(c_last,"%s/_%s.bin",location,prepend_fname); 00564 00565 /* first dump to a partial file */ 00566 f = fopen(c_part,"w"); 00567 if (!f){ 00568 LOG(L_ERR,"ERR:"M_NAME":bin_dump_to_file: error when opening file <%s> for writting [%s]\n",c_part,strerror(errno)); 00569 return 0; 00570 } 00571 k = fwrite(x->s,1,x->len,f); 00572 LOG(L_INFO,"INFO:"M_NAME":bin_dump_to_file: Dumped %d bytes into %s.\n",k,c_part); 00573 fclose(f); 00574 00575 /* then rename it as a complete file with timestamp */ 00576 if (rename(c_part,c_time)<0){ 00577 LOG(L_ERR,"ERR:"M_NAME":bin_dump_to_file: error when renaming <%s> -> <%s> [%s]\n",c_part,c_time,strerror(errno)); 00578 return 0; 00579 } 00580 00581 /* then link the last snapshot to it */ 00582 if (k==x->len) { 00583 if (remove(c_last)<0 && errno!=ENOENT){ 00584 LOG(L_ERR,"ERR:"M_NAME":bin_dump_to_file: error when removing symlink <%s> [%s]\n",c_last,strerror(errno)); 00585 return 0; 00586 } 00587 if (symlink(c_time,c_last)<0){ 00588 LOG(L_ERR,"ERR:"M_NAME":bin_dump_to_file: error when symlinking <%s> -> <%s> [%s]\n",c_time,c_last,strerror(errno)); 00589 return 0; 00590 } 00591 /* then remove old snapshots */ 00592 { 00593 struct dirent **namelist; 00594 int i,n,k=bin_files_keep_count; 00595 int len=strlen(prepend_fname); 00596 n = scandir(location,&namelist,0,alphasort); 00597 if (n>0){ 00598 for(i=n-1;i>=0;i--){ 00599 if (strlen(namelist[i]->d_name)>len && 00600 memcmp(namelist[i]->d_name,prepend_fname,len)==0) { 00601 if (k) k--; 00602 else { 00603 sprintf(c_part,"%s/%s",location,namelist[i]->d_name); 00604 remove(c_part); 00605 } 00606 } 00607 free(namelist[i]); 00608 } 00609 free(namelist); 00610 } 00611 } 00612 return 1; 00613 } 00614 else return 0; 00615 }
| int bin_load_from_file | ( | bin_data * | x, | |
| char * | location, | |||
| char * | prepend_fname | |||
| ) |
Reloads the snapshot from the link to the last time-stamped-file.
| x | - where to load | |
| prepend_fname | - with what to prepend the filename |
Definition at line 624 of file bin.c.
References bin_alloc(), bin_expand(), errno, _bin_data::len, and M_NAME.
00625 { 00626 char c[256]; 00627 FILE *f; 00628 int k; 00629 00630 sprintf(c,"%s/_%s.bin",location,prepend_fname); 00631 f = fopen(c,"r"); 00632 if (!f) { 00633 LOG(L_ERR,"ERR:"M_NAME":bin_load_from_file: error opening %s : %s\n",c,strerror(errno)); 00634 return 0; 00635 } 00636 bin_alloc(x,1024); 00637 while(!feof(f)){ 00638 bin_expand(x,1024); 00639 k = fread(x->s+x->len,1,1024,f); 00640 x->len+=k; 00641 } 00642 LOG(L_INFO,"INFO:"M_NAME":bin_load_from_file: Read %d bytes from %s.\n",x->len,c); 00643 fclose(f); 00644 return 1; 00645 }
| int bin_dump | ( | bin_data * | x, | |
| int | mode, | |||
| char * | location, | |||
| char * | prepend_fname | |||
| ) |
Dump a binary data to certain location.
| mode | - where to dump it to | |
| x | - binary data to dump | |
| what | - dump auth, dialog or registrar information |
Definition at line 655 of file bin.c.
References bin_dump_to_file(), M_NAME, NO_PERSISTENCY, and WITH_FILES.
00656 { 00657 switch (mode){ 00658 case NO_PERSISTENCY: 00659 LOG(L_ERR,"ERR:"M_NAME":bin_dump: Snapshot done but persistency was disabled...\n"); 00660 return 0; 00661 case WITH_FILES: 00662 return bin_dump_to_file(x,location,prepend_fname); 00663 /*case WITH_DATABASE_BULK: 00664 return bin_dump_to_db(x, prepend_fname); 00665 case WITH_DATABASE_CACHE: 00666 LOG(L_ERR,"ERR:"M_NAME":bin_dump: Snapshot done but WITH_DATABASE_CACHE not implemented...\n"); 00667 return 0;*/ 00668 default: 00669 LOG(L_ERR,"ERR:"M_NAME":bin_dump: Snapshot done but no such mode %d\n",mode); 00670 return 0; 00671 } 00672 }
| int bin_load | ( | bin_data * | x, | |
| int | mode, | |||
| char * | location, | |||
| char * | prepend_fname | |||
| ) |
Reloads the bin_data from certain location.
| mode | - where to load it from | |
| x | - where to load into |
Definition at line 680 of file bin.c.
References bin_load_from_file(), M_NAME, NO_PERSISTENCY, and WITH_FILES.
00681 { 00682 switch (mode){ 00683 case NO_PERSISTENCY: 00684 LOG(L_ERR,"ERR:"M_NAME":bin_load: Persistency support was disabled\n"); 00685 return 0; 00686 case WITH_FILES: 00687 return bin_load_from_file(x,location,prepend_fname); 00688 /*case WITH_DATABASE_BULK: 00689 LOG(L_ERR,"ERR:"M_NAME":bin_load: WITH_DATABASE_BULK not implemented...\n"); 00690 return 0; 00691 case WITH_DATABASE_CACHE: 00692 LOG(L_ERR,"ERR:"M_NAME":bin_load: WITH_DATABASE_CACHE not implemented...\n"); 00693 return 0;*/ 00694 default: 00695 LOG(L_ERR,"ERR:"M_NAME":bin_load: Can't resume because no such mode %d\n",mode); 00696 return 0; 00697 } 00698 }
| int bin_files_keep_count = 3 |
1.5.2