NeoMutt  2022-04-29-178-g3b62e6
Teaching an old dog new tricks
DOXYGEN
mbox_sync()

Save changes to the Mailbox. More...

+ Collaboration diagram for mbox_sync():

Functions

static enum MxStatus comp_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
enum MxStatus maildir_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
enum MxStatus mh_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus mbox_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus nntp_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus nm_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 
static enum MxStatus pop_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -. More...
 

Detailed Description

Save changes to the Mailbox.

Parameters
mMailbox to sync
Return values
enumMxStatus

Contract

Function Documentation

◆ comp_mbox_sync()

static enum MxStatus comp_mbox_sync ( struct Mailbox m)
static

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Changes in NeoMutt only affect the tmp file. Calling comp_mbox_sync() will commit them to the compressed file.

Definition at line 618 of file compress.c.

619{
620 if (!m->compress_info)
621 return MX_STATUS_ERROR;
622
623 struct CompressInfo *ci = m->compress_info;
624
625 if (!ci->cmd_close)
626 {
627 mutt_error(_("Can't sync a compressed file without a close-hook"));
628 return MX_STATUS_ERROR;
629 }
630
631 const struct MxOps *ops = ci->child_ops;
632 if (!ops)
633 return MX_STATUS_ERROR;
634
635 if (!lock_realpath(m, true))
636 {
637 mutt_error(_("Unable to lock mailbox"));
638 return MX_STATUS_ERROR;
639 }
640
641 enum MxStatus check = comp_mbox_check(m);
642 if (check != MX_STATUS_OK)
643 goto sync_cleanup;
644
645 check = ops->mbox_sync(m);
646 if (check != MX_STATUS_OK)
647 goto sync_cleanup;
648
649 int rc = execute_command(m, ci->cmd_close, _("Compressing %s"));
650 if (rc == 0)
651 {
652 check = MX_STATUS_ERROR;
653 goto sync_cleanup;
654 }
655
656 check = MX_STATUS_OK;
657
658sync_cleanup:
659 store_size(m);
661 return check;
662}
static int execute_command(struct Mailbox *m, const char *command, const char *progress)
Run a system command.
Definition: compress.c:322
static void store_size(const struct Mailbox *m)
Save the size of the compressed file.
Definition: compress.c:179
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:86
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition: compress.c:125
#define mutt_error(...)
Definition: logging.h:87
static enum MxStatus comp_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: compress.c:582
#define _(a)
Definition: message.h:28
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_snc(), and mbox_close()
Definition: mxapi.h:84
@ MX_STATUS_ERROR
An error occurred.
Definition: mxapi.h:85
@ MX_STATUS_OK
No changes.
Definition: mxapi.h:86
Private data for compress.
Definition: lib.h:47
const struct MxOps * child_ops
callbacks of de-compressed file
Definition: lib.h:52
const char * cmd_close
close-hook command
Definition: lib.h:49
void * compress_info
Compressed mbox module private data.
Definition: mailbox.h:121
Definition: mxapi.h:112
enum MxStatus(* mbox_sync)(struct Mailbox *m)
Definition: mxapi.h:215
+ Here is the call graph for this function:

◆ maildir_mbox_sync()

enum MxStatus maildir_mbox_sync ( struct Mailbox m)

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Return values
enumMxStatus
Note
The flag retvals come from a call to a backend sync function

Definition at line 1374 of file maildir.c.

