/*
 *  Copyright (C) 2000 Helix Code Inc.
 *
 *  Authors: Michael Zucchi <notzed@helixcode.com>
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Library General Public License
 *  as published by the Free Software Foundation; either version 2 of
 *  the License, 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 Library General Public License for more details.
 *
 *  You should have received a copy of the GNU Library General Public
 *  License along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>

#include <string.h>
#include <errno.h>

#include "camel-mime-filter-save.h"

static void camel_mime_filter_save_class_init (CamelMimeFilterSaveClass *klass);
static void camel_mime_filter_save_init       (CamelMimeFilterSave *obj);
static void camel_mime_filter_save_finalize   (CamelObject *o);

static CamelMimeFilterClass *camel_mime_filter_save_parent;

CamelType
camel_mime_filter_save_get_type (void)
{
	static CamelType type = CAMEL_INVALID_TYPE;
	
	if (type == CAMEL_INVALID_TYPE) {
		type = camel_type_register (camel_mime_filter_get_type (), "CamelMimeFilterSave",
					    sizeof (CamelMimeFilterSave),
					    sizeof (CamelMimeFilterSaveClass),
					    (CamelObjectClassInitFunc) camel_mime_filter_save_class_init,
					    NULL,
					    (CamelObjectInitFunc) camel_mime_filter_save_init,
					    (CamelObjectFinalizeFunc) camel_mime_filter_save_finalize);
	}
	
	return type;
}

static void
camel_mime_filter_save_finalize(CamelObject *o)
{
	CamelMimeFilterSave *f = (CamelMimeFilterSave *)o;

	g_free(f->filename);
	if (f->fd != -1) {
		/* FIXME: what do we do with failed writes???? */
		close(f->fd);
	}
}

static void
reset(CamelMimeFilter *mf)
{
	CamelMimeFilterSave *f = (CamelMimeFilterSave *)mf;

	/* i dunno, how do you 'reset' a file?  reopen it? do i care? */
	if (f->fd != -1){
		lseek(f->fd, 0, SEEK_SET);
	}
}

/* all this code just to support this little trivial filter! */
static void
filter(CamelMimeFilter *mf, char *in, size_t len, size_t prespace, char **out, size_t *outlen, size_t *outprespace)
{
	CamelMimeFilterSave *f = (CamelMimeFilterSave *)mf;

	if (f->fd != -1) {
		/* FIXME: check return */
		int outlen = write(f->fd, in, len);
		if (outlen != len) {
			g_warning("could not write to '%s': %s", f->filename?f->filename:"<descriptor>", strerror(errno));
		}
	}
	*out = in;
	*outlen = len;
	*outprespace = prespace;
}

static void
camel_mime_filter_save_class_init (CamelMimeFilterSaveClass *klass)
{
	CamelMimeFilterClass *filter_class = (CamelMimeFilterClass *) klass;

	camel_mime_filter_save_parent = CAMEL_MIME_FILTER_CLASS (camel_type_get_global_classfuncs (camel_mime_filter_get_type ()));

	filter_class->reset = reset;
	filter_class->filter = filter;
}

static void
camel_mime_filter_save_init (CamelMimeFilterSave *f)
{
	f->fd = -1;
}

/**
 * camel_mime_filter_save_new:
 *
 * Create a new CamelMimeFilterSave object.
 * 
 * Return value: A new CamelMimeFilterSave widget.
 **/
CamelMimeFilterSave *
camel_mime_filter_save_new (void)
{
	CamelMimeFilterSave *new = CAMEL_MIME_FILTER_SAVE ( camel_object_new (camel_mime_filter_save_get_type ()));
	return new;
}

CamelMimeFilterSave *
camel_mime_filter_save_new_name (const char *name, int flags, int mode)
{
	CamelMimeFilterSave *new = NULL;

	new = camel_mime_filter_save_new();
	if (new) {
		new->fd = open(name, flags, mode);
		if (new->fd != -1) {
			new->filename = g_strdup(name);
		} else {
			camel_object_unref((CamelObject *)new);
			new = NULL;
		}
	}
	return new;
}

