Blame view

mh/rmf.c 4.96 KB
Wojciech Polak authored
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2 3
   Copyright (C) 2002, 2005, 2006, 2007, 2008, 2009, 2010 Free Software
   Foundation, Inc.
4

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

Wojciech Polak authored
10
   GNU Mailutils is distributed in the hope that it will be useful,
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19 20 21 22 23 24 25 26 27 28 29 30

/* MH rmf command */

#include <mh.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <errno.h>

#include <dirent.h>

31 32
static char doc[] = N_("GNU MH rmf")"\v"
N_("Use -help to obtain the list of traditional MH options.");
33
static char args_doc[] = N_("[+FOLDER]");
34 35 36

/* GNU options */
static struct argp_option options[] = {
37
  {"folder",  ARG_FOLDER, N_("FOLDER"), 0,
Sergey Poznyakoff authored
38
   N_("specify the folder to delete")},
39
  {"interactive", ARG_INTERACTIVE, N_("BOOL"), OPTION_ARG_OPTIONAL,
Sergey Poznyakoff authored
40
    N_("interactive mode: ask for confirmation before removing each folder")},
41
  {"nointeractive", ARG_NOINTERACTIVE, NULL, OPTION_HIDDEN, ""},
42
  {"recursive", ARG_RECURSIVE, NULL, 0,
Sergey Poznyakoff authored
43
   N_("recursively delete all subfolders")},
44
  {"norecursive", ARG_NORECURSIVE, NULL, OPTION_HIDDEN, ""},
45 46 47 48 49
  { 0 }
};

/* Traditional MH options */
struct mh_option mh_option[] = {
50
  { "interactive", MH_OPT_BOOL },
51 52 53 54 55
  { 0 }
};

int explicit_folder; /* Was the folder explicitly given */
int interactive; /* Ask for confirmation before deleting */
56
int recursive;     /* Recursively process all the sub-directories */
57

58 59 60 61
static char *cur_folder_path; /* Full pathname of the current folder */
static char *folder_name;     /* Name of the (topmost) folder to be
				 deleted */

62 63
static error_t
opt_handler (int key, char *arg, struct argp_state *state)
64 65 66
{
  switch (key)
    {
67
    case ARG_FOLDER:
68
      explicit_folder = 1;
69
      folder_name = arg;
70 71
      break;

72
    case ARG_INTERACTIVE:
73 74 75
      interactive = is_true (arg);
      break;

76 77 78 79
    case ARG_NOINTERACTIVE:
      interactive = 0;
      break;
	
80
    case ARG_RECURSIVE:
81
      recursive = is_true (arg);
82 83
      break;
      
84
    case ARG_NORECURSIVE:
85
      recursive = 0;
86 87
      break;

88
    default:
89
      return ARGP_ERR_UNKNOWN;
90 91 92 93
    }
  return 0;
}

94 95 96
static char *
current_folder_path ()
{
97
  mu_mailbox_t mbox = mh_open_folder (mh_current_folder (), MU_STREAM_RDWR);
98
  mu_url_t url;
99
  char *p;
100
  mu_mailbox_get_url (mbox, &url);
101
  p = (char*) mu_url_to_string (url);
102 103 104 105 106 107
  if (strncmp (p, "mh:", 3) == 0)
    p += 3;
  return p;
}

static int
108 109
rmf (const char *name)
{
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
  mu_mailbox_t mbox = NULL;
  int rc;
  
  rc = mu_mailbox_create_default (&mbox, name);
  if (rc)
    {
      mu_error (_("cannot create mailbox %s: %s"),
		name, strerror (rc));
      return 1;
    }

  rc = mu_mailbox_remove (mbox);
  mu_mailbox_destroy (&mbox);
  if (rc)
    {
      mu_error (_("cannot remove %s: %s"), name, mu_strerror (rc));
      return 1;
    }
  return 0;
}

/* Recursive rmf */
static int
recrmf (const char *name)
{
135 136
  DIR *dir;
  struct dirent *entry;
137 138
  int failures = 0;
  
139 140 141 142
  dir = opendir (name);

  if (!dir)
    {
143
      mu_error (_("cannot scan folder %s: %s"), name, strerror (errno));
Sergey Poznyakoff authored
144
      return 1;
145 146
    }

Wojciech Polak authored
147
  if (interactive && !mh_getyn (_("Remove folder %s"), name))
148
    exit (0);
149

150 151 152 153 154 155 156 157 158
  while ((entry = readdir (dir)))
    {
      char *p;
      struct stat st;

      if (strcmp (entry->d_name, ".") == 0
	  || strcmp (entry->d_name, "..") == 0)
	continue;
      
159
      p = mh_safe_make_file_name (name, entry->d_name);
160 161
      if (stat (p, &st) < 0)
	{
162
	  mu_diag_funcall (MU_DIAG_ERROR, "stat", p, errno);
163 164
	}
      else if (S_ISDIR (st.st_mode))
165
	failures += recrmf (p);
166 167 168
      free (p);
    }
  closedir (dir);
169 170

  if (failures == 0)
171
    failures += rmf (name);
172 173
  else
    printf ("%s: folder `%s' not removed\n",
174
	    mu_program_name, name);
175 176

  return failures;
177 178 179 180 181
}

int
main (int argc, char **argv)
{
182
  int status;
183
  char *name;
Wojciech Polak authored
184 185

  /* Native Language Support */
Sergey Poznyakoff authored
186
  MU_APP_INIT_NLS ();
Wojciech Polak authored
187

188
  mh_argp_init ();
189
  mh_argp_parse (&argc, &argv, 0, options, mh_option, args_doc, doc,
190 191
		 opt_handler, NULL, NULL);

192 193
  cur_folder_path = current_folder_path ();

194 195 196 197 198 199 200
  if (!explicit_folder)
    {
      interactive = 1;
      name = cur_folder_path;
    }
  else
    name = mh_expand_name (NULL, folder_name, 0);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
  if (recursive)
    status = recrmf (name);
  else
    {
      if (interactive && !mh_getyn (_("Remove folder %s"), name))
	exit (0);
      status = rmf (name);
    }
  if (status == 0)
    {
      if (cur_folder_path && strcmp (name, cur_folder_path) == 0)
	{
	  mh_set_current_folder ("inbox");
	  mh_global_save_state ();
	  printf ("[+inbox now current]\n");
	}
      return 0;
    }      
  return 1;
220
}