1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
|
/* GNU Mailutils -- a suite of utilities for electronic mail
Copyright (C) 2010-2012 Free Software Foundation, Inc.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library. If not, see
<http://www.gnu.org/licenses/>. */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/stream.h>
#include <mailutils/iterator.h>
#include <mailutils/header.h>
#include <mailutils/nls.h>
#include <mailutils/filter.h>
#include <mailutils/property.h>
#include <mailutils/sys/property.h>
#include <stdlib.h>
static void
_mh_prop_done (struct _mu_property *prop)
{
struct mu_mh_prop *mhprop = prop->_prop_init_data;
mu_header_t header = prop->_prop_data;
mu_header_destroy (&header);
free (mhprop->filename);
free (mhprop);
}
static int
_mh_prop_getval (struct _mu_property *prop,
const char *key, const char **pval)
{
mu_header_t header = prop->_prop_data;
if (!header)
return MU_ERR_NOENT;
return mu_header_sget_value (header, key, pval);
}
static int
_mh_prop_setval (struct _mu_property *prop, const char *key,
const char *val, int overwrite)
{
struct mu_mh_prop *mhprop = prop->_prop_init_data;
mu_header_t header = prop->_prop_data;
if (!header)
{
int rc;
if ((rc = mu_header_create (&header, NULL, 0)) != 0)
{
mu_error (_("cannot create context %s: %s"),
mhprop->filename, mu_strerror (rc));
return 1;
}
prop->_prop_data = header;
}
return mu_header_set_value (header, key, val, overwrite);
}
static int
_mh_prop_unset (struct _mu_property *prop, const char *key)
{
mu_header_t header = prop->_prop_data;
if (!header)
return 0;
return mu_header_remove (header, key, 1);
}
static int
_mh_prop_getitr (struct _mu_property *prop, mu_iterator_t *pitr)
{
mu_header_t header = prop->_prop_data;
return mu_header_get_iterator (header, pitr);
}
static int
_mh_prop_clear (struct _mu_property *prop)
{
mu_header_t header = prop->_prop_data;
return mu_header_clear (header);
}
static int
_mh_prop_read_stream (mu_header_t *phdr, mu_stream_t stream)
{
int rc;
mu_stream_t flt;
const char *argv[4];
mu_off_t size;
size_t total;
char *blurb;
rc = mu_stream_size (stream, &size);
if (rc)
return rc;
argv[0] = "INLINE-COMMENT";
argv[1] = "#";
argv[2] = "-r";
argv[3] = NULL;
rc = mu_filter_create_args (&flt, stream, argv[0], 3, argv,
MU_FILTER_DECODE, MU_STREAM_READ);
if (rc)
{
mu_error (_("cannot open filter stream: %s"), mu_strerror (rc));
return rc;
}
blurb = malloc (size + 1);
if (!blurb)
{
mu_stream_destroy (&flt);
return ENOMEM;
}
total = 0;
while (1)
{
size_t n;
rc = mu_stream_read (flt, blurb + total, size - total, &n);
if (rc)
break;
if (n == 0)
break;
total += n;
}
mu_stream_destroy (&flt);
if (rc)
{
free (blurb);
return rc;
}
rc = mu_header_create (phdr, blurb, total);
free (blurb);
return rc;
}
static int
_mh_prop_write_stream (mu_header_t header, struct mu_mh_prop *mhprop,
mu_stream_t stream)
{
int rc;
mu_stream_t instream;
mu_off_t size;
mu_header_get_streamref (header, &instream);
rc = mu_stream_copy (stream, instream, 0, &size);
if (rc)
{
mu_error (_("error writing to context file %s: %s"),
mhprop->filename, mu_strerror (rc));
return rc;
}
else
rc = mu_stream_truncate (stream, size);
mu_stream_destroy (&instream);
return rc;
}
static int
_mh_prop_fill (struct _mu_property *prop)
{
struct mu_mh_prop *mhprop = prop->_prop_init_data;
int rc;
mu_stream_t stream;
mu_header_t header;
rc = mu_file_stream_create (&stream, mhprop->filename, MU_STREAM_READ);
if (rc)
{
if ((rc = mu_header_create (&header, NULL, 0)) != 0)
mu_error (_("cannot create context %s: %s"),
mhprop->filename, mu_strerror (rc));
}
else
{
rc = _mh_prop_read_stream (&header, stream);
mu_stream_unref (stream);
}
if (rc == 0)
prop->_prop_data = header;
return rc;
}
static int
_mh_prop_save (struct _mu_property *prop)
{
struct mu_mh_prop *mhprop = prop->_prop_init_data;
mu_header_t header = prop->_prop_data;
mu_stream_t stream;
int rc;
if (mhprop->ro)
return 0;
rc = mu_file_stream_create (&stream, mhprop->filename,
MU_STREAM_WRITE|MU_STREAM_CREAT);
if (rc)
return rc;
rc = _mh_prop_write_stream (header, mhprop, stream);
mu_stream_unref (stream);
return rc;
}
int
mu_mh_property_init (struct _mu_property *prop)
{
struct mu_mh_prop *mhprop = prop->_prop_init_data;
if (!mhprop)
return EINVAL;
prop->_prop_data = NULL;
prop->_prop_done = _mh_prop_done;
prop->_prop_fill = _mh_prop_fill;
prop->_prop_save = _mh_prop_save;
prop->_prop_getval = _mh_prop_getval;
prop->_prop_setval = _mh_prop_setval;
prop->_prop_unset = _mh_prop_unset;
prop->_prop_getitr = _mh_prop_getitr;
prop->_prop_clear = _mh_prop_clear;
return 0;
}
|