mrefile: don't open the file when renaming is enough

pull/159/head
Leah Neukirchen 5 years ago
parent bccacce816
commit 65ddebc87c

@ -48,9 +48,10 @@ gethost() {
} }
int int
deliver(char *infilename, FILE *infile) deliver(char *infilename)
{ {
int outfd; int outfd;
FILE *infile;
FILE *outfile; FILE *outfile;
char dst[PATH_MAX]; char dst[PATH_MAX];
char tmp[PATH_MAX]; char tmp[PATH_MAX];
@ -60,6 +61,22 @@ deliver(char *infilename, FILE *infile)
char *line = 0; char *line = 0;
size_t linelen = 0; size_t linelen = 0;
if (infilename) {
if (try_rename) {
infile = 0;
} else {
infile = fopen(infilename, "r");
if (!infile) {
fprintf(stderr, "mrefile: %s: %s\n",
infilename, strerror(errno));
return -1;
}
}
} else {
// mdeliver
infile = stdin;
}
if (Mflag) { if (Mflag) {
// skip to first "From " line // skip to first "From " line
while (1) { while (1) {
@ -68,7 +85,7 @@ deliver(char *infilename, FILE *infile)
if (rd == -1) { if (rd == -1) {
if (errno == 0) if (errno == 0)
errno = EINVAL; // invalid mbox file errno = EINVAL; // invalid mbox file
return -1; goto fail;
} }
if (strncmp("From ", line, 5) == 0) if (strncmp("From ", line, 5) == 0)
@ -76,9 +93,9 @@ deliver(char *infilename, FILE *infile)
} }
} }
while (!feof(infile)) { while (!infile || !feof(infile)) {
delivery++; delivery++;
tryagain: try_again:
gettimeofday(&tv, 0); gettimeofday(&tv, 0);
snprintf(id, sizeof id, "%ld.M%06ldP%ldQ%ld.%s", snprintf(id, sizeof id, "%ld.M%06ldP%ldQ%ld.%s",
@ -93,7 +110,15 @@ tryagain:
if (rename(infilename, dst) == 0) { if (rename(infilename, dst) == 0) {
if (vflag) if (vflag)
printf("%s\n", dst); printf("%s\n", dst);
return 0; goto success;
}
/* rename failed, open file and try copying */
infile = fopen(infilename, "r");
if (!infile) {
fprintf(stderr, "mrefile: %s: %s\n",
infilename, strerror(errno));
return -1;
} }
} }
@ -106,14 +131,14 @@ tryagain:
st.st_mode & 07777); st.st_mode & 07777);
if (outfd < 0) { if (outfd < 0) {
if (errno == EEXIST) if (errno == EEXIST)
goto tryagain; goto try_again;
if (errno == ENOENT) if (errno == ENOENT)
fprintf(stderr, "mrefile: %s/tmp: %s\n", fprintf(stderr, "mrefile: %s/tmp: %s\n",
targetdir, strerror(errno)); targetdir, strerror(errno));
else else
fprintf(stderr, "mrefile: %s: %s\n", fprintf(stderr, "mrefile: %s: %s\n",
tmp, strerror(errno)); tmp, strerror(errno));
return -1; goto fail;
} }
outfile = fdopen(outfd, "w"); outfile = fdopen(outfd, "w");
@ -127,7 +152,7 @@ tryagain:
ssize_t rd = getdelim(&line, &linelen, '\n', infile); ssize_t rd = getdelim(&line, &linelen, '\n', infile);
if (rd == -1) { if (rd == -1) {
if (errno != 0) if (errno != 0)
return -1; goto fail;
break; break;
} }
char *line_start = line; char *line_start = line;
@ -165,14 +190,14 @@ tryagain:
} }
if (fwrite(line_start, 1, rd, outfile) != (size_t)rd) if (fwrite(line_start, 1, rd, outfile) != (size_t)rd)
return -1; goto fail;
} }
if (fflush(outfile) == EOF) if (fflush(outfile) == EOF)
return -1; goto fail;
if (fsync(outfd) < 0) if (fsync(outfd) < 0)
return -1; goto fail;
if (fclose(outfile) == EOF) if (fclose(outfile) == EOF)
return -1; goto fail;
// compress flags // compress flags
int i, j; int i, j;
@ -211,12 +236,21 @@ tryagain:
targetdir, (cflag || is_old) ? "cur" : "new", id, targetdir, (cflag || is_old) ? "cur" : "new", id,
Xflag ? Xflag : statusflags); Xflag ? Xflag : statusflags);
if (rename(tmp, dst) != 0) if (rename(tmp, dst) != 0)
return -1; goto fail;
if (vflag) if (vflag)
printf("%s\n", dst); printf("%s\n", dst);
} }
success:
if (infile)
fclose(infile);
return 0; return 0;
fail:
if (infile)
fclose(infile);
return -1;
} }
void void
@ -225,12 +259,6 @@ refile(char *file)
while (*file == ' ' || *file == '\t') while (*file == ' ' || *file == '\t')
file++; file++;
FILE *f = fopen(file, "r");
if (!f) {
fprintf(stderr, "mrefile: %s: %s\n", file, strerror(errno));
return;
}
// keep flags // keep flags
char *flags = strstr(file, ":2,"); char *flags = strstr(file, ":2,");
if (flags) if (flags)
@ -238,12 +266,11 @@ refile(char *file)
else else
Xflag = ""; Xflag = "";
if (deliver(file, f) < 0) { if (deliver(file) < 0) {
perror("mrefile"); perror("mrefile");
return; return;
} }
fclose(f);
if (!kflag && !try_rename) if (!kflag && !try_rename)
unlink(file); unlink(file);
} }
@ -308,7 +335,7 @@ usage:
gethost(); gethost();
if (deliver(0, stdin) < 0) { if (deliver(0) < 0) {
perror("mdeliver"); perror("mdeliver");
return 2; return 2;
} }

Loading…
Cancel
Save