ches/2017Q1'>branches/2017Q1</option>
<option value='branches/2017Q2'>branches/2017Q2</option>
<option value='branches/2017Q3'>branches/2017Q3</option>
<option value='branches/2017Q4'>branches/2017Q4</option>
<option value='branches/2018Q1'>branches/2018Q1</option>
<option value='branches/2018Q2'>branches/2018Q2</option>
<option value='branches/2018Q3'>branches/2018Q3</option>
<option value='branches/2018Q4'>branches/2018Q4</option>
<option value='branches/2019Q1'>branches/2019Q1</option>
<option value='branches/2019Q2'>branches/2019Q2</option>
<option value='branches/2019Q3'>branches/2019Q3</option>
<option value='branches/2019Q4'>branches/2019Q4</option>
<option value='branches/2020Q1'>branches/2020Q1</option>
<option value='branches/2020Q2'>branches/2020Q2</option>
<option value='branches/2020Q3'>branches/2020Q3</option>
<option value='branches/2020Q4'>branches/2020Q4</option>
<option value='branches/2021Q1'>branches/2021Q1</option>
<option value='branches/RELEASE_8_4_0'>branches/RELEASE_8_4_0</option>
<option value='branches/RELENG_2_1_0'>branches/RELENG_2_1_0</option>
<option value='branches/RELENG_2_2'>branches/RELENG_2_2</option>
<option value='branches/RELENG_9_1_0'>branches/RELENG_9_1_0</option>
<option value='branches/RELENG_9_2_0'>branches/RELENG_9_2_0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3' selected='selected'>dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15'>dependabot/npm_and_yarn/devel/electron4/files/lodash-4.17.15</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2'>dependabot/npm_and_yarn/devel/electron4/files/lodash.merge-4.6.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0'>dependabot/npm_and_yarn/devel/electron4/files/lodash.template-4.5.0</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2'>dependabot/npm_and_yarn/devel/electron4/files/minimist-1.2.2</option>
<option value='dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2'>dependabot/npm_and_yarn/devel/electron4/files/mixin-deep-1.3.2</option>
<option value='main'>main</option>
<option value='master'>master</option>
<option value='svn_head'>svn_head</option>
</select> <input type='submit' value='switch'/></form></td></tr>
<tr><td class='sub'>FreeBSD Ports (https://github.com/freebsd/freebsd-ports)</td><td class='sub right'></td></tr></table>
<table class='tabs'><tr><td>
<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/about/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>about</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>summary</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/refs/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>refs</a><a class='active' href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>log</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/tree/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>tree</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/commit/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>commit</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/diff/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>diff</a><a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/stats/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'>stats</a></td><td class='form'><form class='right' method='get' action='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/lang/scm'>
<input type='hidden' name='h' value='dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3'/><input type='hidden' name='id' value='a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'/><select name='qt'>
<option value='grep'>log msg</option>
<option value='author'>author</option>
<option value='committer'>committer</option>
<option value='range'>range</option>
</select>
<input class='txt' type='search' size='10' name='q' value=''/>
<input type='submit' value='search'/>
</form>
</td></tr></table>
<div class='path'>path: <a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>root</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/lang?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>lang</a>/<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71'>scm</a></div><div class='content'><table class='list nowrap'><tr class='nohover'><th></th><th class='left'>Commit message (<a href='/~lantw44/cgit/cgit.cgi/freebsd-ports/log/lang/scm?h=dependabot/npm_and_yarn/devel/electron4/files/eslint-utils-1.4.3&amp;id=a5e8d0fc1bb9bdd07e196a189f1479cd89e21b71&amp;showmsg=1'>Expand</a>)</th><th class='left'>Author</th><th class='left'>Age</th><th class='left'>Files</th><th class='left'>Lines</th></tr>