Skip to content

Commit

Permalink
Expose mutt_file_seek API
Browse files Browse the repository at this point in the history
  • Loading branch information
gahr authored and flatcap committed Nov 19, 2021
1 parent e1c6f37 commit 0338b9c
Show file tree
Hide file tree
Showing 20 changed files with 150 additions and 97 deletions.
7 changes: 3 additions & 4 deletions alias/alias.c
Original file line number Diff line number Diff line change
Expand Up @@ -511,19 +511,19 @@ void alias_create(struct AddressList *al, const struct ConfigSubset *sub)
}

/* terminate existing file with \n if necessary */
if (fseek(fp_alias, 0, SEEK_END))
if (!mutt_file_seek(fp_alias, 0, SEEK_END))
goto fseek_err;
if (ftell(fp_alias) > 0)
{
if (fseek(fp_alias, -1, SEEK_CUR) < 0)
if (!mutt_file_seek(fp_alias, -1, SEEK_CUR))
goto fseek_err;
if (fread(buf, 1, 1, fp_alias) != 1)
{
mutt_perror(_("Error reading alias file"));
mutt_file_fclose(&fp_alias);
return;
}
if (fseek(fp_alias, 0, SEEK_END) < 0)
if (!mutt_file_seek(fp_alias, 0, SEEK_END))
goto fseek_err;
if (buf[0] != '\n')
fputc('\n', fp_alias);
Expand All @@ -550,7 +550,6 @@ void alias_create(struct AddressList *al, const struct ConfigSubset *sub)
return;

fseek_err:
mutt_perror(_("Error seeking in alias file"));
mutt_file_fclose(&fp_alias);
}

Expand Down
12 changes: 8 additions & 4 deletions attach/mutt_attach.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,13 @@ int mutt_compose_attachment(struct Body *a)

/* Remove headers by copying out data to another file, then
* copying the file back */
fseeko(fp, b->offset, SEEK_SET);
const LOFF_T offset = b->offset;
mutt_body_free(&b);
if (!mutt_file_seek(fp, offset, SEEK_SET))
{
goto bailout;
}

