ses_file.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011 #include "klone_conf.h"
00012 #include <sys/types.h>
00013 #include <sys/stat.h>
00014 #include <stdlib.h>
00015 #include <time.h>
00016 #include <unistd.h>
00017 #include <fcntl.h>
00018 #include <u/libu.h>
00019 #include <klone/session.h>
00020 #include <klone/request.h>
00021 #include <klone/response.h>
00022 #include <klone/vars.h>
00023 #include <klone/utils.h>
00024 #include <klone/ses_prv.h>
00025
00026 static int session_file_save(session_t *ss)
00027 {
00028 io_t *io = NULL;
00029
00030 dbg_return_if (ss == NULL, ~0);
00031
00032
00033 (void) u_remove(ss->filename);
00034
00035 if(vars_count(ss->vars) == 0)
00036 return 0;
00037
00038
00039 dbg_err_if(u_file_open(ss->filename, O_WRONLY | O_CREAT, &io));
00040
00041 dbg_err_if(session_prv_save_to_io(ss, io));
00042
00043 io_free(io);
00044
00045 return 0;
00046 err:
00047 if(io)
00048 io_free(io);
00049 return ~0;
00050 }
00051
00052 static int session_file_load(session_t *ss)
00053 {
00054 io_t *io = NULL;
00055
00056 dbg_err_if (ss == NULL);
00057 dbg_err_if (ss->filename == NULL || ss->filename[0] == '\0');
00058
00059
00060 dbg_err_if(u_file_open(ss->filename, O_RDONLY | O_CREAT, &io));
00061
00062 dbg_err_if(session_prv_load_from_io(ss, io));
00063
00064 io_free(io);
00065
00066 return 0;
00067 err:
00068 if(io)
00069 io_free(io);
00070 return ~0;
00071 }
00072
00073 static int session_file_term(session_t *ss)
00074 {
00075 u_unused_args(ss);
00076 return 0;
00077 }
00078
00079 static int session_file_remove(session_t *ss)
00080 {
00081 dbg_return_if (ss == NULL, ~0);
00082
00083 dbg_if(u_remove(ss->filename));
00084
00085 return 0;
00086 }
00087
00088 int session_file_create(session_opt_t *so, request_t *rq, response_t *rs,
00089 session_t **pss)
00090 {
00091 session_t *ss = NULL;
00092 time_t now;
00093 struct stat st;
00094
00095 dbg_err_if (so == NULL);
00096 dbg_err_if (rq == NULL);
00097 dbg_err_if (rs == NULL);
00098 dbg_err_if (pss == NULL);
00099
00100 ss = u_zalloc(sizeof(session_t));
00101 dbg_err_if(ss == NULL);
00102
00103 ss->filename[0] = '\0';
00104 ss->load = session_file_load;
00105 ss->save = session_file_save;
00106 ss->remove = session_file_remove;
00107 ss->term = session_file_term;
00108 ss->so = so;
00109
00110 dbg_err_if(session_prv_init(ss, rq, rs));
00111
00112 if (ss->filename[0] != '\0' && stat(ss->filename, &st) == 0)
00113 ss->mtime = st.st_mtime;
00114 else
00115 {
00116 dbg_err_if ((now = time(NULL)) == (time_t) -1);
00117 ss->mtime = (int) now;
00118 }
00119
00120 *pss = ss;
00121
00122 return 0;
00123 err:
00124 if(ss)
00125 session_free(ss);
00126 return ~0;
00127 }
00128
00129 int session_file_module_term(session_opt_t *so)
00130 {
00131 u_unused_args(so);
00132 return 0;
00133 }
00134
00135 int session_file_module_init(u_config_t *config, session_opt_t *so)
00136 {
00137 const char *v;
00138
00139
00140 dbg_return_if (so == NULL, ~0);
00141
00142 if(config && (v = u_config_get_subkey_value(config, "file.path")) != NULL)
00143 {
00144 (void) u_strlcpy(so->path, v, sizeof so->path);
00145 } else {
00146
00147 #ifdef OS_WIN
00148 GetTempPath(sizeof so->path, so->path);
00149 #else
00150 (void) u_strlcpy(so->path, "/tmp", sizeof so->path);
00151 #endif
00152 }
00153
00154 return 0;
00155 }
00156