1375{
1376 enum MxStatus check = maildir_mbox_check(m);
1377 if (check == MX_STATUS_ERROR)
1378 return check;
1379
1380 struct HeaderCache *hc = NULL;
1381#ifdef USE_HCACHE
1382 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
1383 if (m->type == MUTT_MAILDIR)
1384 hc = mutt_hcache_open(c_header_cache, mailbox_path(m), NULL);
1385#endif
1386
1387 struct Progress *progress = NULL;
1388 if (m->verbose)
1389 {
1390 char msg[PATH_MAX] = { 0 };
1391 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1392 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1393 }
1394
1395 for (int i = 0; i < m->msg_count; i++)
1396 {
1397 if (m->verbose)
1398 progress_update(progress, i, -1);
1399
1400 if (!maildir_sync_mailbox_message(m, i, hc))
1401 {
1402 progress_free(&progress);
1403 goto err;
1404 }
1405 }
1406 progress_free(&progress);
1407
1408#ifdef USE_HCACHE
1409 if (m->type == MUTT_MAILDIR)
1411#endif
1412
1413 /* XXX race condition? */
1414
1416
1417 /* adjust indices */
1418
1419 if (m->msg_deleted)
1420 {
1421 for (int i = 0, j = 0; i < m->msg_count; i++)
1422 {
1423 struct Email *e = m->emails[i];
1424 if (!e)
1425 break;
1426
1427 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
1428 if (!e->deleted || c_maildir_trash)
1429 e->index = j++;
1430 }
1431 }
1432
1433 return check;
1434
1435err:
1436#ifdef USE_HCACHE
1437 if (m->type == MUTT_MAILDIR)
1439#endif
1440 return MX_STATUS_ERROR;
1441}
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:194
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:73
enum MxStatus maildir_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: maildir.c:1168
void mutt_hcache_close(struct HeaderCache *hc)
Multiplexor for StoreOps::close.
Definition: hcache.c:432
struct HeaderCache * mutt_hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:332
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:211
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
void maildir_update_mtime(struct Mailbox *m)
Update our record of the Maildir modification time.
Definition: maildir.c:477
bool maildir_sync_mailbox_message(struct Mailbox *m, int msgno, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: maildir.c:948
#define PATH_MAX
Definition: mutt.h:40
@ MUTT_PROGRESS_WRITE
Progress tracks elements, according to $write_inc
Definition: lib.h:50
void progress_free(struct Progress **ptr)
Free a Progress Bar.
Definition: progress.c:86
bool progress_update(struct Progress *progress, size_t pos, int percent)
Update the state of the progress bar.
Definition: progress.c:73
struct Progress * progress_new(const char *msg, enum ProgressType type, size_t size)
Create a new Progress Bar.
Definition: progress.c:118
The envelope/body of an email.
Definition: email.h:37
bool deleted
Email is deleted.
Definition: email.h:76
int index
The absolute (unsorted) message number.
Definition: email.h:110
Header cache structure.
Definition: lib.h:87
int msg_count
Total number of messages.
Definition: mailbox.h:88
enum MailboxType type
Mailbox type.
Definition: mailbox.h:102
struct Email ** emails
Array of Emails.
Definition: mailbox.h:96
int msg_deleted
Number of deleted messages.
Definition: mailbox.h:93
bool verbose
Display status messages?
Definition: mailbox.h:115
Container for Accounts, Notifications.
Definition: neomutt.h:37
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:39
+ Here is the call graph for this function:

◆ mh_mbox_sync()

enum MxStatus mh_mbox_sync ( struct Mailbox m)

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Return values
MX_STATUS_REOPENEDmailbox has been externally modified
MX_STATUS_NEW_MAILnew mail has arrived
0Success
-1Error
Note
The flag retvals come from a call to a backend sync function

Definition at line 1037 of file mh.c.

1038{
1039 enum MxStatus check = mh_mbox_check(m);
1040 if (check == MX_STATUS_ERROR)
1041 return check;
1042
1043 struct HeaderCache *hc = NULL;
1044#ifdef USE_HCACHE
1045 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
1046 if (m->type == MUTT_MH)
1047 hc = mutt_hcache_open(c_header_cache, mailbox_path(m), NULL);
1048#endif
1049
1050 struct Progress *progress = NULL;
1051 if (m->verbose)
1052 {
1053 char msg[PATH_MAX] = { 0 };
1054 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1055 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1056 }
1057
1058 for (int i = 0; i < m->msg_count; i++)
1059 {
1060 if (m->verbose)
1061 progress_update(progress, i, -1);
1062
1063 if (mh_sync_mailbox_message(m, i, hc) == -1)
1064 {
1065 progress_free(&progress);
1066 goto err;
1067 }
1068 }
1069 progress_free(&progress);
1070
1071#ifdef USE_HCACHE
1072 if (m->type == MUTT_MH)
1074#endif
1075
1076 mh_seq_update(m);
1077
1078 /* XXX race condition? */
1079
1080 mh_update_mtime(m);
1081
1082 /* adjust indices */
1083
1084 if (m->msg_deleted)
1085 {
1086 for (int i = 0, j = 0; i < m->msg_count; i++)
1087 {
1088 struct Email *e = m->emails[i];
1089 if (!e)
1090 break;
1091
1092 if (!e->deleted)
1093 e->index = j++;
1094 }
1095 }
1096
1097 return check;
1098
1099err:
1100#ifdef USE_HCACHE
1101 if (m->type == MUTT_MH)
1103#endif
1104 return MX_STATUS_ERROR;
1105}
enum MxStatus mh_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: mh.c:892
@ MUTT_MH
'MH' Mailbox type
Definition: mailbox.h:47
int mh_sync_mailbox_message(struct Mailbox *m, int msgno, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: mh.c:756
void mh_update_mtime(struct Mailbox *m)
Update our record of the Maildir modification time.
Definition: mh.c:477
void mh_seq_update(struct Mailbox *m)
Update sequence numbers.
Definition: sequence.c:231
+ Here is the call graph for this function:

◆ mbox_mbox_sync()

static enum MxStatus mbox_mbox_sync ( struct Mailbox m)
static

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Definition at line 1161 of file mbox.c.

1162{
1164 if (!adata)
1165 return MX_STATUS_ERROR;
1166
1167 struct Buffer *tempfile = NULL;
1168 char buf[32] = { 0 };
1169 int j;
1170 bool unlink_tempfile = false;
1171 bool need_sort = false; /* flag to resort mailbox if new mail arrives */
1172 int first = -1; /* first message to be written */
1173 LOFF_T offset; /* location in mailbox to write changed messages */
1174 struct stat st = { 0 };
1175 struct MUpdate *new_offset = NULL;
1176 struct MUpdate *old_offset = NULL;
1177 FILE *fp = NULL;
1178 struct Progress *progress = NULL;
1179 enum MxStatus rc = MX_STATUS_ERROR;
1180
1181 /* sort message by their position in the mailbox on disk */
1182 const short c_sort = cs_subset_sort(NeoMutt->sub, "sort");
1183 const unsigned char c_use_threads = cs_subset_enum(NeoMutt->sub, "use_threads");
1184 if (c_sort != SORT_ORDER)
1185 {
1187 cs_subset_str_native_set(NeoMutt->sub, "use_threads", UT_FLAT, NULL);
1189 cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
1190 cs_subset_str_native_set(NeoMutt->sub, "use_threads", c_use_threads, NULL);
1191 need_sort = true;
1192 }
1193
1194 /* need to open the file for writing in such a way that it does not truncate
1195 * the file, so use read-write mode. */
1196 adata->fp = freopen(mailbox_path(m), "r+", adata->fp);
1197 if (!adata->fp)
1198 {
1199 mx_fastclose_mailbox(m, false);
1200 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1201 goto fatal;
1202 }
1203
1205
1206 if (mbox_lock_mailbox(m, true, true) == -1)
1207 {
1209 mutt_error(_("Unable to lock mailbox"));
1210 goto bail;
1211 }
1212
1213 /* Check to make sure that the file hasn't changed on disk */
1214 enum MxStatus check = mbox_mbox_check(m);
1215 if ((check == MX_STATUS_NEW_MAIL) || (check == MX_STATUS_REOPENED))
1216 {
1217 /* new mail arrived, or mailbox reopened */
1218 rc = check;
1219 goto bail;
1220 }
1221 else if (check < 0)
1222 {
1223 goto fatal;
1224 }
1225
1226 /* Create a temporary file to write the new version of the mailbox in. */
1227 tempfile = mutt_buffer_pool_get();
1228 mutt_buffer_mktemp(tempfile);
1229 int fd = open(mutt_buffer_string(tempfile), O_WRONLY | O_EXCL | O_CREAT, 0600);
1230 if ((fd == -1) || !(fp = fdopen(fd, "w")))
1231 {
1232 if (fd != -1)
1233 {
1234 close(fd);
1235 unlink_tempfile = true;
1236 }
1237 mutt_error(_("Could not create temporary file"));
1238 goto bail;
1239 }
1240 unlink_tempfile = true;
1241
1242 /* find the first deleted/changed message. we save a lot of time by only
1243 * rewriting the mailbox from the point where it has actually changed. */
1244 int i = 0;
1245 for (; (i < m->msg_count) && !m->emails[i]->deleted &&
1246 !m->emails[i]->changed && !m->emails[i]->attach_del;
1247 i++)
1248 {
1249 }
1250 if (i == m->msg_count)
1251 {
1252 /* this means m->changed or m->msg_deleted was set, but no
1253 * messages were found to be changed or deleted. This should
1254 * never happen, is we presume it is a bug in neomutt. */
1255 mutt_error(_("sync: mbox modified, but no modified messages (report this bug)"));
1256 mutt_debug(LL_DEBUG1, "no modified messages\n");
1257 goto bail;
1258 }
1259
1260 /* save the index of the first changed/deleted message */
1261 first = i;
1262 /* where to start overwriting */
1263 offset = m->emails[i]->offset;
1264
1265 /* the offset stored in the header does not include the MMDF_SEP, so make
1266 * sure we seek to the correct location */
1267 if (m->type == MUTT_MMDF)
1268 offset -= (sizeof(MMDF_SEP) - 1);
1269
1270 /* allocate space for the new offsets */
1271 new_offset = mutt_mem_calloc(m->msg_count - first, sizeof(struct MUpdate));
1272 old_offset = mutt_mem_calloc(m->msg_count - first, sizeof(struct MUpdate));
1273
1274 if (m->verbose)
1275 {
1276 char msg[PATH_MAX] = { 0 };
1277 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1278 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1279 }
1280
1281 for (i = first, j = 0; i < m->msg_count; i++)
1282 {
1283 if (m->verbose)
1284 progress_update(progress, i, i / (m->msg_count / 100 + 1));
1285 /* back up some information which is needed to restore offsets when
1286 * something fails. */
1287
1288 old_offset[i - first].valid = true;
1289 old_offset[i - first].hdr = m->emails[i]->offset;
1290 old_offset[i - first].body = m->emails[i]->body->offset;
1291 old_offset[i - first].lines = m->emails[i]->lines;
1292 old_offset[i - first].length = m->emails[i]->body->length;
1293
1294 if (!m->emails[i]->deleted)
1295 {
1296 j++;
1297
1298 if (m->type == MUTT_MMDF)
1299 {
1300 if (fputs(MMDF_SEP, fp) == EOF)
1301 {
1303 goto bail;
1304 }
1305 }
1306
1307 /* save the new offset for this message. we add 'offset' because the
1308 * temporary file only contains saved message which are located after
1309 * 'offset' in the real mailbox */
1310 new_offset[i - first].hdr = ftello(fp) + offset;
1311
1312 struct Message *msg = mx_msg_open(m, m->emails[i]->msgno);
1313 const int rc2 = mutt_copy_message(fp, m->emails[i], msg, MUTT_CM_UPDATE,
1315 mx_msg_close(m, &msg);
1316 if (rc2 != 0)
1317 {
1319 goto bail;
1320 }
1321
1322 /* Since messages could have been deleted, the offsets stored in memory
1323 * will be wrong, so update what we can, which is the offset of this
1324 * message, and the offset of the body. If this is a multipart message,
1325 * we just flush the in memory cache so that the message will be reparsed
1326 * if the user accesses it later. */
1327 new_offset[i - first].body = ftello(fp) - m->emails[i]->body->length + offset;
1328 mutt_body_free(&m->emails[i]->body->parts);
1329
1330 switch (m->type)
1331 {
1332 case MUTT_MMDF:
1333 if (fputs(MMDF_SEP, fp) == EOF)
1334 {
1336 goto bail;
1337 }
1338 break;
1339 default:
1340 if (fputs("\n", fp) == EOF)
1341 {
1343 goto bail;
1344 }
1345 }
1346 }
1347 }
1348
1349 if (mutt_file_fclose(&fp) != 0)
1350 {
1351 mutt_debug(LL_DEBUG1, "mutt_file_fclose (&) returned non-zero\n");
1353 goto bail;
1354 }
1355
1356 /* Save the state of this folder. */
1357 if (stat(mailbox_path(m), &st) == -1)
1358 {
1360 goto bail;
1361 }
1362
1363 unlink_tempfile = false;
1364
1365 fp = fopen(mutt_buffer_string(tempfile), "r");
1366 if (!fp)
1367 {
1369 mx_fastclose_mailbox(m, false);
1370 mutt_debug(LL_DEBUG1, "unable to reopen temp copy of mailbox!\n");
1372 FREE(&new_offset);
1373 FREE(&old_offset);
1374 goto fatal;
1375 }
1376
1377 if (!mutt_file_seek(adata->fp, offset, SEEK_SET) || /* seek the append location */
1378 /* do a sanity check to make sure the mailbox looks ok */
1379 !fgets(buf, sizeof(buf), adata->fp) ||
1380 ((m->type == MUTT_MBOX) && !mutt_str_startswith(buf, "From ")) ||
1381 ((m->type == MUTT_MMDF) && !mutt_str_equal(MMDF_SEP, buf)))
1382 {
1383 mutt_debug(LL_DEBUG1, "message not in expected position\n");
1384 mutt_debug(LL_DEBUG1, " LINE: %s\n", buf);
1385 i = -1;
1386 }
1387 else
1388 {
1389 if (!mutt_file_seek(adata->fp, offset, SEEK_SET)) /* return to proper offset */
1390 {
1391 i = -1;
1392 }
1393 else
1394 {
1395 /* copy the temp mailbox back into place starting at the first
1396 * change/deleted message */
1397 if (m->verbose)
1398 mutt_message(_("Committing changes..."));
1399 i = mutt_file_copy_stream(fp, adata->fp);
1400
1401 if (ferror(adata->fp))
1402 i = -1;
1403 }
1404 if (i >= 0)
1405 {
1406 m->size = ftello(adata->fp); /* update the mailbox->size of the mailbox */
1407 if ((m->size < 0) || (ftruncate(fileno(adata->fp), m->size) != 0))
1408 {
1409 i = -1;
1410 mutt_debug(LL_DEBUG1, "ftruncate() failed\n");
1411 }
1412 }
1413 }
1414
1416 fp = NULL;
1418
1419 if ((mutt_file_fclose(&adata->fp) != 0) || (i == -1))
1420 {
1421 /* error occurred while writing the mailbox back, so keep the temp copy around */
1422
1423 struct Buffer *savefile = mutt_buffer_pool_get();
1424
1425 const char *const c_tmpdir = cs_subset_path(NeoMutt->sub, "tmpdir");
1426 mutt_buffer_printf(savefile, "%s/neomutt.%s-%s-%u", NONULL(c_tmpdir), NONULL(Username),
1427 NONULL(ShortHostname), (unsigned int) getpid());
1428 rename(mutt_buffer_string(tempfile), mutt_buffer_string(savefile));
1430 mx_fastclose_mailbox(m, false);
1432 mutt_error(_("Write failed! Saved partial mailbox to %s"), mutt_buffer_string(savefile));
1433 mutt_buffer_pool_release(&savefile);
1434 FREE(&new_offset);
1435 FREE(&old_offset);
1436 goto fatal;
1437 }
1438
1439 /* Restore the previous access/modification times */
1440 mbox_reset_atime(m, &st);
1441
1442 /* reopen the mailbox in read-only mode */
1443 adata->fp = mbox_open_readwrite(m);
1444 if (!adata->fp)
1445 {
1446 adata->fp = mbox_open_readonly(m);
1447 }
1448 if (!adata->fp)
1449 {
1450 unlink(mutt_buffer_string(tempfile));
1452 mx_fastclose_mailbox(m, false);
1453 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1454 FREE(&new_offset);
1455 FREE(&old_offset);
1456 goto fatal;
1457 }
1458
1459 /* update the offsets of the rewritten messages */
1460 for (i = first, j = first; i < m->msg_count; i++)
1461 {
1462 if (!m->emails[i]->deleted)
1463 {
1464 m->emails[i]->offset = new_offset[i - first].hdr;
1465 m->emails[i]->body->hdr_offset = new_offset[i - first].hdr;
1466 m->emails[i]->body->offset = new_offset[i - first].body;
1467 m->emails[i]->index = j++;
1468 }
1469 }
1470 FREE(&new_offset);
1471 FREE(&old_offset);
1472 unlink(mutt_buffer_string(tempfile)); /* remove partial copy of the mailbox */
1473 mutt_buffer_pool_release(&tempfile);
1475
1476 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1477 if (c_check_mbox_size)
1478 {
1479 struct Mailbox *m_tmp = mailbox_find(mailbox_path(m));
1480 if (m_tmp && !m_tmp->has_new)
1481 mailbox_update(m_tmp);
1482 }
1483
1484 progress_free(&progress);
1485 return 0; /* signal success */
1486
1487bail: /* Come here in case of disaster */
1488
1489 mutt_file_fclose(&fp);
1490
1491 if (tempfile && unlink_tempfile)
1492 unlink(mutt_buffer_string(tempfile));
1493
1494 /* restore offsets, as far as they are valid */
1495 if ((first >= 0) && old_offset)
1496 {
1497 for (i = first; (i < m->msg_count) && old_offset[i - first].valid; i++)
1498 {
1499 m->emails[i]->offset = old_offset[i - first].hdr;
1500 m->emails[i]->body->hdr_offset = old_offset[i - first].hdr;
1501 m->emails[i]->body->offset = old_offset[i - first].body;
1502 m->emails[i]->lines = old_offset[i - first].lines;
1503 m->emails[i]->body->length = old_offset[i - first].length;
1504 }
1505 }
1506
1507 /* this is ok to call even if we haven't locked anything */
1509
1511 FREE(&new_offset);
1512 FREE(&old_offset);
1513
1514 adata->fp = freopen(mailbox_path(m), "r", adata->fp);
1515 if (!adata->fp)
1516 {
1517 mutt_error(_("Could not reopen mailbox"));
1518 mx_fastclose_mailbox(m, false);
1519 goto fatal;
1520 }
1521
1523 if (need_sort)
1524 {
1525 /* if the mailbox was reopened, the thread tree will be invalid so make
1526 * sure to start threading from scratch. */
1528 }
1529
1530fatal:
1531 mutt_buffer_pool_release(&tempfile);
1532 progress_free(&progress);
1533 return rc;
1534}
int mutt_buffer_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:158
static const char * mutt_buffer_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:77
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:97
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:292
int mutt_copy_message(FILE *fp_out, struct Email *e, struct Message *msg, CopyMessageFlags cmflags, CopyHeaderFlags chflags, int wraplen)
Copy a message from a Mailbox.
Definition: copy.c:864
#define MUTT_CM_UPDATE
Update structs on sync.
Definition: copy.h:40
#define CH_UPDATE
Update the status and x-status fields?
Definition: copy.h:52
#define CH_FROM
Retain the "From " message separator?
Definition: copy.h:56
#define CH_UPDATE_LEN
Update Lines: and Content-Length:
Definition: copy.h:62
void mutt_body_free(struct Body **ptr)
Free a Body.
Definition: body.c:57
int mutt_file_copy_stream(FILE *fp_in, FILE *fp_out)
Copy the contents of one file into another.
Definition: file.c:260
int mutt_file_fclose(FILE **fp)
Close a FILE handle (and NULL the pointer)
Definition: file.c:152
bool mutt_file_seek(FILE *fp, LOFF_T offset, int whence)
Wrapper for fseeko with error handling.
Definition: file.c:690
#define mutt_message(...)
Definition: logging.h:86
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
#define mutt_perror(...)
Definition: logging.h:88
static enum MxStatus mbox_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: mbox.c:1031
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:204
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:222
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:139
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:178
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition: mailbox.h:179
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
#define MMDF_SEP
Definition: lib.h:60
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition: mbox.c:139
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:99
static FILE * mbox_open_readwrite(struct Mailbox *m)
Open an mbox read-write.
Definition: mbox.c:904
static FILE * mbox_open_readonly(struct Mailbox *m)
Open an mbox read-only.
Definition: mbox.c:919
static void mbox_unlock_mailbox(struct Mailbox *m)
Unlock a mailbox.
Definition: mbox.c:161
void mbox_reset_atime(struct Mailbox *m, struct stat *st)
Reset the access time on the mailbox file.
Definition: mbox.c:848
void * mutt_mem_calloc(size_t nmemb, size_t size)
Allocate zeroed memory on the heap.
Definition: memory.c:50
#define FREE(x)
Definition: memory.h:43
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:784
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:227
char * ShortHostname
Short version of the hostname.
Definition: mutt_globals.h:50
char * Username
User's login name.
Definition: mutt_globals.h:52
@ UT_FLAT
Unthreaded.
Definition: mutt_thread.h:85
void mutt_buffer_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:599
#define mutt_buffer_mktemp(buf)
Definition: muttlib.h:74
int mx_msg_close(struct Mailbox *m, struct Message **msg)
Close a message.
Definition: mx.c:1193
void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account)
Free up memory associated with the Mailbox.
Definition: mx.c:430
struct Message * mx_msg_open(struct Mailbox *m, int msgno)
Return a stream pointer for a message.
Definition: mx.c:1147
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition: mxapi.h:89
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition: mxapi.h:87
void mutt_buffer_pool_release(struct Buffer **pbuf)
Free a Buffer from the pool.
Definition: pool.c:112
struct Buffer * mutt_buffer_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:101
void mutt_sig_block(void)
Block signals during critical operations.
Definition: signal.c:150
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition: signal.c:168
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:48
#define NONULL(x)
Definition: string2.h:37
void * adata
Private data (for Mailbox backends)
Definition: account.h:43
struct Body * parts
parts of a multipart or message/rfc822
Definition: body.h:72
LOFF_T offset
offset where the actual data begins
Definition: body.h:52
LOFF_T length
length (in bytes) of attachment
Definition: body.h:53
long hdr_offset
Offset in stream where the headers begin.
Definition: body.h:80
String manipulation buffer.
Definition: buffer.h:34
int lines
How many lines in the body of this message?
Definition: email.h:60
struct Body * body
List of MIME parts.
Definition: email.h:67
bool changed
Email has been edited.
Definition: email.h:75
LOFF_T offset
Where in the stream does this message begin?
Definition: email.h:69
bool attach_del
Has an attachment marked for deletion.
Definition: email.h:99
int msgno
Number displayed to the user.
Definition: email.h:111
Store of new offsets, used by mutt_sync_mailbox()
Definition: mbox.c:66
long lines
Definition: mbox.c:70
LOFF_T hdr
Definition: mbox.c:68
LOFF_T length
Definition: mbox.c:71
LOFF_T body
Definition: mbox.c:69
bool valid
Definition: mbox.c:67
A mailbox.
Definition: mailbox.h:79
bool has_new
Mailbox has new mail.
Definition: mailbox.h:85
off_t size
Size of the Mailbox.
Definition: mailbox.h:84
Mbox-specific Account data -.
Definition: lib.h:49
FILE * fp
Mailbox file.
Definition: lib.h:50
A local copy of an email.
Definition: mxapi.h:43
FILE * fp
pointer to the message data
Definition: mxapi.h:44
int cs_subset_str_native_set(const struct ConfigSubset *sub, const char *name, intptr_t value, struct Buffer *err)
Natively set the value of a string config item.
Definition: subset.c:305
+ Here is the call graph for this function:

◆ nntp_mbox_sync()

static enum MxStatus nntp_mbox_sync ( struct Mailbox m)
static

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Note
May also return values from check_mailbox()

Definition at line 2480 of file nntp.c.

2481{
2482 struct NntpMboxData *mdata = m->mdata;
2483
2484 /* check for new articles */
2485 mdata->adata->check_time = 0;
2486 enum MxStatus check = check_mailbox(m);
2487 if (check != MX_STATUS_OK)
2488 return check;
2489
2490#ifdef USE_HCACHE
2491 mdata->last_cached = 0;
2492 struct HeaderCache *hc = nntp_hcache_open(mdata);
2493#endif
2494
2495 for (int i = 0; i < m->msg_count; i++)
2496 {
2497 struct Email *e = m->emails[i];
2498 if (!e)
2499 break;
2500
2501 char buf[16] = { 0 };
2502
2503 snprintf(buf, sizeof(buf), ANUM, nntp_edata_get(e)->article_num);
2504 if (mdata->bcache && e->deleted)
2505 {
2506 mutt_debug(LL_DEBUG2, "mutt_bcache_del %s\n", buf);
2507 mutt_bcache_del(mdata->bcache, buf);
2508 }
2509
2510#ifdef USE_HCACHE
2511 if (hc && (e->changed || e->deleted))
2512 {
2513 if (e->deleted && !e->read)
2514 mdata->unread--;
2515 mutt_debug(LL_DEBUG2, "mutt_hcache_store %s\n", buf);
2516 mutt_hcache_store(hc, buf, strlen(buf), e, 0);
2517 }
2518#endif
2519 }
2520
2521#ifdef USE_HCACHE
2522 if (hc)
2523 {
2525 mdata->last_cached = mdata->last_loaded;
2526 }
2527#endif
2528
2529 /* save .newsrc entries */
2531 nntp_newsrc_update(mdata->adata);
2532 nntp_newsrc_close(mdata->adata);
2533 return MX_STATUS_OK;
2534}
int mutt_bcache_del(struct BodyCache *bcache, const char *id)
Delete a file from the Body Cache.
Definition: bcache.c:265
int mutt_hcache_store(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:552
@ LL_DEBUG2
Log at debug level 2.
Definition: logging.h:41
struct HeaderCache * nntp_hcache_open(struct NntpMboxData *mdata)
Open newsgroup hcache.
Definition: newsrc.c:711
void nntp_newsrc_gen_entries(struct Mailbox *m)
Generate array of .newsrc entries.
Definition: newsrc.c:299
struct NntpEmailData * nntp_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:58
void nntp_newsrc_close(struct NntpAccountData *adata)
Unlock and close .newsrc file.
Definition: newsrc.c:118
int nntp_newsrc_update(struct NntpAccountData *adata)
Update .newsrc file.
Definition: newsrc.c:442
#define ANUM
Definition: lib.h:61
static enum MxStatus check_mailbox(struct Mailbox *m)
Check current newsgroup for new articles.
Definition: nntp.c:1433
bool read
Email is read.
Definition: email.h:48
void * mdata
Driver specific data.
Definition: mailbox.h:133
NNTP-specific Mailbox data -.
Definition: mdata.h:33
anum_t last_cached
Definition: mdata.h:39
struct BodyCache * bcache
Definition: mdata.h:49
struct NntpAccountData * adata
Definition: mdata.h:47
anum_t unread
Definition: mdata.h:40
anum_t last_loaded
Definition: mdata.h:38
+ Here is the call graph for this function:

◆ nm_mbox_sync()

static enum MxStatus nm_mbox_sync ( struct Mailbox m)
static

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Definition at line 2202 of file notmuch.c.

2203{
2204 struct NmMboxData *mdata = nm_mdata_get(m);
2205 if (!mdata)
2206 return MX_STATUS_ERROR;
2207
2208 enum MxStatus rc = MX_STATUS_OK;
2209 struct Progress *progress = NULL;
2210 char *url = mutt_str_dup(mailbox_path(m));
2211 bool changed = false;
2212
2213 mutt_debug(LL_DEBUG1, "nm: sync start\n");
2214
2215 if (m->verbose)
2216 {
2217 /* all is in this function so we don't use data->progress here */
2218 char msg[PATH_MAX] = { 0 };
2219 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
2220 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
2221 }
2222
2223 struct HeaderCache *h = nm_hcache_open(m);
2224
2225 int mh_sync_errors = 0;
2226 for (int i = 0; i < m->msg_count; i++)
2227 {
2228 char old_file[PATH_MAX], new_file[PATH_MAX];
2229 struct Email *e = m->emails[i];
2230 if (!e)
2231 break;
2232
2233 struct NmEmailData *edata = nm_edata_get(e);
2234
2235 if (m->verbose)
2236 progress_update(progress, i, -1);
2237
2238 *old_file = '\0';
2239 *new_file = '\0';
2240
2241 if (edata->oldpath)
2242 {
2243 mutt_str_copy(old_file, edata->oldpath, sizeof(old_file));
2244 old_file[sizeof(old_file) - 1] = '\0';
2245 mutt_debug(LL_DEBUG2, "nm: fixing obsolete path '%s'\n", old_file);
2246 }
2247 else
2248 email_get_fullpath(e, old_file, sizeof(old_file));
2249
2250 mutt_buffer_strcpy(&m->pathbuf, edata->folder);
2251 m->type = edata->type;
2252
2253 bool ok = maildir_sync_mailbox_message(m, i, h);
2254 if (!ok)
2255 {
2256 // Syncing file failed, query notmuch for new filepath.
2257 m->type = MUTT_NOTMUCH;
2258 notmuch_database_t *db = nm_db_get(m, true);
2259 if (db)
2260 {
2261 notmuch_message_t *msg = get_nm_message(db, e);
2262
2264
2265 mutt_buffer_strcpy(&m->pathbuf, edata->folder);
2266 m->type = edata->type;
2267 ok = maildir_sync_mailbox_message(m, i, h);
2268 m->type = MUTT_NOTMUCH;
2269 }
2270 nm_db_release(m);
2271 m->type = edata->type;
2272 }
2273
2274 mutt_buffer_strcpy(&m->pathbuf, url);
2275 m->type = MUTT_NOTMUCH;
2276
2277 if (!ok)
2278 {
2279 mh_sync_errors += 1;
2280 continue;
2281 }
2282
2283 if (!e->deleted)
2284 email_get_fullpath(e, new_file, sizeof(new_file));
2285
2286 if (e->deleted || (strcmp(old_file, new_file) != 0))
2287 {
2288 if (e->deleted && (remove_filename(m, old_file) == 0))
2289 changed = true;
2290 else if (*new_file && *old_file && (rename_filename(m, old_file, new_file, e) == 0))
2291 changed = true;
2292 }
2293
2294 FREE(&edata->oldpath);
2295 }
2296
2297 if (mh_sync_errors > 0)
2298 {
2299 mutt_error(ngettext("Unable to sync %d message due to external mailbox modification",
2300 "Unable to sync %d messages due to external mailbox modification",
2301 mh_sync_errors),
2302 mh_sync_errors);
2303 }
2304
2305 mutt_buffer_strcpy(&m->pathbuf, url);
2306 m->type = MUTT_NOTMUCH;
2307
2308 nm_db_release(m);
2309
2310 if (changed)
2311 {
2313 m->mtime.tv_nsec = 0;
2314 }
2315
2316 nm_hcache_close(h);
2317
2318 progress_free(&progress);
2319 FREE(&url);
2320 mutt_debug(LL_DEBUG1, "nm: .... sync done [rc=%d]\n", rc);
2321 return rc;
2322}
size_t mutt_buffer_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:310
time_t mutt_date_epoch(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:427
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
bool maildir_sync_mailbox_message(struct Mailbox *m, int msgno, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: maildir.c:948
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:250
size_t mutt_str_copy(char *dest, const char *src, size_t dsize)
Copy a string into a buffer (guaranteeing NUL-termination)
Definition: string.c:629
notmuch_database_t * nm_db_get(struct Mailbox *m, bool writable)
Get the Notmuch database.
Definition: db.c:169
int nm_db_release(struct Mailbox *m)
Close the Notmuch database.
Definition: db.c:193
struct NmEmailData * nm_edata_get(struct Email *e)
Get the Notmuch Email data.
Definition: edata.c:72
struct NmMboxData * nm_mdata_get(struct Mailbox *m)
Get the Notmuch Mailbox data.
Definition: mdata.c:97
static char * email_get_fullpath(struct Email *e, char *buf, size_t buflen)
Get the full path of an email.
Definition: notmuch.c:224
static void sync_email_path_with_nm(struct Email *e, notmuch_message_t *msg)
Synchronize Neomutt's Email path with notmuch.
Definition: notmuch.c:1072
static notmuch_message_t * get_nm_message(notmuch_database_t *db, struct Email *e)
Find a Notmuch message.
Definition: notmuch.c:1031
static void nm_hcache_close(struct HeaderCache *h)
Close the header cache.
Definition: notmuch.c:123
static int rename_filename(struct Mailbox *m, const char *old_file, const char *new_file, struct Email *e)
Rename the file.
Definition: notmuch.c:1312
static struct HeaderCache * nm_hcache_open(struct Mailbox *m)
Open a header cache.
Definition: notmuch.c:109
static int remove_filename(struct Mailbox *m, const char *path)
Delete a file.
Definition: notmuch.c:1248
void * edata
Driver-specific data.
Definition: email.h:72
struct timespec mtime
Time Mailbox was last changed.
Definition: mailbox.h:104
struct Buffer pathbuf
Path of the Mailbox.
Definition: mailbox.h:80
Notmuch-specific Email data -.
Definition: edata.h:34
Notmuch-specific Mailbox data -.
Definition: mdata.h:34
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:51
time_t tv_sec
Number of seconds since the epoch.
Definition: file.h:50
+ Here is the call graph for this function:

◆ pop_mbox_sync()

static enum MxStatus pop_mbox_sync ( struct Mailbox m)
static

Save changes to the Mailbox - Implements MxOps::mbox_sync() -.

Update POP mailbox, delete messages from server

Definition at line 858 of file pop.c.

859{
860 int i, j, rc = 0;
861 char buf[1024] = { 0 };
863#ifdef USE_HCACHE
864 struct HeaderCache *hc = NULL;
865#endif
866
867 adata->check_time = 0;
868
869 int num_deleted = 0;
870 for (i = 0; i < m->msg_count; i++)
871 {
872 if (m->emails[i]->deleted)
873 num_deleted++;
874 }
875
876 while (true)
877 {
878 if (pop_reconnect(m) < 0)
879 return MX_STATUS_ERROR;
880
881#ifdef USE_HCACHE
882 hc = pop_hcache_open(adata, mailbox_path(m));
883#endif
884
885 struct Progress *progress = NULL;
886 if (m->verbose)
887 {
888 progress = progress_new(_("Marking messages deleted..."),
889 MUTT_PROGRESS_WRITE, num_deleted);
890 }
891
892 for (i = 0, j = 0, rc = 0; (rc == 0) && (i < m->msg_count); i++)
893 {
894 struct PopEmailData *edata = pop_edata_get(m->emails[i]);
895 if (m->emails[i]->deleted && (edata->refno != -1))
896 {
897 j++;
898 if (m->verbose)
899 progress_update(progress, j, -1);
900 snprintf(buf, sizeof(buf), "DELE %d\r\n", edata->refno);
901 rc = pop_query(adata, buf, sizeof(buf));
902 if (rc == 0)
903 {
904 mutt_bcache_del(adata->bcache, cache_id(edata->uid));
905#ifdef USE_HCACHE
906 mutt_hcache_delete_record(hc, edata->uid, strlen(edata->uid));
907#endif
908 }
909 }
910
911#ifdef USE_HCACHE
912 if (m->emails[i]->changed)
913 {
914 mutt_hcache_store(hc, edata->uid, strlen(edata->uid), m->emails[i], 0);
915 }
916#endif
917 }
918 progress_free(&progress);
919
920#ifdef USE_HCACHE
922#endif
923
924 if (rc == 0)
925 {
926 mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
927 rc = pop_query(adata, buf, sizeof(buf));
928 }
929
930 if (rc == 0)
931 {
932 adata->clear_cache = true;
933 pop_clear_cache(adata);
934 adata->status = POP_DISCONNECTED;
935 return MX_STATUS_OK;
936 }
937
938 if (rc == -2)
939 {
940 mutt_error("%s", adata->err_msg);
941 return MX_STATUS_ERROR;
942 }
943 }
944}
int mutt_hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:631
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:64
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:65
int pop_reconnect(struct Mailbox *m)
Reconnect and verify indexes if connection was lost.
Definition: lib.c:599
#define pop_query(adata, buf, buflen)
Definition: private.h:109
@ POP_DISCONNECTED
Disconnected from server.
Definition: private.h:51
static void pop_clear_cache(struct PopAccountData *adata)
Delete all cached messages.
Definition: pop.c:491
static const char * cache_id(const char *id)
Make a message-cache-compatible id.
Definition: pop.c:83
static struct HeaderCache * pop_hcache_open(struct PopAccountData *adata, const char *path)
Open the header cache.
Definition: pop.c:297
POP-specific Account data -.
Definition: adata.h:37
bool clear_cache
Definition: adata.h:49
time_t check_time
Definition: adata.h:51
char err_msg[POP_CMD_RESPONSE]
Definition: adata.h:56
unsigned int status
Definition: adata.h:39
struct BodyCache * bcache
body cache
Definition: adata.h:55
POP-specific Email data -.
Definition: edata.h:32
+ Here is the call graph for this function: