libjf API reference guide

Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals | Related Pages

jf_journal_file.c

Go to the documentation of this file.
00001 /* 00002 * Copyright 2005 Tiian 00003 * {In real life Tiian is Christian Ferrari} 00004 * This file is part of "libjf" package. 00005 * 00006 * "libjf" is free software; you can redistribute it and/or modify 00007 * it under the terms of the GNU Lesser General Public License as published by 00008 * the Free Software Foundation; either version 2.1 of the License, or 00009 * (at your option) any later version. 00010 * 00011 * "libjf" is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 * GNU Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public License 00017 * along with ; if not, write to the Free Software 00018 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 #include <jf/jf_config.h> 00021 00022 00023 00024 #ifdef HAVE_STDIO_H 00025 # include <stdio.h> 00026 #endif /* HAVE_STDIO_H */ 00027 #ifdef HAVE_STDLIB_H 00028 # include <stdlib.h> 00029 #endif /* HAVE_STDLIB_H */ 00030 #ifdef HAVE_STRING_H 00031 # include <string.h> 00032 #endif /* HAVE_STRING_H */ 00033 00034 #include <jf/jf_errors.h> 00035 #include <jf/jf_journal_file.h> 00036 00037 00038 00039 /* set module trace flag */ 00040 #ifdef JF_TRACE_MODULE 00041 # undef JF_TRACE_MODULE 00042 #endif /* JF_TRACE_MODULE */ 00043 #define JF_TRACE_MODULE JF_TRACE_MOD_LIB_JOURNAL_FILE 00044 00045 00046 00047 void jf_journal_file_reset(jf_journal_file_t *jf) 00048 { 00049 memset(jf, 0, sizeof(jf_journal_file_t)); 00050 jf->file_path = NULL; 00051 jf_cache_file_reset(&(jf->cache_file)); 00052 jf->all_in_cache = TRUE; 00053 } 00054 00055 00056 00057 void jf_journal_file_create(jf_journal_file_t *jf) 00058 { 00059 jf_cache_file_create(&(jf->cache_file)); 00060 } 00061 00062 00063 00064 void jf_journal_file_destroy(jf_journal_file_t *jf) 00065 { 00066 if (jf->file_path != NULL) 00067 free(jf->file_path); 00068 jf_cache_file_destroy(&(jf->cache_file)); 00069 } 00070 00071 00072 00073 int jf_journal_file_init(jf_journal_file_t *jf, 00074 const char *path, FILE *stream, uint32_t open_mode, 00075 const struct jf_journal_file_opts_s *options) 00076 { 00077 enum Exception { SET_PATH 00078 , CACHE_FILE_INIT 00079 , NONE } excp; 00080 int ret_cod = JF_RC_INTERNAL_ERROR; 00081 00082 JF_TRACE(("jf_journal_file_init\n")); 00083 TRY { 00084 jf_offset_t cache_file_limit; 00085 00086 ret_cod = jf_journal_file_set_path(jf, path); 00087 if (JF_RC_OK != ret_cod) 00088 THROW(SET_PATH); 00089 /* @@@ remove me 00090 size_t path_size = strlen(path); 00091 if (path_size >= JF_JOURNAL_FILE_MAX_PATH_SIZE) 00092 path_size = JF_JOURNAL_FILE_MAX_PATH_SIZE - 1; 00093 if (NULL == (jf->file_path = (char *)malloc(path_size + 1))) 00094 THROW(MALLOC_ERROR); 00095 strncpy(jf->file_path, path, path_size); 00096 jf->file_path[path_size] = '\0'; 00097 */ 00098 00099 jf->stream = stream; 00100 jf->last_pos = 0; 00101 jf->last_size = 0; 00102 jf->status = JF_JOURNAL_FILE_ST_RESET; 00103 jf->last_uc_size = 0; 00104 jf->last_uc_pos = 0; 00105 jf->open_mode = open_mode; 00106 jf->all_in_cache = TRUE; 00107 if (options == NULL) { 00108 cache_file_limit = -1; 00109 } else { 00110 cache_file_limit = options->cache_size_limit; 00111 } /* if (options == NULL) */ 00112 ret_cod = jf_cache_file_init( 00113 &(jf->cache_file), 0, cache_file_limit); 00114 if (JF_RC_OK != ret_cod) 00115 THROW(CACHE_FILE_INIT); 00116 00117 THROW(NONE); 00118 } CATCH { 00119 switch (excp) { 00120 case SET_PATH: 00121 case CACHE_FILE_INIT: 00122 break; 00123 case NONE: 00124 ret_cod = JF_RC_OK; 00125 break; 00126 default: 00127 ret_cod = JF_RC_INTERNAL_ERROR; 00128 } /* switch (excp) */ 00129 /* release useless storage */ 00130 if (excp > SET_PATH && excp < NONE) { 00131 free(jf->file_path); 00132 jf->file_path = NULL; 00133 } /* if (excp > MALLOC_ERROR && excp < NONE) */ 00134 } /* TRY-CATCH */ 00135 JF_TRACE(("jf_journal_file_init/excp=%d/ret_cod=%d/errno=%d\n", 00136 excp, ret_cod, errno)); 00137 return ret_cod; 00138 } 00139 00140 00141 00142 int jf_journal_file_set_path(jf_journal_file_t *jf, 00143 const char *new_path) 00144 { 00145 enum Exception { NULL_OBJECT 00146 , REALLOC_ERROR 00147 , NONE } excp; 00148 int ret_cod = JF_RC_INTERNAL_ERROR; 00149 00150 JF_TRACE(("jf_journal_file_set_path\n")); 00151 TRY { 00152 size_t path_size; 00153 char *new_file_path; 00154 #ifdef _EXTRA_CHECK 00155 if (NULL == new_path || NULL == jf) 00156 THROW(NULL_OBJECT); 00157 #endif /* _EXTRA_CHECK */ 00158 path_size = strlen(new_path); 00159 if (path_size >= JF_JOURNAL_FILE_MAX_PATH_SIZE) 00160 path_size = JF_JOURNAL_FILE_MAX_PATH_SIZE - 1; 00161 if (NULL == (new_file_path = (char *)malloc(path_size + 1))) 00162 THROW(REALLOC_ERROR); 00163 strncpy(new_file_path, new_path, path_size); 00164 new_file_path[path_size] = '\0'; 00165 jf->file_path = new_file_path; 00166 00167 THROW(NONE); 00168 } CATCH { 00169 switch (excp) { 00170 case NULL_OBJECT: 00171 ret_cod = JF_RC_NULL_OBJECT; 00172 break; 00173 case REALLOC_ERROR: 00174 ret_cod = JF_RC_REALLOC_ERROR; 00175 break; 00176 case NONE: 00177 ret_cod = JF_RC_OK; 00178 break; 00179 default: 00180 ret_cod = JF_RC_INTERNAL_ERROR; 00181 } /* switch (excp) */ 00182 } /* TRY-CATCH */ 00183 JF_TRACE(("jf_journal_file_set_path/excp=%d/" 00184 "ret_cod=%d/errno=%d\n", excp, ret_cod, errno)); 00185 return ret_cod; 00186 } 00187 00188 00189 00190 int jf_journal_file_set_options( 00191 jf_journal_file_t *jf, 00192 const struct jf_journal_file_opts_s *options) 00193 { 00194 enum Exception { NULL_OPTIONS 00195 , NONE } excp; 00196 int ret_cod = JF_RC_INTERNAL_ERROR; 00197 00198 JF_TRACE(("jf_journal_file_set_options\n")); 00199 TRY { 00200 if (NULL == options) 00201 THROW(NULL_OPTIONS); 00202 00203 jf_cache_file_set_limit(&(jf->cache_file), 00204 options->cache_size_limit); 00205 00206 THROW(NONE); 00207 } CATCH { 00208 switch (excp) { 00209 case NULL_OPTIONS: 00210 ret_cod = JF_RC_NULL_OBJECT; 00211 break; 00212 case NONE: 00213 ret_cod = JF_RC_OK; 00214 break; 00215 default: 00216 ret_cod = JF_RC_INTERNAL_ERROR; 00217 } /* switch (excp) */ 00218 } /* TRY-CATCH */ 00219 JF_TRACE(("jf_journal_file_set_options/excp=%d/ret_cod=%d/" 00220 "errno=%d\n", excp, ret_cod, errno)); 00221 return ret_cod; 00222 } 00223 00224 00225 int jf_journal_file_set_cache_file(jf_journal_file_t *jf, 00226 const jf_cache_file_t *cf) 00227 { 00228 enum Exception { NULL_POINTER_EXCEPTION 00229 , NONE } excp; 00230 int ret_cod = JF_RC_INTERNAL_ERROR; 00231 00232 JF_TRACE(("jf_journal_file_set_cache_file\n")); 00233 TRY { 00234 if (jf == NULL || cf == NULL) 00235 THROW(NULL_POINTER_EXCEPTION); 00236 memcpy(&(jf->cache_file), cf, sizeof(jf_cache_file_t)); 00237 00238 THROW(NONE); 00239 } CATCH { 00240 switch (excp) { 00241 case NULL_POINTER_EXCEPTION: 00242 ret_cod = JF_RC_NULL_OBJECT; 00243 break; 00244 case NONE: 00245 ret_cod = JF_RC_OK; 00246 break; 00247 default: 00248 ret_cod = JF_RC_INTERNAL_ERROR; 00249 } /* switch (excp) */ 00250 } /* TRY-CATCH */ 00251 JF_TRACE(("jf_journal_file_set_cache_file/excp=%d/ret_cod=%d/" 00252 "errno=%d\n", excp, ret_cod, errno)); 00253 return ret_cod; 00254 } 00255

Copyright 2005 © Tiian