NeoMutt
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
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() -.
 
static enum MxStatus maildir_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus mh_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus mbox_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus nntp_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus nm_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 
static enum MxStatus pop_mbox_sync (struct Mailbox *m)
 Save changes to the Mailbox - Implements MxOps::mbox_sync() -.
 

Detailed Description

Save changes to the Mailbox.

Parameters
mMailbox to sync
Return values
enumMxStatus
Precondition
m is not NULL

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 621 of file compress.c.

622{
623 if (!m->compress_info)
624 return MX_STATUS_ERROR;
625
626 struct CompressInfo *ci = m->compress_info;
627
628 if (!ci->cmd_close)
629 {
630 mutt_error(_("Can't sync a compressed file without a close-hook"));
631 return MX_STATUS_ERROR;
632 }
633
634 const struct MxOps *ops = ci->child_ops;
635 if (!ops)
636 return MX_STATUS_ERROR;
637
638 if (!lock_realpath(m, true))
639 {
640 mutt_error(_("Unable to lock mailbox"));
641 return MX_STATUS_ERROR;
642 }
643
644 enum MxStatus check = comp_mbox_check(m);
645 if (check != MX_STATUS_OK)
646 goto sync_cleanup;
647
648 check = ops->mbox_sync(m);
649 if (check != MX_STATUS_OK)
650 goto sync_cleanup;
651
652 if (!execute_command(m, ci->cmd_close, _("Compressing %s")))
653 {
654 check = MX_STATUS_ERROR;
655 goto sync_cleanup;
656 }
657
658 check = MX_STATUS_OK;
659
660sync_cleanup:
661 store_size(m);
663 return check;
664}
static void store_size(const struct Mailbox *m)
Save the size of the compressed file.
Definition: compress.c:184
static bool lock_realpath(struct Mailbox *m, bool excl)
Try to lock the Mailbox.realpath.
Definition: compress.c:89
static void unlock_realpath(struct Mailbox *m)
Unlock the mailbox->realpath.
Definition: compress.c:130
static bool execute_command(struct Mailbox *m, const char *command, const char *progress)
Run a system command.
Definition: compress.c:327
#define mutt_error(...)
Definition: logging2.h:92
static enum MxStatus comp_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: compress.c:585
#define _(a)
Definition: message.h:28
MxStatus
Return values from mbox_check(), mbox_check_stats(), mbox_snc(), and mbox_close()
Definition: mxapi.h:63
@ MX_STATUS_ERROR
An error occurred.
Definition: mxapi.h:64
@ MX_STATUS_OK
No changes.
Definition: mxapi.h:65
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:91
enum MxStatus(* mbox_sync)(struct Mailbox *m)
Definition: mxapi.h:187
+ Here is the call graph for this function:

◆ maildir_mbox_sync()

static enum MxStatus maildir_mbox_sync ( struct Mailbox m)
static

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 1413 of file maildir.c.

