From 5f6ea960908c00743493efa5d8a357a49b8ccba1 Mon Sep 17 00:00:00 2001 From: phk Date: Thu, 23 Feb 1995 04:51:36 +0000 Subject: Make us a little safer, by writing to a tempfile, and rename when done. Still needs more locking I belive. --- usr.sbin/ctm/ctm_rmail/ctm_rmail.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) (limited to 'usr.sbin') diff --git a/usr.sbin/ctm/ctm_rmail/ctm_rmail.c b/usr.sbin/ctm/ctm_rmail/ctm_rmail.c index 04eef38..b7d8134 100644 --- a/usr.sbin/ctm/ctm_rmail/ctm_rmail.c +++ b/usr.sbin/ctm/ctm_rmail/ctm_rmail.c @@ -213,6 +213,7 @@ read_piece(char *input_file) char line[200]; char delta[30]; char pname[1000]; + char tname[1000]; char junk[2]; ifp = stdin; @@ -241,9 +242,10 @@ read_piece(char *input_file) *s = '_'; mk_piece_name(pname, delta, pce, npieces); - if ((ofp = fopen(pname, "w")) == NULL) + sprintf(tname,"tmp.%s",pname); + if ((ofp = fopen(tname, "w")) == NULL) { - err("cannot open '%s' for writing", pname); + err("cannot open '%s' for writing", tname); status++; continue; } @@ -267,7 +269,7 @@ read_piece(char *input_file) fclose(ofp); if (e) - err("error writing %s", pname); + err("error writing %s", tname); if (cksum != claimed_cksum) err("checksum: read %d, calculated %d", claimed_cksum, cksum); @@ -275,7 +277,15 @@ read_piece(char *input_file) if (e || cksum != claimed_cksum) { err("%s %d/%d discarded", delta, pce, npieces); - unlink(pname); + unlink(tname); + status++; + continue; + } + + if (rename(tname, pname) < 0) + { + err("error renaming %s to %s",tname,pname); + unlink(tname); status++; continue; } -- cgit v1.1