summaryrefslogtreecommitdiff
path: root/libmailutils/base/removefile.c
blob: 405eb7ba040c2adfc9cabb3fa253b7d772a08067 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2016 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/>. */

#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <mailutils/stream.h>
#include <mailutils/util.h>
#include <mailutils/diag.h>
#include <mailutils/error.h>
#include <mailutils/errno.h>
#include <mailutils/list.h>
#include <mailutils/iterator.h>
#include <mailutils/nls.h>

static int removedir (const char *path);

int
mu_remove_file (const char *path)
{
  int rc = 0;
  struct stat st;

  if (stat (path, &st))
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("can't stat file %s: %s"),
		 path, mu_strerror (errno)));
      return errno;
    }

  if (S_ISDIR (st.st_mode))
    rc = removedir (path);
  else if (unlink (path))
    {
      rc = errno;
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("can't unlink file %s: %s"),
		 path, mu_strerror (rc)));
    }

  return rc;
}

struct nameent
{
  int isdir;
  char name[1];
};

static int
name_add (mu_list_t list, char const *name)
{
  int rc;
  size_t len = strlen (name);
  struct nameent *ent = malloc (sizeof (*ent) + len);
  if (!ent)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("%s", mu_strerror (errno)));
      return 1;
    }
  ent->isdir = -1;
  strcpy (ent->name, name);
  rc = mu_list_append (list, ent);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("mu_list_append: %s", mu_strerror (rc)));
      free (ent);
    }
    
  return rc;
}

static int
lsdir (const char *path, mu_list_t list)
{
  DIR *dirp;
  struct dirent *dp;
  int rc = 0;

  dirp = opendir (path);
  if (dirp == NULL)
    {
      rc = errno;
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("cannot open directory %s: %s",
		 path, mu_strerror (errno)));
      return rc;
    }

  while ((dp = readdir (dirp)))
    {
      char const *ename = dp->d_name;
      char *filename;
      
      if (ename[ename[0] != '.' ? 0 : ename[1] != '.' ? 1 : 2] == 0)
	continue;

      filename = mu_make_file_name (path, ename);
      if (!filename)
	{
	  rc = errno;
	  mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		    ("%s: can't create file name: %s",
		     path, mu_strerror (errno)));
	  break;
	}

      rc = name_add (list, filename);
      free (filename);
      if (rc)
	break;
    }

  closedir (dirp);

  return rc;
}

static int
namecmp (const void *a, const void *b)
{
  struct nameent const *enta = a;
  struct nameent const *entb = b;
  int d = enta->isdir - entb->isdir;
  if (d)
    return d;
  return strcmp (entb->name, enta->name);
}

static int
check_parent_access (const char *path)
{
  int rc;
  char *name, *p;

  name = strdup (path);
  if (!name)
    return errno;
  p = strrchr (name, '/');
  if (p)
    *p = 0;
  else
    strcpy (name, ".");
  rc = access (name, R_OK|W_OK|X_OK);
  free (name);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		(_("not enough privileges to remove files from %s"),
		 name));
      return EACCES;
    }

  return 0;
}

static int
removedir (const char *path)
{
  int rc;
  struct stat st;
  mu_list_t namelist;
  mu_iterator_t itr;
  struct nameent *ent;

  rc = check_parent_access (path);
  if (rc)
    return rc;
  
  rc = mu_list_create (&namelist);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("mu_list_create: %s", mu_strerror (rc)));
      return rc;
    }
  mu_list_set_destroy_item (namelist, mu_list_free_item);
  mu_list_set_comparator (namelist, namecmp);

  rc = name_add (namelist, path);
  if (rc)
    {
      mu_list_destroy (&namelist);
      return rc;
    }
  
  rc = mu_list_get_iterator (namelist, &itr);
  if (rc)
    {
      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		("mu_list_get_iterator: %s", mu_strerror (rc)));
      mu_list_destroy (&namelist);
      return rc;
    }

  for (mu_iterator_first (itr);
       !mu_iterator_is_done (itr);
       mu_iterator_next (itr)) 
    {
      mu_iterator_current (itr, (void **)&ent);
      
      if (lstat (ent->name, &st))
	{
	  rc = errno;
	  if (rc == ENOENT)
	    continue;
	  mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
		    (_("can't lstat file %s: %s"),
		     ent->name, mu_strerror (rc)));
	  break;
	}

      if (S_ISDIR (st.st_mode))
	{
	  ent->isdir = 1;
	  if (access (ent->name, R_OK|W_OK|X_OK))
	    {
	      rc = EACCES;
	      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
			(_("not enough privileges to remove files from %s"),
			 ent->name));
	    }
	  else 
	    rc = lsdir (ent->name, namelist);
	  if (rc)
	    break;
	}
      else
	ent->isdir = 0;
    }

  if (rc == 0)
    {
      mu_list_sort (namelist, namecmp);
       
      for (mu_iterator_first (itr);
	   !mu_iterator_is_done (itr);
	   mu_iterator_next (itr))
	{
	  mu_iterator_current (itr, (void **)&ent);
	  rc = (ent->isdir ? rmdir : unlink) (ent->name);
	  if (rc)
	    {
	      mu_debug (MU_DEBCAT_STREAM, MU_DEBUG_ERROR,
			(_("can't remove %s: %s"),
			 ent->name, mu_strerror (rc)));
	    }
	}
    }
  mu_iterator_destroy (&itr);
  mu_list_destroy (&namelist);

  return rc;
}
  
  

Return to:

Send suggestions and report system problems to the System administrator.