summaryrefslogtreecommitdiff
path: root/usr.sbin
diff options
context:
space:
mode:
authorCharles Longeau <chl@cvs.openbsd.org>2012-08-30 19:33:26 +0000
committerCharles Longeau <chl@cvs.openbsd.org>2012-08-30 19:33:26 +0000
commit992af837a8e67e4f5cf4c6aae93aa4c355642042 (patch)
tree10db110345c3baa7bec276ebc59d234eafbcc546 /usr.sbin
parentaea301efee5ad7534164bdef8e09060069d523b1 (diff)
switch compress_backend to use FILE * instead of file descriptors, like
crypto_backend ok gilles@
Diffstat (limited to 'usr.sbin')
-rw-r--r--usr.sbin/smtpd/compress_backend.c10
-rw-r--r--usr.sbin/smtpd/compress_gzip.c50
-rw-r--r--usr.sbin/smtpd/queue_backend.c34
-rw-r--r--usr.sbin/smtpd/smtpd.h10
4 files changed, 53 insertions, 51 deletions
diff --git a/usr.sbin/smtpd/compress_backend.c b/usr.sbin/smtpd/compress_backend.c
index fec14d8739c..b285de302ed 100644
--- a/usr.sbin/smtpd/compress_backend.c
+++ b/usr.sbin/smtpd/compress_backend.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: compress_backend.c,v 1.4 2012/08/29 16:26:17 gilles Exp $ */
+/* $OpenBSD: compress_backend.c,v 1.5 2012/08/30 19:33:25 chl Exp $ */
/*
* Copyright (c) 2012 Charles Longeau <chl@openbsd.org>
@@ -43,15 +43,15 @@ compress_backend_lookup(const char *name)
}
int
-compress_file(int fdin, int fdout)
+compress_file(FILE *in, FILE *out)
{
- return env->sc_compress->compress_file(fdin, fdout);
+ return env->sc_compress->compress_file(in, out);
}
int
-uncompress_file(int fdin, int fdout)
+uncompress_file(FILE *in, FILE *out)
{
- return env->sc_compress->uncompress_file(fdin, fdout);
+ return env->sc_compress->uncompress_file(in, out);
}
size_t
diff --git a/usr.sbin/smtpd/compress_gzip.c b/usr.sbin/smtpd/compress_gzip.c
index e2b86b1b76d..28578548e79 100644
--- a/usr.sbin/smtpd/compress_gzip.c
+++ b/usr.sbin/smtpd/compress_gzip.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: compress_gzip.c,v 1.1 2012/08/26 13:38:43 gilles Exp $ */
+/* $OpenBSD: compress_gzip.c,v 1.2 2012/08/30 19:33:25 chl Exp $ */
/*
* Copyright (c) 2012 Gilles Chehade <gilles@openbsd.org>
@@ -41,8 +41,8 @@
#define GZIP_BUFFER_SIZE 8192
-static int compress_file_gzip(int, int);
-static int uncompress_file_gzip(int, int);
+static int compress_file_gzip(FILE *, FILE *);
+static int uncompress_file_gzip(FILE *, FILE *);
static size_t compress_buffer_gzip(const char *, size_t, char *, size_t);
static size_t uncompress_buffer_gzip(const char *, size_t, char *, size_t);
@@ -54,62 +54,60 @@ struct compress_backend compress_gzip = {
};
static int
-compress_file_gzip(int fdin, int fdout)
+compress_file_gzip(FILE *in, FILE *out)
{
- gzFile gzfd;
- char buf[GZIP_BUFFER_SIZE];
+ gzFile gzf;
+ char ibuf[GZIP_BUFFER_SIZE];
int r, w;
int ret = 0;
- if (fdin == -1 || fdout == -1)
+ if (in == NULL || out == NULL)
return (0);
- gzfd = gzdopen(fdout, "wb");
- if (gzfd == NULL)
+ gzf = gzdopen(fileno(out), "wb");
+ if (gzf == NULL)
return (0);
- while ((r = read(fdin, buf, sizeof(buf))) > 0) {
- w = gzwrite(gzfd, buf, r);
- if (w != r)
+ while ((r = fread(ibuf, 1, GZIP_BUFFER_SIZE, in)) != 0) {
+ if ((w = gzwrite(gzf, ibuf, r)) != r)
goto end;
}
- if (r == -1)
+ if (! feof(in))
goto end;
ret = 1;
end:
- gzclose(gzfd);
+ gzclose(gzf);
return (ret);
}
static int
-uncompress_file_gzip(int fdin, int fdout)
+uncompress_file_gzip(FILE *in, FILE *out)
{
- gzFile gzfd;
- char buf[GZIP_BUFFER_SIZE];
+ gzFile gzf;
+ char obuf[GZIP_BUFFER_SIZE];
int r, w;
int ret = 0;
- if (fdin == -1 || fdout == -1)
+ if (in == NULL || out == NULL)
return (0);
-
- gzfd = gzdopen(fdin, "r");
- if (gzfd == NULL)
+
+ gzf = gzdopen(fileno(in), "r");
+ if (gzf == NULL)
return (0);
- while ((r = gzread(gzfd, buf, sizeof(buf))) > 0) {
- w = write(fdout, buf, r);
- if (w != r)
+ while ((r = gzread(gzf, obuf, sizeof(obuf))) > 0) {
+ if ((w = fwrite(obuf, r, 1, out)) != 1)
goto end;
}
- if (r == -1)
+ if (! gzeof(gzf))
goto end;
ret = 1;
end:
- gzclose(gzfd);
+ gzclose(gzf);
return (ret);
}
diff --git a/usr.sbin/smtpd/queue_backend.c b/usr.sbin/smtpd/queue_backend.c
index 5eac3b478c4..c65a26bc9ca 100644
--- a/usr.sbin/smtpd/queue_backend.c
+++ b/usr.sbin/smtpd/queue_backend.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: queue_backend.c,v 1.36 2012/08/30 19:28:40 chl Exp $ */
+/* $OpenBSD: queue_backend.c,v 1.37 2012/08/30 19:33:25 chl Exp $ */
/*
* Copyright (c) 2011 Gilles Chehade <gilles@openbsd.org>
@@ -102,7 +102,6 @@ queue_message_commit(uint32_t msgid)
{
char msgpath[MAXPATHLEN];
char tmppath[MAXPATHLEN];
- int fdin = -1, fdout = -1;
FILE *ifp = NULL;
FILE *ofp = NULL;
@@ -112,14 +111,16 @@ queue_message_commit(uint32_t msgid)
if (env->sc_queue_flags & QUEUE_COMPRESS) {
bsnprintf(tmppath, sizeof tmppath, "%s.comp", msgpath);
- fdin = open(msgpath, O_RDONLY);
- fdout = open(tmppath, O_RDWR | O_CREAT | O_EXCL, 0600);
- if (fdin == -1 || fdout == -1)
+ ifp = fopen(msgpath, "r");
+ ofp = fopen(tmppath, "w+");
+ if (ifp == NULL || ofp == NULL)
goto err;
- if (! compress_file(fdin, fdout))
+ if (! compress_file(ifp, ofp))
goto err;
- close(fdin);
- close(fdout);
+ fclose(ifp);
+ fclose(ofp);
+ ifp = NULL;
+ ofp = NULL;
if (rename(tmppath, msgpath) == -1) {
if (errno == ENOSPC)
@@ -153,10 +154,6 @@ queue_message_commit(uint32_t msgid)
return env->sc_queue->message(QOP_COMMIT, &msgid);
err:
- if (fdin != -1)
- close(fdin);
- if (fdout != -1)
- close(fdout);
if (ifp)
fclose(ifp);
if (ofp)
@@ -196,10 +193,17 @@ queue_message_fd_r(uint32_t msgid)
if (env->sc_queue_flags & QUEUE_COMPRESS) {
fdout = mktmpfile();
- if (! uncompress_file(fdin, fdout))
+ ifp = fdopen(fdin, "r");
+ ofp = fdopen(fdout, "w+");
+ if (ifp == NULL || ofp == NULL)
goto err;
- close(fdin);
- fdin = fdout;
+ if (! uncompress_file(ifp, ofp))
+ goto err;
+ fseek(ofp, SEEK_SET, 0);
+ fdin = fileno(ofp);
+ fclose(ifp);
+ ifp = NULL;
+ ofp = NULL;
}
return (fdin);
diff --git a/usr.sbin/smtpd/smtpd.h b/usr.sbin/smtpd/smtpd.h
index 92343b4fbf8..8705507dbf8 100644
--- a/usr.sbin/smtpd/smtpd.h
+++ b/usr.sbin/smtpd/smtpd.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: smtpd.h,v 1.342 2012/08/30 18:25:44 gilles Exp $ */
+/* $OpenBSD: smtpd.h,v 1.343 2012/08/30 19:33:25 chl Exp $ */
/*
* Copyright (c) 2008 Gilles Chehade <gilles@openbsd.org>
@@ -819,8 +819,8 @@ struct queue_backend {
};
struct compress_backend {
- int (*compress_file)(int, int);
- int (*uncompress_file)(int, int);
+ int (*compress_file)(FILE *, FILE *);
+ int (*uncompress_file)(FILE *, FILE *);
size_t (*compress_buffer)(const char *, size_t, char *, size_t);
size_t (*uncompress_buffer)(const char *, size_t, char *, size_t);
};
@@ -1100,8 +1100,8 @@ void qwalk_close(void *);
/* compress_backend.c */
struct compress_backend *compress_backend_lookup(const char *);
-int compress_file(int, int);
-int uncompress_file(int, int);
+int compress_file(FILE *, FILE *);
+int uncompress_file(FILE *, FILE *);
size_t compress_buffer(const char *, size_t, char *, size_t);
size_t uncompress_buffer(const char *, size_t, char *, size_t);