mutt_buffer_mktemp(tmpfile);
FILE *fp_tmp = mutt_file_fopen(mutt_buffer_string(tmpfile), "w");
if (!fp_tmp)
Expand Down Expand Up @@ -900,7 +905,7 @@ int mutt_save_attachment(FILE *fp, struct Body *m, const char *path,
e_new->msgno = e->msgno; /* required for MH/maildir */
e_new->read = true;

if (fseeko(fp, m->offset, SEEK_SET) < 0)
if (!mutt_file_seek(fp, m->offset, SEEK_SET))
return -1;
if (!fgets(buf, sizeof(buf), fp))
return -1;
Expand Down Expand Up @@ -946,9 +951,8 @@ int mutt_save_attachment(FILE *fp, struct Body *m, const char *path,
mutt_perror("fopen");
return -1;
}
if (fseeko((s.fp_in = fp), m->offset, SEEK_SET) != 0)
if (!mutt_file_seek((s.fp_in = fp), m->offset, SEEK_SET))
{
mutt_perror("fseeko");
mutt_file_fclose(&s.fp_out);
return -1;
}
Expand Down
16 changes: 8 additions & 8 deletions copy.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ int mutt_copy_hdr(FILE *fp_in, FILE *fp_out, LOFF_T off_start, LOFF_T off_end,
return -1;

if (ftello(fp_in) != off_start)
if (fseeko(fp_in, off_start, SEEK_SET) < 0)
if (!mutt_file_seek(fp_in, off_start, SEEK_SET))
return -1;

buf[0] = '\n';
Expand Down Expand Up @@ -599,7 +599,7 @@ static int count_delete_lines(FILE *fp, struct Body *b, LOFF_T *length, size_t d

if (b->deleted)
{
if (fseeko(fp, b->offset, SEEK_SET) != 0)
if (!mutt_file_seek(fp, b->offset, SEEK_SET))
{
return -1;
}
Expand Down Expand Up @@ -687,7 +687,7 @@ int mutt_copy_message_fp(FILE *fp_out, FILE *fp_in, struct Email *e,
mutt_buffer_addch(quoted_date, '"');

/* Count the number of lines and bytes to be deleted */
if (fseeko(fp_in, body->offset, SEEK_SET) != 0)
if (!mutt_file_seek(fp_in, body->offset, SEEK_SET))
{
goto attach_del_cleanup;
}
Expand All @@ -714,7 +714,7 @@ int mutt_copy_message_fp(FILE *fp_out, FILE *fp_in, struct Email *e,
new_offset = ftello(fp_out);

/* Copy the body */
if (fseeko(fp_in, body->offset, SEEK_SET) < 0)
if (!mutt_file_seek(fp_in, body->offset, SEEK_SET))
goto attach_del_cleanup;
if (copy_delete_attach(body, fp_in, fp_out, mutt_buffer_string(quoted_date)))
goto attach_del_cleanup;
Expand Down Expand Up @@ -819,7 +819,7 @@ int mutt_copy_message_fp(FILE *fp_out, FILE *fp_in, struct Email *e,
mutt_write_mime_header(cur, fp_out, NeoMutt->sub);
fputc('\n', fp_out);

if (fseeko(fp, cur->offset, SEEK_SET) < 0)
if (!mutt_file_seek(fp, cur->offset, SEEK_SET))
return -1;
if (mutt_file_copy_bytes(fp, fp_out, cur->length) == -1)
{
Expand All @@ -832,7 +832,7 @@ int mutt_copy_message_fp(FILE *fp_out, FILE *fp_in, struct Email *e,
}
else
{
if (fseeko(fp_in, body->offset, SEEK_SET) < 0)
if (!mutt_file_seek(fp_in, body->offset, SEEK_SET))
return -1;
if (cmflags & MUTT_CM_PREFIX)
{
Expand Down Expand Up @@ -918,7 +918,7 @@ static int append_message(struct Mailbox *dest, FILE *fp_in, struct Mailbox *src
struct Message *msg = NULL;
int rc;

if (fseeko(fp_in, e->offset, SEEK_SET) < 0)
if (!mutt_file_seek(fp_in, e->offset, SEEK_SET))
return -1;
if (!fgets(buf, sizeof(buf), fp_in))
return -1;
Expand Down Expand Up @@ -1019,7 +1019,7 @@ static int copy_delete_attach(struct Body *b, FILE *fp_in, FILE *fp_out, const c
}

/* Skip the deleted body */
if (fseeko(fp_in, part->offset + part->length, SEEK_SET) != 0)
if (!mutt_file_seek(fp_in, part->offset + part->length, SEEK_SET))
{
return -1;
}
Expand Down
12 changes: 9 additions & 3 deletions email/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ struct Envelope *mutt_rfc822_read_header(FILE *fp, struct Email *e, bool user_hd
continue;
}

(void) fseeko(fp, loc, SEEK_SET);
(void) mutt_file_seek(fp, loc, SEEK_SET);
break; /* end of header */
}

Expand Down Expand Up @@ -1464,7 +1464,10 @@ static void parse_part(FILE *fp, struct Body *b, int *counter)
#endif
bound = mutt_param_get(&b->parameter, "boundary");

fseeko(fp, b->offset, SEEK_SET);
if (!mutt_file_seek(fp, b->offset, SEEK_SET))
{
goto bail;
}
b->parts = parse_multipart(fp, bound, b->offset + b->length,
mutt_istr_equal("digest", b->subtype), counter);
break;
Expand All @@ -1473,7 +1476,10 @@ static void parse_part(FILE *fp, struct Body *b, int *counter)
if (!b->subtype)
break;

fseeko(fp, b->offset, SEEK_SET);
if (!mutt_file_seek(fp, b->offset, SEEK_SET))
{
goto bail;
}
if (mutt_is_message_type(b->type, b->subtype))
b->parts = rfc822_parse_message(fp, b, counter);
else if (mutt_istr_equal(b->subtype, "external-body"))
Expand Down
15 changes: 11 additions & 4 deletions handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -1055,7 +1055,7 @@ static int alternative_handler(struct Body *a, struct State *s)
{
const bool c_weed = cs_subset_bool(NeoMutt->sub, "weed");
if (s->flags & MUTT_DISPLAY && !c_weed &&
(fseeko(s->fp_in, choice->hdr_offset, SEEK_SET) == 0))
mutt_file_seek(s->fp_in, choice->hdr_offset, SEEK_SET))
{
mutt_file_copy_bytes(s->fp_in, s->fp_out, choice->offset - choice->hdr_offset);
}
Expand Down Expand Up @@ -1249,7 +1249,7 @@ static int multipart_handler(struct Body *a, struct State *s)
{
state_putc(s, '\n');
}
else if (fseeko(s->fp_in, p->hdr_offset, SEEK_SET) == 0)
else if (mutt_file_seek(s->fp_in, p->hdr_offset, SEEK_SET))
{
mutt_file_copy_bytes(s->fp_in, s->fp_out, p->offset - p->hdr_offset);
}
Expand Down Expand Up @@ -1307,7 +1307,10 @@ static int run_decode_and_handler(struct Body *b, struct State *s,
struct Buffer *tempfile = NULL;
#endif

(void) fseeko(s->fp_in, b->offset, SEEK_SET);
if (!mutt_file_seek(s->fp_in, b->offset, SEEK_SET))
{
return -1;
}

#ifdef USE_FMEMOPEN
char *temp = NULL;
Expand Down Expand Up @@ -1856,6 +1859,11 @@ void mutt_decode_attachment(struct Body *b, struct State *s)
int istext = mutt_is_text_part(b) && (b->disposition == DISP_INLINE);
iconv_t cd = (iconv_t) (-1);

if (!mutt_file_seek(s->fp_in, b->offset, SEEK_SET))
{
return;
}

if (istext && (b->charset || (s->flags & MUTT_CHARCONV)))
{
const char *charset = b->charset;
Expand All @@ -1872,7 +1880,6 @@ void mutt_decode_attachment(struct Body *b, struct State *s)
cd = mutt_ch_iconv_open(c_charset, charset, MUTT_ICONV_HOOK_FROM);
}

(void) fseeko(s->fp_in, b->offset, SEEK_SET);
switch (b->encoding)
{
case ENC_QUOTED_PRINTABLE:
Expand Down
65 changes: 39 additions & 26 deletions mbox/mbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,8 @@ static enum MxOpenReturns mmdf_parse_mailbox(struct Mailbox *m)

if (!is_from(buf, return_path, sizeof(return_path), &t))
{
if (fseeko(adata->fp, loc, SEEK_SET) != 0)
if (!mutt_file_seek(adata->fp, loc, SEEK_SET))
{
mutt_debug(LL_DEBUG1, "#1 fseek() failed\n");
mutt_error(_("Mailbox is corrupt"));
goto fail;
}
Expand All @@ -275,11 +274,10 @@ static enum MxOpenReturns mmdf_parse_mailbox(struct Mailbox *m)

if ((tmploc > 0) && (tmploc < m->size))
{
if ((fseeko(adata->fp, tmploc, SEEK_SET) != 0) ||
if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
!fgets(buf, sizeof(buf) - 1, adata->fp) || !mutt_str_equal(MMDF_SEP, buf))
{
if (fseeko(adata->fp, loc, SEEK_SET) != 0)
mutt_debug(LL_DEBUG1, "#2 fseek() failed\n");
(void) mutt_file_seek(adata->fp, loc, SEEK_SET);
e->body->length = -1;
}
}
Expand Down Expand Up @@ -439,17 +437,14 @@ static enum MxOpenReturns mbox_parse_mailbox(struct Mailbox *m)
{
/* check to see if the content-length looks valid. we expect to
* to see a valid message separator at this point in the stream */
if ((fseeko(adata->fp, tmploc, SEEK_SET) != 0) ||
if (!mutt_file_seek(adata->fp, tmploc, SEEK_SET) ||
!fgets(buf, sizeof(buf), adata->fp) || !mutt_str_startswith(buf, "From "))
{
mutt_debug(LL_DEBUG1, "bad content-length in message %d (cl=" OFF_T_FMT ")\n",
e_cur->index, e_cur->body->length);
mutt_debug(LL_DEBUG1, " LINE: %s", buf);
/* nope, return the previous position */
if ((loc < 0) || (fseeko(adata->fp, loc, SEEK_SET) != 0))
{
mutt_debug(LL_DEBUG1, "#1 fseek() failed\n");
}
(void) mutt_file_seek(adata->fp, loc, SEEK_SET);
e_cur->body->length = -1;
}
}
Expand All @@ -469,8 +464,7 @@ static enum MxOpenReturns mbox_parse_mailbox(struct Mailbox *m)
int cl = e_cur->body->length;

/* count the number of lines in this message */
if ((loc < 0) || (fseeko(adata->fp, loc, SEEK_SET) != 0))
mutt_debug(LL_DEBUG1, "#2 fseek() failed\n");
(void) mutt_file_seek(adata->fp, loc, SEEK_SET);
while (cl-- > 0)
{
if (fgetc(adata->fp) == '\n')
Expand All @@ -479,8 +473,7 @@ static enum MxOpenReturns mbox_parse_mailbox(struct Mailbox *m)
}

/* return to the offset of the next message separator */
if (fseeko(adata->fp, tmploc, SEEK_SET) != 0)
mutt_debug(LL_DEBUG1, "#3 fseek() failed\n");
(void) mutt_file_seek(adata->fp, tmploc, SEEK_SET);
}
}

Expand Down Expand Up @@ -742,7 +735,10 @@ static int fseek_last_message(FILE *fp)
char buf[BUFSIZ + 7] = { 0 }; // 7 for "\n\nFrom "
size_t bytes_read;

fseek(fp, 0, SEEK_END);
if (!mutt_file_seek(fp, 0, SEEK_END))
{
return -1;
}
pos = ftello(fp);

/* Set 'bytes_read' to the size of the last, probably partial, buf;
Expand All @@ -756,7 +752,10 @@ static int fseek_last_message(FILE *fp)
{
/* we save in the buf at the end the first 7 chars from the last read */
memcpy(buf + BUFSIZ, buf, 7);
fseeko(fp, pos, SEEK_SET);
if (!mutt_file_seek(fp, pos, SEEK_SET))
{
return -1;
}
bytes_read = fread(buf, sizeof(char), bytes_read, fp);
if (bytes_read == 0)
return -1;
Expand All @@ -765,7 +764,10 @@ static int fseek_last_message(FILE *fp)
{
if (mutt_str_startswith(buf + i, "\n\nFrom "))
{ /* found it - go to the beginning of the From */
fseeko(fp, pos + i + 2, SEEK_SET);
if (!mutt_file_seek(fp, pos + i + 2, SEEK_SET))
{
return -1;
}
return 0;
}
}
Expand All @@ -775,7 +777,10 @@ static int fseek_last_message(FILE *fp)
/* here we are at the beginning of the file */
if (mutt_str_startswith(buf, "From "))
{
fseek(fp, 0, SEEK_SET);
if (!mutt_file_seek(fp, 0, SEEK_SET))
{
return -1;
}
return 0;
}

Expand Down Expand Up @@ -1008,7 +1013,11 @@ static bool mbox_mbox_open_append(struct Mailbox *m, OpenMailboxFlags flags)
}
}

fseek(adata->fp, 0, SEEK_END);
if (!mutt_file_seek(adata->fp, 0, SEEK_END))
{
mutt_file_fclose(&adata->fp);
return false;
}

return true;
}
Expand Down Expand Up @@ -1076,15 +1085,19 @@ static enum MxStatus mbox_mbox_check(struct Mailbox *m)
* see the message separator at *exactly* what used to be the end of the
* folder. */
char buf[1024];
if (fseeko(adata->fp, m->size, SEEK_SET) != 0)
mutt_debug(LL_DEBUG1, "#1 fseek() failed\n");
if (!mutt_file_seek(adata->fp, m->size, SEEK_SET))
{
goto error;
}
if (fgets(buf, sizeof(buf), adata->fp))
{
if (((m->type == MUTT_MBOX) && mutt_str_startswith(buf, "From ")) ||
((m->type == MUTT_MMDF) && mutt_str_equal(buf, MMDF_SEP)))
{
if (fseeko(adata->fp, m->size, SEEK_SET) != 0)
mutt_debug(LL_DEBUG1, "#2 fseek() failed\n");
if (!mutt_file_seek(adata->fp, m->size, SEEK_SET))
{
goto error;
}

int old_msg_count = m->msg_count;
if (m->type == MUTT_MBOX)
Expand Down Expand Up @@ -1135,6 +1148,7 @@ static enum MxStatus mbox_mbox_check(struct Mailbox *m)

/* fatal error */

error:
mbox_unlock_mailbox(m);
mx_fastclose_mailbox(m);
mutt_sig_unblock();
Expand Down Expand Up @@ -1359,7 +1373,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
goto fatal;
}

if ((fseeko(adata->fp, offset, SEEK_SET) != 0) || /* seek the append location */
if (!mutt_file_seek(adata->fp, offset, SEEK_SET) || /* seek the append location */
/* do a sanity check to make sure the mailbox looks ok */
!fgets(buf, sizeof(buf), adata->fp) ||
((m->type == MUTT_MBOX) && !mutt_str_startswith(buf, "From ")) ||
Expand All @@ -1371,10 +1385,9 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
}
else
{
if (fseeko(adata->fp, offset, SEEK_SET) != 0) /* return to proper offset */
if (!mutt_file_seek(adata->fp, offset, SEEK_SET)) /* return to proper offset */
{
i = -1;
mutt_debug(LL_DEBUG1, "fseek() failed\n");
}
else
{
Expand Down
Loading

0 comments on commit 0338b9c

Please sign in to comment.