*/
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);
}
}