Skip to content

Commit

Permalink
merge: refactoring to simplify functions
Browse files Browse the repository at this point in the history
 * Refactor msg_search
 * Refactor maildir_open_mailbox_append and mh_open_mailbox_append
 * Refactor mutt_file_unlink
 * Improve const-correctness
  • Loading branch information
flatcap committed Mar 8, 2018
2 parents c9c1d6e + be1fb2c commit efe8a33
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 182 deletions.
102 changes: 53 additions & 49 deletions mh.c
Original file line number Diff line number Diff line change
Expand Up @@ -1337,44 +1337,46 @@ static int maildir_open_mailbox(struct Context *ctx)

static int maildir_open_mailbox_append(struct Context *ctx, int flags)
{
if (flags & MUTT_APPENDNEW)
if (!(flags & MUTT_APPENDNEW))
{
if (mkdir(ctx->path, S_IRWXU))
{
mutt_perror(ctx->path);
return -1;
}
return 0;
}

char tmp[_POSIX_PATH_MAX];
if (mkdir(ctx->path, S_IRWXU))
{
mutt_perror(ctx->path);
return -1;
}

char tmp[_POSIX_PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
rmdir(ctx->path);
return -1;
}

snprintf(tmp, sizeof(tmp), "%s/new", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
rmdir(ctx->path);
return -1;
}
rmdir(tmp);
rmdir(ctx->path);
return -1;
}

snprintf(tmp, sizeof(tmp), "%s/tmp", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path);
rmdir(tmp);
snprintf(tmp, sizeof(tmp), "%s/new", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path);
rmdir(tmp);
rmdir(ctx->path);
return -1;
}

snprintf(tmp, sizeof(tmp), "%s/tmp", ctx->path);
if (mkdir(tmp, S_IRWXU))
{
mutt_perror(tmp);
snprintf(tmp, sizeof(tmp), "%s/cur", ctx->path);
rmdir(tmp);
snprintf(tmp, sizeof(tmp), "%s/new", ctx->path);
rmdir(tmp);
rmdir(ctx->path);
return -1;
}
rmdir(tmp);
rmdir(ctx->path);
return -1;
}

return 0;
Expand All @@ -1387,25 +1389,27 @@ static int mh_open_mailbox(struct Context *ctx)

static int mh_open_mailbox_append(struct Context *ctx, int flags)
{
if (flags & MUTT_APPENDNEW)
if (!(flags & MUTT_APPENDNEW))
{
if (mkdir(ctx->path, S_IRWXU))
{
mutt_perror(ctx->path);
return -1;
}
return 0;
}

char tmp[_POSIX_PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", ctx->path);
const int i = creat(tmp, S_IRWXU);
if (i == -1)
{
mutt_perror(tmp);
rmdir(ctx->path);
return -1;
}
close(i);
if (mkdir(ctx->path, S_IRWXU))
{
mutt_perror(ctx->path);
return -1;
}

char tmp[_POSIX_PATH_MAX];
snprintf(tmp, sizeof(tmp), "%s/.mh_sequences", ctx->path);
const int i = creat(tmp, S_IRWXU);
if (i == -1)
{
mutt_perror(tmp);
rmdir(ctx->path);
return -1;
}
close(i);

return 0;
}
Expand Down
48 changes: 26 additions & 22 deletions mutt/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -227,35 +227,39 @@ int mutt_file_fsync_close(FILE **f)
*/
void mutt_file_unlink(const char *s)
{
struct stat sb, sb2;
struct stat sb;
/* Defend against symlink attacks */

if ((lstat(s, &sb) == 0) && S_ISREG(sb.st_mode))
const bool is_regular_file = (lstat(s, &sb) == 0) && S_ISREG(sb.st_mode);
if (!is_regular_file)
{
const int fd = open(s, O_RDWR | O_NOFOLLOW);
if (fd < 0)
return;
return;
}

if ((fstat(fd, &sb2) != 0) || !S_ISREG(sb2.st_mode) ||
(sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino))
{
close(fd);
return;
}
const int fd = open(s, O_RDWR | O_NOFOLLOW);
if (fd < 0)
return;

FILE *f = fdopen(fd, "r+");
if (f)
struct stat sb2;
if ((fstat(fd, &sb2) != 0) || !S_ISREG(sb2.st_mode) ||
(sb.st_dev != sb2.st_dev) || (sb.st_ino != sb2.st_ino))
{
close(fd);
return;
}

FILE *f = fdopen(fd, "r+");
if (f)
{
unlink(s);
char buf[2048];
memset(buf, 0, sizeof(buf));
while (sb.st_size > 0)
{
unlink(s);
char buf[2048];
memset(buf, 0, sizeof(buf));
while (sb.st_size > 0)
{
fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f);
sb.st_size -= MIN(sizeof(buf), sb.st_size);
}
mutt_file_fclose(&f);
fwrite(buf, 1, MIN(sizeof(buf), sb.st_size), f);
sb.st_size -= MIN(sizeof(buf), sb.st_size);
}
mutt_file_fclose(&f);
}
}

Expand Down
Loading

0 comments on commit efe8a33

Please sign in to comment.