summaryrefslogtreecommitdiffstats
path: root/src/md5util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2003-08-28 22:28:36 +0000
committerLennart Poettering <lennart@poettering.net>2003-08-28 22:28:36 +0000
commit38cea8764bd4302bfa6f16b086b04dfa25be344b (patch)
treef3bc9c133bddbd905f1d7f7221ea70a5f0eb3b91 /src/md5util.c
parent98f36517251b5bccdb80d90d698c173c18ee4a29 (diff)
directory merge fixes
git-svn-id: file:///home/lennart/svn/public/syrep/trunk@10 07ea20a6-d2c5-0310-9e02-9ef735347d72
Diffstat (limited to 'src/md5util.c')
-rw-r--r--src/md5util.c104
1 files changed, 65 insertions, 39 deletions
diff --git a/src/md5util.c b/src/md5util.c
index a49e5d5..fcbf440 100644
--- a/src/md5util.c
+++ b/src/md5util.c
@@ -1,20 +1,19 @@
/***
- This file is part of pam_dotfile.
+ This file is part of syrep.
- pam_dotfile is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
+ syrep 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 of the License, or
(at your option) any later version.
- pam_dotfile 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.
+ syrep 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 pam_dotfile; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- USA
+ along with syrep; if not, write to the Free Software Foundation,
+ Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
***/
#include <unistd.h>
@@ -23,6 +22,7 @@
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <sys/stat.h>
#include "md5util.h"
#include "md5.h"
@@ -46,48 +46,74 @@ int fdmd5(int fd, size_t l, char *md) {
size_t m;
int r = 0;
md5_state_t s;
+ struct stat pre, post;
+ void *p = NULL;
md5_init(&s);
- m = l < MMAPSIZE ? l : MMAPSIZE;
+ if (fstat(fd, &pre) < 0) {
+ fprintf(stderr, "fstat(): %s\n", strerror(errno));
+ goto finish;
+ }
- while (l > 0 && ((d = mmap(NULL, m, PROT_READ, MAP_SHARED, fd, o)) != MAP_FAILED)) {
- md5_append(&s, d, m);
- munmap(d, m);
-
- o += m;
- l -= m;
+ if (l > BUFSIZE) {
+
m = l < MMAPSIZE ? l : MMAPSIZE;
-
+
+ while (l && ((d = mmap(NULL, m, PROT_READ, MAP_SHARED, fd, o)) != MAP_FAILED)) {
+ md5_append(&s, d, m);
+ munmap(d, m);
+
+ o += m;
+ l -= m;
+ m = l < MMAPSIZE ? l : MMAPSIZE;
+
+ }
+
+
+ if (l > 0)
+ fprintf(stderr, "mmap() failed: %s\n", strerror(errno));
}
if (l > 0) {
- void *p;
- fprintf(stderr, "mmap() failed: %s\n", strerror(errno));
-
- if (!(p = malloc(BUFSIZE)))
- r = -1;
- else {
+ if (!(p = malloc(BUFSIZE))) {
+ fprintf(stderr, "malloc(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ while (l) {
+ ssize_t r;
- for (;;) {
- ssize_t r;
-
- if ((r = read(fd, p, BUFSIZE)) < 0) {
- fprintf(stderr, "read(): %s\n", strerror(errno));
- free(p);
- return -1;
- }
-
- if (!r)
- break;
-
- md5_append(&s, p, r);
+ if ((r = read(fd, p, BUFSIZE)) < 0) {
+ fprintf(stderr, "read(): %s\n", strerror(errno));
+ goto finish;
}
- free(p);
+ if (!r)
+ break;
+
+ md5_append(&s, p, r);
+
+ l -= r;
}
}
-
+
+ if (fstat(fd, &post) < 0) {
+ fprintf(stderr, "fstat(): %s\n", strerror(errno));
+ goto finish;
+ }
+
+ if (pre.st_mtime != post.st_mtime) {
+ fprintf(stderr, "File modified while calculating digest.\n");
+ goto finish;
+ }
+
md5_finish(&s, md);
+
+finish:
+
+ if (p)
+ free(p);
+
return r;
}