File: maildir_fixup.c

package info (click to toggle)
mailutils 1%3A3.19-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 38,832 kB
  • sloc: ansic: 186,739; sh: 110,867; yacc: 7,463; cpp: 3,834; makefile: 3,161; lex: 1,972; python: 1,617; exp: 1,563; awk: 152; lisp: 132; sed: 31
file content (202 lines) | stat: -rw-r--r-- 5,207 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2025 Free Software Foundation, Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#if defined(HAVE_CONFIG_H)
# include <config.h>
#endif
#include <mailutils/mailutils.h>
#include <sysexits.h>
#include "mu.h"
#include "muaux.h"

/* Utility-specific exit codes */
enum
  {
    EXIT_OK,        /* Success */
    EXIT_HARDFAIL,  /* None of the maildirs fixed */
    EXIT_SOFTFAIL,  /* Some of the maildirs failed to fix */ 
  };

static int verbose_option;
static int dry_run_option;
static unsigned long fail_count;
static unsigned long succ_count;

static struct mu_option options[] = 
{
  { "verbose", 'v', NULL, MU_OPTION_DEFAULT,
    "verbosely list what is being done",
    mu_c_incr, &verbose_option },
  { "dry-run", 'n', NULL, MU_OPTION_DEFAULT,
    "do nothing, print everything",
    mu_c_incr, &dry_run_option },  
    MU_OPTION_END
};

static int
mailbox_fixup (void *item, void *data)
{
  struct mu_list_response *resp = item;
  mu_mailbox_t mbox = NULL;
  int rc;

  if (!(resp->type & MU_FOLDER_ATTRIBUTE_FILE))
    return 0;
  
  if (verbose_option)
    mu_diag_output (MU_DIAG_INFO, "Fixing %s", resp->name);
  if (dry_run_option)
    return 0;
  
  rc = manlock_open_mailbox (&mbox, resp->name, 1, MU_STREAM_RDWR);
  if (rc == 0)
    {
      rc = mu_mailbox_scan (mbox, 1, NULL);
      mu_mailbox_close (mbox);
      manlock_unlock (mbox);
      mu_mailbox_destroy (&mbox);
    }
  else
    mu_error ("can't open mailbox: %s", mu_strerror (rc));
  
  if (rc)
    fail_count++;
  else
    succ_count++;
  return 0;
}

static inline int
filename_ok (char const *fname)
{
  return fname[0] == '/' ||
         fname[0] == '~' ||
         (fname[0] == '.' &&
	  (fname[1] == '/' || (fname[1] == '.' && fname[2] == '/')));
}

static int
fix_mailboxes_in_folder (char *fname)
{
  mu_folder_t folder;
  struct mu_folder_scanner scn = MU_FOLDER_SCANNER_INITIALIZER;
  int rc;
     
  if (!filename_ok (fname))
    {
      char *cwd = mu_getcwd ();
      size_t prefix_len = strlen (cwd);
      if (cwd[prefix_len-1] != '/')
	prefix_len++;
      fname = mu_make_file_name (cwd, fname);
      free (cwd);
      if (!fname)
	mu_alloc_die ();
    }
  
  rc = mu_folder_create (&folder, fname);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_create", fname, rc);
      return rc;
    }
  rc = mu_folder_open (folder, MU_STREAM_READ);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_open", fname, rc);
      goto err;
    }

  rc = mu_list_create (&scn.result);
  if (rc)
    {
      mu_diag_funcall (MU_DIAG_ERROR, "mu_list_create", NULL, rc);
      goto err;
    }
  
  rc = mu_folder_scan (folder, &scn);
  if (rc)
    mu_diag_funcall (MU_DIAG_ERROR, "mu_folder_scan", NULL, rc);
  else
    mu_list_foreach (scn.result, mailbox_fixup, NULL);
  
 err:
  mu_list_destroy (&scn.result);
  mu_folder_destroy (&folder);
  return rc;
}

static char docstring[] = N_(
  "recursively scans all maildirs in the folder\n"
  "\nThis induces fixing of the maildir message attributes (bug #56428) "
  "and attaching persistent UID numbers (commits fd9b19bac-d7110faa).\n"
);
static char argdoc[] = N_("FOLDER ...");
static char *capa[] = {
  "debug",
  "locking",
  NULL
};

static struct mu_cfg_param config_param[] = {
  { "mandatory-locking", mu_cfg_section },
  { NULL }
};

int
main (int argc, char **argv)
{
  int i;

  mu_registrar_record (mu_maildir_record);
  mu_registrar_set_default_scheme ("maildir");
  manlock_cfg_init ();
  mu_cli_simple (argc, argv,
		 MU_CLI_OPTION_OPTIONS, options,
		 MU_CLI_OPTION_OPTIONS, common_options,
		 MU_CLI_OPTION_PROG_NAME, getenv ("MAILUTILS_PROGNAME"),
		 MU_CLI_OPTION_PROG_DOC, docstring,
		 MU_CLI_OPTION_PROG_ARGS, argdoc,
		 MU_CLI_OPTION_RETURN_ARGC, &argc,
                 MU_CLI_OPTION_RETURN_ARGV, &argv,
		 MU_CLI_OPTION_PACKAGE_NAME, PACKAGE_NAME,   
		 MU_CLI_OPTION_PACKAGE_URL, PACKAGE_URL,   
		 MU_CLI_OPTION_BUG_ADDRESS, PACKAGE_BUGREPORT,   
		 MU_CLI_OPTION_VERSION_HOOK, mu_version_hook,
		 MU_CLI_OPTION_CAPABILITIES, capa,
		 MU_CLI_OPTION_CONFIG, config_param,
		 MU_CLI_OPTION_CONF_SITE_FILE, NULL,
		 MU_CLI_OPTION_CONF_PROGNAME, "maildir_fixup",
		 MU_CLI_OPTION_END);
                 
  if (argc == 0)
    {
      mu_error ("required argument missing; try --help for more info");
      exit (EX_USAGE);
    }
  
  if (dry_run_option)
    verbose_option++;
  
  for (i = 0; i < argc; i++)
    fix_mailboxes_in_folder (argv[i]);

  if (fail_count)
    exit (succ_count == 0 ? EXIT_HARDFAIL : EXIT_SOFTFAIL);
  
  exit (EXIT_OK); 
}