1414{
1415 enum MxStatus check = maildir_check(m);
1416 if (check == MX_STATUS_ERROR)
1417 return check;
1418
1419 struct HeaderCache *hc = NULL;
1420#ifdef USE_HCACHE
1421 const char *const c_header_cache = cs_subset_path(NeoMutt->sub, "header_cache");
1422 if (m->type == MUTT_MAILDIR)
1423 hc = hcache_open(c_header_cache, mailbox_path(m), NULL);
1424#endif
1425
1426 struct Progress *progress = NULL;
1427 if (m->verbose)
1428 {
1429 char msg[PATH_MAX] = { 0 };
1430 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1431 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1432 }
1433
1434 for (int i = 0; i < m->msg_count; i++)
1435 {
1436 progress_update(progress, i, -1);
1437
1438 struct Email *e = m->emails[i];
1439 if (!maildir_sync_mailbox_message(m, e, hc))
1440 {
1441 progress_free(&progress);
1442 goto err;
1443 }
1444 }
1445 progress_free(&progress);
1446
1447#ifdef USE_HCACHE
1448 if (m->type == MUTT_MAILDIR)
1449 hcache_close(&hc);
1450#endif
1451
1452 /* XXX race condition? */
1453
1455
1456 /* adjust indices */
1457
1458 if (m->msg_deleted)
1459 {
1460 const bool c_maildir_trash = cs_subset_bool(NeoMutt->sub, "maildir_trash");
1461 for (int i = 0, j = 0; i < m->msg_count; i++)
1462 {
1463 struct Email *e = m->emails[i];
1464 if (!e)
1465 break;
1466
1467 if (!e->deleted || c_maildir_trash)
1468 e->index = j++;
1469 }
1470 }
1471
1472 return check;
1473
1474err:
1475#ifdef USE_HCACHE
1476 if (m->type == MUTT_MAILDIR)
1477 hcache_close(&hc);
1478#endif
1479 return MX_STATUS_ERROR;
1480}
const char * cs_subset_path(const struct ConfigSubset *sub, const char *name)
Get a path config item by name.
Definition: helpers.c:169
bool cs_subset_bool(const struct ConfigSubset *sub, const char *name)
Get a boolean config item by name.
Definition: helpers.c:48
struct HeaderCache * hcache_open(const char *path, const char *folder, hcache_namer_t namer)
Multiplexor for StoreOps::open.
Definition: hcache.c:494
void hcache_close(struct HeaderCache **ptr)
Multiplexor for StoreOps::close.
Definition: hcache.c:563
static const char * mailbox_path(const struct Mailbox *m)
Get the Mailbox's path string.
Definition: mailbox.h:210
@ MUTT_MAILDIR
'Maildir' Mailbox type
Definition: mailbox.h:48
static enum MxStatus maildir_check(struct Mailbox *m)
Check for new mail.
Definition: maildir.c:1199
static void maildir_update_mtime(struct Mailbox *m)
Update our record of the Maildir modification time.
Definition: maildir.c:496
bool maildir_sync_mailbox_message(struct Mailbox *m, struct Email *e, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: maildir.c:977
#define PATH_MAX
Definition: mutt.h:41
@ 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:92
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:124
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:109
Header Cache.
Definition: lib.h:88
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:116
Container for Accounts, Notifications.
Definition: neomutt.h:41
struct ConfigSubset * sub
Inherited config items.
Definition: neomutt.h:45
+ Here is the call graph for this function:

◆ mh_mbox_sync()

static enum MxStatus mh_mbox_sync ( struct Mailbox m)
static

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 1036 of file mh.c.

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

1064{
1066 if (!adata)
1067 return MX_STATUS_ERROR;
1068
1069 struct Buffer *tempfile = NULL;
1070 char buf[32] = { 0 };
1071 int j;
1072 bool unlink_tempfile = false;
1073 bool need_sort = false; /* flag to resort mailbox if new mail arrives */
1074 int first = -1; /* first message to be written */
1075 LOFF_T offset; /* location in mailbox to write changed messages */
1076 struct stat st = { 0 };
1077 struct MUpdate *new_offset = NULL;
1078 struct MUpdate *old_offset = NULL;
1079 FILE *fp = NULL;
1080 struct Progress *progress = NULL;
1081 enum MxStatus rc = MX_STATUS_ERROR;
1082
1083 /* sort message by their position in the mailbox on disk */
1084 const enum SortType c_sort = cs_subset_sort(NeoMutt->sub, "sort");
1085 const unsigned char c_use_threads = cs_subset_enum(NeoMutt->sub, "use_threads");
1086 if (c_sort != SORT_ORDER)
1087 {
1089 cs_subset_str_native_set(NeoMutt->sub, "use_threads", UT_FLAT, NULL);
1091 cs_subset_str_native_set(NeoMutt->sub, "sort", c_sort, NULL);
1092 cs_subset_str_native_set(NeoMutt->sub, "use_threads", c_use_threads, NULL);
1093 need_sort = true;
1094 }
1095
1096 /* need to open the file for writing in such a way that it does not truncate
1097 * the file, so use read-write mode. */
1098 adata->fp = freopen(mailbox_path(m), "r+", adata->fp);
1099 if (!adata->fp)
1100 {
1101 mx_fastclose_mailbox(m, false);
1102 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1103 goto fatal;
1104 }
1105
1107
1108 if (mbox_lock_mailbox(m, true, true) == -1)
1109 {
1111 mutt_error(_("Unable to lock mailbox"));
1112 goto bail;
1113 }
1114
1115 /* Check to make sure that the file hasn't changed on disk */
1116 enum MxStatus check = mbox_mbox_check(m);
1117 if ((check == MX_STATUS_NEW_MAIL) || (check == MX_STATUS_REOPENED))
1118 {
1119 /* new mail arrived, or mailbox reopened */
1120 rc = check;
1121 goto bail;
1122 }
1123 else if (check < 0)
1124 {
1125 goto fatal;
1126 }
1127
1128 /* Create a temporary file to write the new version of the mailbox in. */
1129 tempfile = buf_pool_get();
1130 buf_mktemp(tempfile);
1131 int fd = open(buf_string(tempfile), O_WRONLY | O_EXCL | O_CREAT, 0600);
1132 if ((fd == -1) || !(fp = fdopen(fd, "w")))
1133 {
1134 if (fd != -1)
1135 {
1136 close(fd);
1137 unlink_tempfile = true;
1138 }
1139 mutt_error(_("Could not create temporary file"));
1140 goto bail;
1141 }
1142 unlink_tempfile = true;
1143
1144 /* find the first deleted/changed message. we save a lot of time by only
1145 * rewriting the mailbox from the point where it has actually changed. */
1146 int i = 0;
1147 for (; (i < m->msg_count) && !m->emails[i]->deleted &&
1148 !m->emails[i]->changed && !m->emails[i]->attach_del;
1149 i++)
1150 {
1151 }
1152 if (i == m->msg_count)
1153 {
1154 /* this means m->changed or m->msg_deleted was set, but no
1155 * messages were found to be changed or deleted. This should
1156 * never happen, is we presume it is a bug in neomutt. */
1157 mutt_error(_("sync: mbox modified, but no modified messages (report this bug)"));
1158 mutt_debug(LL_DEBUG1, "no modified messages\n");
1159 goto bail;
1160 }
1161
1162 /* save the index of the first changed/deleted message */
1163 first = i;
1164 /* where to start overwriting */
1165 offset = m->emails[i]->offset;
1166
1167 /* the offset stored in the header does not include the MMDF_SEP, so make
1168 * sure we seek to the correct location */
1169 if (m->type == MUTT_MMDF)
1170 offset -= (sizeof(MMDF_SEP) - 1);
1171
1172 /* allocate space for the new offsets */
1173 new_offset = mutt_mem_calloc(m->msg_count - first, sizeof(struct MUpdate));
1174 old_offset = mutt_mem_calloc(m->msg_count - first, sizeof(struct MUpdate));
1175
1176 if (m->verbose)
1177 {
1178 char msg[PATH_MAX] = { 0 };
1179 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
1180 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
1181 }
1182
1183 for (i = first, j = 0; i < m->msg_count; i++)
1184 {
1185 progress_update(progress, i, i / (m->msg_count / 100 + 1));
1186 /* back up some information which is needed to restore offsets when
1187 * something fails. */
1188
1189 old_offset[i - first].valid = true;
1190 old_offset[i - first].hdr = m->emails[i]->offset;
1191 old_offset[i - first].body = m->emails[i]->body->offset;
1192 old_offset[i - first].lines = m->emails[i]->lines;
1193 old_offset[i - first].length = m->emails[i]->body->length;
1194
1195 if (!m->emails[i]->deleted)
1196 {
1197 j++;
1198
1199 if (m->type == MUTT_MMDF)
1200 {
1201 if (fputs(MMDF_SEP, fp) == EOF)
1202 {
1203 mutt_perror("%s", buf_string(tempfile));
1204 goto bail;
1205 }
1206 }
1207
1208 /* save the new offset for this message. we add 'offset' because the
1209 * temporary file only contains saved message which are located after
1210 * 'offset' in the real mailbox */
1211 new_offset[i - first].hdr = ftello(fp) + offset;
1212
1213 struct Message *msg = mx_msg_open(m, m->emails[i]);
1214 const int rc2 = mutt_copy_message(fp, m->emails[i], msg, MUTT_CM_UPDATE,
1216 mx_msg_close(m, &msg);
1217 if (rc2 != 0)
1218 {
1219 mutt_perror("%s", buf_string(tempfile));
1220 goto bail;
1221 }
1222
1223 /* Since messages could have been deleted, the offsets stored in memory
1224 * will be wrong, so update what we can, which is the offset of this
1225 * message, and the offset of the body. If this is a multipart message,
1226 * we just flush the in memory cache so that the message will be reparsed
1227 * if the user accesses it later. */
1228 new_offset[i - first].body = ftello(fp) - m->emails[i]->body->length + offset;
1229 mutt_body_free(&m->emails[i]->body->parts);
1230
1231 if (m->type == MUTT_MMDF)
1232 {
1233 if (fputs(MMDF_SEP, fp) == EOF)
1234 {
1235 mutt_perror("%s", buf_string(tempfile));
1236 goto bail;
1237 }
1238 }
1239 else
1240 {
1241 if (fputs("\n", fp) == EOF)
1242 {
1243 mutt_perror("%s", buf_string(tempfile));
1244 goto bail;
1245 }
1246 }
1247 }
1248 }
1249
1250 if (mutt_file_fclose(&fp) != 0)
1251 {
1252 mutt_debug(LL_DEBUG1, "mutt_file_fclose (&) returned non-zero\n");
1253 mutt_perror("%s", buf_string(tempfile));
1254 goto bail;
1255 }
1256
1257 /* Save the state of this folder. */
1258 if (stat(mailbox_path(m), &st) == -1)
1259 {
1260 mutt_perror("%s", mailbox_path(m));
1261 goto bail;
1262 }
1263
1264 unlink_tempfile = false;
1265
1266 fp = fopen(buf_string(tempfile), "r");
1267 if (!fp)
1268 {
1270 mx_fastclose_mailbox(m, false);
1271 mutt_debug(LL_DEBUG1, "unable to reopen temp copy of mailbox!\n");
1272 mutt_perror("%s", buf_string(tempfile));
1273 FREE(&new_offset);
1274 FREE(&old_offset);
1275 goto fatal;
1276 }
1277
1278 if (!mutt_file_seek(adata->fp, offset, SEEK_SET) || /* seek the append location */
1279 /* do a sanity check to make sure the mailbox looks ok */
1280 !fgets(buf, sizeof(buf), adata->fp) ||
1281 ((m->type == MUTT_MBOX) && !mutt_str_startswith(buf, "From ")) ||
1282 ((m->type == MUTT_MMDF) && !mutt_str_equal(MMDF_SEP, buf)))
1283 {
1284 mutt_debug(LL_DEBUG1, "message not in expected position\n");
1285 mutt_debug(LL_DEBUG1, " LINE: %s\n", buf);
1286 i = -1;
1287 }
1288 else
1289 {
1290 if (!mutt_file_seek(adata->fp, offset, SEEK_SET)) /* return to proper offset */
1291 {
1292 i = -1;
1293 }
1294 else
1295 {
1296 /* copy the temp mailbox back into place starting at the first
1297 * change/deleted message */
1298 if (m->verbose)
1299 mutt_message(_("Committing changes..."));
1300 i = mutt_file_copy_stream(fp, adata->fp);
1301
1302 if (ferror(adata->fp))
1303 i = -1;
1304 }
1305 if (i >= 0)
1306 {
1307 m->size = ftello(adata->fp); /* update the mailbox->size of the mailbox */
1308 if ((m->size < 0) || (ftruncate(fileno(adata->fp), m->size) != 0))
1309 {
1310 i = -1;
1311 mutt_debug(LL_DEBUG1, "ftruncate() failed\n");
1312 }
1313 }
1314 }
1315
1317 fp = NULL;
1319
1320 if ((mutt_file_fclose(&adata->fp) != 0) || (i == -1))
1321 {
1322 /* error occurred while writing the mailbox back, so keep the temp copy around */
1323
1324 struct Buffer *savefile = buf_pool_get();
1325
1326 const char *const c_tmp_dir = cs_subset_path(NeoMutt->sub, "tmp_dir");
1327 buf_printf(savefile, "%s/neomutt.%s-%s-%u", NONULL(c_tmp_dir),
1328 NONULL(Username), NONULL(ShortHostname), (unsigned int) getpid());
1329 rename(buf_string(tempfile), buf_string(savefile));
1331 mx_fastclose_mailbox(m, false);
1332 buf_pretty_mailbox(savefile);
1333 mutt_error(_("Write failed! Saved partial mailbox to %s"), buf_string(savefile));
1334 buf_pool_release(&savefile);
1335 FREE(&new_offset);
1336 FREE(&old_offset);
1337 goto fatal;
1338 }
1339
1340 /* Restore the previous access/modification times */
1341 mbox_reset_atime(m, &st);
1342
1343 /* reopen the mailbox in read-only mode */
1344 adata->fp = mbox_open_readwrite(m);
1345 if (!adata->fp)
1346 {
1347 adata->fp = mbox_open_readonly(m);
1348 }
1349 if (!adata->fp)
1350 {
1351 unlink(buf_string(tempfile));
1353 mx_fastclose_mailbox(m, false);
1354 mutt_error(_("Fatal error! Could not reopen mailbox!"));
1355 FREE(&new_offset);
1356 FREE(&old_offset);
1357 goto fatal;
1358 }
1359
1360 /* update the offsets of the rewritten messages */
1361 for (i = first, j = first; i < m->msg_count; i++)
1362 {
1363 if (!m->emails[i]->deleted)
1364 {
1365 m->emails[i]->offset = new_offset[i - first].hdr;
1366 m->emails[i]->body->hdr_offset = new_offset[i - first].hdr;
1367 m->emails[i]->body->offset = new_offset[i - first].body;
1368 m->emails[i]->index = j++;
1369 }
1370 }
1371 FREE(&new_offset);
1372 FREE(&old_offset);
1373 unlink(buf_string(tempfile)); /* remove partial copy of the mailbox */
1374 buf_pool_release(&tempfile);
1376
1377 const bool c_check_mbox_size = cs_subset_bool(NeoMutt->sub, "check_mbox_size");
1378 if (c_check_mbox_size)
1379 {
1380 struct Mailbox *m_tmp = mailbox_find(mailbox_path(m));
1381 if (m_tmp && !m_tmp->has_new)
1382 mailbox_update(m_tmp);
1383 }
1384
1385 progress_free(&progress);
1386 return 0; /* signal success */
1387
1388bail: /* Come here in case of disaster */
1389
1390 mutt_file_fclose(&fp);
1391
1392 if (tempfile && unlink_tempfile)
1393 unlink(buf_string(tempfile));
1394
1395 /* restore offsets, as far as they are valid */
1396 if ((first >= 0) && old_offset)
1397 {
1398 for (i = first; (i < m->msg_count) && old_offset[i - first].valid; i++)
1399 {
1400 m->emails[i]->offset = old_offset[i - first].hdr;
1401 m->emails[i]->body->hdr_offset = old_offset[i - first].hdr;
1402 m->emails[i]->body->offset = old_offset[i - first].body;
1403 m->emails[i]->lines = old_offset[i - first].lines;
1404 m->emails[i]->body->length = old_offset[i - first].length;
1405 }
1406 }
1407
1408 /* this is ok to call even if we haven't locked anything */
1410
1412 FREE(&new_offset);
1413 FREE(&old_offset);
1414
1415 adata->fp = freopen(mailbox_path(m), "r", adata->fp);
1416 if (!adata->fp)
1417 {
1418 mutt_error(_("Could not reopen mailbox"));
1419 mx_fastclose_mailbox(m, false);
1420 goto fatal;
1421 }
1422
1424 if (need_sort)
1425 {
1426 /* if the mailbox was reopened, the thread tree will be invalid so make
1427 * sure to start threading from scratch. */
1429 }
1430
1431fatal:
1432 buf_pool_release(&tempfile);
1433 progress_free(&progress);
1434 return rc;
1435}
int buf_printf(struct Buffer *buf, const char *fmt,...)
Format a string overwriting a Buffer.
Definition: buffer.c:173
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:93
unsigned char cs_subset_enum(const struct ConfigSubset *sub, const char *name)
Get a enumeration config item by name.
Definition: helpers.c:72
short cs_subset_sort(const struct ConfigSubset *sub, const char *name)
Get a sort config item by name.
Definition: helpers.c:267
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:884
#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:262
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:733
char * ShortHostname
Short version of the hostname.
Definition: globals.c:40
char * Username
User's login name.
Definition: globals.c:42
#define mutt_message(...)
Definition: logging2.h:91
#define mutt_debug(LEVEL,...)
Definition: logging2.h:89
#define mutt_perror(...)
Definition: logging2.h:93
static enum MxStatus mbox_mbox_check(struct Mailbox *m)
Check for new mail - Implements MxOps::mbox_check() -.
Definition: mbox.c:929
@ LL_DEBUG1
Log at debug level 1.
Definition: logging2.h:43
void mailbox_update(struct Mailbox *m)
Get the mailbox's current size.
Definition: mailbox.c:208
void mailbox_changed(struct Mailbox *m, enum NotifyMailbox action)
Notify observers of a change to a Mailbox.
Definition: mailbox.c:226
struct Mailbox * mailbox_find(const char *path)
Find the mailbox with a given path.
Definition: mailbox.c:143
@ NT_MAILBOX_RESORT
Email list needs resorting.
Definition: mailbox.h:177
@ NT_MAILBOX_UPDATE
Update internal tables.
Definition: mailbox.h:178
@ MUTT_MMDF
'mmdf' Mailbox type
Definition: mailbox.h:46
@ MUTT_MBOX
'mbox' Mailbox type
Definition: mailbox.h:45
#define MMDF_SEP
Definition: lib.h:62
static int mbox_lock_mailbox(struct Mailbox *m, bool excl, bool retry)
Lock a mailbox.
Definition: mbox.c:138
static struct MboxAccountData * mbox_adata_get(struct Mailbox *m)
Get the private data associated with a Mailbox.
Definition: mbox.c:123
static FILE * mbox_open_readwrite(struct Mailbox *m)
Open an mbox read-write.
Definition: mbox.c:802
static FILE * mbox_open_readonly(struct Mailbox *m)
Open an mbox read-only.
Definition: mbox.c:817
static void mbox_unlock_mailbox(struct Mailbox *m)
Unlock a mailbox.
Definition: mbox.c:162
void mbox_reset_atime(struct Mailbox *m, struct stat *st)
Reset the access time on the mailbox file.
Definition: mbox.c:747
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:45
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition: string.c:798
size_t mutt_str_startswith(const char *str, const char *prefix)
Check whether a string starts with a prefix.
Definition: string.c:228
@ UT_FLAT
Unthreaded.
Definition: mutt_thread.h:97
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:562
int mx_msg_close(struct Mailbox *m, struct Message **ptr)
Close a message.
Definition: mx.c:1206
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, struct Email *e)
Return a stream pointer for a message.
Definition: mx.c:1160
@ MX_STATUS_REOPENED
Mailbox was reopened.
Definition: mxapi.h:68
@ MX_STATUS_NEW_MAIL
New mail received in Mailbox.
Definition: mxapi.h:66
struct Buffer * buf_pool_get(void)
Get a Buffer from the pool.
Definition: pool.c:81
void buf_pool_release(struct Buffer **ptr)
Return a Buffer to the pool.
Definition: pool.c:94
void mutt_sig_block(void)
Block signals during critical operations.
Definition: signal.c:163
void mutt_sig_unblock(void)
Restore previously blocked signals.
Definition: signal.c:181
SortType
Methods for sorting.
Definition: sort2.h:38
@ SORT_ORDER
Sort by the order the messages appear in the mailbox.
Definition: sort2.h:44
#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:98
Store of new offsets, used by mutt_sync_mailbox()
Definition: mbox.c:67
long lines
Definition: mbox.c:71
LOFF_T hdr
Definition: mbox.c:69
LOFF_T length
Definition: mbox.c:72
LOFF_T body
Definition: mbox.c:70
bool valid
Definition: mbox.c:68
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: message.h:34
FILE * fp
pointer to the message data
Definition: message.h:35
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:304
#define buf_mktemp(buf)
Definition: tmp.h:33
+ 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 2511 of file nntp.c.

2512{
2513 struct NntpMboxData *mdata = m->mdata;
2514
2515 /* check for new articles */
2516 mdata->adata->check_time = 0;
2517 enum MxStatus check = check_mailbox(m);
2518 if (check != MX_STATUS_OK)
2519 return check;
2520
2521#ifdef USE_HCACHE
2522 mdata->last_cached = 0;
2523 struct HeaderCache *hc = nntp_hcache_open(mdata);
2524#endif
2525
2526 for (int i = 0; i < m->msg_count; i++)
2527 {
2528 struct Email *e = m->emails[i];
2529 if (!e)
2530 break;
2531
2532 char buf[16] = { 0 };
2533
2534 snprintf(buf, sizeof(buf), ANUM, nntp_edata_get(e)->article_num);
2535 if (mdata->bcache && e->deleted)
2536 {
2537 mutt_debug(LL_DEBUG2, "mutt_bcache_del %s\n", buf);
2538 mutt_bcache_del(mdata->bcache, buf);
2539 }
2540
2541#ifdef USE_HCACHE
2542 if (hc && (e->changed || e->deleted))
2543 {
2544 if (e->deleted && !e->read)
2545 mdata->unread--;
2546 mutt_debug(LL_DEBUG2, "hcache_store %s\n", buf);
2547 hcache_store(hc, buf, strlen(buf), e, 0);
2548 }
2549#endif
2550 }
2551
2552#ifdef USE_HCACHE
2553 if (hc)
2554 {
2555 hcache_close(&hc);
2556 mdata->last_cached = mdata->last_loaded;
2557 }
2558#endif
2559
2560 /* save .newsrc entries */
2562 nntp_newsrc_update(mdata->adata);
2563 nntp_newsrc_close(mdata->adata);
2564 return MX_STATUS_OK;
2565}
int mutt_bcache_del(struct BodyCache *bcache, const char *id)
Delete a file from the Body Cache.
Definition: bcache.c:266
int hcache_store(struct HeaderCache *hc, const char *key, size_t keylen, struct Email *e, uint32_t uidvalidity)
Multiplexor for StoreOps::store.
Definition: hcache.c:686
@ LL_DEBUG2
Log at debug level 2.
Definition: logging2.h:44
struct HeaderCache * nntp_hcache_open(struct NntpMboxData *mdata)
Open newsgroup hcache.
Definition: newsrc.c:714
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:60
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:441
#define ANUM
Definition: lib.h:62
static enum MxStatus check_mailbox(struct Mailbox *m)
Check current newsgroup for new articles.
Definition: nntp.c:1456
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:34
anum_t last_cached
Definition: mdata.h:40
struct BodyCache * bcache
Definition: mdata.h:50
struct NntpAccountData * adata
Definition: mdata.h:48
anum_t unread
Definition: mdata.h:41
anum_t last_loaded
Definition: mdata.h:39
+ 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 2216 of file notmuch.c.

2217{
2218 struct NmMboxData *mdata = nm_mdata_get(m);
2219 if (!mdata)
2220 return MX_STATUS_ERROR;
2221
2222 enum MxStatus rc = MX_STATUS_OK;
2223 struct Progress *progress = NULL;
2224 char *url = mutt_str_dup(mailbox_path(m));
2225 bool changed = false;
2226
2227 mutt_debug(LL_DEBUG1, "nm: sync start\n");
2228
2229 if (m->verbose)
2230 {
2231 /* all is in this function so we don't use data->progress here */
2232 char msg[PATH_MAX] = { 0 };
2233 snprintf(msg, sizeof(msg), _("Writing %s..."), mailbox_path(m));
2234 progress = progress_new(msg, MUTT_PROGRESS_WRITE, m->msg_count);
2235 }
2236
2237 struct HeaderCache *hc = nm_hcache_open(m);
2238
2239 int mh_sync_errors = 0;
2240 for (int i = 0; i < m->msg_count; i++)
2241 {
2242 char old_file[PATH_MAX], new_file[PATH_MAX];
2243 struct Email *e = m->emails[i];
2244 if (!e)
2245 break;
2246
2247 struct NmEmailData *edata = nm_edata_get(e);
2248
2249 progress_update(progress, i, -1);
2250
2251 *old_file = '\0';
2252 *new_file = '\0';
2253
2254 if (edata->oldpath)
2255 {
2256 mutt_str_copy(old_file, edata->oldpath, sizeof(old_file));
2257 old_file[sizeof(old_file) - 1] = '\0';
2258 mutt_debug(LL_DEBUG2, "nm: fixing obsolete path '%s'\n", old_file);
2259 }
2260 else
2261 {
2262 email_get_fullpath(e, old_file, sizeof(old_file));
2263 }
2264
2265 buf_strcpy(&m->pathbuf, edata->folder);
2266 m->type = edata->type;
2267
2268 bool ok = maildir_sync_mailbox_message(m, e, hc);
2269 if (!ok)
2270 {
2271 // Syncing file failed, query notmuch for new filepath.
2272 m->type = MUTT_NOTMUCH;
2273 notmuch_database_t *db = nm_db_get(m, true);
2274 if (db)
2275 {
2276 notmuch_message_t *msg = get_nm_message(db, e);
2277
2279
2280 buf_strcpy(&m->pathbuf, edata->folder);
2281 m->type = edata->type;
2282 ok = maildir_sync_mailbox_message(m, e, hc);
2283 m->type = MUTT_NOTMUCH;
2284 }
2285 nm_db_release(m);
2286 m->type = edata->type;
2287 }
2288
2289 buf_strcpy(&m->pathbuf, url);
2290 m->type = MUTT_NOTMUCH;
2291
2292 if (!ok)
2293 {
2294 mh_sync_errors += 1;
2295 continue;
2296 }
2297
2298 if (!e->deleted)
2299 email_get_fullpath(e, new_file, sizeof(new_file));
2300
2301 if (e->deleted || !mutt_str_equal(old_file, new_file))
2302 {
2303 if (e->deleted && (remove_filename(m, old_file) == 0))
2304 changed = true;
2305 else if (*new_file && *old_file && (rename_filename(m, old_file, new_file, e) == 0))
2306 changed = true;
2307 }
2308
2309 FREE(&edata->oldpath);
2310 }
2311
2312 if (mh_sync_errors > 0)
2313 {
2314 mutt_error(ngettext("Unable to sync %d message due to external mailbox modification",
2315 "Unable to sync %d messages due to external mailbox modification",
2316 mh_sync_errors),
2317 mh_sync_errors);
2318 }
2319
2320 buf_strcpy(&m->pathbuf, url);
2321 m->type = MUTT_NOTMUCH;
2322
2323 nm_db_release(m);
2324
2325 if (changed)
2326 {
2327 mdata->mtime.tv_sec = mutt_date_now();
2328 mdata->mtime.tv_nsec = 0;
2329 }
2330
2331 nm_hcache_close(&hc);
2332
2333 progress_free(&progress);
2334 FREE(&url);
2335 mutt_debug(LL_DEBUG1, "nm: .... sync done [rc=%d]\n", rc);
2336 return rc;
2337}
size_t buf_strcpy(struct Buffer *buf, const char *s)
Copy a string into a Buffer.
Definition: buffer.c:407
@ MUTT_NOTMUCH
'Notmuch' (virtual) Mailbox type
Definition: mailbox.h:51
bool maildir_sync_mailbox_message(struct Mailbox *m, struct Email *e, struct HeaderCache *hc)
Save changes to the mailbox.
Definition: maildir.c:977
time_t mutt_date_now(void)
Return the number of seconds since the Unix epoch.
Definition: date.c:446
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition: string.c:251
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:653
notmuch_database_t * nm_db_get(struct Mailbox *m, bool writable)
Get the Notmuch database.
Definition: db.c:200
int nm_db_release(struct Mailbox *m)
Close the Notmuch database.
Definition: db.c:224
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:96
static char * email_get_fullpath(struct Email *e, char *buf, size_t buflen)
Get the full path of an email.
Definition: notmuch.c:229
static void sync_email_path_with_nm(struct Email *e, notmuch_message_t *msg)
Synchronize Neomutt's Email path with notmuch.
Definition: notmuch.c:1079
static notmuch_message_t * get_nm_message(notmuch_database_t *db, struct Email *e)
Find a Notmuch message.
Definition: notmuch.c:1038
static int rename_filename(struct Mailbox *m, const char *old_file, const char *new_file, struct Email *e)
Rename the file.
Definition: notmuch.c:1322
static struct HeaderCache * nm_hcache_open(struct Mailbox *m)
Open a header cache.
Definition: notmuch.c:110
static int remove_filename(struct Mailbox *m, const char *path)
Delete a file.
Definition: notmuch.c:1258
static void nm_hcache_close(struct HeaderCache **ptr)
Close the header cache.
Definition: notmuch.c:124
void * edata
Driver-specific data.
Definition: email.h:72
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:35
struct timespec mtime
Time Mailbox was last changed.
Definition: mdata.h:44
long tv_nsec
Number of nanosecond, on top.
Definition: file.h:52
time_t tv_sec
Number of seconds since the epoch.
Definition: file.h:51
+ 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 progress_update(progress, j, -1);
899 snprintf(buf, sizeof(buf), "DELE %d\r\n", edata->refno);
900 rc = pop_query(adata, buf, sizeof(buf));
901 if (rc == 0)
902 {
903 mutt_bcache_del(adata->bcache, cache_id(edata->uid));
904#ifdef USE_HCACHE
905 hcache_delete_record(hc, edata->uid, strlen(edata->uid));
906#endif
907 }
908 }
909
910#ifdef USE_HCACHE
911 if (m->emails[i]->changed)
912 {
913 hcache_store(hc, edata->uid, strlen(edata->uid), m->emails[i], 0);
914 }
915#endif
916 }
917 progress_free(&progress);
918
919#ifdef USE_HCACHE
920 hcache_close(&hc);
921#endif
922
923 if (rc == 0)
924 {
925 mutt_str_copy(buf, "QUIT\r\n", sizeof(buf));
926 rc = pop_query(adata, buf, sizeof(buf));
927 }
928
929 if (rc == 0)
930 {
931 adata->clear_cache = true;
932 pop_clear_cache(adata);
933 adata->status = POP_DISCONNECTED;
934 return MX_STATUS_OK;
935 }
936
937 if (rc == -2)
938 {
939 mutt_error("%s", adata->err_msg);
940 return MX_STATUS_ERROR;
941 }
942 }
943}
int hcache_delete_record(struct HeaderCache *hc, const char *key, size_t keylen)
Multiplexor for StoreOps::delete_record.
Definition: hcache.c:759
struct PopAccountData * pop_adata_get(struct Mailbox *m)
Get the Account data for this mailbox.
Definition: adata.c:73
struct PopEmailData * pop_edata_get(struct Email *e)
Get the private data for this Email.
Definition: edata.c:68
int pop_reconnect(struct Mailbox *m)
Reconnect and verify indexes if connection was lost.
Definition: lib.c:606
#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:82
static struct HeaderCache * pop_hcache_open(struct PopAccountData *adata, const char *path)
Open the header cache.
Definition: pop.c:298
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: