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 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
*
* Copyright (C) 1997-2016 Stuart Parmenter and others,
* See the file AUTHORS for a list.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses/>.
*/
/*
* LibBalsaMimeStreamShared: a subclass of GMimeStreamFs that supports
* locking.
*
* A single lock is shared by the original stream and all substreams and
* filtered streams derived from it. Methods that change the stream
* pointer (read, write, reset, seek) will fail with return value -1 if
* the stream is not locked. The lock should be held while carrying out
* sequences of operations such as seek+read, seek+write, read+tell,
* read+test-eos.
*/
#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif /* HAVE_CONFIG_H */
#include "mime-stream-shared.h"
#include <unistd.h>
#include <gmime/gmime-stream.h>
#include <gmime/gmime-stream-filter.h>
typedef struct _LibBalsaMimeStreamSharedLock LibBalsaMimeStreamSharedLock;
struct _LibBalsaMimeStreamShared {
GMimeStreamFs parent_object;
LibBalsaMimeStreamSharedLock *lock;
};
static void lbmss_finalize(GObject *object);
static ssize_t lbmss_stream_read(GMimeStream * stream, char *buf,
size_t len);
static ssize_t lbmss_stream_write(GMimeStream * stream, const char *buf,
size_t len);
static int lbmss_stream_reset(GMimeStream * stream);
static gint64 lbmss_stream_seek(GMimeStream * stream, gint64 offset,
GMimeSeekWhence whence);
static GMimeStream *lbmss_stream_substream(GMimeStream * stream,
gint64 start, gint64 end);
static GMutex lbmss_mutex;
static GCond lbmss_cond;
G_DEFINE_TYPE(LibBalsaMimeStreamShared, libbalsa_mime_stream_shared, GMIME_TYPE_STREAM_FS)
static void
libbalsa_mime_stream_shared_class_init(LibBalsaMimeStreamSharedClass * klass)
{
GMimeStreamClass *stream_class = GMIME_STREAM_CLASS(klass);
GObjectClass *object_class = G_OBJECT_CLASS(klass);
object_class->finalize = lbmss_finalize;
stream_class->read = lbmss_stream_read;
stream_class->write = lbmss_stream_write;
stream_class->reset = lbmss_stream_reset;
stream_class->seek = lbmss_stream_seek;
stream_class->substream = lbmss_stream_substream;
}
static void
libbalsa_mime_stream_shared_init(LibBalsaMimeStreamShared * stream)
{
}
/* The shared lock. */
struct _LibBalsaMimeStreamSharedLock {
GThread *thread;
guint count;
};
static LibBalsaMimeStreamSharedLock *
lbmss_lock_new(void)
{
LibBalsaMimeStreamSharedLock *lock;
lock = g_new(LibBalsaMimeStreamSharedLock, 1);
lock->thread = NULL;
lock->count = 0;
return lock;
}
/* Object class method. */
static void
lbmss_finalize(GObject *object)
{
LibBalsaMimeStreamShared *shared_stream = (LibBalsaMimeStreamShared *) object;
GMimeStreamFs *fs_stream = (GMimeStreamFs *) object;
if (fs_stream->owner)
g_free(shared_stream->lock);
G_OBJECT_CLASS(libbalsa_mime_stream_shared_parent_class)->finalize(object);
}
/* Stream class methods. */
#define lbmss_thread_has_lock(stream) \
(LIBBALSA_MIME_STREAM_SHARED(stream)->lock->thread == g_thread_self())
static ssize_t
lbmss_stream_read(GMimeStream * stream, char *buf, size_t len)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(libbalsa_mime_stream_shared_parent_class)->read(stream, buf, len);
}
static ssize_t
lbmss_stream_write(GMimeStream * stream, const char *buf, size_t len)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(libbalsa_mime_stream_shared_parent_class)->write(stream, buf, len);
}
static int
lbmss_stream_reset(GMimeStream * stream)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(libbalsa_mime_stream_shared_parent_class)->reset(stream);
}
static gint64
lbmss_stream_seek(GMimeStream * stream, gint64 offset,
GMimeSeekWhence whence)
{
g_return_val_if_fail(lbmss_thread_has_lock(stream), -1);
return GMIME_STREAM_CLASS(libbalsa_mime_stream_shared_parent_class)->seek(stream, offset, whence);
}
static GMimeStream *
lbmss_stream_substream(GMimeStream * stream, gint64 start, gint64 end)
{
LibBalsaMimeStreamShared *shared_stream;
GMimeStreamFs *fstream;
shared_stream =
g_object_new(LIBBALSA_TYPE_MIME_STREAM_SHARED, NULL, NULL);
shared_stream->lock = LIBBALSA_MIME_STREAM_SHARED(stream)->lock;
fstream = GMIME_STREAM_FS(shared_stream);
fstream->owner = FALSE;
fstream->fd = GMIME_STREAM_FS(stream)->fd;
g_mime_stream_construct(GMIME_STREAM(fstream), start, end);
return GMIME_STREAM(fstream);
}
/* Public methods. */
/**
* libbalsa_mime_stream_shared_new:
* @fd: file descriptor
*
* Create a new GMimeStreamShared object around @fd.
*
* Returns a stream using @fd.
*
**/
GMimeStream *
libbalsa_mime_stream_shared_new(int fd)
{
LibBalsaMimeStreamShared *shared_stream;
GMimeStreamFs *fstream;
gint64 start;
shared_stream =
g_object_new(LIBBALSA_TYPE_MIME_STREAM_SHARED, NULL, NULL);
shared_stream->lock = lbmss_lock_new();
fstream = GMIME_STREAM_FS(shared_stream);
fstream->owner = TRUE;
fstream->eos = FALSE;
fstream->fd = fd;
start = lseek(fd, 0, SEEK_CUR);
g_mime_stream_construct(GMIME_STREAM(fstream), start, -1);
return GMIME_STREAM(fstream);
}
/**
* libbalsa_mime_stream_shared_lock:
* @stream: shared stream
*
* Lock the shared stream
**/
void
libbalsa_mime_stream_shared_lock(GMimeStream * stream)
{
LibBalsaMimeStreamShared *shared_stream;
LibBalsaMimeStreamSharedLock *lock;
GThread *thread_self;
g_return_if_fail(GMIME_IS_STREAM(stream));
if (GMIME_IS_STREAM_FILTER(stream))
stream = ((GMimeStreamFilter *) stream)->source;
if (!LIBBALSA_IS_MIME_STREAM_SHARED(stream))
return;
shared_stream = (LibBalsaMimeStreamShared *) stream;
lock = shared_stream->lock;
thread_self = g_thread_self();
g_mutex_lock(&lbmss_mutex);
while (lock->count > 0 && lock->thread != thread_self)
g_cond_wait(&lbmss_cond, &lbmss_mutex);
++lock->count;
lock->thread = thread_self;
g_mutex_unlock(&lbmss_mutex);
}
/**
* libbalsa_mime_stream_shared_unlock:
* @stream: shared stream
*
* Unlock the shared stream
**/
void
libbalsa_mime_stream_shared_unlock(GMimeStream * stream)
{
LibBalsaMimeStreamShared *shared_stream;
LibBalsaMimeStreamSharedLock *lock;
g_return_if_fail(GMIME_IS_STREAM(stream));
if (GMIME_IS_STREAM_FILTER(stream))
stream = ((GMimeStreamFilter *) stream)->source;
if (!LIBBALSA_IS_MIME_STREAM_SHARED(stream))
return;
shared_stream = (LibBalsaMimeStreamShared *) stream;
lock = shared_stream->lock;
g_return_if_fail(lock->count > 0);
g_mutex_lock(&lbmss_mutex);
if (--lock->count == 0) {
lock->thread = NULL;
g_cond_signal(&lbmss_cond);
}
g_mutex_unlock(&lbmss_mutex);
}
|